Skip to content

Commit 5e633e1

Browse files
committed
Initial commit
0 parents  commit 5e633e1

18 files changed

+5025
-0
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
end_of_line = lf
7+
indent_size = 2
8+
indent_style = space
9+
insert_final_newline = true
10+
max_line_length = 80
11+
trim_trailing_whitespace = true
12+
13+
[*.md]
14+
max_line_length = 0
15+
trim_trailing_whitespace = false

.github/CONTRIBUTING.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Example Contributing Guidelines
2+
3+
This is an example of GitHub's contributing guidelines file. Check out GitHub's [CONTRIBUTING.md help center article](https://help.github.com/articles/setting-guidelines-for-repository-contributors/) for more information.

.github/ISSUE_TEMPLATE.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
* **I'm submitting a ...**
2+
[ ] bug report
3+
[ ] feature request
4+
[ ] question about the decisions made in the repository
5+
[ ] question about how to use this project
6+
7+
* **Summary**
8+
9+
10+
11+
* **Other information** (e.g. detailed explanation, stacktraces, related issues, suggestions how to fix, links for us to have context, eg. StackOverflow, personal fork, etc.)

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
* **What kind of change does this PR introduce?** (Bug fix, feature, docs update, ...)
2+
3+
4+
5+
* **What is the current behavior?** (You can also link to an open issue here)
6+
7+
8+
9+
* **What is the new behavior (if this is a feature change)?**
10+
11+
12+
13+
* **Other information**:

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
node_modules
2+
build
3+
test
4+
src/**.js
5+
.idea/*
6+
7+
coverage
8+
.nyc_output
9+
*.log
10+
11+
package-lock.json

.npmignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
src
2+
test
3+
tsconfig.json
4+
tsconfig.module.json
5+
tslint.json
6+
.travis.yml
7+
.github
8+
.prettierignore
9+
.vscode
10+
build/docs
11+
**/*.spec.*
12+
coverage
13+
.nyc_output
14+
.idea
15+
*.log

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# package.json is formatted by package managers, so we ignore it here
2+
package.json

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Alexander Belov
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# @algo-js/priority-queue
2+
3+
Fast heap-based priority queue for production

package.json

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
{
2+
"name": "@algo-js/priority-queue",
3+
"version": "1.0.0",
4+
"description": "Fast heap-based priority queue for production",
5+
"main": "build/main/index.js",
6+
"typings": "build/main/index.d.ts",
7+
"module": "build/module/index.js",
8+
"repository": "https://github.com/IPRIT/algo-js/priority-queue",
9+
"license": "MIT",
10+
"keywords": [],
11+
"scripts": {
12+
"describe": "npm-scripts-info",
13+
"build": "run-s clean && run-p build:*",
14+
"build:main": "tsc -p tsconfig.json",
15+
"build:module": "tsc -p tsconfig.module.json",
16+
"fix": "run-s fix:*",
17+
"fix:prettier": "prettier \"src/**/*.ts\" --write",
18+
"fix:tslint": "tslint --fix --project .",
19+
"test": "run-s build test:*",
20+
"test:lint": "tslint --project . && prettier \"src/**/*.ts\" --list-different",
21+
"test:unit": "nyc --silent ava",
22+
"watch": "run-s clean build:main && run-p \"build:main -- -w\" \"test:unit -- --watch\"",
23+
"cov": "run-s build test:unit cov:html && open-cli coverage/index.html",
24+
"cov:html": "nyc report --reporter=html",
25+
"cov:send": "nyc report --reporter=lcov && codecov",
26+
"cov:check": "nyc report && nyc check-coverage --lines 100 --functions 100 --branches 100",
27+
"doc": "run-s doc:html && open-cli build/docs/index.html",
28+
"doc:html": "typedoc src/ --exclude **/*.spec.ts --target ES6 --mode file --out build/docs",
29+
"doc:json": "typedoc src/ --exclude **/*.spec.ts --target ES6 --mode file --json build/docs/typedoc.json",
30+
"doc:publish": "gh-pages -m \"[ci skip] Updates\" -d build/docs",
31+
"version": "standard-version",
32+
"clean": "trash build test",
33+
"prepare-release": "run-s test cov:check doc:html version doc:publish",
34+
"preinstall": "node -e \"if(process.env.npm_execpath.indexOf('yarn') === -1) console.warn('@algo-js/core must be installed with Yarn: https://yarnpkg.com/')\""
35+
},
36+
"scripts-info": {
37+
"info": "Display information about the package scripts",
38+
"build": "Clean and rebuild the project",
39+
"fix": "Try to automatically fix any linting problems",
40+
"test": "Lint and unit test the project",
41+
"watch": "Watch and rebuild the project on save, then rerun relevant tests",
42+
"cov": "Rebuild, run tests, then create and open the coverage report",
43+
"doc": "Generate HTML API documentation and open it in a browser",
44+
"doc:json": "Generate API documentation in typedoc JSON format",
45+
"version": "Bump package.json version, update CHANGELOG.md, tag release",
46+
"prepare-release": "One-step: clean, build, test, publish docs, and prep a release"
47+
},
48+
"engines": {
49+
"node": ">=8.9"
50+
},
51+
"dependencies": {
52+
"@algo-js/heap-tree": "^1.1.4"
53+
},
54+
"devDependencies": {
55+
"@bitjson/npm-scripts-info": "^1.0.0",
56+
"@bitjson/typedoc": "^0.15.0-0",
57+
"@istanbuljs/nyc-config-typescript": "^0.1.3",
58+
"ava": "2.2.0",
59+
"codecov": "^3.5.0",
60+
"cz-conventional-changelog": "^2.1.0",
61+
"gh-pages": "^2.0.1",
62+
"npm-run-all": "^4.1.5",
63+
"nyc": "^14.1.1",
64+
"open-cli": "^5.0.0",
65+
"prettier": "^1.18.2",
66+
"standard-version": "^6.0.1",
67+
"trash-cli": "^3.0.0",
68+
"tslint": "^5.18.0",
69+
"tslint-config-prettier": "^1.18.0",
70+
"tslint-immutable": "^6.0.1",
71+
"typescript": "^3.5.3"
72+
},
73+
"ava": {
74+
"failFast": true,
75+
"files": [
76+
"build/main/**/*.spec.js"
77+
],
78+
"sources": [
79+
"build/main/**/*.js"
80+
]
81+
},
82+
"config": {
83+
"commitizen": {
84+
"path": "cz-conventional-changelog"
85+
}
86+
},
87+
"prettier": {
88+
"singleQuote": true
89+
},
90+
"nyc": {
91+
"extends": "@istanbuljs/nyc-config-typescript",
92+
"exclude": [
93+
"**/*.spec.js"
94+
]
95+
}
96+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './lib';

src/lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './priority-queue/priority-queue';
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import test from 'ava';
2+
import { PriorityQueue } from './priority-queue';
3+
4+
interface Item {
5+
id: number;
6+
}
7+
8+
test('queue/dequeue', t => {
9+
const queue = new PriorityQueue<Item>();
10+
11+
queue.queue({ id: 1 }, 2);
12+
queue.queue({ id: 2 }, 20);
13+
queue.queue({ id: 3 }, 3);
14+
queue.queue({ id: 4 }, 5);
15+
16+
t.deepEqual(queue.peek(), { id: 2 });
17+
t.deepEqual(queue.dequeue(), { id: 2 });
18+
t.deepEqual(queue.dequeue(), { id: 4 });
19+
t.deepEqual(queue.dequeue(), { id: 3 });
20+
t.deepEqual(queue.dequeue(), { id: 1 });
21+
22+
t.deepEqual(queue.peek(), null);
23+
t.deepEqual(queue.dequeue(), null);
24+
});
25+
26+
test('size', t => {
27+
const queue = new PriorityQueue<Item>();
28+
29+
queue.queue({ id: 1 }, 2);
30+
queue.queue({ id: 2 }, 20);
31+
queue.queue({ id: 3 }, 3);
32+
queue.queue({ id: 4 }, 5);
33+
34+
t.is(queue.size, 4);
35+
36+
queue.peek();
37+
t.is(queue.size, 4);
38+
39+
queue.dequeue();
40+
t.is(queue.size, 3);
41+
queue.dequeue();
42+
t.is(queue.size, 2);
43+
queue.dequeue();
44+
t.is(queue.size, 1);
45+
46+
queue.dequeue();
47+
t.is(queue.size, 0);
48+
queue.dequeue();
49+
t.is(queue.size, 0);
50+
});
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { MaxHeapTree } from '@algo-js/heap-tree';
2+
3+
export interface Queue<T> {
4+
queue: (item: T, priority: number) => this;
5+
dequeue: () => T;
6+
7+
readonly size: number;
8+
}
9+
10+
export class PriorityQueue<T> extends MaxHeapTree<T> implements Queue<T> {
11+
/**
12+
* @returns {number}
13+
*/
14+
get size(): number {
15+
return this.heap.length;
16+
}
17+
public static defaultPriority = 1;
18+
19+
/**
20+
* @param {Map} priorities
21+
* @param {*} a
22+
* @param {*} b
23+
* @returns {number}
24+
*/
25+
public static priorityCompareFn<T>(
26+
priorities: Map<T, number>,
27+
a: T,
28+
b: T
29+
): number {
30+
const pa = priorities.get(a);
31+
const pb = priorities.get(b);
32+
return pa === pb ? 0 : pa < pb ? -1 : 1;
33+
}
34+
35+
private priorities: Map<T, number>;
36+
37+
constructor() {
38+
const priorities = new Map<T, number>();
39+
const compareFn = PriorityQueue.priorityCompareFn.bind(null, priorities);
40+
41+
super(compareFn);
42+
43+
this.priorities = priorities;
44+
}
45+
46+
/**
47+
* @param {*} element
48+
* @param {number} priority
49+
* @returns {PriorityQueue}
50+
*/
51+
public queue(
52+
element: T,
53+
priority: number = PriorityQueue.defaultPriority
54+
): this {
55+
if (this.priorities.has(element)) {
56+
throw new Error('Cannot add element that already exist');
57+
}
58+
59+
this.priorities.set(element, priority);
60+
super.add(element);
61+
62+
return this;
63+
}
64+
65+
/**
66+
* @returns {*}
67+
*/
68+
public dequeue(): T {
69+
const element = super.poll();
70+
71+
if (element) {
72+
this.priorities.delete(element);
73+
}
74+
75+
return element;
76+
}
77+
78+
public add(element: T): this {
79+
throw new Error('Use `queue` method instead');
80+
}
81+
82+
public poll(): T {
83+
throw new Error('Use `dequeue` method instead');
84+
}
85+
}

tsconfig.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2017",
4+
"outDir": "build/main",
5+
"rootDir": "src",
6+
"moduleResolution": "node",
7+
"module": "commonjs",
8+
"declaration": true,
9+
"inlineSourceMap": true,
10+
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
11+
12+
// "strict": true /* Enable all strict type-checking options. */,
13+
14+
/* Strict Type-Checking Options */
15+
// "noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */,
16+
// "strictNullChecks": true /* Enable strict null checks. */,
17+
// "strictFunctionTypes": true /* Enable strict checking of function types. */,
18+
// "strictPropertyInitialization": true /* Enable strict checking of property initialization in classes. */,
19+
// "noImplicitThis": true /* Raise error on 'this' expressions with an implied 'any' type. */,
20+
// "alwaysStrict": true /* Parse in strict mode and emit "use strict" for each source file. */,
21+
22+
/* Additional Checks */
23+
"noUnusedLocals": false /* Report errors on unused locals. */,
24+
"noUnusedParameters": false /* Report errors on unused parameters. */,
25+
"noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
26+
"noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */,
27+
28+
/* Debugging Options */
29+
"traceResolution": false /* Report module resolution log messages. */,
30+
"listEmittedFiles": false /* Print names of generated files part of the compilation. */,
31+
"listFiles": false /* Print names of files part of the compilation. */,
32+
"pretty": true /* Stylize errors and messages using color and context. */,
33+
34+
/* Experimental Options */
35+
// "experimentalDecorators": true /* Enables experimental support for ES7 decorators. */,
36+
// "emitDecoratorMetadata": true /* Enables experimental support for emitting type metadata for decorators. */,
37+
38+
"lib": ["es2017"],
39+
"types": ["node"],
40+
"typeRoots": ["node_modules/@types", "src/types"]
41+
},
42+
"include": ["src/**/*.ts"],
43+
"exclude": ["node_modules/**"],
44+
"compileOnSave": false
45+
}

0 commit comments

Comments
 (0)