@the-w1zard/dx 1.0.7 → 1.0.9

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/src/set.ts DELETED
@@ -1,198 +0,0 @@
1
- import { M2I } from "./map";
2
-
3
- export class SET2I<T = A> extends Set<T> implements SET2<T> {
4
- public s = this.add;
5
- public take(value: T): T | UD {
6
- if (!this.delete(value)) return undefined as UD;
7
- return value;
8
- }
9
- public peek(value: T): T | false { return this.has(value) ? value : false; }
10
- public toggle(value: T): B {
11
- if (this.has(value)) {
12
- this.delete(value);
13
- return false;
14
- }
15
- this.add(value);
16
- return true;
17
- }
18
- public first(): T | UD {
19
- const result = this.values().next();
20
- return result.done ? undefined as UD : result.value;
21
- }
22
- public last(): T | UD {
23
- let result: T | UD = undefined as UD;
24
- for (const value of this) result = value;
25
- return result;
26
- }
27
- public union(other: Iterable<T>): SET2I<T> {
28
- const out = new SET2I(this);
29
- for (const value of other) out.add(value);
30
- return out;
31
- }
32
- public intersection(other: Iterable<T>): SET2I<T> {
33
- const values = new Set(other);
34
- const out = new SET2I<T>();
35
- for (const value of this) if (values.has(value)) out.add(value);
36
- return out;
37
- }
38
- public difference(other: Iterable<T>): SET2I<T> {
39
- const values = new Set(other);
40
- const out = new SET2I<T>();
41
- for (const value of this) if (!values.has(value)) out.add(value);
42
- return out;
43
- }
44
- public symmetricDifference(other: Iterable<T>): SET2I<T> {
45
- const values = new Set(other);
46
- const out = new SET2I<T>();
47
-
48
- for (const value of this) if (!values.has(value)) out.add(value);
49
- for (const value of values) if (!this.has(value)) out.add(value);
50
-
51
- return out;
52
- }
53
- public isSubsetOf(other: Iterable<T>): B {
54
- const values = new Set(other);
55
- for (const value of this) if (!values.has(value)) return false;
56
- return true;
57
- }
58
- public isSupersetOf(other: Iterable<T>): B {
59
- for (const value of other) if (!this.has(value)) return false;
60
- return true;
61
- }
62
- public isDisjointFrom(other: Iterable<T>): B {
63
- const values = new Set(other);
64
- for (const value of this) if (values.has(value)) return false;
65
- return true;
66
- }
67
- public equals(other: Iterable<T>): B {
68
- const values = new Set(other);
69
- return this.size === values.size && this.isSubsetOf(values);
70
- }
71
- public filter(fn: (v: T, set: this) => B): SET2I<T> {
72
- const out = new SET2I<T>();
73
- for (const value of this) if (fn(value, this)) out.add(value);
74
- return out;
75
- }
76
- public map<R>(fn: (v: T, set: this) => R): SET2I<R> {
77
- const out = new SET2I<R>();
78
- for (const value of this) out.add(fn(value, this));
79
- return out;
80
- }
81
- public partition(fn: (v: T, set: this) => B): [SET2I<T>, SET2I<T>] {
82
- const yes = new SET2I<T>();
83
- const no = new SET2I<T>();
84
-
85
- for (const value of this) {
86
- (fn(value, this) ? yes : no).add(value);
87
- }
88
-
89
- return [yes, no];
90
- }
91
- public differenceBy<K>(other: Iterable<T>, fn: (v: T) => K): SET2I<T> {
92
- const keys = new Set<K>();
93
- for (const value of other) keys.add(fn(value));
94
-
95
- const out = new SET2I<T>();
96
- for (const value of this) if (!keys.has(fn(value))) out.add(value);
97
- return out;
98
- }
99
- public uniqueBy<K>(fn: (v: T) => K): SET2I<T> {
100
- const keys = new Set<K>();
101
- const out = new SET2I<T>();
102
-
103
- for (const value of this) {
104
- const key = fn(value);
105
- if (keys.has(key)) continue;
106
- keys.add(key);
107
- out.add(value);
108
- }
109
-
110
- return out;
111
- }
112
- public find(fn: (v: T, set: this) => B): T | UD {
113
- for (const value of this) if (fn(value, this)) return value;
114
- return undefined as UD;
115
- }
116
- public every(fn: (v: T, set: this) => B): B {
117
- for (const value of this) if (!fn(value, this)) return false;
118
- return true;
119
- }
120
- public some(fn: (v: T, set: this) => B): B {
121
- for (const value of this) if (fn(value, this)) return true;
122
- return false;
123
- }
124
- public reduce<R>(fn: (acc: R, v: T, set: this) => R, initial: R): R {
125
- let result = initial;
126
- for (const value of this) result = fn(result, value, this);
127
- return result;
128
- }
129
- public addAll(values: Iterable<T>): this {
130
- for (const value of values) this.add(value);
131
- return this;
132
- }
133
- public deleteAll(values: Iterable<T>): N {
134
- let count = 0;
135
- for (const value of values) if (this.delete(value)) count++;
136
- return count as N;
137
- }
138
- public retainAll(values: Iterable<T>): this {
139
- const keep = new Set(values);
140
- for (const value of this) if (!keep.has(value)) this.delete(value);
141
- return this;
142
- }
143
- public update(values: Iterable<T>): this {
144
- return this.addAll(values);
145
- }
146
- public replace(values: Iterable<T>): this {
147
- const replacement = new Set(values);
148
- this.clear();
149
- for (const value of replacement) this.add(value);
150
- return this;
151
- }
152
- public swap(a: T, b: T): B {
153
- if (!this.has(a)) return false;
154
- this.delete(a);
155
- this.add(b);
156
- return true;
157
- }
158
- public clone(): this {
159
- return new SET2I(this) as this;
160
- }
161
- public toArray(): T[] {
162
- return [...this];
163
- }
164
- public toMap<K, R>(fn: (v: T) => readonly [K, R]): M2I<K, R> {
165
- const out = new M2<K, R>();
166
- for (const value of this) {
167
- const [key, result] = fn(value);
168
- out.set(key, result);
169
- }
170
- return out;
171
- }
172
- public toObject(fn?: (v: T) => S): OB<T> {
173
- const out = {} as OB<T>;
174
- for (const value of this) {
175
- out[(fn ? fn(value) : String(value)) as S] = value;
176
- }
177
- return out;
178
- }
179
- public toJSON(): T[] {
180
- return this.toArray();
181
- }
182
- static fromArray<T>(values: T[]): SET2I<T> {
183
- return new SET2I(values);
184
- }
185
- static fromObject<O extends Record<string, A>>(obj: O): SET2I<O[keyof O]> {
186
- return new SET2I(Object.values(obj) as O[keyof O][]);
187
- }
188
- static fromEntries<T>(entries: Iterable<readonly [unknown, T]>): SET2I<T> {
189
- const out = new SET2I<T>();
190
- for (const [, value] of entries) out.add(value);
191
- return out;
192
- }
193
- static from<T>(values: Iterable<T>): SET2I<T> {
194
- return new SET2I(values);
195
- }
196
- }
197
-
198
- globalThis.SET2 = SET2I;
package/src/set_byte.js DELETED
@@ -1,261 +0,0 @@
1
- export class SET2BI {
2
- cap;
3
- flags; // [pid] -> 0/1, O(1) has()
4
- slots; // [idx] -> pid, dense, for iteration
5
- pidToIdx; // [pid] -> idx into slots, -1 = absent
6
- #size = 0;
7
- constructor(maxParticles = 8192, seed) {
8
- this.cap = maxParticles;
9
- this.flags = new Uint8Array(maxParticles);
10
- this.slots = new Int32Array(maxParticles).fill(-1);
11
- this.pidToIdx = new Int32Array(maxParticles).fill(-1);
12
- if (seed)
13
- for (const pid of seed)
14
- this.add(pid);
15
- }
16
- get size() { return this.#size; }
17
- // ---------------------------------------------------------------- core
18
- s = this.add.bind(this); // [s alias]
19
- add(pid) {
20
- if (this.flags[pid] === 0) {
21
- this.flags[pid] = 1;
22
- const idx = this.#size;
23
- this.slots[idx] = pid;
24
- this.pidToIdx[pid] = idx;
25
- this.#size++;
26
- }
27
- return this;
28
- }
29
- has(pid) { return this.flags[pid] === 1; } // [HAS]
30
- delete(pid) {
31
- if (this.flags[pid] !== 1)
32
- return false;
33
- this.flags[pid] = 0;
34
- const idx = this.pidToIdx[pid];
35
- const lastIdx = this.#size - 1;
36
- const lastPid = this.slots[lastIdx];
37
- if (idx !== lastIdx) {
38
- this.slots[idx] = lastPid;
39
- this.pidToIdx[lastPid] = idx;
40
- }
41
- this.slots[lastIdx] = -1;
42
- this.pidToIdx[pid] = -1;
43
- this.#size--;
44
- return true;
45
- }
46
- clear() {
47
- for (let i = 0; i < this.#size; i++)
48
- this.flags[this.slots[i]] = 0;
49
- this.slots.fill(-1, 0, this.#size);
50
- this.pidToIdx.fill(-1); // cheap enough; or loop same range if cap is huge
51
- this.#size = 0;
52
- }
53
- // ---------------------------------------------------------------- iteration
54
- *[Symbol.iterator]() { for (let i = 0; i < this.#size; i++)
55
- yield this.slots[i]; }
56
- values() { return this[Symbol.iterator](); }
57
- keys() { return this[Symbol.iterator](); }
58
- *entries() { for (let i = 0; i < this.#size; i++)
59
- yield [this.slots[i], this.slots[i]]; }
60
- // ---------------------------------------------------------------- sugar
61
- take(pid) { return this.delete(pid) ? pid : undefined; } // [TAKE]
62
- peek(pid) { return this.has(pid) ? pid : false; } // [PEEK]
63
- toggle(pid) {
64
- if (this.has(pid)) {
65
- this.delete(pid);
66
- return false;
67
- }
68
- this.add(pid);
69
- return true;
70
- }
71
- first() { return this.#size === 0 ? undefined : this.slots[0]; } // [FIRST]
72
- last() { return this.#size === 0 ? undefined : this.slots[this.#size - 1]; } // [LAST]
73
- // ---------------------------------------------------------------- set algebra (-> new SET2BI, same cap)
74
- union(other) {
75
- const out = new SET2BI(this.cap, this);
76
- for (const pid of other)
77
- out.add(pid);
78
- return out;
79
- }
80
- intersection(other) {
81
- const probe = new SET2BI(this.cap, other);
82
- const out = new SET2BI(this.cap);
83
- for (const pid of this)
84
- if (probe.has(pid))
85
- out.add(pid);
86
- return out;
87
- }
88
- difference(other) {
89
- const probe = new SET2BI(this.cap, other);
90
- const out = new SET2BI(this.cap);
91
- for (const pid of this)
92
- if (!probe.has(pid))
93
- out.add(pid);
94
- return out;
95
- }
96
- symmetricDifference(other) {
97
- const probe = new SET2BI(this.cap, other);
98
- const out = new SET2BI(this.cap);
99
- for (const pid of this)
100
- if (!probe.has(pid))
101
- out.add(pid);
102
- for (const pid of probe)
103
- if (!this.has(pid))
104
- out.add(pid);
105
- return out;
106
- }
107
- isSubsetOf(other) {
108
- const probe = new SET2BI(this.cap, other);
109
- for (const pid of this)
110
- if (!probe.has(pid))
111
- return false;
112
- return true;
113
- }
114
- isSupersetOf(other) {
115
- for (const pid of other)
116
- if (!this.has(pid))
117
- return false;
118
- return true;
119
- }
120
- isDisjointFrom(other) {
121
- for (const pid of other)
122
- if (this.has(pid))
123
- return false;
124
- return true;
125
- }
126
- equals(other) {
127
- const probe = new SET2BI(this.cap, other);
128
- return this.#size === probe.size && this.isSubsetOf(probe);
129
- }
130
- // ---------------------------------------------------------------- functional
131
- filter(fn) {
132
- const out = new SET2BI(this.cap);
133
- for (const pid of this)
134
- if (fn(pid, this))
135
- out.add(pid);
136
- return out;
137
- }
138
- map(fn) {
139
- const out = new SET2BI(this.cap); // fn must still yield in-range pids
140
- for (const pid of this)
141
- out.add(fn(pid, this));
142
- return out;
143
- }
144
- partition(fn) {
145
- const yes = new SET2BI(this.cap), no = new SET2BI(this.cap);
146
- for (const pid of this)
147
- (fn(pid, this) ? yes : no).add(pid);
148
- return [yes, no];
149
- }
150
- differenceBy(other, fn) {
151
- const keys = new Set();
152
- for (const pid of other)
153
- keys.add(fn(pid));
154
- const out = new SET2BI(this.cap);
155
- for (const pid of this)
156
- if (!keys.has(fn(pid)))
157
- out.add(pid);
158
- return out;
159
- }
160
- uniqueBy(fn) {
161
- const keys = new Set();
162
- const out = new SET2BI(this.cap);
163
- for (const pid of this) {
164
- const key = fn(pid);
165
- if (keys.has(key))
166
- continue;
167
- keys.add(key);
168
- out.add(pid);
169
- }
170
- return out;
171
- }
172
- find(fn) {
173
- for (const pid of this)
174
- if (fn(pid, this))
175
- return pid;
176
- return undefined;
177
- }
178
- every(fn) {
179
- for (const pid of this)
180
- if (!fn(pid, this))
181
- return false;
182
- return true;
183
- }
184
- some(fn) {
185
- for (const pid of this)
186
- if (fn(pid, this))
187
- return true;
188
- return false;
189
- }
190
- reduce(fn, initial) {
191
- let result = initial;
192
- for (const pid of this)
193
- result = fn(result, pid, this);
194
- return result;
195
- }
196
- // ---------------------------------------------------------------- bulk mutation
197
- addAll(pids) { for (const pid of pids)
198
- this.add(pid); return this; } // [ADDALL]
199
- deleteAll(pids) {
200
- let count = 0;
201
- for (const pid of pids)
202
- if (this.delete(pid))
203
- count++;
204
- return count;
205
- }
206
- retainAll(pids) {
207
- const keep = new SET2BI(this.cap, pids);
208
- for (const pid of [...this])
209
- if (!keep.has(pid))
210
- this.delete(pid);
211
- return this;
212
- }
213
- update(pids) { return this.addAll(pids); } // [UPDATE]
214
- replace(pids) {
215
- this.clear();
216
- for (const pid of pids)
217
- this.add(pid);
218
- return this;
219
- }
220
- swap(a, b) {
221
- if (!this.has(a))
222
- return false;
223
- this.delete(a);
224
- this.add(b);
225
- return true;
226
- }
227
- // ---------------------------------------------------------------- export
228
- clone() { return new SET2BI(this.cap, this); } // [CLONE]
229
- toArray() { return [...this]; } // [TOARRAY]
230
- toMap(fn) {
231
- const out = new Map();
232
- for (const pid of this) {
233
- const [key, result] = fn(pid);
234
- out.set(key, result);
235
- }
236
- return out;
237
- }
238
- toObject(fn) {
239
- const out = {};
240
- for (const pid of this)
241
- out[fn ? fn(pid) : String(pid)] = pid;
242
- return out;
243
- }
244
- toJSON() { return this.toArray(); } // [TOJSON]
245
- static fromArray(pids, maxParticles) {
246
- return new SET2BI(maxParticles ?? Math.max(...pids, 0) + 1, pids);
247
- }
248
- static fromObject(obj, maxParticles) {
249
- return SET2BI.fromArray(Object.values(obj), maxParticles);
250
- }
251
- static fromEntries(entries, maxParticles) {
252
- const pids = [];
253
- for (const [, pid] of entries)
254
- pids.push(pid);
255
- return SET2BI.fromArray(pids, maxParticles);
256
- }
257
- static from(pids, maxParticles) {
258
- return new SET2BI(maxParticles ?? 8192, pids);
259
- }
260
- }
261
- globalThis.SET2B = SET2BI;
package/src/set_byte.ts DELETED
@@ -1,223 +0,0 @@
1
- export class SET2BI {
2
- readonly cap: N;
3
- readonly flags: Uint8Array; // [pid] -> 0/1, O(1) has()
4
- readonly slots: I32; // [idx] -> pid, dense, for iteration
5
- readonly pidToIdx: I32; // [pid] -> idx into slots, -1 = absent
6
-
7
- #size = 0;
8
-
9
- constructor(maxParticles: N = 8192, seed?: Iterable<N>) {
10
- this.cap = maxParticles;
11
- this.flags = new Uint8Array(maxParticles);
12
- this.slots = new Int32Array(maxParticles).fill(-1);
13
- this.pidToIdx = new Int32Array(maxParticles).fill(-1);
14
- if (seed) for (const pid of seed) this.add(pid);
15
- }
16
-
17
- get size(): N { return this.#size; }
18
-
19
- // ---------------------------------------------------------------- core
20
- public s = this.add.bind(this); // [s alias]
21
- public add(pid: N): this { // [ADD]
22
- if (this.flags[pid] === 0) {
23
- this.flags[pid] = 1;
24
- const idx = this.#size;
25
- this.slots[idx] = pid;
26
- this.pidToIdx[pid] = idx;
27
- this.#size++;
28
- }
29
- return this;
30
- }
31
- public has(pid: N): B { return this.flags[pid] === 1; } // [HAS]
32
- public delete(pid: N): B { // [DELETE]
33
- if (this.flags[pid] !== 1) return false;
34
- this.flags[pid] = 0;
35
- const idx = this.pidToIdx[pid];
36
- const lastIdx = this.#size - 1;
37
- const lastPid = this.slots[lastIdx];
38
- if (idx !== lastIdx) {
39
- this.slots[idx] = lastPid;
40
- this.pidToIdx[lastPid] = idx;
41
- }
42
- this.slots[lastIdx] = -1;
43
- this.pidToIdx[pid] = -1;
44
- this.#size--;
45
- return true;
46
- }
47
- public clear(): V { // [CLEAR]
48
- for (let i = 0; i < this.#size; i++) this.flags[this.slots[i]] = 0;
49
- this.slots.fill(-1, 0, this.#size);
50
- this.pidToIdx.fill(-1); // cheap enough; or loop same range if cap is huge
51
- this.#size = 0;
52
- }
53
-
54
- // ---------------------------------------------------------------- iteration
55
- public *[Symbol.iterator](): Generator<N> { for (let i = 0; i < this.#size; i++) yield this.slots[i]; }
56
- public values(): Generator<N> { return this[Symbol.iterator](); }
57
- public keys(): Generator<N> { return this[Symbol.iterator](); }
58
- public *entries(): Generator<[N, N]> { for (let i = 0; i < this.#size; i++) yield [this.slots[i], this.slots[i]]; }
59
-
60
- // ---------------------------------------------------------------- sugar
61
- public take(pid: N): N | UD { return this.delete(pid) ? pid : undefined; } // [TAKE]
62
- public peek(pid: N): N | false { return this.has(pid) ? pid : false; } // [PEEK]
63
- public toggle(pid: N): B { // [TOGGLE]
64
- if (this.has(pid)) { this.delete(pid); return false; }
65
- this.add(pid);
66
- return true;
67
- }
68
- public first(): N | UD { return this.#size === 0 ? undefined : this.slots[0]; } // [FIRST]
69
- public last(): N | UD { return this.#size === 0 ? undefined : this.slots[this.#size - 1]; } // [LAST]
70
-
71
- // ---------------------------------------------------------------- set algebra (-> new SET2BI, same cap)
72
- public union(other: Iterable<N>): SET2BI { // [UNION]
73
- const out = new SET2BI(this.cap, this);
74
- for (const pid of other) out.add(pid);
75
- return out;
76
- }
77
- public intersection(other: Iterable<N>): SET2BI { // [INTERSECTION]
78
- const probe = new SET2BI(this.cap, other);
79
- const out = new SET2BI(this.cap);
80
- for (const pid of this) if (probe.has(pid)) out.add(pid);
81
- return out;
82
- }
83
- public difference(other: Iterable<N>): SET2BI { // [DIFFERENCE]
84
- const probe = new SET2BI(this.cap, other);
85
- const out = new SET2BI(this.cap);
86
- for (const pid of this) if (!probe.has(pid)) out.add(pid);
87
- return out;
88
- }
89
- public symmetricDifference(other: Iterable<N>): SET2BI { // [SYMMETRICDIFFERENCE]
90
- const probe = new SET2BI(this.cap, other);
91
- const out = new SET2BI(this.cap);
92
- for (const pid of this) if (!probe.has(pid)) out.add(pid);
93
- for (const pid of probe) if (!this.has(pid)) out.add(pid);
94
- return out;
95
- }
96
- public isSubsetOf(other: Iterable<N>): B { // [ISSUBSETOF]
97
- const probe = new SET2BI(this.cap, other);
98
- for (const pid of this) if (!probe.has(pid)) return false;
99
- return true;
100
- }
101
- public isSupersetOf(other: Iterable<N>): B { // [ISSUPERSETOF]
102
- for (const pid of other) if (!this.has(pid)) return false;
103
- return true;
104
- }
105
- public isDisjointFrom(other: Iterable<N>): B { // [ISDISJOINT]
106
- for (const pid of other) if (this.has(pid)) return false;
107
- return true;
108
- }
109
- public equals(other: Iterable<N>): B { // [EQUALS]
110
- const probe = new SET2BI(this.cap, other);
111
- return this.#size === probe.size && this.isSubsetOf(probe);
112
- }
113
-
114
- // ---------------------------------------------------------------- functional
115
- public filter(fn: (pid: N, set: this) => B): SET2BI { // [FILTER]
116
- const out = new SET2BI(this.cap);
117
- for (const pid of this) if (fn(pid, this)) out.add(pid);
118
- return out;
119
- }
120
- public map(fn: (pid: N, set: this) => N): SET2BI { // [MAP]
121
- const out = new SET2BI(this.cap); // fn must still yield in-range pids
122
- for (const pid of this) out.add(fn(pid, this));
123
- return out;
124
- }
125
- public partition(fn: (pid: N, set: this) => B): [SET2BI, SET2BI] { // [PARTITION]
126
- const yes = new SET2BI(this.cap), no = new SET2BI(this.cap);
127
- for (const pid of this) (fn(pid, this) ? yes : no).add(pid);
128
- return [yes, no];
129
- }
130
- public differenceBy<G>(other: Iterable<N>, fn: (pid: N) => G): SET2BI { // [DIFFERENCEBY]
131
- const keys = new Set<G>();
132
- for (const pid of other) keys.add(fn(pid));
133
- const out = new SET2BI(this.cap);
134
- for (const pid of this) if (!keys.has(fn(pid))) out.add(pid);
135
- return out;
136
- }
137
- public uniqueBy<G>(fn: (pid: N) => G): SET2BI { // [UNIQUEBY]
138
- const keys = new Set<G>();
139
- const out = new SET2BI(this.cap);
140
- for (const pid of this) {
141
- const key = fn(pid);
142
- if (keys.has(key)) continue;
143
- keys.add(key);
144
- out.add(pid);
145
- }
146
- return out;
147
- }
148
- public find(fn: (pid: N, set: this) => B): N | UD { // [FIND]
149
- for (const pid of this) if (fn(pid, this)) return pid;
150
- return undefined;
151
- }
152
- public every(fn: (pid: N, set: this) => B): B { // [EVERY]
153
- for (const pid of this) if (!fn(pid, this)) return false;
154
- return true;
155
- }
156
- public some(fn: (pid: N, set: this) => B): B { // [SOME]
157
- for (const pid of this) if (fn(pid, this)) return true;
158
- return false;
159
- }
160
- public reduce<G>(fn: (acc: G, pid: N, set: this) => G, initial: G): G { // [REDUCE]
161
- let result = initial;
162
- for (const pid of this) result = fn(result, pid, this);
163
- return result;
164
- }
165
-
166
- // ---------------------------------------------------------------- bulk mutation
167
- public addAll(pids: Iterable<N>): this { for (const pid of pids) this.add(pid); return this; } // [ADDALL]
168
- public deleteAll(pids: Iterable<N>): N { // [DELETEALL]
169
- let count = 0;
170
- for (const pid of pids) if (this.delete(pid)) count++;
171
- return count;
172
- }
173
- public retainAll(pids: Iterable<N>): this { // [RETAINALL]
174
- const keep = new SET2BI(this.cap, pids);
175
- for (const pid of [...this]) if (!keep.has(pid)) this.delete(pid);
176
- return this;
177
- }
178
- public update(pids: Iterable<N>): this { return this.addAll(pids); } // [UPDATE]
179
- public replace(pids: Iterable<N>): this { // [REPLACE]
180
- this.clear();
181
- for (const pid of pids) this.add(pid);
182
- return this;
183
- }
184
- public swap(a: N, b: N): B { // [SWAP]
185
- if (!this.has(a)) return false;
186
- this.delete(a);
187
- this.add(b);
188
- return true;
189
- }
190
-
191
- // ---------------------------------------------------------------- export
192
- public clone(): SET2BI { return new SET2BI(this.cap, this); } // [CLONE]
193
- public toArray(): AR<N> { return [...this]; } // [TOARRAY]
194
- public toMap<K, T>(fn: (pid: N) => [K, T]): Map<K, T> { // [TOMAP]
195
- const out = new Map<K, T>();
196
- for (const pid of this) { const [key, result] = fn(pid); out.set(key, result); }
197
- return out;
198
- }
199
- public toObject(fn?: (pid: N) => S): OB<N> { // [TOOBJECT]
200
- const out = {} as OB<N>;
201
- for (const pid of this) out[fn ? fn(pid) : String(pid)] = pid;
202
- return out;
203
- }
204
- public toJSON(): AR<N> { return this.toArray(); } // [TOJSON]
205
-
206
- static fromArray(pids: AR<N>, maxParticles?: N): SET2BI { // [FROMARRAY]
207
- return new SET2BI(maxParticles ?? Math.max(...pids, 0) + 1, pids);
208
- }
209
- static fromObject(obj: Record<S, N>, maxParticles?: N): SET2BI { // [FROMOBJECT]
210
- return SET2BI.fromArray(Object.values(obj), maxParticles);
211
- }
212
- static fromEntries(entries: I<readonly [A, N]>, maxParticles?: N): SET2BI { // [FROMENTIRES]
213
- const pids: AR<N> = [];
214
- for (const [, pid] of entries) pids.push(pid);
215
- return SET2BI.fromArray(pids, maxParticles);
216
- }
217
- static from(pids: Iterable<N>, maxParticles?: N): SET2BI { // [FROM]
218
- return new SET2BI(maxParticles ?? 8192, pids);
219
- }
220
- }
221
-
222
- globalThis.SET2B = SET2BI;
223
-