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

add tuple map class #139

Merged
merged 1 commit into from
Sep 20, 2024
Merged
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
66 changes: 66 additions & 0 deletions src/utilityFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,69 @@ function createDictionaryWithVector(

return returnval;
}

export class TupleMap<K = Object, V = Object> {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using objects as keys in TupleMap can lead to unexpected behavior because object references are compared, not their content. Consider using a serialization method for object keys to ensure consistent behavior.

private map: Map<K, TupleMap<K, V>> = new Map();
private value?: V;

set(keys: K[], value: V): void {
bmschmidt marked this conversation as resolved.
Show resolved Hide resolved
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic to traverse the map is repeated in set, get, has, and delete methods. Consider refactoring this into a separate method to adhere to the DRY principle.

let currentMap: TupleMap<K, V> = this;
for (const key of keys) {
if (!currentMap.map.has(key)) {
currentMap.map.set(key, new TupleMap<K, V>());
}
currentMap = currentMap.map.get(key);
}
currentMap.value = value;
}

get(keys: K[]): V | undefined {
let currentMap: TupleMap<K, V> = this;
for (const key of keys) {
currentMap = currentMap.map.get(key) as TupleMap<K, V>;
bmschmidt marked this conversation as resolved.
Show resolved Hide resolved
if (!currentMap) {
return undefined;
}
}
return currentMap.value;
}

has(keys: K[]): boolean {
let currentMap: TupleMap<K, V> = this;
for (const key of keys) {
currentMap = currentMap.map.get(key) as TupleMap<K, V>;
if (!currentMap) {
return false;
}
}
return currentMap.value !== undefined;
}

delete(keys: K[]): boolean {
let currentMap: TupleMap<K, V> = this;
const stack: TupleMap<K, V>[] = [];

for (const key of keys) {
if (!currentMap.map.has(key)) {
return false;
}
stack.push(currentMap);
currentMap = currentMap.map.get(key) as TupleMap<K, V>;
}

currentMap.value = undefined;

// Clean up empty nested maps
for (let i = keys.length - 1; i >= 0; i--) {
const parentMap = stack[i];
const key = keys[i];
const childMap = parentMap.map.get(key) as TupleMap<K, V>;

// Remove map if it has no value and no nested maps
if (!childMap.value && childMap.map.size === 0) {
parentMap.map.delete(key);
}
}
return true;
}
}