Skip to content

Commit

Permalink
Add queue
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaitanLyss committed Jul 20, 2024
1 parent 176362b commit 770bfb1
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@selenite/commons",
"version": "0.8.5",
"version": "0.9.0",
"scripts": {
"dev": "npm run wasm && vite dev",
"wasm": "wasm-pack build ./selenite-commons-rs --target web",
Expand Down
50 changes: 50 additions & 0 deletions src/lib/datastructure.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { describe, test, expect } from 'vitest';
import { Queue } from './datastructure';

describe('datastructures', () => {
describe('queue', () => {
test('queue sizes should be valid', () => {
const queue = new Queue<number>()
expect(queue.size).toBe(0)
queue.add(1)
expect(queue.size).toBe(1)
queue.add(2)
expect(queue.size).toBe(2)
queue.remove()
expect(queue.size).toBe(1)
queue.remove()
expect(queue.size).toBe(0)
})
test('should be FIFO', () => {
const queue = new Queue([1, 2, 3]);
let res: number[] = [];
let c = 0
while (queue.size > 0) {
c++
if (c > 3) {
throw new Error("Too many iterations.")
}
res.push(queue.remove());
}
expect(res).toEqual([1, 2, 3]);
});
test('should error on peek empty queue', () => {
const queue = new Queue();
expect(() => queue.peek()).toThrowError("Can't peek, queue is empty.");
});
test('should error on remove empty queue', () => {
const queue = new Queue();
expect(() => queue.remove()).toThrowError("Can't remove an element, queue is empty.");
})
test('peek should return first element without changing queue', () => {
const queue = new Queue([1, 2, 3]);
expect(queue.peek()).toBe(1);
expect(queue.size).toBe(3);
});
test('pop should remove first element and return it', () => {
const queue = new Queue([1, 2, 3]);
expect(queue.remove()).toBe(1);
expect(queue.size).toBe(2);
});
});
});
42 changes: 42 additions & 0 deletions src/lib/datastructure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export class Queue<T> {
#inStack: T[] = [];
#outStack: T[] = [];

constructor(iterable?: Iterable<T>) {
if (iterable) this.#inStack.push(...iterable);
}

add(e: T) {
this.#inStack.push(e);
}

peek(): T {
this.handleEmptyOut("Can't peek, queue is empty.")
return this.#outStack.at(-1)!;
}

remove(): T {
this.handleEmptyOut("Can't remove an element, queue is empty.");
return this.#outStack.pop()!;
}

private handleEmptyOut(errMsg: string) {
if (this.#outStack.length === 0) {
this.swapStacks();
}
if (this.#outStack.length === 0) {
throw new Error(errMsg);
}
}

get size() {
return this.#inStack.length + this.#outStack.length;
}

private swapStacks() {
let mem = this.#outStack;
this.#outStack = this.#inStack;
this.#inStack = mem;
this.#outStack.reverse();
}
}
4 changes: 3 additions & 1 deletion src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ export * from './actions/index.js'
export * as Utils from './utils'
export * from './utils/index.js'
export * as Types from './type'
export * from './type'
export * from './type'
export * as DataStructure from './datastructure'
export * from './datastructure'

0 comments on commit 770bfb1

Please sign in to comment.