Advertisement
Guest User

Untitled

a guest
May 25th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. import MinHeap from '../heap/MinHeap';
  2. import Comparator from '../../utils/comparator/Comparator';
  3.  
  4. // It is the same as min heap except that when comparing two elements
  5. // we take into account its priority instead of the element's value.
  6. export default class PriorityQueue extends MinHeap {
  7. constructor() {
  8. // Call MinHip constructor first.
  9. super();
  10.  
  11. // Setup priorities map.
  12. this.priorities = new Map();
  13.  
  14. // Use custom comparator for heap elements that will take element priority
  15. // instead of element value into account.
  16. this.compare = new Comparator(this.comparePriority.bind(this));
  17. }
  18.  
  19. /**
  20. * Add item to the priority queue.
  21. * @param {*} item - item we're going to add to the queue.
  22. * @param {number} [priority] - items priority.
  23. * @return {PriorityQueue}
  24. */
  25. add(item, priority = 0) {
  26. this.priorities.set(item, priority);
  27. super.add(item);
  28. return this;
  29. }
  30.  
  31. /**
  32. * Remove item from priority queue.
  33. * @param {*} item - item we're going to remove.
  34. * @param {Comparator} [customFindingComparator] - custom function for finding the item to remove
  35. * @return {PriorityQueue}
  36. */
  37. remove(item, customFindingComparator) {
  38. super.remove(item, customFindingComparator);
  39. this.priorities.delete(item);
  40. return this;
  41. }
  42.  
  43. /**
  44. * Change priority of the item in a queue.
  45. * @param {*} item - item we're going to re-prioritize.
  46. * @param {number} priority - new item's priority.
  47. * @return {PriorityQueue}
  48. */
  49. changePriority(item, priority) {
  50. this.remove(item, new Comparator(this.compareValue));
  51. this.add(item, priority);
  52. return this;
  53. }
  54.  
  55. /**
  56. * Find item by ite value.
  57. * @param {*} item
  58. * @return {Number[]}
  59. */
  60. findByValue(item) {
  61. return this.find(item, new Comparator(this.compareValue));
  62. }
  63.  
  64. /**
  65. * Check if item already exists in a queue.
  66. * @param {*} item
  67. * @return {boolean}
  68. */
  69. hasValue(item) {
  70. return this.findByValue(item).length > 0;
  71. }
  72.  
  73. /**
  74. * Compares priorities of two items.
  75. * @param {*} a
  76. * @param {*} b
  77. * @return {number}
  78. */
  79. comparePriority(a, b) {
  80. if (this.priorities.get(a) === this.priorities.get(b)) {
  81. return 0;
  82. }
  83. return this.priorities.get(a) < this.priorities.get(b) ? -1 : 1;
  84. }
  85.  
  86. /**
  87. * Compares values of two items.
  88. * @param {*} a
  89. * @param {*} b
  90. * @return {number}
  91. */
  92. compareValue(a, b) {
  93. if (a === b) {
  94. return 0;
  95. }
  96. return a < b ? -1 : 1;
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement