Skip to content

Commit 0c87bcf

Browse files
authored
add fix function (#82)
1 parent afd58da commit 0c87bcf

8 files changed

Lines changed: 118 additions & 1 deletion

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
### Added
10+
- `.fix()` to repair queue positions after mutable priorities change.
11+
912
## [6.3.5] - 2025-10-12
1013
### Fixed
1114
- Allows creating queues from list of values in O(n) runtime via constructor & fromArray

‎src/maxPriorityQueue.d.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { LegacyOptions } from './minPriorityQueue';
55
export interface MaxPriorityQueue<T> extends PriorityQueue<T> {
66
enqueue(value: T): MaxPriorityQueue<T>;
77
push(value: T): MaxPriorityQueue<T>;
8+
fix(): MaxPriorityQueue<T>;
89
}
910

1011
export const MaxPriorityQueue: {

‎src/minPriorityQueue.d.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface LegacyOptions<T> {
88
export interface MinPriorityQueue<T> extends PriorityQueue<T> {
99
enqueue(value: T): MinPriorityQueue<T>;
1010
push(value: T): MinPriorityQueue<T>;
11+
fix(): MinPriorityQueue<T>;
1112
}
1213

1314
export const MinPriorityQueue: {

‎src/priorityQueue.d.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface PriorityQueue<T> extends Iterable<T> {
1212
pop(): T | null;
1313
remove(cb: (value: T) => boolean): T[];
1414
contains(cb: (value: T) => boolean): boolean;
15+
fix(): PriorityQueue<T>;
1516
toArray(): T[];
1617
clear(): void;
1718
}

‎src/priorityQueue.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ class PriorityQueue {
148148
return this._heap.isEmpty();
149149
}
150150

151+
/**
152+
* Fixes element positions in the queue
153+
* @public
154+
* @returns {PriorityQueue}
155+
*/
156+
fix() {
157+
this._heap.fix();
158+
return this;
159+
}
160+
151161
/**
152162
* Clears the queue
153163
* @public

‎test/PriorityQueue.test.js‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,40 @@ describe('PriorityQueue', () => {
129129
});
130130
});
131131

132+
describe('fix', () => {
133+
it('fixes element positions when multiple priorities change', () => {
134+
const one = { id: 'one', priority: 1 };
135+
const two = { id: 'two', priority: 2 };
136+
const three = { id: 'three', priority: 3 };
137+
const four = { id: 'four', priority: 4 };
138+
const five = { id: 'five', priority: 5 };
139+
const six = { id: 'six', priority: 6 };
140+
const seven = { id: 'seven', priority: 7 };
141+
const qTest = PriorityQueue.fromArray(
142+
[one, two, three, four, five, six, seven],
143+
(a, b) => a.priority - b.priority
144+
);
145+
146+
one.priority = 8;
147+
four.priority = 0;
148+
seven.priority = 3.5;
149+
150+
expect(qTest.fix()).to.equal(qTest);
151+
expect(qTest.size()).to.equal(7);
152+
expect(qTest.toArray()).to.eql([
153+
four,
154+
two,
155+
three,
156+
seven,
157+
five,
158+
six,
159+
one
160+
]);
161+
expect(qTest.front()).to.equal(four);
162+
expect(qTest.back()).to.equal(one);
163+
});
164+
});
165+
132166
describe('iterator', () => {
133167
it('allows iterating on queue elements', () => {
134168
const testArr = [20, 30, 40, 50, 80, 90];

‎test/maxPriorityQueue.test.js‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,40 @@ describe('MaxPriorityQueue', () => {
127127
expect(qTest.pop()).to.eql(20);
128128
});
129129
});
130+
131+
describe('fix', () => {
132+
it('fixes element positions when multiple priorities change', () => {
133+
const one = { id: 'one', priority: 1 };
134+
const two = { id: 'two', priority: 2 };
135+
const three = { id: 'three', priority: 3 };
136+
const four = { id: 'four', priority: 4 };
137+
const five = { id: 'five', priority: 5 };
138+
const six = { id: 'six', priority: 6 };
139+
const seven = { id: 'seven', priority: 7 };
140+
const qTest = MaxPriorityQueue.fromArray(
141+
[one, two, three, four, five, six, seven],
142+
(value) => value.priority
143+
);
144+
145+
one.priority = 8;
146+
four.priority = 0;
147+
seven.priority = 3.5;
148+
149+
expect(qTest.fix()).to.equal(qTest);
150+
expect(qTest.size()).to.equal(7);
151+
expect(qTest.toArray()).to.eql([
152+
one,
153+
six,
154+
five,
155+
seven,
156+
three,
157+
two,
158+
four
159+
]);
160+
expect(qTest.front()).to.equal(one);
161+
expect(qTest.back()).to.equal(four);
162+
});
163+
});
130164
});
131165

132166
describe('constructor with initial values', () => {

‎test/minPriorityQueue.test.js‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ describe('MinPriorityQueue', () => {
111111
expect(qTest.contains((n) => n === 100)).to.equal(false);
112112
});
113113
});
114-
114+
115115
describe('remove', () => {
116116
it('remove elements that match a criteria', () => {
117117
const testArr = [20, 30, 40, 50, 80, 90];
@@ -124,6 +124,39 @@ describe('MinPriorityQueue', () => {
124124
});
125125
});
126126

127+
describe('fix', () => {
128+
it('fixes element positions when multiple priorities change', () => {
129+
const one = { id: 'one', priority: 1 };
130+
const two = { id: 'two', priority: 2 };
131+
const three = { id: 'three', priority: 3 };
132+
const four = { id: 'four', priority: 4 };
133+
const five = { id: 'five', priority: 5 };
134+
const six = { id: 'six', priority: 6 };
135+
const seven = { id: 'seven', priority: 7 };
136+
const qTest = MinPriorityQueue.fromArray(
137+
[one, two, three, four, five, six, seven],
138+
(value) => value.priority
139+
);
140+
141+
one.priority = 8;
142+
four.priority = 0;
143+
seven.priority = 3.5;
144+
145+
expect(qTest.fix()).to.equal(qTest);
146+
expect(qTest.size()).to.equal(7);
147+
expect(qTest.toArray()).to.eql([
148+
four,
149+
two,
150+
three,
151+
seven,
152+
five,
153+
six,
154+
one
155+
]);
156+
expect(qTest.front()).to.equal(four);
157+
expect(qTest.back()).to.equal(one);
158+
});
159+
});
127160
});
128161

129162
describe('constructor with initial values', () => {

0 commit comments

Comments
 (0)