0b8891b1c332e9118b15036b1c094488bec5ab7570f64de9e3949c645bc9b274785f21769be38178411ac4ad142251162f8bceb2a83c8ebc6217e972db82b9 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { Base, initContainer } from "../ContainerBase";
  2. declare class PriorityQueue<T> extends Base {
  3. /**
  4. * @description PriorityQueue's constructor.
  5. * @param container - Initialize container, must have a forEach function.
  6. * @param cmp - Compare function.
  7. * @param copy - When the container is an array, you can choose to directly operate on the original object of
  8. * the array or perform a shallow copy. The default is shallow copy.
  9. * @example
  10. * new PriorityQueue();
  11. * new PriorityQueue([1, 2, 3]);
  12. * new PriorityQueue([1, 2, 3], (x, y) => x - y);
  13. * new PriorityQueue([1, 2, 3], (x, y) => x - y, false);
  14. */
  15. constructor(container?: initContainer<T>, cmp?: (x: T, y: T) => number, copy?: boolean);
  16. clear(): void;
  17. /**
  18. * @description Push element into a container in order.
  19. * @param item - The element you want to push.
  20. * @returns The size of heap after pushing.
  21. * @example
  22. * queue.push(1);
  23. */
  24. push(item: T): void;
  25. /**
  26. * @description Removes the top element.
  27. * @returns The element you popped.
  28. * @example
  29. * queue.pop();
  30. */
  31. pop(): T | undefined;
  32. /**
  33. * @description Accesses the top element.
  34. * @example
  35. * const top = queue.top();
  36. */
  37. top(): T | undefined;
  38. /**
  39. * @description Check if element is in heap.
  40. * @param item - The item want to find.
  41. * @returns Whether element is in heap.
  42. * @example
  43. * const que = new PriorityQueue([], (x, y) => x.id - y.id);
  44. * const obj = { id: 1 };
  45. * que.push(obj);
  46. * console.log(que.find(obj)); // true
  47. */
  48. find(item: T): boolean;
  49. /**
  50. * @description Remove specified item from heap.
  51. * @param item - The item want to remove.
  52. * @returns Whether remove success.
  53. * @example
  54. * const que = new PriorityQueue([], (x, y) => x.id - y.id);
  55. * const obj = { id: 1 };
  56. * que.push(obj);
  57. * que.remove(obj);
  58. */
  59. remove(item: T): boolean;
  60. /**
  61. * @description Update item and it's pos in the heap.
  62. * @param item - The item want to update.
  63. * @returns Whether update success.
  64. * @example
  65. * const que = new PriorityQueue([], (x, y) => x.id - y.id);
  66. * const obj = { id: 1 };
  67. * que.push(obj);
  68. * obj.id = 2;
  69. * que.updateItem(obj);
  70. */
  71. updateItem(item: T): boolean;
  72. /**
  73. * @returns Return a copy array of heap.
  74. * @example
  75. * const arr = queue.toArray();
  76. */
  77. toArray(): T[];
  78. }
  79. export default PriorityQueue;