2
0

BinaryNodeHeap.java 2.4 KB

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