-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3993ab6
commit ef50e0b
Showing
6 changed files
with
117 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,6 +102,3 @@ dist | |
|
||
# TernJS port file | ||
.tern-port | ||
|
||
|
||
yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,32 @@ | ||
# priority-queue | ||
Priority queue(优先队列) implementation in JavaScript | ||
> Priority queue(优先队列) implementation in JavaScript | ||
> 学习项目,参考自:[mourner/tinyqueue](https://github.com/mourner/tinyqueue) | ||
## install | ||
```sh | ||
npm i @kartjim/priority-queue | ||
``` | ||
|
||
## import | ||
```js | ||
import PriorityQueue from '@kartjim/priority-queue'; | ||
``` | ||
|
||
or use CDN: | ||
```js | ||
<script src="https://cdn.jsdelivr.net/npm/@kartjim/[email protected]/priorityqueue.min.js"></script> | ||
``` | ||
|
||
## API | ||
- constructor | ||
- `constructor(data?: T[],compare?: (a: T, b: T) => number);` | ||
- `push(val: T) : void;` | ||
- push an element into PriorityQueue | ||
- `peek() : T | undefined;` | ||
- get the top element | ||
- `pop() : T | undefined;` | ||
- pop an element, and return it | ||
- `isEmpty() : boolean;` | ||
- check if the PriorityQueue is empty | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import PriorityQueue from '../index.js'; | ||
|
||
const NUM = 1e5; | ||
|
||
const data = []; | ||
for (let i = 0; i < NUM; i++) { | ||
data.push({ | ||
value: Math.random() | ||
}); | ||
} | ||
|
||
const heap = new PriorityQueue(data, (a, b) => a.value - b.value); | ||
|
||
console.time(`push ${NUM}`); | ||
for (let i = 0; i < NUM; i++) { | ||
heap.push(data[i]); | ||
} | ||
console.timeEnd(`push ${NUM}`); // push 100000: 47.846ms | ||
|
||
console.time(`pop ${NUM}`); | ||
for (let i = 0; i < NUM; i++) { | ||
heap.pop(); | ||
} | ||
console.timeEnd(`pop ${NUM}`); // pop 100000: 153.495ms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import {test} from 'tape'; | ||
import PriorityQueue from '../index.js'; | ||
|
||
// create data | ||
const data = []; | ||
for (let i = 0; i < 50; i++) { | ||
data.push(~~(50 * Math.random())); | ||
} | ||
|
||
const sorted = data.slice().sort((a, b) => a - b); | ||
|
||
test('maintains a default params PriorityQueue', (t) => { | ||
const heap = new PriorityQueue(); | ||
for (let i = 0; i < data.length; i++) { | ||
heap.push(data[i]); | ||
} | ||
|
||
t.equal(heap.peek(), sorted[0]); | ||
|
||
let ans = []; | ||
while (!heap.isEmpty()) { | ||
ans.push(heap.pop()); | ||
} | ||
|
||
t.deepEqual(ans, sorted); | ||
// console.log(ans); | ||
|
||
t.end(); | ||
}); | ||
|
||
test('get data from constructor', (t) => { | ||
const heap = new PriorityQueue(data.slice()); | ||
|
||
let ans = []; | ||
while (!heap.isEmpty()) { | ||
ans.push(heap.pop()); | ||
} | ||
|
||
t.deepEqual(ans, sorted); | ||
|
||
t.end(); | ||
}); | ||
|
||
test('push and pop some elements', (t) => { | ||
const heap = new PriorityQueue(); | ||
|
||
t.same(heap.data, []); | ||
heap.push(3); | ||
heap.push(2); | ||
t.equal(heap.pop(), 2); | ||
t.equal(heap.pop(), 3); | ||
t.equal(heap.pop(), undefined); | ||
|
||
t.end(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters