3eb49ffaad969f2bdc52ba3fe4622ba540f78cd161648862a13ba897cb9d28eb68eb8e4d2c43bdd178d2b0b97c9b7f890a4a23970c38187e299b80e257e4f6 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. Object.defineProperty(exports, "t", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _ContainerBase = require("../ContainerBase");
  7. class PriorityQueue extends _ContainerBase.Base {
  8. constructor(t = [], s = function(t, s) {
  9. if (t > s) return -1;
  10. if (t < s) return 1;
  11. return 0;
  12. }, i = true) {
  13. super();
  14. this.v = s;
  15. if (Array.isArray(t)) {
  16. this.C = i ? [ ...t ] : t;
  17. } else {
  18. this.C = [];
  19. const s = this;
  20. t.forEach((function(t) {
  21. s.C.push(t);
  22. }));
  23. }
  24. this.i = this.C.length;
  25. const e = this.i >> 1;
  26. for (let t = this.i - 1 >> 1; t >= 0; --t) {
  27. this.k(t, e);
  28. }
  29. }
  30. m(t) {
  31. const s = this.C[t];
  32. while (t > 0) {
  33. const i = t - 1 >> 1;
  34. const e = this.C[i];
  35. if (this.v(e, s) <= 0) break;
  36. this.C[t] = e;
  37. t = i;
  38. }
  39. this.C[t] = s;
  40. }
  41. k(t, s) {
  42. const i = this.C[t];
  43. while (t < s) {
  44. let s = t << 1 | 1;
  45. const e = s + 1;
  46. let h = this.C[s];
  47. if (e < this.i && this.v(h, this.C[e]) > 0) {
  48. s = e;
  49. h = this.C[e];
  50. }
  51. if (this.v(h, i) >= 0) break;
  52. this.C[t] = h;
  53. t = s;
  54. }
  55. this.C[t] = i;
  56. }
  57. clear() {
  58. this.i = 0;
  59. this.C.length = 0;
  60. }
  61. push(t) {
  62. this.C.push(t);
  63. this.m(this.i);
  64. this.i += 1;
  65. }
  66. pop() {
  67. if (this.i === 0) return;
  68. const t = this.C[0];
  69. const s = this.C.pop();
  70. this.i -= 1;
  71. if (this.i) {
  72. this.C[0] = s;
  73. this.k(0, this.i >> 1);
  74. }
  75. return t;
  76. }
  77. top() {
  78. return this.C[0];
  79. }
  80. find(t) {
  81. return this.C.indexOf(t) >= 0;
  82. }
  83. remove(t) {
  84. const s = this.C.indexOf(t);
  85. if (s < 0) return false;
  86. if (s === 0) {
  87. this.pop();
  88. } else if (s === this.i - 1) {
  89. this.C.pop();
  90. this.i -= 1;
  91. } else {
  92. this.C.splice(s, 1, this.C.pop());
  93. this.i -= 1;
  94. this.m(s);
  95. this.k(s, this.i >> 1);
  96. }
  97. return true;
  98. }
  99. updateItem(t) {
  100. const s = this.C.indexOf(t);
  101. if (s < 0) return false;
  102. this.m(s);
  103. this.k(s, this.i >> 1);
  104. return true;
  105. }
  106. toArray() {
  107. return [ ...this.C ];
  108. }
  109. }
  110. var _default = PriorityQueue;
  111. exports.default = _default;
  112. //# sourceMappingURL=PriorityQueue.js.map