Skip to content

Commit

Permalink
fix bug with getFreeTailIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
NeoPhi committed Mar 30, 2024
1 parent af3166d commit 053368a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# 1.4.0

- Fix data corruption with setting and immediately deleting the same key when more than one item is already in the free list

# 1.3.0

- Fix compile settings for MJS

# 1.2.0

- Support CJS and MJS exports
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ export class SieveCache<K, V> implements Map<K, V> {
#getFreeTailIndex(): number {
const freeIndex = this.#freeTailIndex;
this.#freeTailIndex = this.#nextIndexes[freeIndex];
this.#nextIndexes[freeIndex] = 0;
if (this.#freeTailIndex === 0) {
this.#freeHeadIndex = 0;
}
Expand Down
18 changes: 18 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,22 @@ describe("SieveCache", () => {
sieveCache.set("c", "c");
deepStrictEqual(new Set(sieveCache.keys()), new Set(["b", "c"]));
});

it("manages free indexes correctly when deleting", () => {
const sieveCache = new SieveCache(3);
sieveCache.set("1", "1");
sieveCache.set("3", "3");
sieveCache.set("2", "2");
sieveCache.delete("3");
sieveCache.set("0", "0");
sieveCache.delete("1");
sieveCache.delete("2");
sieveCache.set("3", "3");
sieveCache.delete("3");
sieveCache.delete("0");
sieveCache.set("3", "3");
sieveCache.set("1", "1");
sieveCache.set("2", "2");
strictEqual(sieveCache.get("3"), "3");
});
});

0 comments on commit 053368a

Please sign in to comment.