|
1 | 1 | import { NestedMap } from './nested-map'; |
2 | | -import { IterationOptions } from './types'; |
| 2 | +import { IterationOptions, PartialKey } from './types'; |
3 | 3 |
|
4 | 4 | export interface NestedSet<K> extends Set<K> {} |
5 | 5 | export class NestedSet<K> implements Set<K> { |
6 | | - private readonly _map = new NestedMap<K, 1>(); |
| 6 | + #map = new NestedMap<K, 1>(); |
7 | 7 |
|
8 | 8 | add(value: K): this { |
9 | | - this._map.set(value, 1); |
| 9 | + this.#map.set(value, 1); |
10 | 10 | return this; |
11 | 11 | } |
12 | 12 | clear(): void { |
13 | | - this._map.clear(); |
| 13 | + this.#map.clear(); |
14 | 14 | } |
15 | 15 | delete(value: K): boolean { |
16 | | - return this._map.delete(value); |
| 16 | + return this.#map.delete(value); |
17 | 17 | } |
18 | 18 | forEach( |
19 | 19 | callbackfn: (value: K, value2: K, set: Set<K>) => void, |
20 | 20 | thisArg?: unknown, |
21 | 21 | ): void { |
22 | 22 | thisArg ??= this; |
23 | | - this._map.forEach.call( |
| 23 | + this.#map.forEach.call( |
24 | 24 | thisArg, |
25 | 25 | (_, k: K) => callbackfn(k, k, this), |
26 | 26 | thisArg, |
27 | 27 | ); |
28 | 28 | } |
29 | 29 | has(value: K): boolean { |
30 | | - return this._map.has(value); |
| 30 | + return this.#map.has(value); |
31 | 31 | } |
32 | 32 |
|
33 | 33 | get size(): number { |
34 | | - return this._map.size; |
| 34 | + return this.#map.size; |
35 | 35 | } |
36 | 36 |
|
37 | 37 | *entries(options?: IterationOptions<K>): SetIterator<[K, K]> { |
38 | | - for (const key of this._map.keys(options)) { |
| 38 | + for (const key of this.#map.keys(options)) { |
39 | 39 | yield [key, key]; |
40 | 40 | } |
41 | 41 | } |
42 | 42 |
|
43 | 43 | keys(options?: IterationOptions<K>): SetIterator<K> { |
44 | | - return this._map.keys(options); |
| 44 | + return this.#map.keys(options); |
45 | 45 | } |
46 | 46 |
|
47 | 47 | values(options?: IterationOptions<K>): SetIterator<K> { |
48 | | - return this._map.keys(options); |
| 48 | + return this.#map.keys(options); |
49 | 49 | } |
50 | 50 |
|
51 | 51 | [Symbol.iterator](): SetIterator<K> { |
52 | | - return this._map.keys(); |
| 52 | + return this.#map.keys(); |
53 | 53 | } |
54 | 54 |
|
55 | 55 | get [Symbol.toStringTag](): string { |
56 | 56 | return 'NestedSet'; |
57 | 57 | } |
| 58 | + |
| 59 | + /** |
| 60 | + * Creates a new NestedSet instance that represents a subtree starting from the specified basePath. |
| 61 | + * The new NestedSet is a view from the same structure and values as the original map for the specified subtree. |
| 62 | + * Changes to the subtree in the new map will be reflected in the original map and vice versa. |
| 63 | + * @param basePath - The path to the subtree root |
| 64 | + * @returns A new NestedSet instance exposing the subtree |
| 65 | + */ |
| 66 | + getSubSet(basePath: PartialKey<K>): NestedSet<K> { |
| 67 | + const map = this.#map.getSubMap(basePath); |
| 68 | + const result = new NestedSet<K>(); |
| 69 | + result.#map = map; |
| 70 | + return result; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Efficiently deep clones the NestedSet or a subtree if basePath is provided (O(N)), using BFS pre-order for insertion order preservation. |
| 75 | + * @param basePath - Optional path to clone only a subtree |
| 76 | + * @returns A new NestedSet instance with the same structure and values |
| 77 | + */ |
| 78 | + clone(basePath?: PartialKey<K>): NestedSet<K> { |
| 79 | + const result = new NestedSet<K>(); |
| 80 | + result.#map = this.#map.clone(basePath); |
| 81 | + return result; |
| 82 | + } |
58 | 83 | } |
0 commit comments