recursive-set 7.0.1 → 8.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -20
- package/README.md +201 -207
- package/dist/cjs/index.js +664 -436
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.d.ts +156 -108
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +663 -436
- package/dist/esm/index.js.map +1 -1
- package/package.json +51 -51
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2025 Christian Strerath
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Christian Strerath
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
21
|
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,207 +1,201 @@
|
|
|
1
|
-
# RecursiveSet
|
|
2
|
-
|
|
3
|
-
[](LICENSE)
|
|
4
|
-
[](https://www.npmjs.com/package/recursive-set)
|
|
5
|
-
|
|
6
|
-
High-performance
|
|
7
|
-
|
|
8
|
-
## Overview
|
|
9
|
-
|
|
10
|
-
`RecursiveSet`
|
|
11
|
-
|
|
12
|
-
Key
|
|
13
|
-
|
|
14
|
-
-
|
|
15
|
-
-
|
|
16
|
-
-
|
|
17
|
-
-
|
|
18
|
-
|
|
19
|
-
## Installation
|
|
20
|
-
|
|
21
|
-
```bash
|
|
22
|
-
npm install recursive-set
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
// Sets of Sets
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
//
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
- `
|
|
151
|
-
- `
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
- `
|
|
159
|
-
- `
|
|
160
|
-
- `
|
|
161
|
-
- `
|
|
162
|
-
- `
|
|
163
|
-
- `
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
- `
|
|
170
|
-
- `
|
|
171
|
-
- `
|
|
172
|
-
- `
|
|
173
|
-
- `
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
```
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
## License
|
|
206
|
-
|
|
207
|
-
MIT License © 2025 Christian Strerath. See `LICENSE`
|
|
1
|
+
# RecursiveSet
|
|
2
|
+
|
|
3
|
+
[](LICENSE)
|
|
4
|
+
[](https://www.npmjs.com/package/recursive-set)
|
|
5
|
+
|
|
6
|
+
High-performance collection library for TypeScript supporting **value semantics** (deep equality) and recursive structures. Version 8 introduces a new hash-based architecture optimized for high-throughput workloads like SAT solvers and graph algorithms.
|
|
7
|
+
|
|
8
|
+
## Overview
|
|
9
|
+
|
|
10
|
+
`RecursiveSet` provides mathematical sets and maps where equality is determined by structure/content rather than object reference (e.g., `{1, 2}` is equal to `{2, 1}`).
|
|
11
|
+
|
|
12
|
+
**Key Architectural Features:**
|
|
13
|
+
|
|
14
|
+
- **Open Addressing:** Uses linear probing with a load factor of 0.75 for cache-efficient lookups.
|
|
15
|
+
- **Backshift Deletion:** Maintains probe chain integrity without "tombstones," preventing performance degradation over time.
|
|
16
|
+
- **Zero-Allocation Hashing:** Uses static buffers and raw bitwise operations to hash numbers without triggering the Garbage Collector.
|
|
17
|
+
- **Structure of Arrays (SoA):** Data is stored in flat arrays to maximize CPU cache locality.
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install recursive-set
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Quickstart
|
|
26
|
+
|
|
27
|
+
### Working with Sets
|
|
28
|
+
|
|
29
|
+
Sets automatically deduplicate elements based on their value or structure.
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { RecursiveSet } from "recursive-set";
|
|
33
|
+
|
|
34
|
+
// Primitive values
|
|
35
|
+
const numbers = new RecursiveSet(1, 2, 3);
|
|
36
|
+
numbers.add(1); // No effect, 1 is already present
|
|
37
|
+
|
|
38
|
+
// Recursive structures (Sets of Sets)
|
|
39
|
+
const setA = new RecursiveSet(1, 2);
|
|
40
|
+
const setB = new RecursiveSet(2, 1);
|
|
41
|
+
const metaSet = new RecursiveSet<RecursiveSet<number>>();
|
|
42
|
+
|
|
43
|
+
metaSet.add(setA);
|
|
44
|
+
metaSet.add(setB);
|
|
45
|
+
|
|
46
|
+
console.log(metaSet.size); // 1, because setA equals setB
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Working with Maps
|
|
50
|
+
|
|
51
|
+
`RecursiveMap` allows using complex objects (like Tuples or Sets) as keys.
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import { RecursiveMap, Tuple } from "recursive-set";
|
|
55
|
+
|
|
56
|
+
const transitions = new RecursiveMap<Tuple<[string, string]>, number>();
|
|
57
|
+
const edge = new Tuple("q0", "q1");
|
|
58
|
+
|
|
59
|
+
transitions.set(edge, 1);
|
|
60
|
+
|
|
61
|
+
// Retrieval using a new, structurally identical key
|
|
62
|
+
console.log(transitions.get(new Tuple("q0", "q1"))); // 1
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Lifecycle (Mutable → Frozen)
|
|
66
|
+
|
|
67
|
+
To guarantee hash stability, collections become **immutable** (frozen) once their hash code is computed or they are inserted into another collection.
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
const A = new RecursiveSet(1);
|
|
71
|
+
const B = new RecursiveSet(A); // Accessing A's hash to store it in B freezes A.
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
A.add(2); // Throws Error: Frozen Set modified.
|
|
75
|
+
} catch (e) {
|
|
76
|
+
// Expected behavior
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Use mutableCopy to continue editing
|
|
80
|
+
const C = A.mutableCopy();
|
|
81
|
+
C.add(2);
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Contracts & Invariants
|
|
85
|
+
|
|
86
|
+
This library optimizes for raw speed and assumes strict adherence to the following contracts. Violating them leads to undefined behavior.
|
|
87
|
+
|
|
88
|
+
1. **Finite Numbers Only:** `NaN` and `Infinity` are **strictly forbidden**. They break strict equality checks and integer optimization paths.
|
|
89
|
+
2. **Strict Value Semantics:** Plain JavaScript objects (`{}`) are **not supported**. Keys must implement the `Structural` interface (provide `equals`, `hashCode`, and `toString`).
|
|
90
|
+
3. **Hash Quality:** The $O(1)$ performance guarantee relies on a good distribution. Returning a constant `hashCode` (e.g., `42`) forces all elements into a single bucket, degrading performance to $O(N)$.
|
|
91
|
+
4. **Deterministic Visualization:** Custom `toString()` implementations **must** utilize `compareVisualLogic` for nested structures. Failing to do so results in unstable string output.
|
|
92
|
+
5. **Immutability:** Once an object is added to a collection, its `hashCode` **must not change**.
|
|
93
|
+
6. **No Circular Dependencies:** A `RecursiveSet` cannot contain itself, directly or indirectly. Runtime checks are omitted for performance; creating a cycle will cause a Stack Overflow during hashing.
|
|
94
|
+
|
|
95
|
+
## API Reference
|
|
96
|
+
|
|
97
|
+
### Core Types
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
type Primitive = number | string;
|
|
101
|
+
type Value = Primitive | Structural;
|
|
102
|
+
|
|
103
|
+
interface Structural {
|
|
104
|
+
readonly hashCode: number;
|
|
105
|
+
equals(other: unknown): boolean;
|
|
106
|
+
toString(): string;
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### RecursiveSet
|
|
111
|
+
|
|
112
|
+
#### Construction
|
|
113
|
+
|
|
114
|
+
- `new RecursiveSet<T>(...elements: T[])`: Creates a set from the given arguments.
|
|
115
|
+
|
|
116
|
+
#### Mutation (Unfrozen state only)
|
|
117
|
+
|
|
118
|
+
- `add(element: T): void`: Adds an element ($O(1)$ amortized).
|
|
119
|
+
- `remove(element: T): void`: Removes an element ($O(1)$ amortized).
|
|
120
|
+
|
|
121
|
+
#### Functional Methods (Zero-Allocation)
|
|
122
|
+
|
|
123
|
+
*Added in v8.1.0* — These methods iterate directly over internal storage, avoiding the memory overhead of spreading into a temporary Array (`[...set]`).
|
|
124
|
+
|
|
125
|
+
- `map<U>(fn: (v: T) => U): RecursiveSet<U>`
|
|
126
|
+
- Returns a new set. Pre-allocates storage to prevent resizing during mapping.
|
|
127
|
+
- `filter(fn: (v: T) => boolean): RecursiveSet<T>`
|
|
128
|
+
- Returns a new set containing only elements that satisfy the predicate.
|
|
129
|
+
- `reduce<U>(fn: (acc: U, v: T) => U, init: U): U`
|
|
130
|
+
- Aggregates values without intermediate allocations.
|
|
131
|
+
- `every(fn: (v: T) => boolean): boolean`
|
|
132
|
+
- **Fail-Fast:** Returns `false` immediately upon the first mismatch ($O(1)$ best case).
|
|
133
|
+
- `some(fn: (v: T) => boolean): boolean`
|
|
134
|
+
- **Fail-Fast:** Returns `true` immediately upon the first match ($O(1)$ best case).
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
// Example: High-Performance Check
|
|
138
|
+
const largeSet = new RecursiveSet(0, 1, 2, ...);
|
|
139
|
+
|
|
140
|
+
// Instead of: [...largeSet].some(x => x === 0) <-- Allocates Array(N)
|
|
141
|
+
// Use: largeSet.some(x => x === 0) <-- Zero allocation, instant return
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
#### Set Operations
|
|
145
|
+
|
|
146
|
+
All operations return a new `RecursiveSet` instance.
|
|
147
|
+
|
|
148
|
+
- `union(other): RecursiveSet<T>`
|
|
149
|
+
- `intersection(other): RecursiveSet<T>`
|
|
150
|
+
- `difference(other): RecursiveSet<T>` ($A \setminus B$)
|
|
151
|
+
- `symmetricDifference(other): RecursiveSet<T>`
|
|
152
|
+
- `cartesianProduct<U>(other): RecursiveSet<Tuple<[T, U]>>`
|
|
153
|
+
- `powerset(): RecursiveSet<RecursiveSet<T>>` (Throws if size > 20)
|
|
154
|
+
|
|
155
|
+
#### Properties
|
|
156
|
+
|
|
157
|
+
- `has(element: T): boolean`
|
|
158
|
+
- `equals(other: unknown): boolean`
|
|
159
|
+
- `isSubset(other): boolean`
|
|
160
|
+
- `isSuperset(other): boolean`
|
|
161
|
+
- `mutableCopy(): RecursiveSet<T>`: Returns a shallow mutable clone.
|
|
162
|
+
- `size: number`
|
|
163
|
+
- `hashCode: number`: Computes hash and freezes the set.
|
|
164
|
+
|
|
165
|
+
### RecursiveMap
|
|
166
|
+
|
|
167
|
+
A hash map supporting `Value` keys.
|
|
168
|
+
|
|
169
|
+
- `set(key: K, value: V): void`
|
|
170
|
+
- `get(key: K): V | undefined`
|
|
171
|
+
- `delete(key: K): boolean`
|
|
172
|
+
- `has(key: K): boolean`
|
|
173
|
+
- `mutableCopy(): RecursiveMap<K, V>`
|
|
174
|
+
|
|
175
|
+
### Tuple
|
|
176
|
+
|
|
177
|
+
An immutable, hashable sequence of values. Useful for composite keys.
|
|
178
|
+
|
|
179
|
+
- `new Tuple(...elements: T[])`
|
|
180
|
+
- `get(index: number): T[index]`
|
|
181
|
+
- `length: number`
|
|
182
|
+
|
|
183
|
+
## Credits
|
|
184
|
+
|
|
185
|
+
This library was developed as a student research project under the supervision of **[Karl Stroetmann](https://github.com/karlstroetmann/)**.
|
|
186
|
+
|
|
187
|
+
Special thanks for his architectural guidance on homogeneous sets and the theoretical foundations required for high-performance set engines.
|
|
188
|
+
|
|
189
|
+
## Contributing
|
|
190
|
+
|
|
191
|
+
```bash
|
|
192
|
+
git clone [https://github.com/cstrerath/recursive-set.git](https://github.com/cstrerath/recursive-set.git)
|
|
193
|
+
npm install
|
|
194
|
+
npm run build
|
|
195
|
+
npx tsx test/test.ts
|
|
196
|
+
npx tsx test/nqueens.ts
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
## License
|
|
200
|
+
|
|
201
|
+
MIT License © 2025 Christian Strerath. See `LICENSE`.
|