Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8ea21a6

Browse files
committed
Make it possible to use objects in priority queue.
1 parent 881e3ae commit 8ea21a6

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed
Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,59 @@
11
import MinHeap from '../heap/MinHeap';
22
import Comparator from '../../utils/comparator/Comparator';
33

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.
66
export default class PriorityQueue extends MinHeap {
77
constructor() {
8+
// Call MinHip constructor first.
89
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.
1016
this.compare = new Comparator(this.comparePriority.bind(this));
1117
}
1218

1319
/**
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.
1623
* @return {PriorityQueue}
1724
*/
1825
add(item, priority = 0) {
19-
this.priorities[item]=priority;
26+
this.priorities.set(item,priority);
2027
super.add(item);
21-
2228
return this;
2329
}
2430

2531
/**
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
2835
* @return {PriorityQueue}
2936
*/
3037
remove(item, customFindingComparator) {
3138
super.remove(item, customFindingComparator);
32-
delete this.priorities[item];
33-
39+
this.priorities.delete(item);
3440
return this;
3541
}
3642

3743
/**
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.
4047
* @return {PriorityQueue}
4148
*/
4249
changePriority(item, priority) {
4350
this.remove(item, new Comparator(this.compareValue));
4451
this.add(item, priority);
45-
4652
return this;
4753
}
4854

4955
/**
56+
* Find item by ite value.
5057
* @param {*} item
5158
* @return {Number[]}
5259
*/
@@ -55,6 +62,7 @@ export default class PriorityQueue extends MinHeap {
5562
}
5663

5764
/**
65+
* Check if item already exists in a queue.
5866
* @param {*} item
5967
* @return {boolean}
6068
*/
@@ -63,19 +71,20 @@ export default class PriorityQueue extends MinHeap {
6371
}
6472

6573
/**
74+
* Compares priorities of two items.
6675
* @param {*} a
6776
* @param {*} b
6877
* @return {number}
6978
*/
7079
comparePriority(a, b) {
71-
if (this.priorities[a] === this.priorities[b]) {
80+
if (this.priorities.get(a) === this.priorities.get(b)) {
7281
return 0;
7382
}
74-
75-
return this.priorities[a] < this.priorities[b] ? -1 : 1;
83+
return this.priorities.get(a) < this.priorities.get(b) ? -1 : 1;
7684
}
7785

7886
/**
87+
* Compares values of two items.
7988
* @param {*} a
8089
* @param {*} b
8190
* @return {number}
@@ -84,7 +93,6 @@ export default class PriorityQueue extends MinHeap {
8493
if (a === b) {
8594
return 0;
8695
}
87-
8896
return a < b ? -1 : 1;
8997
}
9098
}

‎src/data-structures/priority-queue/__test__/PriorityQueue.test.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,23 @@ describe('PriorityQueue', () => {
2020
expect(priorityQueue.peek()).toBe(100);
2121
});
2222

23+
it('should be possible to use objects in priority queue', () => {
24+
const priorityQueue = new PriorityQueue();
25+
26+
const user1 = { name: 'Mike' };
27+
const user2 = { name: 'Bill' };
28+
const user3 = { name: 'Jane' };
29+
30+
priorityQueue.add(user1, 1);
31+
expect(priorityQueue.peek()).toBe(user1);
32+
33+
priorityQueue.add(user2, 2);
34+
expect(priorityQueue.peek()).toBe(user1);
35+
36+
priorityQueue.add(user3, 0);
37+
expect(priorityQueue.peek()).toBe(user3);
38+
});
39+
2340
it('should poll from queue with respect to priorities', () => {
2441
const priorityQueue = new PriorityQueue();
2542

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /