1
1
import MinHeap from '../heap/MinHeap' ;
2
2
import Comparator from '../../utils/comparator/Comparator' ;
3
3
4
- // It is the same as min heap except that when comparing to elements
5
- // we take into account not element's value but rather its priority .
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
6
export default class PriorityQueue extends MinHeap {
7
7
constructor ( ) {
8
+ // Call MinHip constructor first.
8
9
super ( ) ;
9
- this . priorities = { } ;
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.
10
16
this . compare = new Comparator ( this . comparePriority . bind ( this ) ) ;
11
17
}
12
18
13
19
/**
14
- * @param {* } item
15
- * @param {number } [priority]
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.
16
23
* @return {PriorityQueue }
17
24
*/
18
25
add ( item , priority = 0 ) {
19
- this . priorities [ item ] = priority ;
26
+ this . priorities . set ( item , priority ) ;
20
27
super . add ( item ) ;
21
-
22
28
return this ;
23
29
}
24
30
25
31
/**
26
- * @param {* } item
27
- * @param {Comparator } [customFindingComparator]
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
28
35
* @return {PriorityQueue }
29
36
*/
30
37
remove ( item , customFindingComparator ) {
31
38
super . remove ( item , customFindingComparator ) ;
32
- delete this . priorities [ item ] ;
33
-
39
+ this . priorities . delete ( item ) ;
34
40
return this ;
35
41
}
36
42
37
43
/**
38
- * @param {* } item
39
- * @param {number } priority
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.
40
47
* @return {PriorityQueue }
41
48
*/
42
49
changePriority ( item , priority ) {
43
50
this . remove ( item , new Comparator ( this . compareValue ) ) ;
44
51
this . add ( item , priority ) ;
45
-
46
52
return this ;
47
53
}
48
54
49
55
/**
56
+ * Find item by ite value.
50
57
* @param {* } item
51
58
* @return {Number[] }
52
59
*/
@@ -55,6 +62,7 @@ export default class PriorityQueue extends MinHeap {
55
62
}
56
63
57
64
/**
65
+ * Check if item already exists in a queue.
58
66
* @param {* } item
59
67
* @return {boolean }
60
68
*/
@@ -63,19 +71,20 @@ export default class PriorityQueue extends MinHeap {
63
71
}
64
72
65
73
/**
74
+ * Compares priorities of two items.
66
75
* @param {* } a
67
76
* @param {* } b
68
77
* @return {number }
69
78
*/
70
79
comparePriority ( a , b ) {
71
- if ( this . priorities [ a ] === this . priorities [ b ] ) {
80
+ if ( this . priorities . get ( a ) === this . priorities . get ( b ) ) {
72
81
return 0 ;
73
82
}
74
-
75
- return this . priorities [ a ] < this . priorities [ b ] ? - 1 : 1 ;
83
+ return this . priorities . get ( a ) < this . priorities . get ( b ) ? - 1 : 1 ;
76
84
}
77
85
78
86
/**
87
+ * Compares values of two items.
79
88
* @param {* } a
80
89
* @param {* } b
81
90
* @return {number }
@@ -84,7 +93,6 @@ export default class PriorityQueue extends MinHeap {
84
93
if ( a === b ) {
85
94
return 0 ;
86
95
}
87
-
88
96
return a < b ? - 1 : 1 ;
89
97
}
90
98
}
0 commit comments