BinaryNodeHeap.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.pathfinding.utils;
  20. import com.l2jserver.gameserver.pathfinding.geonodes.GeoNode;
  21. /**
  22. * @author -Nemesiss-
  23. */
  24. public class BinaryNodeHeap
  25. {
  26. private final GeoNode[] _list;
  27. private int _size;
  28. public BinaryNodeHeap(int size)
  29. {
  30. _list = new GeoNode[size + 1];
  31. _size = 0;
  32. }
  33. public void add(GeoNode n)
  34. {
  35. _size++;
  36. int pos = _size;
  37. _list[pos] = n;
  38. while (pos != 1)
  39. {
  40. int p2 = pos / 2;
  41. if (_list[pos].getCost() <= _list[p2].getCost())
  42. {
  43. GeoNode temp = _list[p2];
  44. _list[p2] = _list[pos];
  45. _list[pos] = temp;
  46. pos = p2;
  47. }
  48. else
  49. {
  50. break;
  51. }
  52. }
  53. }
  54. public GeoNode removeFirst()
  55. {
  56. GeoNode first = _list[1];
  57. _list[1] = _list[_size];
  58. _list[_size] = null;
  59. _size--;
  60. int pos = 1;
  61. int cpos;
  62. int dblcpos;
  63. GeoNode temp;
  64. while (true)
  65. {
  66. cpos = pos;
  67. dblcpos = cpos * 2;
  68. if ((dblcpos + 1) <= _size)
  69. {
  70. if (_list[cpos].getCost() >= _list[dblcpos].getCost())
  71. {
  72. pos = dblcpos;
  73. }
  74. if (_list[pos].getCost() >= _list[dblcpos + 1].getCost())
  75. {
  76. pos = dblcpos + 1;
  77. }
  78. }
  79. else if (dblcpos <= _size)
  80. {
  81. if (_list[cpos].getCost() >= _list[dblcpos].getCost())
  82. {
  83. pos = dblcpos;
  84. }
  85. }
  86. if (cpos != pos)
  87. {
  88. temp = _list[cpos];
  89. _list[cpos] = _list[pos];
  90. _list[pos] = temp;
  91. }
  92. else
  93. {
  94. break;
  95. }
  96. }
  97. return first;
  98. }
  99. public boolean contains(GeoNode n)
  100. {
  101. if (_size == 0)
  102. {
  103. return false;
  104. }
  105. for (int i = 1; i <= _size; i++)
  106. {
  107. if (_list[i].equals(n))
  108. {
  109. return true;
  110. }
  111. }
  112. return false;
  113. }
  114. public boolean isEmpty()
  115. {
  116. return _size == 0;
  117. }
  118. }