Skip to content

Commit f71ae76

Browse files
committed
feat: everything
1 parent 87b1a40 commit f71ae76

5 files changed

Lines changed: 118 additions & 13 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Package template
22

3-
Package template for roblox-ts with rUnit tests and scripts already set up
3+
Package template for roblox-ts with rUnit testing and scripts already set up

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"name": "@rbxts/my-package",
3-
"version": "1.0.0",
4-
"description": "My package description",
5-
"author": "me",
2+
"name": "@rbxts/better-queue",
3+
"version": "0.1.0",
4+
"description": "Simple O(1) queue implementation for Roblox",
5+
"author": "runicly",
66
"main": "out/init.lua",
77
"scripts": {
88
"setup-rokit": "rokit trust lune-org/lune && rokit trust rojo-rbx/rojo && rokit install",
@@ -17,7 +17,7 @@
1717
],
1818
"repository": {
1919
"type": "git",
20-
"url": "git+https://github.com/me/my-repo.git"
20+
"url": "git+https://github.com/R-unic/better-queue.git"
2121
},
2222
"license": "ISC",
2323
"types": "out/index.d.ts",

src/index.ts

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,49 @@
1-
export function hello(name: string): string {
2-
return `Hello, ${name}!`;
1+
export class Queue<T extends defined> {
2+
private readonly cache: Map<number, T>; // cause roblox-ts doesnt let u set specific array elements
3+
private first = 0;
4+
private last: number;
5+
6+
public constructor(cache: T[] = []) {
7+
this.cache = cache as never;
8+
this.last = cache.size();
9+
}
10+
11+
public enqueue(value: T): void {
12+
const last = this.last++;
13+
this.cache.set(last + 1, value);
14+
}
15+
16+
public dequeue(): T | undefined {
17+
if (this.isEmpty()) return;
18+
19+
const first = this.first++;
20+
const value = this.cache.get(first + 1);
21+
this.cache.delete(first);
22+
23+
return value;
24+
}
25+
26+
public toArray(): T[] {
27+
const result: T[] = [];
28+
for (let i = this.first; i < this.last; i++) {
29+
const value = this.cache.get(i + 1);
30+
if (value === undefined) continue;
31+
result.push(value);
32+
}
33+
34+
return result;
35+
}
36+
37+
public at(index: number): T | undefined {
38+
if (index < 0 || index >= this.size()) return;
39+
return this.cache.get(this.first + index + 1);
40+
}
41+
42+
public isEmpty(): boolean {
43+
return this.size() <= 0;
44+
}
45+
46+
public size(): number {
47+
return math.max(this.last - this.first, 0);
48+
}
349
}

tests/src/spec.ts

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,70 @@
11
import { Assert, Fact, Theory, InlineData } from "@rbxts/runit";
2-
import { hello } from "../../src";
2+
import { Queue } from "../../src";
3+
4+
const iterator = <T extends defined>(arr: T[]) => {
5+
let i = 0;
6+
return () => arr[i++];
7+
}
8+
9+
class QueueTest {
10+
@Fact
11+
public creationAndToArray(): void {
12+
const q = new Queue([1, 2, 3, 4]);
13+
const getNext = iterator(q.toArray());
14+
Assert.equal(1, getNext());
15+
Assert.equal(2, getNext());
16+
Assert.equal(3, getNext());
17+
Assert.equal(4, getNext());
18+
}
19+
20+
@Theory
21+
@InlineData([1, 2, 3], 3)
22+
@InlineData([], 0)
23+
public size(array: defined[], expectedSize: number): void {
24+
const q = new Queue(array);
25+
Assert.equal(expectedSize, array.size());
26+
Assert.equal(expectedSize, q.size());
27+
}
28+
29+
@Theory
30+
@InlineData([1, 2, 3], false)
31+
@InlineData([], true)
32+
public isEmpty(array: defined[], expectedIsEmpty: boolean): void {
33+
const q = new Queue(array);
34+
Assert.equal(expectedIsEmpty, q.isEmpty());
35+
}
36+
37+
@Fact
38+
public enqueue(): void {
39+
const q = new Queue;
40+
q.enqueue(1);
41+
q.enqueue(2);
42+
const getNext = iterator(q.toArray());
43+
Assert.equal(1, getNext());
44+
Assert.equal(2, getNext());
45+
}
46+
47+
@Fact
48+
public dequeue(): void {
49+
const q = new Queue([1, 2, 3]);
50+
const n = q.dequeue();
51+
Assert.equal(1, n);
52+
53+
const getNext = iterator(q.toArray());
54+
Assert.equal(2, getNext());
55+
Assert.equal(3, getNext());
56+
}
357

4-
class MyPackageTest {
558
@Fact
6-
public hello(): void {
7-
Assert.equal("Hello, Runic!", hello("Runic"));
59+
public at(): void {
60+
const q = new Queue([1, 2, 3, 4]);
61+
Assert.undefined(q.at(-1));
62+
Assert.equal(1, q.at(0));
63+
Assert.equal(2, q.at(1));
64+
Assert.equal(3, q.at(2));
65+
Assert.equal(4, q.at(3));
66+
Assert.undefined(q.at(4));
867
}
968
}
1069

11-
export = MyPackageTest;
70+
export = QueueTest;

tests/test-environment.rbxl

2.17 KB
Binary file not shown.

0 commit comments

Comments
 (0)