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

feat: add doubly-linked list implementation #970

Merged
merged 4 commits into from
Aug 14, 2021
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
100 changes: 100 additions & 0 deletions src/tools/cache.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { expect, test } from "@jest/globals";
import { LRUCache, MRUCache } from "./cache";

test("new LRU cache is not full", () => {
const cache = new LRUCache<number>(3);
expect(cache.isFull()).toBe(false);
});

test("put into empty LRU cache", () => {
const cache = new LRUCache<number>(3);
const value = cache.put(10);
expect(value).toBe(undefined);
expect(cache.oldest()).toBe(10);
expect(cache.oldest()).toBe(cache.newest());
});

test("remove from empty LRU cache", () => {
const cache = new LRUCache<number>(3);
cache.remove(10);
expect(cache.asArray()).toEqual({});
});

test("put into non-empty LRU cache", () => {
const cache = new LRUCache<number>(3);
cache.put(10);
cache.put(20);
const value = cache.put(30);
expect(value).toBe(undefined);
expect(cache.oldest()).toBe(10);
expect(cache.newest()).toBe(30);
expect(cache.asArray()).toEqual({ 1: 10, 2: 20, 3: 30 });
});

test("remove from non-empty LRU cache", () => {
const cache = new LRUCache<number>(3);
cache.put(10);
cache.put(20);
cache.remove(10);
expect(cache.oldest()).toBe(20);
expect(cache.asArray()).toEqual({ 1: 20 });
});

test("put into full LRU cache", () => {
const cache = new LRUCache<number>(3);
cache.put(10);
cache.put(20);
cache.put(30);
const value = cache.put(40);
expect(value).toBe(10);
expect(cache.asArray()).toEqual({ 1: 20, 2: 30, 3: 40 });
});

test("new MRU cache is not full", () => {
const cache = new MRUCache<number>(3);
expect(cache.isFull()).toBe(false);
});

test("remove from empty MRU uache", () => {
const cache = new MRUCache<number>(3);
cache.remove(10);
expect(cache.asArray()).toEqual({});
});

test("put into empty MRU cache", () => {
const cache = new MRUCache<number>(3);
const value = cache.put(10);
expect(value).toBe(undefined);
expect(cache.oldest()).toBe(10);
expect(cache.oldest()).toBe(cache.newest());
});

test("put into non-empty MRU cache", () => {
const cache = new MRUCache<number>(3);
cache.put(10);
cache.put(20);
const value = cache.put(30);
expect(value).toBe(undefined);
expect(cache.oldest()).toBe(10);
expect(cache.newest()).toBe(30);
expect(cache.asArray()).toEqual({ 1: 10, 2: 20, 3: 30 });
});

test("remove from non-empty MRU cache", () => {
const cache = new MRUCache<number>(3);
cache.put(10);
cache.put(20);
cache.remove(10);
expect(cache.oldest()).toBe(20);
expect(cache.asArray()).toEqual({ 1: 20 });
});

test("put into full MRU cache", () => {
const cache = new MRUCache<number>(3);
cache.put(10);
cache.put(20);
cache.put(30);
const value = cache.put(40);
expect(value).toBe(30);
expect(cache.asArray()).toEqual({ 1: 10, 2: 20, 3: 40 });
});
67 changes: 67 additions & 0 deletions src/tools/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { LuaObj } from "@wowts/lua";
import { List, ListNode } from "./list";

class Cache<T> {
list: List<T>;
nodeByValue: LuaObj<ListNode<T>> = {};

constructor(public size: number) {
this.list = new List<T>();
}

isFull() {
return this.list.length >= this.size;
}

newest() {
return this.list.back();
}

oldest() {
return this.list.front();
}

asArray() {
return this.list.asArray();
}

evict() {
return this.list.shift();
}

put(value: T) {
this.remove(value);
const evicted = (this.isFull() && this.evict()) || undefined;
/* Pretend to cast to string to satisfy TypeScript.
* Lua tables can accept anything as a valid key.
*/
const key = value as unknown as string;
this.nodeByValue[key] = this.list.push(value);
return evicted;
}

remove(value: T) {
/* Pretend to cast to string to satisfy TypeScript.
* Lua tables can accept anything as a valid key.
*/
const key = value as unknown as string;
const node = this.nodeByValue[key];
if (node) {
this.list.remove(node);
}
}
}

export class LRUCache<T> extends Cache<T> {
evict() {
// LRU policy evicts the oldest item
return this.list.shift();
}
}

export class MRUCache<T> extends Cache<T> {
evict() {
// MRU policy evicts the newest item
return this.list.pop();
}
}
Loading