Skip to content

Commit

Permalink
done!
Browse files Browse the repository at this point in the history
  • Loading branch information
can-dy-jack committed Jan 17, 2023
1 parent 3993ab6 commit ef50e0b
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 17 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,3 @@ dist

# TernJS port file
.tern-port


yarn.lock
27 changes: 26 additions & 1 deletion README.md
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

24 changes: 24 additions & 0 deletions __test__/bench.js
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
55 changes: 55 additions & 0 deletions __test__/test.js
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();
});
15 changes: 8 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default class PriorityQueue {
constructor(
data = [],
compare = function (a, b) {
return a === b ? 0 : a > b ? 1 : -1;
return a > b ? 1 : a < b ? -1 : 0;
}
) {
this.data = data;
Expand Down Expand Up @@ -51,21 +51,22 @@ export default class PriorityQueue {
if (this.isEmpty()) return undefined;

const top = this.data[0];
const bottom = this.data.pop();

this.size--;
if (this.isEmpty()) {
return this.data.pop();
if (!this.isEmpty()) {
this.data[0] = bottom;
this._up(0);
}
this.data[0] = this.data.pop();
this._up(0);
return top;
}

_up(pos) {
let half = Math.floor(this.size / 2);
let val = this.data[pos];
while (pos < half) {
let more = pos * 2 + 1;
let right = more + 2;
let more = (pos << 1) + 1;
let right = more + 1;
if (right < this.size && this.compare(this.data[right], this.data[more]) < 0) {
more = right;
}
Expand Down
10 changes: 4 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
"unpkg": "priorityqueue.min.js",
"types": "index.d.ts",
"scripts": {
"lint": "eslint index.js",
"pretest": "npm run lint",
"test": "tape -r esm test.js",
"bench": "node -r esm bench.js",
"lint": "eslint index.js __test__/test.js __test__/bench.js",
"test": "tape -r esm __test__/test.js",
"bench": "node -r esm __test__/bench.js",
"build": "rollup -c",
"watch": "rollup -cw",
"prepublishOnly": "npm test"
"watch": "rollup -cw"
},
"files": [
"index.js",
Expand Down

0 comments on commit ef50e0b

Please sign in to comment.