Skip to content

Commit

Permalink
add eslint
Browse files Browse the repository at this point in the history
add prettier
add better benchmark
  • Loading branch information
keriati committed Jan 1, 2024
1 parent 8780b72 commit 0711526
Show file tree
Hide file tree
Showing 18 changed files with 1,425 additions and 334 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist/
node_modules
coverage
31 changes: 31 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module.exports = {
env: {
browser: true,
es2021: true,
},
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier",
],
overrides: [
{
env: {
node: true,
},
files: [".eslintrc.{js,cjs}"],
parserOptions: {
sourceType: "script",
},
},
],
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: "latest",
sourceType: "module",
},
plugins: ["@typescript-eslint"],
rules: {
"@typescript-eslint/no-unused-vars": "error",
},
};
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist
node_modules
coverage
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Overview

`BucketQueue` is a priority queue implementation ideal for algorithms requiring efficient, priority-based item
management, such as Dijkstra's algorithm. It's designed to offer quick enqueue and dequeue operations, particularly
management, such as Dijkstra's algorithm. It's designed to offer quick enqueue and dequeue operations, particularly
when the priority key space is small and comprised of positive integers, a common scenario in many programming puzzles.

## Installation
Expand All @@ -19,10 +19,14 @@ npm install bucket-priority-queue
Here's how you can integrate BucketQueue into your project:

```typescript
import { BucketQueue } from 'bucket-priority-queue';
import { BucketQueue } from "bucket-priority-queue";

// Initialize the queue with optional initial items
const queue = new BucketQueue<number>([[1, 1], [2, 2], [3, 3]]);
const queue = new BucketQueue<number>([
[1, 1],
[2, 2],
[3, 3],
]);

// Adding items with priority
queue.push(5, 1); // item 5 with priority 1
Expand All @@ -43,4 +47,4 @@ const lowest = queue.popLowest(); // returns item with lowest priority

**popLowest(): T | undefined** - Removes and returns the item with the lowest priority.

**size: number** - Returns the current size of the queue.
**size: number** - Returns the current size of the queue.
45 changes: 0 additions & 45 deletions benchmark/index.ts

This file was deleted.

52 changes: 52 additions & 0 deletions benchmark/popPush.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import Benchmark from "benchmark";
import { BucketQueue } from "../src";
import Heap from "heap-js";

const suit = new Benchmark.Suite({
initCount: 1000,
});

const rounds = 1_000_000;
const priorityMax = 1_000;

const items: [string, number][] = [];

for (let i = 0; i < rounds; i++) {
items.push([`item-${i}`, i % priorityMax]);
}

const bq = new BucketQueue<string>();
const heap = new Heap<[string, number]>((a, b) => a[1] - b[1]);

for (let i = 0; i < items.length; i++) {
bq.push(items[i][0], items[i][1]);
heap.push(items[i]);
}

suit
.add("BucketQueue", () => {
const i1 = bq.popLowest() as string;
const i2 = bq.popLowest() as string;
const i3 = bq.popLowest() as string;

bq.push(i3, 0);
bq.push(i2, 0);
bq.push(i1, 0);
})
.add("Heap", () => {
const i1 = heap.pop() as [string, number];
const i2 = heap.pop() as [string, number];
const i3 = heap.pop() as [string, number];

heap.push(i3);
heap.push(i2);
heap.push(i1);
})
.on("cycle", function (e: Event) {
console.log("" + e.target);
})
.on("complete", function () {
// @ts-expect-error - no error expected
console.log("Fastest is " + this.filter("fastest").map("name"));
})
.run();
4 changes: 2 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/index.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// src/BucketQueue.ts
var BucketQueue = class {
constructor(items = []) {
this.buckets = [];
this.buckets = new Array(100);
this.priorityMax = 0;
this.priorityMin = 0;
this._size = 0;
Expand All @@ -12,7 +12,7 @@ var BucketQueue = class {
}
push(item, priority) {
if (!this.buckets[priority]) {
this.buckets[priority] = [];
this.buckets[priority] = new Array(1e3);
}
this.buckets[priority].push(item);
if (this._size === 0) {
Expand Down
2 changes: 1 addition & 1 deletion dist/index.mjs.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
};
preset: "ts-jest",
testEnvironment: "node",
};
Loading

0 comments on commit 0711526

Please sign in to comment.