123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- /**
- * A bidirectional map written in TypeScript.
- *
- * @author Trevor Sears <trevorsears.main@gmail.com>
- * @version v1.0.0
- * @since v0.1.0
- */
- export class BiMap<K = any, V = any> {
- public primaryMap: Map<K, V>;
- public secondaryMap: Map<V, K>;
-
- public constructor() {
- this.primaryMap = new Map<K, V>();
- this.secondaryMap = new Map<V, K>();
- }
- public getValues(): IterableIterator<V> {
- return this.primaryMap.values();
- }
-
- 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)) {
- const 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)) {
- const 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();
- }
- public size(): number {
- return this.primaryMap.size;
- }
- }
|