Skip to content

Commit

Permalink
add tuple map class (#139)
Browse files Browse the repository at this point in the history
We often have to use a tuple of things as indexes in deepscatter. This creates a class that allows accessing things by string (or object) indexes so that you can do things like:

```js
const a = new TupleMap();
a.set(["foo", "bar"], 2)
a.get(["foo", "bar"])
// == 3
```

Even though the objects don't match.
<!-- ELLIPSIS_HIDDEN -->


----

| 🚀 | This description was created by cd93751  | 
|--------|--------|

feat: add `TupleMap` class for tuple-based indexing in `src/utilityFunctions.ts`

### Summary:
Introduces `TupleMap` class for efficient tuple-based indexing with string or object keys, supporting various methods and cleaning up empty maps.

**Key points**:
- Introduces `TupleMap` class in `src/utilityFunctions.ts` for tuple-based indexing.
- Supports `set`, `get`, `has`, and `delete` methods.
- Allows string or object keys in tuples.
- Handles non-matching object keys gracefully.
- Cleans up empty nested maps after deletion.
- Example usage: `a.set(["foo", "bar"], 2); a.get(["foo", "bar"])`.


----
Generated with ❤️ by [ellipsis.dev](https://www.ellipsis.dev)



<!-- ELLIPSIS_HIDDEN -->
  • Loading branch information
bmschmidt authored Sep 20, 2024
1 parent 0958348 commit 43b574f
Showing 1 changed file with 66 additions and 0 deletions.
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> {
private map: Map<K, TupleMap<K, V>> = new Map();
private value?: V;

set(keys: K[], value: V): void {
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>;
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;
}
}

0 comments on commit 43b574f

Please sign in to comment.