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

Fix tests and clean up #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
159 changes: 60 additions & 99 deletions ts/bi-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,103 +11,64 @@
* @version v0.1.0
* @since v0.1.0
*/

export class BiMap<K = any, V = any> {

private primaryMap: Map<K, V>;

private secondaryMap: Map<V, K>;

public constructor() {

this.primaryMap = new Map<K, V>();
this.secondaryMap = new Map<V, K>();

}

public get(key: K): V | undefined {

return this.getFromKey(key);

}

public set(key: K, value: V): void {

this.setFromKey(key, value);

}

public getFromKey(key: K): V | undefined {

return this.primaryMap.get(key);

}

public getFromValue(value: V): K | undefined {

return this.secondaryMap.get(value);

}

public setFromKey(key: K, value: V): void {

this.primaryMap.set(key, value);
this.secondaryMap.set(value, key);

}

public setFromValue(value: V, key: K): void {

this.setFromKey(key, value);

}

public removeByKey(key: K): V | undefined {

if (this.primaryMap.has(key)) {

let value: V = this.primaryMap.get(key) as V;

this.primaryMap.delete(key);
this.secondaryMap.delete(value);

return value;

} else return undefined;

}

public removeByValue(value: V): K | undefined {

if (this.secondaryMap.has(value)) {

let key: K = this.secondaryMap.get(value) as K;

this.primaryMap.delete(key);
this.secondaryMap.delete(value);

return key;

} else return undefined;

}

public hasKey(key: K): boolean {

return this.primaryMap.has(key);

}

public hasValue(value: V): boolean {

return this.secondaryMap.has(value);

}

public clear(): void {

this.primaryMap.clear();
this.secondaryMap.clear();

}

}
private primaryMap = new Map<K, V>();
private secondaryMap = new Map<V, K>();

public get(key: K): V | undefined {
return this.getFromKey(key);
}

public set(key: K, value: V): void {
const _val = this.primaryMap.get(key);
const _key = this.secondaryMap.get(value);
if (_val) this.secondaryMap.delete(_val);
if (_key) this.primaryMap.delete(_key);
this.primaryMap.set(key, value);
this.secondaryMap.set(value, key);
}

public getFromKey(key: K): V | undefined {
return this.primaryMap.get(key);
}

public getFromValue(value: V): K | undefined {
return this.secondaryMap.get(value);
}

public removeByKey(key: K): V | undefined {
const value = this.primaryMap.get(key);

if (value !== undefined) {
this.primaryMap.delete(key);
this.secondaryMap.delete(value);
}

return value;
}

public removeByValue(value: V): K | undefined {
const key = this.secondaryMap.get(value);

if (key !== undefined) {
this.primaryMap.delete(key);
this.secondaryMap.delete(value);
}

return key;
}

public hasKey(key: K): boolean {
return this.primaryMap.has(key);
}

public hasValue(value: V): boolean {
return this.secondaryMap.has(value);
}

public clear(): void {
this.primaryMap.clear();
this.secondaryMap.clear();
}
}
84 changes: 42 additions & 42 deletions ts/tests/bi-map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,56 +96,56 @@ describe("Per-method Tests", () => {
});

});

describe("#getFromValue", () => {

test("", () => {



describe("#removeByKey", () => {

test("Regular key --> returns deleted value.", () => {
bimap = new BiMap<string, number>();
bimap.set("one", 1);

expect(bimap.hasKey("one")).toBe(true);
expect(bimap.hasKey("two")).toBe(false);
expect(bimap.removeByKey("one")).toBe(1);
expect(bimap.hasKey("one")).toBe(false);
expect(bimap.getFromKey("one")).toBeUndefined();
});

});

describe("#setFromKey", () => {



});

describe("#setFromValue", () => {



});

describe("#removeByKey", () => {



});


describe("#removeByValue", () => {



});

describe("#hasKey", () => {



});

describe("#hasValue", () => {



test("Regular key --> returns deleted value.", () => {
bimap = new BiMap<string, number>();
bimap.set("one", 1);

expect(bimap.hasValue(1)).toBe(true);
expect(bimap.hasValue(2)).toBe(false);
expect(bimap.removeByValue(1)).toBe("one");
expect(bimap.hasValue(1)).toBe(false);
expect(bimap.getFromValue(1)).toBeUndefined();
});

});

describe("#clear", () => {



test("nukes all assoc", () => {
bimap = new BiMap<string, number>();
bimap.set("one", 1);
bimap.set("two", 2);

expect(bimap.hasKey("one")).toBe(true);
expect(bimap.hasKey("two")).toBe(true);
expect(bimap.hasValue(1)).toBe(true);
expect(bimap.hasValue(2)).toBe(true);

bimap.clear()

expect(bimap.hasKey("one")).toBe(false);
expect(bimap.hasKey("two")).toBe(false);
expect(bimap.hasValue(1)).toBe(false);
expect(bimap.hasValue(2)).toBe(false);
});

});

Expand Down