PriorityQueue.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. var __extends = this && this.t || function() {
  2. var extendStatics = function(i, t) {
  3. extendStatics = Object.setPrototypeOf || {
  4. __proto__: []
  5. } instanceof Array && function(i, t) {
  6. i.__proto__ = t;
  7. } || function(i, t) {
  8. for (var r in t) if (Object.prototype.hasOwnProperty.call(t, r)) i[r] = t[r];
  9. };
  10. return extendStatics(i, t);
  11. };
  12. return function(i, t) {
  13. if (typeof t !== "function" && t !== null) throw new TypeError("Class extends value " + String(t) + " is not a constructor or null");
  14. extendStatics(i, t);
  15. function __() {
  16. this.constructor = i;
  17. }
  18. i.prototype = t === null ? Object.create(t) : (__.prototype = t.prototype, new __);
  19. };
  20. }();
  21. var __read = this && this._ || function(i, t) {
  22. var r = typeof Symbol === "function" && i[Symbol.iterator];
  23. if (!r) return i;
  24. var e = r.call(i), n, u = [], s;
  25. try {
  26. while ((t === void 0 || t-- > 0) && !(n = e.next()).done) u.push(n.value);
  27. } catch (i) {
  28. s = {
  29. error: i
  30. };
  31. } finally {
  32. try {
  33. if (n && !n.done && (r = e["return"])) r.call(e);
  34. } finally {
  35. if (s) throw s.error;
  36. }
  37. }
  38. return u;
  39. };
  40. var __spreadArray = this && this.P || function(i, t, r) {
  41. if (r || arguments.length === 2) for (var e = 0, n = t.length, u; e < n; e++) {
  42. if (u || !(e in t)) {
  43. if (!u) u = Array.prototype.slice.call(t, 0, e);
  44. u[e] = t[e];
  45. }
  46. }
  47. return i.concat(u || Array.prototype.slice.call(t));
  48. };
  49. import { Base } from "../ContainerBase";
  50. var PriorityQueue = function(i) {
  51. __extends(PriorityQueue, i);
  52. function PriorityQueue(t, r, e) {
  53. if (t === void 0) {
  54. t = [];
  55. }
  56. if (r === void 0) {
  57. r = function(i, t) {
  58. if (i > t) return -1;
  59. if (i < t) return 1;
  60. return 0;
  61. };
  62. }
  63. if (e === void 0) {
  64. e = true;
  65. }
  66. var n = i.call(this) || this;
  67. n.A = r;
  68. if (Array.isArray(t)) {
  69. n.m = e ? __spreadArray([], __read(t), false) : t;
  70. } else {
  71. n.m = [];
  72. t.forEach((function(i) {
  73. return n.m.push(i);
  74. }));
  75. }
  76. n.o = n.m.length;
  77. var u = n.o >> 1;
  78. for (var s = n.o - 1 >> 1; s >= 0; --s) {
  79. n.j(s, u);
  80. }
  81. return n;
  82. }
  83. PriorityQueue.prototype.B = function(i) {
  84. var t = this.m[i];
  85. while (i > 0) {
  86. var r = i - 1 >> 1;
  87. var e = this.m[r];
  88. if (this.A(e, t) <= 0) break;
  89. this.m[i] = e;
  90. i = r;
  91. }
  92. this.m[i] = t;
  93. };
  94. PriorityQueue.prototype.j = function(i, t) {
  95. var r = this.m[i];
  96. while (i < t) {
  97. var e = i << 1 | 1;
  98. var n = e + 1;
  99. var u = this.m[e];
  100. if (n < this.o && this.A(u, this.m[n]) > 0) {
  101. e = n;
  102. u = this.m[n];
  103. }
  104. if (this.A(u, r) >= 0) break;
  105. this.m[i] = u;
  106. i = e;
  107. }
  108. this.m[i] = r;
  109. };
  110. PriorityQueue.prototype.clear = function() {
  111. this.o = 0;
  112. this.m.length = 0;
  113. };
  114. PriorityQueue.prototype.push = function(i) {
  115. this.m.push(i);
  116. this.B(this.o);
  117. this.o += 1;
  118. };
  119. PriorityQueue.prototype.pop = function() {
  120. if (!this.o) return;
  121. var i = this.m.pop();
  122. this.o -= 1;
  123. if (this.o) {
  124. this.m[0] = i;
  125. this.j(0, this.o >> 1);
  126. }
  127. };
  128. PriorityQueue.prototype.top = function() {
  129. return this.m[0];
  130. };
  131. PriorityQueue.prototype.find = function(i) {
  132. return this.m.indexOf(i) >= 0;
  133. };
  134. PriorityQueue.prototype.remove = function(i) {
  135. var t = this.m.indexOf(i);
  136. if (t < 0) return false;
  137. if (t === 0) {
  138. this.pop();
  139. } else if (t === this.o - 1) {
  140. this.m.pop();
  141. this.o -= 1;
  142. } else {
  143. this.m.splice(t, 1, this.m.pop());
  144. this.o -= 1;
  145. this.B(t);
  146. this.j(t, this.o >> 1);
  147. }
  148. return true;
  149. };
  150. PriorityQueue.prototype.updateItem = function(i) {
  151. var t = this.m.indexOf(i);
  152. if (t < 0) return false;
  153. this.B(t);
  154. this.j(t, this.o >> 1);
  155. return true;
  156. };
  157. PriorityQueue.prototype.toArray = function() {
  158. return __spreadArray([], __read(this.m), false);
  159. };
  160. return PriorityQueue;
  161. }(Base);
  162. export default PriorityQueue;