@holoscript/std 6.0.3 → 7.0.1

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.
Files changed (128) hide show
  1. package/README.md +8 -0
  2. package/dist/chunk-DGUM43GV.js +11 -0
  3. package/dist/chunk-DGUM43GV.js.map +1 -0
  4. package/dist/{chunk-PFX2JM4X.js → chunk-FAMOLKH3.js} +2 -1
  5. package/dist/{chunk-PFX2JM4X.js.map → chunk-FAMOLKH3.js.map} +1 -1
  6. package/dist/{chunk-7HVUYGPS.js → chunk-SOKH6FJ7.js} +37 -5
  7. package/dist/chunk-SOKH6FJ7.js.map +1 -0
  8. package/dist/{chunk-W2Q3LUCM.js → chunk-XJIFG7G3.js} +3 -3
  9. package/dist/chunk-XJIFG7G3.js.map +1 -0
  10. package/dist/collections.cjs +1 -0
  11. package/dist/collections.cjs.map +1 -1
  12. package/dist/collections.js +2 -1
  13. package/dist/fs/fs.d.ts +288 -0
  14. package/dist/fs/fs.d.ts.map +1 -0
  15. package/dist/fs/index.cjs +1049 -0
  16. package/dist/fs/index.cjs.map +1 -0
  17. package/dist/fs/index.d.ts +51 -0
  18. package/dist/fs/index.d.ts.map +1 -0
  19. package/dist/fs/index.js +918 -0
  20. package/dist/fs/index.js.map +1 -0
  21. package/dist/fs/path.d.ts +135 -0
  22. package/dist/fs/path.d.ts.map +1 -0
  23. package/dist/fs/sandbox.d.ts +49 -0
  24. package/dist/fs/sandbox.d.ts.map +1 -0
  25. package/dist/fs/watch.d.ts +133 -0
  26. package/dist/fs/watch.d.ts.map +1 -0
  27. package/dist/index.cjs +54 -7
  28. package/dist/index.cjs.map +1 -1
  29. package/dist/index.js +19 -4
  30. package/dist/index.js.map +1 -1
  31. package/dist/math.cjs +21 -2
  32. package/dist/math.cjs.map +1 -1
  33. package/dist/math.js +2 -1
  34. package/dist/string.cjs +2 -2
  35. package/dist/string.cjs.map +1 -1
  36. package/dist/string.js +2 -1
  37. package/dist/time.js +1 -0
  38. package/dist/traits/EconomicPrimitives.js +1 -0
  39. package/dist/traits/EconomicTraits.js +1 -0
  40. package/dist/traits/VRRTraits.d.ts +2 -2
  41. package/dist/types.d.ts.map +1 -1
  42. package/package.json +24 -9
  43. package/CHANGELOG.md +0 -7
  44. package/dist/__tests__/EconomicPrimitives.test.d.ts +0 -2
  45. package/dist/__tests__/EconomicPrimitives.test.d.ts.map +0 -1
  46. package/dist/__tests__/EconomicTraits.test.d.ts +0 -2
  47. package/dist/__tests__/EconomicTraits.test.d.ts.map +0 -1
  48. package/dist/__tests__/SimulationLabPrimitives.test.d.ts +0 -2
  49. package/dist/__tests__/SimulationLabPrimitives.test.d.ts.map +0 -1
  50. package/dist/__tests__/collections.test.d.ts +0 -2
  51. package/dist/__tests__/collections.test.d.ts.map +0 -1
  52. package/dist/__tests__/events.test.d.ts +0 -2
  53. package/dist/__tests__/events.test.d.ts.map +0 -1
  54. package/dist/__tests__/index.test.d.ts +0 -2
  55. package/dist/__tests__/index.test.d.ts.map +0 -1
  56. package/dist/__tests__/materials.test.d.ts +0 -2
  57. package/dist/__tests__/materials.test.d.ts.map +0 -1
  58. package/dist/__tests__/math.test.d.ts +0 -2
  59. package/dist/__tests__/math.test.d.ts.map +0 -1
  60. package/dist/__tests__/physics.test.d.ts +0 -2
  61. package/dist/__tests__/physics.test.d.ts.map +0 -1
  62. package/dist/__tests__/spatial.test.d.ts +0 -2
  63. package/dist/__tests__/spatial.test.d.ts.map +0 -1
  64. package/dist/__tests__/standard-library-fundamentals.test.d.ts +0 -2
  65. package/dist/__tests__/standard-library-fundamentals.test.d.ts.map +0 -1
  66. package/dist/__tests__/string-and-time-utilities.test.d.ts +0 -2
  67. package/dist/__tests__/string-and-time-utilities.test.d.ts.map +0 -1
  68. package/dist/__tests__/string-decoupled.test.d.ts +0 -2
  69. package/dist/__tests__/string-decoupled.test.d.ts.map +0 -1
  70. package/dist/__tests__/string.test.d.ts +0 -2
  71. package/dist/__tests__/string.test.d.ts.map +0 -1
  72. package/dist/__tests__/time.test.d.ts +0 -2
  73. package/dist/__tests__/time.test.d.ts.map +0 -1
  74. package/dist/chunk-7HVUYGPS.js.map +0 -1
  75. package/dist/chunk-W2Q3LUCM.js.map +0 -1
  76. package/dist/string.basic.test.d.ts +0 -5
  77. package/dist/string.basic.test.d.ts.map +0 -1
  78. package/dist/string.test.d.ts +0 -5
  79. package/dist/string.test.d.ts.map +0 -1
  80. package/dist/traits/__tests__/ARTraits.test.d.ts +0 -2
  81. package/dist/traits/__tests__/ARTraits.test.d.ts.map +0 -1
  82. package/dist/traits/__tests__/EconomicPrimitives.test.d.ts +0 -2
  83. package/dist/traits/__tests__/EconomicPrimitives.test.d.ts.map +0 -1
  84. package/dist/traits/__tests__/EconomicTraits.test.d.ts +0 -2
  85. package/dist/traits/__tests__/EconomicTraits.test.d.ts.map +0 -1
  86. package/error-utf8.log +0 -6
  87. package/error.log +0 -0
  88. package/src/__tests__/EconomicPrimitives.test.ts +0 -690
  89. package/src/__tests__/EconomicTraits.test.ts +0 -425
  90. package/src/__tests__/SimulationLabPrimitives.test.ts +0 -415
  91. package/src/__tests__/collections.test.ts +0 -182
  92. package/src/__tests__/events.test.ts +0 -135
  93. package/src/__tests__/index.test.ts +0 -14
  94. package/src/__tests__/materials.test.ts +0 -84
  95. package/src/__tests__/math.test.ts +0 -246
  96. package/src/__tests__/physics.test.ts +0 -83
  97. package/src/__tests__/spatial.test.ts +0 -333
  98. package/src/__tests__/standard-library-fundamentals.test.ts +0 -1301
  99. package/src/__tests__/string-and-time-utilities.test.ts +0 -1070
  100. package/src/__tests__/string-decoupled.test.ts +0 -14
  101. package/src/__tests__/string.test.ts +0 -164
  102. package/src/__tests__/time.test.ts +0 -110
  103. package/src/collections.ts +0 -850
  104. package/src/events.ts +0 -88
  105. package/src/index.ts +0 -474
  106. package/src/materials.ts +0 -109
  107. package/src/math.ts +0 -674
  108. package/src/physics.ts +0 -141
  109. package/src/spatial.ts +0 -320
  110. package/src/string.basic.test.ts +0 -14
  111. package/src/string.test.ts +0 -335
  112. package/src/string.ts +0 -531
  113. package/src/time.ts +0 -621
  114. package/src/traits/ARTraits.ts +0 -103
  115. package/src/traits/EconomicPrimitives.ts +0 -755
  116. package/src/traits/EconomicTraits.ts +0 -552
  117. package/src/traits/IoTTraits.ts +0 -102
  118. package/src/traits/SimulationLabPrimitives.ts +0 -650
  119. package/src/traits/SimulationLabTraits.ts +0 -191
  120. package/src/traits/VRRTraits.ts +0 -326
  121. package/src/traits/__tests__/ARTraits.test.ts +0 -14
  122. package/src/traits/__tests__/EconomicPrimitives.test.ts +0 -14
  123. package/src/traits/__tests__/EconomicTraits.test.ts +0 -14
  124. package/src/types.ts +0 -411
  125. package/std_error.log +0 -5
  126. package/tsconfig.json +0 -18
  127. package/tsup.config.ts +0 -17
  128. package/vitest.config.ts +0 -10
@@ -1,850 +0,0 @@
1
- /**
2
- * @holoscript/std - Collections Module
3
- *
4
- * Provides List, Map, and Set implementations with functional operations
5
- * for use in HoloScript Plus programs.
6
- */
7
-
8
- import type { Vec3 } from './types.js';
9
-
10
- /**
11
- * Immutable List with functional operations
12
- */
13
- export class List<T> {
14
- private readonly items: readonly T[];
15
-
16
- constructor(items: Iterable<T> = []) {
17
- this.items = Array.from(items);
18
- }
19
-
20
- static of<T>(...items: T[]): List<T> {
21
- return new List(items);
22
- }
23
-
24
- static from<T>(iterable: Iterable<T>): List<T> {
25
- return new List(iterable);
26
- }
27
-
28
- static range(start: number, end: number, step = 1): List<number> {
29
- const items: number[] = [];
30
- for (let i = start; step > 0 ? i < end : i > end; i += step) {
31
- items.push(i);
32
- }
33
- return new List(items);
34
- }
35
-
36
- static repeat<T>(value: T, count: number): List<T> {
37
- return new List(Array(count).fill(value));
38
- }
39
-
40
- get length(): number {
41
- return this.items.length;
42
- }
43
-
44
- get isEmpty(): boolean {
45
- return this.items.length === 0;
46
- }
47
-
48
- get(index: number): T | undefined {
49
- return index < 0 ? this.items[this.items.length + index] : this.items[index];
50
- }
51
-
52
- first(): T | undefined {
53
- return this.items[0];
54
- }
55
-
56
- last(): T | undefined {
57
- return this.items[this.items.length - 1];
58
- }
59
-
60
- map<U>(fn: (item: T, index: number) => U): List<U> {
61
- return new List(this.items.map(fn));
62
- }
63
-
64
- flatMap<U>(fn: (item: T, index: number) => List<U> | U[]): List<U> {
65
- const result: U[] = [];
66
- this.items.forEach((item, index) => {
67
- const mapped = fn(item, index);
68
- if (mapped instanceof List) {
69
- result.push(...mapped.toArray());
70
- } else {
71
- result.push(...mapped);
72
- }
73
- });
74
- return new List(result);
75
- }
76
-
77
- filter(predicate: (item: T, index: number) => boolean): List<T> {
78
- return new List(this.items.filter(predicate));
79
- }
80
-
81
- reject(predicate: (item: T, index: number) => boolean): List<T> {
82
- return this.filter((item, index) => !predicate(item, index));
83
- }
84
-
85
- reduce<U>(fn: (acc: U, item: T, index: number) => U, initial: U): U {
86
- return this.items.reduce(fn, initial);
87
- }
88
-
89
- fold<U>(initial: U, fn: (acc: U, item: T) => U): U {
90
- return this.items.reduce(fn, initial);
91
- }
92
-
93
- find(predicate: (item: T, index: number) => boolean): T | undefined {
94
- return this.items.find(predicate);
95
- }
96
-
97
- findIndex(predicate: (item: T, index: number) => boolean): number {
98
- return this.items.findIndex(predicate);
99
- }
100
-
101
- indexOf(item: T): number {
102
- return this.items.indexOf(item);
103
- }
104
-
105
- includes(item: T): boolean {
106
- return this.items.includes(item);
107
- }
108
-
109
- some(predicate: (item: T, index: number) => boolean): boolean {
110
- return this.items.some(predicate);
111
- }
112
-
113
- every(predicate: (item: T, index: number) => boolean): boolean {
114
- return this.items.every(predicate);
115
- }
116
-
117
- none(predicate: (item: T, index: number) => boolean): boolean {
118
- return !this.some(predicate);
119
- }
120
-
121
- count(predicate: (item: T, index: number) => boolean): number {
122
- return this.filter(predicate).length;
123
- }
124
-
125
- forEach(fn: (item: T, index: number) => void): void {
126
- this.items.forEach(fn);
127
- }
128
-
129
- concat(...lists: (List<T> | T[])[]): List<T> {
130
- const arrays = lists.map((l) => (l instanceof List ? l.toArray() : l));
131
- return new List([...this.items, ...arrays.flat()]);
132
- }
133
-
134
- append(item: T): List<T> {
135
- return new List([...this.items, item]);
136
- }
137
-
138
- prepend(item: T): List<T> {
139
- return new List([item, ...this.items]);
140
- }
141
-
142
- insert(index: number, item: T): List<T> {
143
- const arr = [...this.items];
144
- arr.splice(index, 0, item);
145
- return new List(arr);
146
- }
147
-
148
- remove(index: number): List<T> {
149
- const arr = [...this.items];
150
- arr.splice(index, 1);
151
- return new List(arr);
152
- }
153
-
154
- update(index: number, item: T): List<T> {
155
- const arr = [...this.items];
156
- arr[index] = item;
157
- return new List(arr);
158
- }
159
-
160
- slice(start?: number, end?: number): List<T> {
161
- return new List(this.items.slice(start, end));
162
- }
163
-
164
- take(n: number): List<T> {
165
- return this.slice(0, n);
166
- }
167
-
168
- takeLast(n: number): List<T> {
169
- return this.slice(-n);
170
- }
171
-
172
- takeWhile(predicate: (item: T, index: number) => boolean): List<T> {
173
- const result: T[] = [];
174
- for (let i = 0; i < this.items.length; i++) {
175
- if (!predicate(this.items[i], i)) break;
176
- result.push(this.items[i]);
177
- }
178
- return new List(result);
179
- }
180
-
181
- drop(n: number): List<T> {
182
- return this.slice(n);
183
- }
184
-
185
- dropLast(n: number): List<T> {
186
- return this.slice(0, -n);
187
- }
188
-
189
- dropWhile(predicate: (item: T, index: number) => boolean): List<T> {
190
- let startIndex = 0;
191
- for (let i = 0; i < this.items.length; i++) {
192
- if (!predicate(this.items[i], i)) break;
193
- startIndex = i + 1;
194
- }
195
- return this.slice(startIndex);
196
- }
197
-
198
- reverse(): List<T> {
199
- return new List([...this.items].reverse());
200
- }
201
-
202
- sort(compareFn?: (a: T, b: T) => number): List<T> {
203
- return new List([...this.items].sort(compareFn));
204
- }
205
-
206
- sortBy<U>(fn: (item: T) => U): List<T> {
207
- return this.sort((a, b) => {
208
- const va = fn(a);
209
- const vb = fn(b);
210
- if (va < vb) return -1;
211
- if (va > vb) return 1;
212
- return 0;
213
- });
214
- }
215
-
216
- shuffle(): List<T> {
217
- const arr = [...this.items];
218
- for (let i = arr.length - 1; i > 0; i--) {
219
- const j = Math.floor(Math.random() * (i + 1));
220
- [arr[i], arr[j]] = [arr[j], arr[i]];
221
- }
222
- return new List(arr);
223
- }
224
-
225
- unique(): List<T> {
226
- return new List([...new Set(this.items)]);
227
- }
228
-
229
- uniqueBy<U>(fn: (item: T) => U): List<T> {
230
- const seen = new Set<U>();
231
- const result: T[] = [];
232
- for (const item of this.items) {
233
- const key = fn(item);
234
- if (!seen.has(key)) {
235
- seen.add(key);
236
- result.push(item);
237
- }
238
- }
239
- return new List(result);
240
- }
241
-
242
- groupBy<K>(fn: (item: T) => K): HoloMap<K, List<T>> {
243
- const groups = new Map<K, T[]>();
244
- for (const item of this.items) {
245
- const key = fn(item);
246
- const group = groups.get(key) || [];
247
- group.push(item);
248
- groups.set(key, group);
249
- }
250
- const result = new Map<K, List<T>>();
251
- for (const [key, items] of groups) {
252
- result.set(key, new List(items));
253
- }
254
- return new HoloMap(result);
255
- }
256
-
257
- partition(predicate: (item: T, index: number) => boolean): [List<T>, List<T>] {
258
- const pass: T[] = [];
259
- const fail: T[] = [];
260
- this.items.forEach((item, index) => {
261
- if (predicate(item, index)) {
262
- pass.push(item);
263
- } else {
264
- fail.push(item);
265
- }
266
- });
267
- return [new List(pass), new List(fail)];
268
- }
269
-
270
- zip<U>(other: List<U>): List<[T, U]> {
271
- const length = Math.min(this.length, other.length);
272
- const result: [T, U][] = [];
273
- for (let i = 0; i < length; i++) {
274
- result.push([this.items[i], other.get(i)!]);
275
- }
276
- return new List(result);
277
- }
278
-
279
- zipWith<U, V>(other: List<U>, fn: (a: T, b: U) => V): List<V> {
280
- return this.zip(other).map(([a, b]) => fn(a, b));
281
- }
282
-
283
- flatten<U>(this: List<List<U> | U[]>): List<U> {
284
- const result: U[] = [];
285
- for (const item of this.items) {
286
- if (item instanceof List) {
287
- result.push(...item.toArray());
288
- } else if (Array.isArray(item)) {
289
- result.push(...item);
290
- }
291
- }
292
- return new List(result);
293
- }
294
-
295
- sum(this: List<number>): number {
296
- return this.reduce((acc, n) => acc + n, 0);
297
- }
298
-
299
- min(this: List<number>): number | undefined {
300
- if (this.isEmpty) return undefined;
301
- return Math.min(...this.items);
302
- }
303
-
304
- max(this: List<number>): number | undefined {
305
- if (this.isEmpty) return undefined;
306
- return Math.max(...this.items);
307
- }
308
-
309
- average(this: List<number>): number | undefined {
310
- if (this.isEmpty) return undefined;
311
- return this.sum() / this.length;
312
- }
313
-
314
- join(separator = ','): string {
315
- return this.items.join(separator);
316
- }
317
-
318
- toArray(): T[] {
319
- return [...this.items];
320
- }
321
-
322
- toSet(): HoloSet<T> {
323
- return new HoloSet(this.items);
324
- }
325
-
326
- [Symbol.iterator](): Iterator<T> {
327
- return this.items[Symbol.iterator]();
328
- }
329
-
330
- toString(): string {
331
- return `List(${this.items.join(', ')})`;
332
- }
333
- }
334
-
335
- /**
336
- * Immutable Map with functional operations
337
- */
338
- export class HoloMap<K, V> {
339
- private readonly _map: ReadonlyMap<K, V>;
340
-
341
- constructor(entries?: Iterable<[K, V]> | Map<K, V>) {
342
- this._map = entries instanceof Map ? entries : new Map(entries);
343
- }
344
-
345
- static of<K, V>(...entries: [K, V][]): HoloMap<K, V> {
346
- return new HoloMap(entries);
347
- }
348
-
349
- static from<K, V>(entries: Iterable<[K, V]>): HoloMap<K, V> {
350
- return new HoloMap(entries);
351
- }
352
-
353
- static fromObject<V>(obj: Record<string, V>): HoloMap<string, V> {
354
- return new HoloMap(Object.entries(obj));
355
- }
356
-
357
- get size(): number {
358
- return this._map.size;
359
- }
360
-
361
- get isEmpty(): boolean {
362
- return this._map.size === 0;
363
- }
364
-
365
- get(key: K): V | undefined {
366
- return this._map.get(key);
367
- }
368
-
369
- getOrDefault(key: K, defaultValue: V): V {
370
- return this._map.has(key) ? this._map.get(key)! : defaultValue;
371
- }
372
-
373
- has(key: K): boolean {
374
- return this._map.has(key);
375
- }
376
-
377
- set(key: K, value: V): HoloMap<K, V> {
378
- const newMap = new Map(this._map);
379
- newMap.set(key, value);
380
- return new HoloMap(newMap);
381
- }
382
-
383
- delete(key: K): HoloMap<K, V> {
384
- const newMap = new Map(this._map);
385
- newMap.delete(key);
386
- return new HoloMap(newMap);
387
- }
388
-
389
- update(key: K, fn: (value: V | undefined) => V): HoloMap<K, V> {
390
- return this.set(key, fn(this.get(key)));
391
- }
392
-
393
- merge(other: HoloMap<K, V>): HoloMap<K, V> {
394
- const newMap = new Map(this._map);
395
- for (const [key, value] of other) {
396
- newMap.set(key, value);
397
- }
398
- return new HoloMap(newMap);
399
- }
400
-
401
- keys(): List<K> {
402
- return new List(this._map.keys());
403
- }
404
-
405
- values(): List<V> {
406
- return new List(this._map.values());
407
- }
408
-
409
- entries(): List<[K, V]> {
410
- return new List(this._map.entries());
411
- }
412
-
413
- map<U>(fn: (value: V, key: K) => U): HoloMap<K, U> {
414
- const entries: [K, U][] = [];
415
- for (const [key, value] of this._map) {
416
- entries.push([key, fn(value, key)]);
417
- }
418
- return new HoloMap(entries);
419
- }
420
-
421
- mapKeys<K2>(fn: (key: K, value: V) => K2): HoloMap<K2, V> {
422
- const entries: [K2, V][] = [];
423
- for (const [key, value] of this._map) {
424
- entries.push([fn(key, value), value]);
425
- }
426
- return new HoloMap(entries);
427
- }
428
-
429
- filter(predicate: (value: V, key: K) => boolean): HoloMap<K, V> {
430
- const entries: [K, V][] = [];
431
- for (const [key, value] of this._map) {
432
- if (predicate(value, key)) {
433
- entries.push([key, value]);
434
- }
435
- }
436
- return new HoloMap(entries);
437
- }
438
-
439
- reduce<U>(fn: (acc: U, value: V, key: K) => U, initial: U): U {
440
- let acc = initial;
441
- for (const [key, value] of this._map) {
442
- acc = fn(acc, value, key);
443
- }
444
- return acc;
445
- }
446
-
447
- forEach(fn: (value: V, key: K) => void): void {
448
- this._map.forEach(fn);
449
- }
450
-
451
- find(predicate: (value: V, key: K) => boolean): [K, V] | undefined {
452
- for (const [key, value] of this._map) {
453
- if (predicate(value, key)) {
454
- return [key, value];
455
- }
456
- }
457
- return undefined;
458
- }
459
-
460
- some(predicate: (value: V, key: K) => boolean): boolean {
461
- for (const [key, value] of this._map) {
462
- if (predicate(value, key)) return true;
463
- }
464
- return false;
465
- }
466
-
467
- every(predicate: (value: V, key: K) => boolean): boolean {
468
- for (const [key, value] of this._map) {
469
- if (!predicate(value, key)) return false;
470
- }
471
- return true;
472
- }
473
-
474
- toObject(this: HoloMap<string, V>): Record<string, V> {
475
- const obj: Record<string, V> = {};
476
- for (const [key, value] of this._map) {
477
- obj[key] = value;
478
- }
479
- return obj;
480
- }
481
-
482
- toNativeMap(): Map<K, V> {
483
- return new Map(this._map);
484
- }
485
-
486
- [Symbol.iterator](): Iterator<[K, V]> {
487
- return this._map[Symbol.iterator]();
488
- }
489
-
490
- toString(): string {
491
- const entries = [...this._map].map(([k, v]) => `${k}: ${v}`).join(', ');
492
- return `HoloMap { ${entries} }`;
493
- }
494
- }
495
-
496
- /**
497
- * Immutable Set with functional operations
498
- */
499
- export class HoloSet<T> {
500
- private readonly set: ReadonlySet<T>;
501
-
502
- constructor(items?: Iterable<T>) {
503
- this.set = new Set(items);
504
- }
505
-
506
- static of<T>(...items: T[]): HoloSet<T> {
507
- return new HoloSet(items);
508
- }
509
-
510
- static from<T>(iterable: Iterable<T>): HoloSet<T> {
511
- return new HoloSet(iterable);
512
- }
513
-
514
- get size(): number {
515
- return this.set.size;
516
- }
517
-
518
- get isEmpty(): boolean {
519
- return this.set.size === 0;
520
- }
521
-
522
- has(item: T): boolean {
523
- return this.set.has(item);
524
- }
525
-
526
- add(item: T): HoloSet<T> {
527
- const newSet = new Set(this.set);
528
- newSet.add(item);
529
- return new HoloSet(newSet);
530
- }
531
-
532
- delete(item: T): HoloSet<T> {
533
- const newSet = new Set(this.set);
534
- newSet.delete(item);
535
- return new HoloSet(newSet);
536
- }
537
-
538
- addAll(items: Iterable<T>): HoloSet<T> {
539
- const newSet = new Set(this.set);
540
- for (const item of items) {
541
- newSet.add(item);
542
- }
543
- return new HoloSet(newSet);
544
- }
545
-
546
- union(other: HoloSet<T>): HoloSet<T> {
547
- return this.addAll(other);
548
- }
549
-
550
- intersection(other: HoloSet<T>): HoloSet<T> {
551
- const result = new Set<T>();
552
- for (const item of this.set) {
553
- if (other.has(item)) {
554
- result.add(item);
555
- }
556
- }
557
- return new HoloSet(result);
558
- }
559
-
560
- difference(other: HoloSet<T>): HoloSet<T> {
561
- const result = new Set<T>();
562
- for (const item of this.set) {
563
- if (!other.has(item)) {
564
- result.add(item);
565
- }
566
- }
567
- return new HoloSet(result);
568
- }
569
-
570
- symmetricDifference(other: HoloSet<T>): HoloSet<T> {
571
- return this.difference(other).union(other.difference(this));
572
- }
573
-
574
- isSubsetOf(other: HoloSet<T>): boolean {
575
- for (const item of this.set) {
576
- if (!other.has(item)) return false;
577
- }
578
- return true;
579
- }
580
-
581
- isSupersetOf(other: HoloSet<T>): boolean {
582
- return other.isSubsetOf(this);
583
- }
584
-
585
- isDisjointFrom(other: HoloSet<T>): boolean {
586
- return this.intersection(other).isEmpty;
587
- }
588
-
589
- map<U>(fn: (item: T) => U): HoloSet<U> {
590
- const result = new Set<U>();
591
- for (const item of this.set) {
592
- result.add(fn(item));
593
- }
594
- return new HoloSet(result);
595
- }
596
-
597
- filter(predicate: (item: T) => boolean): HoloSet<T> {
598
- const result = new Set<T>();
599
- for (const item of this.set) {
600
- if (predicate(item)) {
601
- result.add(item);
602
- }
603
- }
604
- return new HoloSet(result);
605
- }
606
-
607
- reduce<U>(fn: (acc: U, item: T) => U, initial: U): U {
608
- let acc = initial;
609
- for (const item of this.set) {
610
- acc = fn(acc, item);
611
- }
612
- return acc;
613
- }
614
-
615
- forEach(fn: (item: T) => void): void {
616
- this.set.forEach(fn);
617
- }
618
-
619
- find(predicate: (item: T) => boolean): T | undefined {
620
- for (const item of this.set) {
621
- if (predicate(item)) return item;
622
- }
623
- return undefined;
624
- }
625
-
626
- some(predicate: (item: T) => boolean): boolean {
627
- for (const item of this.set) {
628
- if (predicate(item)) return true;
629
- }
630
- return false;
631
- }
632
-
633
- every(predicate: (item: T) => boolean): boolean {
634
- for (const item of this.set) {
635
- if (!predicate(item)) return false;
636
- }
637
- return true;
638
- }
639
-
640
- toArray(): T[] {
641
- return [...this.set];
642
- }
643
-
644
- toList(): List<T> {
645
- return new List(this.set);
646
- }
647
-
648
- toNativeSet(): Set<T> {
649
- return new Set(this.set);
650
- }
651
-
652
- [Symbol.iterator](): Iterator<T> {
653
- return this.set[Symbol.iterator]();
654
- }
655
-
656
- toString(): string {
657
- return `HoloSet { ${[...this.set].join(', ')} }`;
658
- }
659
- }
660
-
661
- /**
662
- * Spatial grid for efficient spatial queries (useful for game worlds)
663
- */
664
- export class SpatialGrid<T> {
665
- private readonly cellSize: number;
666
- private readonly cells: Map<string, T[]> = new Map();
667
-
668
- constructor(cellSize: number) {
669
- this.cellSize = cellSize;
670
- }
671
-
672
- private getCellKey(x: number, y: number, z: number): string {
673
- const cx = Math.floor(x / this.cellSize);
674
- const cy = Math.floor(y / this.cellSize);
675
- const cz = Math.floor(z / this.cellSize);
676
- return `${cx},${cy},${cz}`;
677
- }
678
-
679
- insert(position: Vec3, item: T): void {
680
- const key = this.getCellKey(position.x, position.y, position.z);
681
- if (!this.cells.has(key)) {
682
- this.cells.set(key, []);
683
- }
684
- this.cells.get(key)!.push(item);
685
- }
686
-
687
- remove(position: Vec3, item: T): boolean {
688
- const key = this.getCellKey(position.x, position.y, position.z);
689
- const cell = this.cells.get(key);
690
- if (!cell) return false;
691
- const index = cell.indexOf(item);
692
- if (index === -1) return false;
693
- cell.splice(index, 1);
694
- if (cell.length === 0) {
695
- this.cells.delete(key);
696
- }
697
- return true;
698
- }
699
-
700
- query(center: Vec3, radius: number): T[] {
701
- const results: T[] = [];
702
- const cellRadius = Math.ceil(radius / this.cellSize);
703
- const cx = Math.floor(center.x / this.cellSize);
704
- const cy = Math.floor(center.y / this.cellSize);
705
- const cz = Math.floor(center.z / this.cellSize);
706
-
707
- for (let x = cx - cellRadius; x <= cx + cellRadius; x++) {
708
- for (let y = cy - cellRadius; y <= cy + cellRadius; y++) {
709
- for (let z = cz - cellRadius; z <= cz + cellRadius; z++) {
710
- const cell = this.cells.get(`${x},${y},${z}`);
711
- if (cell) {
712
- results.push(...cell);
713
- }
714
- }
715
- }
716
- }
717
-
718
- return results;
719
- }
720
-
721
- queryBox(min: Vec3, max: Vec3): T[] {
722
- const results: T[] = [];
723
- const minCx = Math.floor(min.x / this.cellSize);
724
- const minCy = Math.floor(min.y / this.cellSize);
725
- const minCz = Math.floor(min.z / this.cellSize);
726
- const maxCx = Math.floor(max.x / this.cellSize);
727
- const maxCy = Math.floor(max.y / this.cellSize);
728
- const maxCz = Math.floor(max.z / this.cellSize);
729
-
730
- for (let x = minCx; x <= maxCx; x++) {
731
- for (let y = minCy; y <= maxCy; y++) {
732
- for (let z = minCz; z <= maxCz; z++) {
733
- const cell = this.cells.get(`${x},${y},${z}`);
734
- if (cell) {
735
- results.push(...cell);
736
- }
737
- }
738
- }
739
- }
740
-
741
- return results;
742
- }
743
-
744
- clear(): void {
745
- this.cells.clear();
746
- }
747
-
748
- get cellCount(): number {
749
- return this.cells.size;
750
- }
751
-
752
- get itemCount(): number {
753
- let count = 0;
754
- for (const cell of this.cells.values()) {
755
- count += cell.length;
756
- }
757
- return count;
758
- }
759
- }
760
-
761
- /**
762
- * Priority queue (min-heap by default)
763
- */
764
- export class PriorityQueue<T> {
765
- private heap: { item: T; priority: number }[] = [];
766
-
767
- constructor(private compareFn: (a: number, b: number) => number = (a, b) => a - b) {}
768
-
769
- static minHeap<T>(): PriorityQueue<T> {
770
- return new PriorityQueue((a, b) => a - b);
771
- }
772
-
773
- static maxHeap<T>(): PriorityQueue<T> {
774
- return new PriorityQueue((a, b) => b - a);
775
- }
776
-
777
- get size(): number {
778
- return this.heap.length;
779
- }
780
-
781
- get isEmpty(): boolean {
782
- return this.heap.length === 0;
783
- }
784
-
785
- enqueue(item: T, priority: number): void {
786
- this.heap.push({ item, priority });
787
- this.bubbleUp(this.heap.length - 1);
788
- }
789
-
790
- dequeue(): T | undefined {
791
- if (this.isEmpty) return undefined;
792
- const result = this.heap[0].item;
793
- const last = this.heap.pop()!;
794
- if (this.heap.length > 0) {
795
- this.heap[0] = last;
796
- this.bubbleDown(0);
797
- }
798
- return result;
799
- }
800
-
801
- peek(): T | undefined {
802
- return this.heap[0]?.item;
803
- }
804
-
805
- peekPriority(): number | undefined {
806
- return this.heap[0]?.priority;
807
- }
808
-
809
- clear(): void {
810
- this.heap = [];
811
- }
812
-
813
- private bubbleUp(index: number): void {
814
- while (index > 0) {
815
- const parentIndex = Math.floor((index - 1) / 2);
816
- if (this.compareFn(this.heap[index].priority, this.heap[parentIndex].priority) >= 0) break;
817
- [this.heap[index], this.heap[parentIndex]] = [this.heap[parentIndex], this.heap[index]];
818
- index = parentIndex;
819
- }
820
- }
821
-
822
- private bubbleDown(index: number): void {
823
- while (true) {
824
- const leftChild = 2 * index + 1;
825
- const rightChild = 2 * index + 2;
826
- let smallest = index;
827
-
828
- if (
829
- leftChild < this.heap.length &&
830
- this.compareFn(this.heap[leftChild].priority, this.heap[smallest].priority) < 0
831
- ) {
832
- smallest = leftChild;
833
- }
834
- if (
835
- rightChild < this.heap.length &&
836
- this.compareFn(this.heap[rightChild].priority, this.heap[smallest].priority) < 0
837
- ) {
838
- smallest = rightChild;
839
- }
840
-
841
- if (smallest === index) break;
842
- [this.heap[index], this.heap[smallest]] = [this.heap[smallest], this.heap[index]];
843
- index = smallest;
844
- }
845
- }
846
- }
847
-
848
- // Convenience aliases
849
- export { HoloMap as Map };
850
- export { HoloSet as Set };