Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2 Feature/slot map #4

Merged
merged 8 commits into from
Mar 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
save-exact=true
59 changes: 43 additions & 16 deletions src/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { Entity, World, IWorld } from "../index";
import { Entity, World, IWorld, ComponentFactory } from "../index";

describe("Entity", () => {
it("should throw error", () => {
Expand All @@ -21,9 +21,9 @@ describe("World", () => {

const entity0 = world.entity().build();
const entity1 = world.entity().build();
const result = world.query(Entity).result();

expect(entity0).toBe(0);
expect(entity1).toBe(1);
expect(result).toEqual([[entity0], [entity1]]);
});

it("should throw when given unregistered component", () => {
Expand All @@ -49,7 +49,6 @@ describe("World", () => {
world.entity().with(Name)("Tom").build();
world.delete(entity1);

expect(world.entity().build()).toEqual(entity1);
expect(world.query(Name).result()).toEqual([
[{ name: "Bob" }],
[{ name: "Tom" }],
Expand All @@ -60,7 +59,7 @@ describe("World", () => {
const world = World({});

expect(() => {
world.delete(0);
world.delete({ generation: 99, index: 0 });
}).not.toThrow();
});
});
Expand Down Expand Up @@ -141,7 +140,7 @@ describe("World", () => {

it("should throw on unknown entity", () => {
expect(() => {
world.add(Name, 77)("Bob");
world.add(Name, { generation: 99, index: 9 })("Bob");
}).toThrowError("unknown entity");
});
});
Expand Down Expand Up @@ -176,35 +175,45 @@ describe("World", () => {
it("should throw on unregistered component", () => {
world.entity().build();
expect(() => {
world.remove(Position, 0);
world.remove(Position, { generation: 1, index: 1 });
}).toThrowError("unknown Component");
});

it("should throw on unknown entity", () => {
world.register(Position);
expect(() => {
world.remove(Position, 0);
world.remove(Position, { generation: 1, index: 1 });
}).toThrowError("unknown entity");
});
});

describe("query_iter", () => {
let A: ComponentFactory;
let B: ComponentFactory;

beforeEach(() => {
A = () => ({});
B = () => ({});

world.register(Name);
world.register(Position);
world.register(Velocity);
world.register(A);
world.register(B);
world
.entity()
.with(Name)("Bob")
.with(Position)(1, 2)
.with(Velocity)(0, 2)
.with(A)()
.build();
world.entity().with(Name)("Roger").with(Position)(0, -1).build();
world
.entity()
.with(Name)("Tom")
.with(Position)(-9, 3)
.with(Velocity)(1, 1)
.with(B)()
.build();
});

Expand All @@ -218,7 +227,7 @@ describe("World", () => {
});

it("should allow negative searches", () => {
const result = world.query_iter(Name).not(Velocity).collect();
const result = world.query_iter(Name).not(A).not(B).collect();

expect(result).toEqual([[{ name: "Roger" }]]);
});
Expand All @@ -227,9 +236,18 @@ describe("World", () => {
const result = world.query_iter(Position, Entity).collect();

expect(result).toEqual([
[{ x: 1, y: 2 }, 0],
[{ x: 0, y: -1 }, 1],
[{ x: -9, y: 3 }, 2],
[
{ x: 1, y: 2 },
{ generation: 1, index: 0 },
],
[
{ x: 0, y: -1 },
{ generation: 1, index: 1 },
],
[
{ x: -9, y: 3 },
{ generation: 1, index: 2 },
],
]);
});

Expand All @@ -241,7 +259,7 @@ describe("World", () => {
});

it("should be iterable", () => {
const result = [];
const result: string[] = [];

for (const [name] of world.query_iter(Name)) {
result.push(name.name);
Expand Down Expand Up @@ -310,9 +328,18 @@ describe("World", () => {
const result = world.query(Position, Entity).result();

expect(result).toEqual([
[{ x: 1, y: 2 }, 0],
[{ x: 0, y: -1 }, 1],
[{ x: -9, y: 3 }, 2],
[
{ x: 1, y: 2 },
{ generation: 1, index: 0 },
],
[
{ x: 0, y: -1 },
{ generation: 1, index: 1 },
],
[
{ x: -9, y: 3 },
{ generation: 1, index: 2 },
],
]);
});

Expand Down
21 changes: 21 additions & 0 deletions src/binary_number_heap/__tests__/binary_number_heap.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { BinaryNumberHeap } from "../binary_number_heap";

describe("BinaryNumberHeap", () => {
it("should store numbers in order", () => {
const heap = new BinaryNumberHeap();
[10, 3, 4, 8, 2, 9, 7, 1, 2, 6, 3, 5].forEach((x) => {
heap.push(x);
});

heap.remove(2);

const result: number[] = [];

while (heap.size() > 0) {
result.push(heap.pop()!);
}

expect(result).toEqual([1, 2, 3, 3, 4, 5, 6, 7, 8, 9, 10]);
});
});
96 changes: 96 additions & 0 deletions src/binary_number_heap/binary_number_heap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
// https://eloquentjavascript.net/1st_edition/appendix2.html
// We aren't making it generic in order to get the most performance
export class BinaryNumberHeap {
content: number[] = [];

push(value: number) {
this.content.push(value);
this.bubbleUp(this.content.length - 1);
}

pop(): number | undefined {
const result = this.content[0];
const end = this.content.pop()!;

if (this.content.length > 0) {
this.content[0] = end;
this.sinkDown(0);
}

return result;
}

remove(value: number) {
const length = this.content.length;

for (let i = 0; i < length; ++i) {
if (this.content[i] !== value) {
continue;
}

const end = this.content.pop()!;
if (i === length - 1) {
break;
}

this.content[i] = end;
this.bubbleUp(i);
this.sinkDown(i);
break;
}
}

size(): number {
return this.content.length;
}

private bubbleUp(index: number) {
const element = this.content[index];

while (index > 0) {
const parentN = (((index + 1) / 2) | 0) - 1;
const parent = this.content[parentN];
if (element >= parent) {
break;
}

this.content[parentN] = element;
this.content[index] = parent;
index = parentN;
}
}

private sinkDown(index: number) {
const length = this.content.length;
const element = this.content[index];

while (true) {
const child2N = (index + 1) * 2;
const child1N = child2N - 1;
let swap: number | null = null;

if (child1N < length) {
const child1 = this.content[child1N];
if (child1 < element) {
swap = child1N;
}
}

if (child2N < length) {
const child2 = this.content[child2N];
if (child2 < (swap === null ? element : this.content[child1N])) {
swap = child2N;
}
}

if (swap === null) {
break;
}

this.content[index] = this.content[swap];
this.content[swap] = element;
index = swap;
}
}
}
Loading