@daneren2005/shared-memory-objects 1.6.1 → 1.6.3

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 (47) hide show
  1. package/README.md +3 -2
  2. package/dist/allocated-memory.js +1 -2
  3. package/dist/cached-item-list.js +1 -2
  4. package/dist/constant-string.js +13 -14
  5. package/dist/local-pool.js +5 -6
  6. package/dist/lock/read-write-lock.js +0 -1
  7. package/dist/lock/simple-lock.js +0 -1
  8. package/dist/memory-buffer.js +1 -2
  9. package/dist/memory-heap.js +24 -25
  10. package/dist/serialize-object.js +4 -5
  11. package/dist/shared-list.js +43 -44
  12. package/dist/shared-map.js +74 -75
  13. package/dist/shared-pointer-list.js +15 -16
  14. package/dist/shared-pool.d.ts +1 -0
  15. package/dist/shared-pool.js +36 -32
  16. package/dist/shared-pool.js.map +1 -1
  17. package/dist/shared-stack.js +33 -34
  18. package/dist/shared-string.js +19 -20
  19. package/dist/shared-vector.js +23 -24
  20. package/dist/spatial/shared-quadtree.js +81 -82
  21. package/dist/spatial/shared-quadtree.js.map +1 -1
  22. package/dist/spatial/shared-spatial-grid.js +104 -110
  23. package/dist/spatial/shared-spatial-grid.js.map +1 -1
  24. package/dist/spatial/shared-spatial-map.d.ts +2 -0
  25. package/dist/spatial/shared-spatial-map.js +205 -202
  26. package/dist/spatial/shared-spatial-map.js.map +1 -1
  27. package/dist/spatial/spatial-entity.js +1 -2
  28. package/dist/spatial/spatial-neighbors.js +1 -2
  29. package/dist/utils/16-from-32-array.js +1 -2
  30. package/dist/utils/16-from-32-float.js +1 -2
  31. package/dist/utils/16-from-32-int.js +1 -2
  32. package/dist/utils/16-from-32-uint.js +1 -2
  33. package/dist/utils/16-from-64-array.js +1 -2
  34. package/dist/utils/16-from-64-float.js +1 -2
  35. package/dist/utils/16-from-64-int.js +1 -2
  36. package/dist/utils/16-from-64-uint.js +1 -2
  37. package/dist/utils/32-from-64-float.js +1 -2
  38. package/dist/utils/32-from-64-int.js +1 -2
  39. package/dist/utils/32-from-64-uint.js +1 -2
  40. package/dist/utils/array-type.js +1 -2
  41. package/dist/utils/atomic-math.js +1 -2
  42. package/dist/utils/float32-atomics.js +1 -2
  43. package/dist/utils/float64-atomics.js +1 -2
  44. package/dist/utils/hash.js +1 -2
  45. package/dist/utils/pointer.js +1 -2
  46. package/dist/utils/typedarray.js +1 -2
  47. package/package.json +11 -11
@@ -1,15 +1,15 @@
1
- import { getPointer as e, loadRawPointer as t, storeRawPointer as n } from "../utils/pointer.js";
2
- import r from "../allocated-memory.js";
3
- import { readLock as i, readUnlock as a, writeLock as o, writeUnlock as s } from "../lock/read-write-lock.js";
4
- import { ARRAY_TYPE as c, getArrayTypeCode as l, getByteMultipler as u, makeArrayView as d } from "../utils/array-type.js";
5
- import { lock as f, unlock as p } from "../lock/simple-lock.js";
6
- import m from "../shared-map.js";
7
- import h from "../shared-stack.js";
8
- import g from "../shared-pool.js";
1
+ import e from "../allocated-memory.js";
2
+ import t from "../shared-pool.js";
3
+ import n from "../shared-map.js";
4
+ import r from "../shared-stack.js";
5
+ import { lock as i, unlock as a } from "../lock/simple-lock.js";
6
+ import { readLock as o, readUnlock as s, writeLock as c, writeUnlock as l } from "../lock/read-write-lock.js";
7
+ import { getPointer as u, loadRawPointer as d, storeRawPointer as f } from "../utils/pointer.js";
8
+ import { ARRAY_TYPE as p, getArrayTypeCode as m, getByteMultipler as h, makeArrayView as g } from "../utils/array-type.js";
9
9
  import { addCandidate as _, candidateLimit as v, distanceSquaredToRect as y } from "./spatial-neighbors.js";
10
10
  import { getSpatialMapCapacity as ee, getSpatialPoolChunkSize as te } from "./spatial-entity.js";
11
- //#region src/spatial/shared-spatial-map.ts
12
- var b = 0, x = 1, S = 2, C = 3, w = 4, T = 5, E = 6, D = 8, ne = 10, O = 0, k = 1, A = 2, j = 8, M = 256, re = 255, N = 0, P = 1, F = 2, I = 3, L = 4, R = 5, z = 6, B = 7, V = 0, H = 1, U = 2, W = 3, G = 4, K = 5, q = 6, J = 7, Y = 8, X = 16777216, ie = X, ae = 4294967295, oe = 73856093, se = 19349663, ce = 50, le = 256, Z = 1, Q = 2, ue = 1e6, de = 4, fe = Float32Array, pe = class l {
11
+ // src/spatial/shared-spatial-map.ts
12
+ var ne = 0, b = 1, x = 2, S = 3, C = 4, w = 5, T = 6, E = 8, D = 10, O = 0, k = 1, A = 2, j = 8, M = 256, re = 255, N = 0, P = 1, F = 2, I = 3, L = 4, R = 5, z = 6, B = 7, V = 0, H = 1, U = 2, W = 3, G = 4, K = 5, q = 6, J = 7, Y = 8, X = 16777216, ie = X, ae = 4294967295, oe = 4294967296, se = 73856093, ce = 19349663, le = 50, ue = 256, Z = 1, Q = 2, de = 1e6, fe = 4, pe = Float32Array, me = class m {
13
13
  static ALLOCATE_COUNT = 11;
14
14
  memory;
15
15
  firstBlock;
@@ -41,6 +41,7 @@ var b = 0, x = 1, S = 2, C = 3, w = 4, T = 5, E = 6, D = 8, ne = 10, O = 0, k =
41
41
  tableLock;
42
42
  coordType;
43
43
  nullIndex;
44
+ idLimit;
44
45
  bucketCount;
45
46
  maxBucketCount;
46
47
  bucketMask;
@@ -52,7 +53,7 @@ var b = 0, x = 1, S = 2, C = 3, w = 4, T = 5, E = 6, D = 8, ne = 10, O = 0, k =
52
53
  return this.cellSize;
53
54
  }
54
55
  get buckets() {
55
- return Atomics.load(this.firstBlock.data, w);
56
+ return Atomics.load(this.firstBlock.data, C);
56
57
  }
57
58
  get maxBuckets() {
58
59
  return this.maxBucketCount;
@@ -61,43 +62,43 @@ var b = 0, x = 1, S = 2, C = 3, w = 4, T = 5, E = 6, D = 8, ne = 10, O = 0, k =
61
62
  return this.coordType;
62
63
  }
63
64
  constructor(i, a) {
64
- if (this.memory = i, a && "firstBlock" in a) this.firstBlock = new r(i, a.firstBlock), this.coordType = this.firstBlock.data[T], this.bucketPointers = new h(i, { firstBlock: e(t(this.firstBlock.data, b), i.positionBits) }), this.entityPool = new g(i, { firstBlock: e(t(this.firstBlock.data, x), i.positionBits) }), this.slotPool = new g(i, { firstBlock: e(t(this.firstBlock.data, S), i.positionBits) }), this.idMap = new m(i, { firstBlock: e(t(this.firstBlock.data, C), i.positionBits) }), this.bucketCount = Atomics.load(this.firstBlock.data, w), this.maxBucketCount = this.firstBlock.data[ne], this.nullIndex = ge(this.coordType);
65
+ if (this.memory = i, a && "firstBlock" in a) this.firstBlock = new e(i, a.firstBlock), this.coordType = this.firstBlock.data[w], this.bucketPointers = new r(i, { firstBlock: u(d(this.firstBlock.data, ne), i.positionBits) }), this.entityPool = new t(i, { firstBlock: u(d(this.firstBlock.data, b), i.positionBits) }), this.slotPool = new t(i, { firstBlock: u(d(this.firstBlock.data, x), i.positionBits) }), this.idMap = new n(i, { firstBlock: u(d(this.firstBlock.data, S), i.positionBits) }), this.bucketCount = Atomics.load(this.firstBlock.data, C), this.maxBucketCount = this.firstBlock.data[D], this.nullIndex = _e(this.coordType);
65
66
  else {
66
- let e = a?.gridSize ?? ce;
67
- this.bucketCount = Math.min(1073741824, Math.max(1, Math.floor(a?.buckets ?? le)));
68
- let t = a?.maxEntities ?? ue, r = a?.maxSlots ?? t * de;
69
- if (this.maxBucketCount = Math.min(1073741824, Math.max(this.bucketCount, Math.floor(a?.maxBuckets ?? _e(r * 2)))), this.coordType = me(a?.type), this.nullIndex = ge(this.coordType), this.coordType === c.float32 && (t > X || r > X)) throw Error(`Float32 SharedSpatialMap supports at most ${X} entities and slots; pass type: Float64Array for more`);
70
- let o = {
71
- type: he(this.coordType),
67
+ let e = a?.gridSize ?? le;
68
+ this.bucketCount = Math.min(1073741824, Math.max(1, Math.floor(a?.buckets ?? ue)));
69
+ let o = a?.maxEntities ?? de, s = a?.maxSlots ?? o * fe;
70
+ if (this.maxBucketCount = Math.min(1073741824, Math.max(this.bucketCount, Math.floor(a?.maxBuckets ?? ve(s * 2)))), this.coordType = he(a?.type), this.nullIndex = _e(this.coordType), this.coordType === p.float32 && (o > X || s > X)) throw Error(`Float32 SharedSpatialMap supports at most ${X} entities and slots; pass type: Float64Array for more`);
71
+ let c = {
72
+ type: ge(this.coordType),
72
73
  dataLength: Y,
73
- maxLength: t,
74
- maxChunkSize: te(t, Y, u(this.coordType), i.maxAllocationLength)
75
- }, s = {
76
- type: he(this.coordType),
74
+ maxLength: o,
75
+ maxChunkSize: te(o, Y, h(this.coordType), i.maxAllocationLength)
76
+ }, l = {
77
+ type: ge(this.coordType),
77
78
  dataLength: B,
78
- maxLength: r,
79
- maxChunkSize: te(r, B, u(this.coordType), i.maxAllocationLength)
80
- }, d = g.getAllocateCount(i, o), f = g.getAllocateCount(i, s), p = m.ALLOCATE_COUNT, _ = l.ALLOCATE_COUNT;
81
- this.firstBlock = i.allocUI32(_ + d + f + p), this.firstBlock.data[w] = this.bucketCount, this.firstBlock.data[T] = this.coordType, this.firstBlock.data[ne] = this.maxBucketCount;
79
+ maxLength: s,
80
+ maxChunkSize: te(s, B, h(this.coordType), i.maxAllocationLength)
81
+ }, u = t.getAllocateCount(i, c), d = t.getAllocateCount(i, l), g = n.ALLOCATE_COUNT, _ = m.ALLOCATE_COUNT;
82
+ this.firstBlock = i.allocUI32(_ + u + d + g), this.firstBlock.data[C] = this.bucketCount, this.firstBlock.data[w] = this.coordType, this.firstBlock.data[D] = this.maxBucketCount;
82
83
  let v = this.getConfigView();
83
- v[0] = e, this.bucketPointers = new h(i), n(this.firstBlock.data, b, this.bucketPointers.pointer), this.appendBucketSegments(this.bucketCount);
84
- let y = this.firstBlock.getSubAllocation(_, d);
85
- this.entityPool = new g(i, {
84
+ v[0] = e, this.bucketPointers = new r(i), f(this.firstBlock.data, ne, this.bucketPointers.pointer), this.appendBucketSegments(this.bucketCount);
85
+ let y = this.firstBlock.getSubAllocation(_, u);
86
+ this.entityPool = new t(i, {
86
87
  firstBlock: y,
87
- ...o
88
- }), n(this.firstBlock.data, x, y.pointer);
89
- let E = this.firstBlock.getSubAllocation(_ + d, f);
90
- this.slotPool = new g(i, {
88
+ ...c
89
+ }), f(this.firstBlock.data, b, y.pointer);
90
+ let T = this.firstBlock.getSubAllocation(_ + u, d);
91
+ this.slotPool = new t(i, {
92
+ firstBlock: T,
93
+ ...l
94
+ }), f(this.firstBlock.data, x, T.pointer);
95
+ let E = this.firstBlock.getSubAllocation(_ + u + d, g);
96
+ this.idMap = new n(i, {
91
97
  firstBlock: E,
92
- ...s
93
- }), n(this.firstBlock.data, S, E.pointer);
94
- let D = this.firstBlock.getSubAllocation(_ + d + f, p);
95
- this.idMap = new m(i, {
96
- firstBlock: D,
97
- capacity: ee(t, i.maxAllocationLength)
98
- }), n(this.firstBlock.data, C, D.pointer);
98
+ capacity: ee(o, i.maxAllocationLength)
99
+ }), f(this.firstBlock.data, S, E.pointer);
99
100
  }
100
- this.bucketMask = $(this.bucketCount), this.tableLock = new Int32Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + D * Uint32Array.BYTES_PER_ELEMENT, 2);
101
+ this.idLimit = this.coordType === p.float32 ? X : oe, this.bucketMask = $(this.bucketCount), this.tableLock = new Int32Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + E * Uint32Array.BYTES_PER_ELEMENT, 2);
101
102
  let o = this.getConfigView();
102
103
  this.cellSize = o[0];
103
104
  }
@@ -106,49 +107,52 @@ var b = 0, x = 1, S = 2, C = 3, w = 4, T = 5, E = 6, D = 8, ne = 10, O = 0, k =
106
107
  }
107
108
  get usedMemory() {
108
109
  let t = this.firstBlock.usedMemory + this.bucketPointers.usedMemory + this.entityPool.usedMemory + this.slotPool.usedMemory + this.idMap.usedMemory;
109
- for (let n of this.bucketPointers) t += new r(this.memory, e(n, this.memory.positionBits)).usedMemory;
110
+ for (let n of this.bucketPointers) t += new e(this.memory, u(n, this.memory.positionBits)).usedMemory;
110
111
  return t;
111
112
  }
112
- insert(e, t, n, r = 0, o = 0) {
113
- i(this.tableLock);
113
+ insert(e, t, n, r = 0, i = 0) {
114
+ o(this.tableLock);
114
115
  try {
115
- this.syncBucketCount(), this.insertLocked(e, t, n, r, o);
116
+ this.syncBucketCount(), this.insertLocked(e, t, n, r, i);
116
117
  } finally {
117
- a(this.tableLock);
118
+ s(this.tableLock);
118
119
  }
119
120
  this.growBucketsIfNeeded();
120
121
  }
121
122
  bulkInsert(e) {
122
123
  let t = /* @__PURE__ */ new Map();
123
- for (let n of e) t.set(n.id, n);
124
- let n = Array.from(t.values()), r = this.idMap.getAll(n.map((e) => e.id)), o = [], s = 0, c = 0;
124
+ for (let n of e) this.assertValidId(n.id), t.set(n.id, n);
125
+ let n = Array.from(t.values()), r = this.idMap.getAll(n.map((e) => e.id)), i = [], a = 0, c = 0;
125
126
  for (let e = 0; e < n.length; e++) if (r[e] === void 0) {
126
127
  let t = n[e];
127
- c++, s += (this.colOf(t.x + (t.width ?? 0)) - this.colOf(t.x) + 1) * (this.rowOf(t.y + (t.height ?? 0)) - this.rowOf(t.y) + 1);
128
+ c++, a += (this.colOf(t.x + (t.width ?? 0)) - this.colOf(t.x) + 1) * (this.rowOf(t.y + (t.height ?? 0)) - this.rowOf(t.y) + 1);
128
129
  }
129
- this.ensureBucketCapacity(this.slotPool.length + s);
130
- let l = this.entityPool.reserveContiguous(c), u = this.slotPool.reserveContiguous(s);
131
- i(this.tableLock);
130
+ this.ensureBucketCapacity(this.slotPool.length + a);
131
+ let l = this.entityPool.reserveContiguous(c), u = this.slotPool.reserveContiguous(a);
132
+ o(this.tableLock);
132
133
  try {
133
134
  this.syncBucketCount();
134
135
  for (let e = 0; e < n.length; e++) {
135
- let t = n[e], i = t.width ?? 0, a = t.height ?? 0;
136
+ let t = n[e], a = t.width ?? 0, o = t.height ?? 0;
136
137
  if (r[e] !== void 0) {
137
- this.updateLocked(t.id, t.x, t.y, i, a);
138
+ this.updateLocked(t.id, t.x, t.y, a, o);
138
139
  continue;
139
140
  }
140
- let s = this.colOf(t.x), c = this.rowOf(t.y), d = this.colOf(t.x + i), f = this.rowOf(t.y + a), p = l++, m = this.entityPool.blockFor(p), h = this.entityPool.blockOffset(p);
141
- m[h + V] = s, m[h + H] = c, m[h + U] = d, m[h + W] = f, m[h + G] = t.x, m[h + K] = t.y, m[h + q] = i, m[h + J] = a, o.push([t.id, p]);
141
+ let s = this.colOf(t.x), c = this.rowOf(t.y), d = this.colOf(t.x + a), f = this.rowOf(t.y + o), p = l++, m = this.entityPool.blockFor(p), h = this.entityPool.blockOffset(p);
142
+ m[h + V] = s, m[h + H] = c, m[h + U] = d, m[h + W] = f, m[h + G] = t.x, m[h + K] = t.y, m[h + q] = a, m[h + J] = o, i.push([t.id, p]);
142
143
  for (let e = c; e <= f; e++) for (let n = s; n <= d; n++) this.linkReservedBucket(n, e, u++, t.id, p, s, c);
143
144
  }
144
- this.idMap.setAll(o);
145
+ this.idMap.setAll(i);
145
146
  } finally {
146
- a(this.tableLock);
147
+ s(this.tableLock);
147
148
  }
148
149
  this.growBucketsForBulk();
149
150
  }
151
+ assertValidId(e) {
152
+ if (!Number.isInteger(e) || e < 0 || e >= this.idLimit) throw Error(`SharedSpatialMap id ${e} must be an integer in [0, ${this.idLimit})` + (this.coordType === p.float32 ? "; pass type: Float64Array for ids up to 2^32 - 1" : ""));
153
+ }
150
154
  insertLocked(e, t, n, r, i) {
151
- if (this.idMap.get(e) !== void 0) {
155
+ if (this.assertValidId(e), this.idMap.get(e) !== void 0) {
152
156
  this.updateLocked(e, t, n, r, i);
153
157
  return;
154
158
  }
@@ -157,20 +161,20 @@ var b = 0, x = 1, S = 2, C = 3, w = 4, T = 5, E = 6, D = 8, ne = 10, O = 0, k =
157
161
  let u = this.entityPool.push(l);
158
162
  this.idMap.set(e, u), this.linkRange(e, u, a, o, s, c);
159
163
  }
160
- update(e, t, n, r = 0, o = 0) {
161
- let s = this.idMap.get(e);
162
- if (s !== void 0) {
163
- let e = this.entityPool.blockFor(s), i = this.entityPool.blockOffset(s), a = this.colOf(t), c = this.rowOf(n), l = this.colOf(t + r), u = this.rowOf(n + o);
164
- if (a === e[i + V] && c === e[i + H] && l === e[i + U] && u === e[i + W]) {
165
- e[i + G] = t, e[i + K] = n, e[i + q] = r, e[i + J] = o;
164
+ update(e, t, n, r = 0, i = 0) {
165
+ let a = this.idMap.get(e);
166
+ if (a !== void 0) {
167
+ let e = this.entityPool.blockFor(a), o = this.entityPool.blockOffset(a), s = this.colOf(t), c = this.rowOf(n), l = this.colOf(t + r), u = this.rowOf(n + i);
168
+ if (s === e[o + V] && c === e[o + H] && l === e[o + U] && u === e[o + W]) {
169
+ e[o + G] = t, e[o + K] = n, e[o + q] = r, e[o + J] = i;
166
170
  return;
167
171
  }
168
172
  }
169
- i(this.tableLock);
173
+ o(this.tableLock);
170
174
  try {
171
- this.syncBucketCount(), this.updateLocked(e, t, n, r, o);
175
+ this.syncBucketCount(), this.updateLocked(e, t, n, r, i);
172
176
  } finally {
173
- a(this.tableLock);
177
+ s(this.tableLock);
174
178
  }
175
179
  this.growBucketsIfNeeded();
176
180
  }
@@ -181,18 +185,14 @@ var b = 0, x = 1, S = 2, C = 3, w = 4, T = 5, E = 6, D = 8, ne = 10, O = 0, k =
181
185
  return;
182
186
  }
183
187
  let o = this.entityPool.blockFor(a), s = this.entityPool.blockOffset(a), c = o[s + V], l = o[s + H], u = o[s + U], d = o[s + W], f = this.colOf(t), p = this.rowOf(n), m = this.colOf(t + r), h = this.rowOf(n + i);
184
- if (f === c && p === l && m === u && h === d) {
185
- o[s + G] = t, o[s + K] = n, o[s + q] = r, o[s + J] = i;
186
- return;
187
- }
188
- this.relinkRange(e, a, f, p, m, h, c, l, u, d), o[s + V] = f, o[s + H] = p, o[s + U] = m, o[s + W] = h, o[s + G] = t, o[s + K] = n, o[s + q] = r, o[s + J] = i;
188
+ f === c && p === l && m === u && h === d ? (o[s + G] = t, o[s + K] = n, o[s + q] = r, o[s + J] = i) : (this.relinkRange(e, a, f, p, m, h, c, l, u, d), o[s + V] = f, o[s + H] = p, o[s + U] = m, o[s + W] = h, o[s + G] = t, o[s + K] = n, o[s + q] = r, o[s + J] = i);
189
189
  }
190
190
  remove(e) {
191
- i(this.tableLock);
191
+ o(this.tableLock);
192
192
  try {
193
193
  return this.syncBucketCount(), this.removeLocked(e);
194
194
  } finally {
195
- a(this.tableLock);
195
+ s(this.tableLock);
196
196
  }
197
197
  }
198
198
  removeLocked(e) {
@@ -208,12 +208,12 @@ var b = 0, x = 1, S = 2, C = 3, w = 4, T = 5, E = 6, D = 8, ne = 10, O = 0, k =
208
208
  let a = [];
209
209
  return this.searchInto(a, e, t, n, r, i), a;
210
210
  }
211
- searchInto(e, t, n, r, o, s) {
212
- i(this.tableLock);
211
+ searchInto(e, t, n, r, i, a) {
212
+ o(this.tableLock);
213
213
  try {
214
- return this.syncBucketCount(), this.searchIntoLocked(e, t, n, r, o, s);
214
+ return this.syncBucketCount(), this.searchIntoLocked(e, t, n, r, i, a);
215
215
  } finally {
216
- a(this.tableLock);
216
+ s(this.tableLock);
217
217
  }
218
218
  }
219
219
  searchIntoLocked(e, t, n, r, i, a) {
@@ -232,12 +232,12 @@ var b = 0, x = 1, S = 2, C = 3, w = 4, T = 5, E = 6, D = 8, ne = 10, O = 0, k =
232
232
  }
233
233
  return e;
234
234
  }
235
- neighbors(e, t, n, r, o) {
236
- i(this.tableLock);
235
+ neighbors(e, t, n, r, i) {
236
+ o(this.tableLock);
237
237
  try {
238
- return this.syncBucketCount(), this.neighborsLocked(e, t, n, r, o);
238
+ return this.syncBucketCount(), this.neighborsLocked(e, t, n, r, i);
239
239
  } finally {
240
- a(this.tableLock);
240
+ s(this.tableLock);
241
241
  }
242
242
  }
243
243
  neighborsLocked(e, t, n, r, i) {
@@ -251,20 +251,19 @@ var b = 0, x = 1, S = 2, C = 3, w = 4, T = 5, E = 6, D = 8, ne = 10, O = 0, k =
251
251
  id: void 0,
252
252
  distance: a
253
253
  }, f = (r, f) => {
254
- if (i === void 0) {
255
- n === 1 ? this.collectNearestBucket(d, r, f, e, t) : this.collectNeighborCandidates(o, r, f, l, u, e, t, n, a);
256
- return;
257
- }
258
- s.length = 0, this.collectNeighborBucket(s, r, f);
259
- for (let r of s) {
260
- if (c?.has(r)) continue;
261
- c?.add(r);
262
- let s = this.idMap.get(r);
263
- if (s !== void 0 && (i === void 0 || i(r))) {
264
- if (n === 1) {
265
- let n = this.entityDistanceSquared(s, e, t);
266
- (n < d.distance || n === d.distance && (d.id === void 0 || r < d.id)) && (d.id = r, d.distance = n);
267
- } else this.addEntityCandidate(o, r, s, e, t, n, a);
254
+ if (i === void 0) n === 1 ? this.collectNearestBucket(d, r, f, e, t) : this.collectNeighborCandidates(o, r, f, l, u, e, t, n, a);
255
+ else {
256
+ s.length = 0, this.collectNeighborBucket(s, r, f);
257
+ for (let r of s) {
258
+ if (c?.has(r)) continue;
259
+ c?.add(r);
260
+ let s = this.idMap.get(r);
261
+ if (s !== void 0 && (i === void 0 || i(r))) {
262
+ if (n === 1) {
263
+ let n = this.entityDistanceSquared(s, e, t);
264
+ (n < d.distance || n === d.distance && (d.id === void 0 || r < d.id)) && (d.id = r, d.distance = n);
265
+ } else this.addEntityCandidate(o, r, s, e, t, n, a);
266
+ }
268
267
  }
269
268
  }
270
269
  };
@@ -282,7 +281,7 @@ var b = 0, x = 1, S = 2, C = 3, w = 4, T = 5, E = 6, D = 8, ne = 10, O = 0, k =
282
281
  return n === 1 ? d.id === void 0 ? [] : [d.id] : o.map((e) => e.id);
283
282
  }
284
283
  clear() {
285
- o(this.tableLock);
284
+ c(this.tableLock);
286
285
  try {
287
286
  this.syncBucketCount();
288
287
  for (let e = 0; e < this.bucketPointers.length; e++) {
@@ -295,18 +294,18 @@ var b = 0, x = 1, S = 2, C = 3, w = 4, T = 5, E = 6, D = 8, ne = 10, O = 0, k =
295
294
  for (let [t] of this.idMap) e.push(t);
296
295
  for (let t of e) this.idMap.delete(t);
297
296
  } finally {
298
- s(this.tableLock);
297
+ l(this.tableLock);
299
298
  }
300
299
  }
301
300
  free() {
302
- for (let t of this.bucketPointers) new r(this.memory, e(t, this.memory.positionBits)).free();
301
+ for (let t of this.bucketPointers) new e(this.memory, u(t, this.memory.positionBits)).free();
303
302
  this.bucketPointers.free(), this.entityPool.free(), this.slotPool.free(), this.idMap.free(), this.firstBlock.free();
304
303
  }
305
304
  getSharedMemory() {
306
305
  return { firstBlock: this.firstBlock.getSharedMemory() };
307
306
  }
308
307
  getConfigView() {
309
- return d(this.coordType, this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + E * Uint32Array.BYTES_PER_ELEMENT, 1);
308
+ return g(this.coordType, this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + T * Uint32Array.BYTES_PER_ELEMENT, 1);
310
309
  }
311
310
  colOf(e) {
312
311
  return Math.floor(e / this.cellSize);
@@ -320,17 +319,17 @@ var b = 0, x = 1, S = 2, C = 3, w = 4, T = 5, E = 6, D = 8, ne = 10, O = 0, k =
320
319
  return u * u;
321
320
  }
322
321
  bucketOf(e, t) {
323
- let n = (Math.imul(e, oe) ^ Math.imul(t, se)) >>> 0, r = this.bucketMask;
322
+ let n = (Math.imul(e, se) ^ Math.imul(t, ce)) >>> 0, r = this.bucketMask;
324
323
  return r >= 0 ? n & r : n % this.bucketCount;
325
324
  }
326
325
  bucketOffset(e) {
327
326
  return (e & re) * A;
328
327
  }
329
328
  getBucketSegment(t) {
330
- let n = t >>> j, i = this.bucketSegments[n];
331
- if (i !== void 0) return i;
332
- let a = this.bucketPointers.at(n), o = new r(this.memory, e(a, this.memory.positionBits)), s = new Int32Array(o.data.buffer, o.bufferByteOffset, M * A);
333
- return this.bucketHeadSegments[n] = new Uint32Array(o.data.buffer, o.bufferByteOffset, M * A), this.bucketSegments[n] = s, s;
329
+ let n = t >>> j, r = this.bucketSegments[n];
330
+ if (r !== void 0) return r;
331
+ let i = this.bucketPointers.at(n), a = new e(this.memory, u(i, this.memory.positionBits)), o = new Int32Array(a.data.buffer, a.bufferByteOffset, M * A);
332
+ return this.bucketHeadSegments[n] = new Uint32Array(a.data.buffer, a.bufferByteOffset, M * A), this.bucketSegments[n] = o, o;
334
333
  }
335
334
  appendBucketSegments(e) {
336
335
  let t = Math.ceil(e / M);
@@ -341,31 +340,31 @@ var b = 0, x = 1, S = 2, C = 3, w = 4, T = 5, E = 6, D = 8, ne = 10, O = 0, k =
341
340
  }
342
341
  }
343
342
  syncBucketCount() {
344
- let e = Atomics.load(this.firstBlock.data, w);
343
+ let e = Atomics.load(this.firstBlock.data, C);
345
344
  e !== this.bucketCount && (this.bucketCount = e, this.bucketMask = $(e));
346
345
  }
347
346
  growBucketsIfNeeded() {
348
- let e = Atomics.load(this.firstBlock.data, w);
347
+ let e = Atomics.load(this.firstBlock.data, C);
349
348
  if (!(e >= this.maxBucketCount || this.slotPool.length * Q <= e * Z)) {
350
- o(this.tableLock);
349
+ c(this.tableLock);
351
350
  try {
352
351
  if (this.syncBucketCount(), this.slotPool.length * Q <= this.bucketCount * Z) return;
353
352
  let e = Math.min(this.bucketCount * 2, this.maxBucketCount);
354
353
  if (e === this.bucketCount) return;
355
354
  this.resizeBuckets(e);
356
355
  } finally {
357
- s(this.tableLock);
356
+ l(this.tableLock);
358
357
  }
359
358
  }
360
359
  }
361
360
  ensureBucketCapacity(e) {
362
- let t = Math.ceil(e * Q / Z), n = Math.min(_e(t), this.maxBucketCount);
363
- if (!(n <= Atomics.load(this.firstBlock.data, w))) {
364
- o(this.tableLock);
361
+ let t = Math.ceil(e * Q / Z), n = Math.min(ve(t), this.maxBucketCount);
362
+ if (!(n <= Atomics.load(this.firstBlock.data, C))) {
363
+ c(this.tableLock);
365
364
  try {
366
365
  this.syncBucketCount(), n > this.bucketCount && this.resizeBuckets(n);
367
366
  } finally {
368
- s(this.tableLock);
367
+ l(this.tableLock);
369
368
  }
370
369
  }
371
370
  }
@@ -377,16 +376,21 @@ var b = 0, x = 1, S = 2, C = 3, w = 4, T = 5, E = 6, D = 8, ne = 10, O = 0, k =
377
376
  for (let e = k; e < t.length; e += A) t[e] = this.nullIndex;
378
377
  }
379
378
  this.bucketCount = e, this.bucketMask = $(e);
380
- for (let [e, t] of this.slotPool.entries()) {
381
- let n = this.bucketOf(t[P], t[F]), r = this.bucketHeadSegments[n >>> j], i = this.bucketOffset(n) + k;
382
- t[R] = r[i], r[i] = e;
379
+ let t = this.slotPool, n = t.getRecycledMask(), r = n.length, i = t.maxChunkSize;
380
+ for (let e = 0; e < r; e += i) {
381
+ let a = t.blockFor(e), o = Math.min(e + i, r);
382
+ for (let t = e; t < o; t++) {
383
+ if (n[t] === 1) continue;
384
+ let r = (t - e) * B, i = this.bucketOf(a[r + P], a[r + F]), o = this.bucketHeadSegments[i >>> j], s = this.bucketOffset(i) + k;
385
+ a[r + R] = o[s], o[s] = t;
386
+ }
383
387
  }
384
- Atomics.store(this.firstBlock.data, w, e);
388
+ Atomics.store(this.firstBlock.data, C, e);
385
389
  }
386
390
  growBucketsForBulk() {
387
391
  for (;;) {
388
- let e = Atomics.load(this.firstBlock.data, w);
389
- if (this.growBucketsIfNeeded(), Atomics.load(this.firstBlock.data, w) === e) return;
392
+ let e = Atomics.load(this.firstBlock.data, C);
393
+ if (this.growBucketsIfNeeded(), Atomics.load(this.firstBlock.data, C) === e) return;
390
394
  }
391
395
  }
392
396
  linkRange(e, t, n, r, i, a) {
@@ -396,18 +400,18 @@ var b = 0, x = 1, S = 2, C = 3, w = 4, T = 5, E = 6, D = 8, ne = 10, O = 0, k =
396
400
  }
397
401
  linkIntoBucket(e, t, n) {
398
402
  n[P] = e, n[F] = t;
399
- let r = this.slotPool, i = r.push(n), a = this.bucketOf(e, t), o = this.getBucketSegment(a), s = this.bucketHeadSegments[a >>> j], c = this.bucketOffset(a), l = c + O;
400
- f(o, l);
401
- let u = c + k;
402
- r.set(i, R, s[u]), s[u] = i, p(o, l);
403
- }
404
- linkReservedBucket(e, t, n, r, i, a, o) {
405
- let s = this.slotPool, c = s.blockFor(n), l = s.blockOffset(n);
406
- c[l + N] = r, c[l + P] = e, c[l + F] = t, c[l + I] = a, c[l + L] = o, c[l + z] = i;
407
- let u = this.bucketOf(e, t), d = this.getBucketSegment(u), m = this.bucketHeadSegments[u >>> j], h = this.bucketOffset(u), g = h + O;
408
- f(d, g);
403
+ let r = this.slotPool, o = r.push(n), s = this.bucketOf(e, t), c = this.getBucketSegment(s), l = this.bucketHeadSegments[s >>> j], u = this.bucketOffset(s), d = u + O;
404
+ i(c, d);
405
+ let f = u + k;
406
+ r.set(o, R, l[f]), l[f] = o, a(c, d);
407
+ }
408
+ linkReservedBucket(e, t, n, r, o, s, c) {
409
+ let l = this.slotPool, u = l.blockFor(n), d = l.blockOffset(n);
410
+ u[d + N] = r, u[d + P] = e, u[d + F] = t, u[d + I] = s, u[d + L] = c, u[d + z] = o;
411
+ let f = this.bucketOf(e, t), p = this.getBucketSegment(f), m = this.bucketHeadSegments[f >>> j], h = this.bucketOffset(f), g = h + O;
412
+ i(p, g);
409
413
  let _ = h + k;
410
- c[l + R] = m[_], m[_] = n, p(d, g);
414
+ u[d + R] = m[_], m[_] = n, a(p, g);
411
415
  }
412
416
  relinkRange(e, t, n, r, i, a, o, s, c, l) {
413
417
  let u = n > o ? n : o, d = i < c ? i : c, f = r > s ? r : s, p = a < l ? a : l, m = u <= d && f <= p, h = this.slotScratch;
@@ -426,19 +430,19 @@ var b = 0, x = 1, S = 2, C = 3, w = 4, T = 5, E = 6, D = 8, ne = 10, O = 0, k =
426
430
  }
427
431
  }
428
432
  }
429
- refreshAnchorInBucket(e, t, n, r, i, a) {
430
- let o = this.nullIndex, s = this.getBucketSegment(e), c = this.bucketOffset(e), l = c + O;
431
- f(s, l);
432
- let u = this.slotPool, d = this.bucketHeadSegments[e >>> j][c + k];
433
- for (; d !== o;) {
434
- let e = u.blockFor(d), o = u.blockOffset(d);
435
- if (e[o + N] === t && e[o + P] === n && e[o + F] === r) {
436
- e[o + I] = i, e[o + L] = a;
433
+ refreshAnchorInBucket(e, t, n, r, o, s) {
434
+ let c = this.nullIndex, l = this.getBucketSegment(e), u = this.bucketOffset(e), d = u + O;
435
+ i(l, d);
436
+ let f = this.slotPool, p = this.bucketHeadSegments[e >>> j][u + k];
437
+ for (; p !== c;) {
438
+ let e = f.blockFor(p), i = f.blockOffset(p);
439
+ if (e[i + N] === t && e[i + P] === n && e[i + F] === r) {
440
+ e[i + I] = o, e[i + L] = s;
437
441
  break;
438
442
  }
439
- d = e[o + R];
443
+ p = e[i + R];
440
444
  }
441
- p(s, l);
445
+ a(l, d);
442
446
  }
443
447
  unlinkRange(e, t, n, r, i) {
444
448
  let a = this.nullIndex, o = this.slotPool;
@@ -447,66 +451,66 @@ var b = 0, x = 1, S = 2, C = 3, w = 4, T = 5, E = 6, D = 8, ne = 10, O = 0, k =
447
451
  t !== a && o.deleteIndex(t);
448
452
  }
449
453
  }
450
- collectBucket(e, t, n, r, i, a, o, s) {
451
- let c = this.nullIndex, l = r + k, u = r + O;
452
- f(t, u);
453
- let d = n[l], m = this.slotPool;
454
- for (; d !== c;) {
455
- let t = m.blockFor(d), n = m.blockOffset(d);
456
- if (t[n + P] === i && t[n + F] === a) {
457
- let r = t[n + I], c = t[n + L];
458
- i === (r > o ? r : o) && a === (c > s ? c : s) && e.push(t[n + N]);
454
+ collectBucket(e, t, n, r, o, s, c, l) {
455
+ let u = this.nullIndex, d = r + k, f = r + O;
456
+ i(t, f);
457
+ let p = n[d], m = this.slotPool;
458
+ for (; p !== u;) {
459
+ let t = m.blockFor(p), n = m.blockOffset(p);
460
+ if (t[n + P] === o && t[n + F] === s) {
461
+ let r = t[n + I], i = t[n + L];
462
+ o === (r > c ? r : c) && s === (i > l ? i : l) && e.push(t[n + N]);
459
463
  }
460
- d = t[n + R];
464
+ p = t[n + R];
461
465
  }
462
- p(t, u);
466
+ a(t, f);
463
467
  }
464
468
  collectNeighborBucket(e, t, n) {
465
- let r = this.bucketOf(t, n), i = this.getBucketSegment(r), a = this.bucketHeadSegments[r >>> j], o = this.bucketOffset(r), s = o + k;
466
- if (a[s] === this.nullIndex) return;
467
- let c = o + O;
468
- f(i, c);
469
- let l = a[s];
470
- for (; l !== this.nullIndex;) {
471
- let r = this.slotPool.blockFor(l), i = this.slotPool.blockOffset(l);
472
- r[i + P] === t && r[i + F] === n && e.push(r[i + N]), l = r[i + R];
473
- }
474
- p(i, c);
475
- }
476
- collectNearestBucket(e, t, n, r, i) {
477
- let a = this.bucketOf(t, n), o = this.getBucketSegment(a), s = this.bucketHeadSegments[a >>> j], c = this.bucketOffset(a), l = c + k;
469
+ let r = this.bucketOf(t, n), o = this.getBucketSegment(r), s = this.bucketHeadSegments[r >>> j], c = this.bucketOffset(r), l = c + k;
478
470
  if (s[l] === this.nullIndex) return;
479
471
  let u = c + O;
480
- f(o, u);
472
+ i(o, u);
481
473
  let d = s[l];
482
474
  for (; d !== this.nullIndex;) {
483
- let a = this.slotPool.blockFor(d), o = this.slotPool.blockOffset(d);
484
- if (a[o + P] === t && a[o + F] === n) {
485
- let t = a[o + z], n = this.entityPool.blockFor(t), s = this.entityPool.blockOffset(t), c = a[o + N], l = this.entityBlockDistanceSquared(n, s, r, i);
475
+ let r = this.slotPool.blockFor(d), i = this.slotPool.blockOffset(d);
476
+ r[i + P] === t && r[i + F] === n && e.push(r[i + N]), d = r[i + R];
477
+ }
478
+ a(o, u);
479
+ }
480
+ collectNearestBucket(e, t, n, r, o) {
481
+ let s = this.bucketOf(t, n), c = this.getBucketSegment(s), l = this.bucketHeadSegments[s >>> j], u = this.bucketOffset(s), d = u + k;
482
+ if (l[d] === this.nullIndex) return;
483
+ let f = u + O;
484
+ i(c, f);
485
+ let p = l[d];
486
+ for (; p !== this.nullIndex;) {
487
+ let i = this.slotPool.blockFor(p), a = this.slotPool.blockOffset(p);
488
+ if (i[a + P] === t && i[a + F] === n) {
489
+ let t = i[a + z], n = this.entityPool.blockFor(t), s = this.entityPool.blockOffset(t), c = i[a + N], l = this.entityBlockDistanceSquared(n, s, r, o);
486
490
  (l < e.distance || l === e.distance && (e.id === void 0 || c < e.id)) && (e.id = c, e.distance = l);
487
491
  }
488
- d = a[o + R];
492
+ p = i[a + R];
489
493
  }
490
- p(o, u);
494
+ a(c, f);
491
495
  }
492
- collectNeighborCandidates(e, t, n, r, i, a, o, s, c) {
493
- let l = this.bucketOf(t, n), u = this.getBucketSegment(l), d = this.bucketHeadSegments[l >>> j], m = this.bucketOffset(l), h = m + k;
494
- if (d[h] === this.nullIndex) return;
496
+ collectNeighborCandidates(e, t, n, r, o, s, c, l, u) {
497
+ let d = this.bucketOf(t, n), f = this.getBucketSegment(d), p = this.bucketHeadSegments[d >>> j], m = this.bucketOffset(d), h = m + k;
498
+ if (p[h] === this.nullIndex) return;
495
499
  let g = m + O;
496
- f(u, g);
497
- let v = d[h];
500
+ i(f, g);
501
+ let v = p[h];
498
502
  for (; v !== this.nullIndex;) {
499
- let l = this.slotPool.blockFor(v), u = this.slotPool.blockOffset(v);
500
- if (l[u + P] === t && l[u + F] === n) {
501
- let d = l[u + z], f = this.entityPool.blockFor(d), p = this.entityPool.blockOffset(d), m = f[p + V], h = f[p + U], g = f[p + H], v = f[p + W];
502
- if (t === (r < m ? m : r > h ? h : r) && n === (i < g ? g : i > v ? v : i)) {
503
- let t = this.entityBlockDistanceSquared(f, p, a, o);
504
- t <= c && _(e, l[u + N], t, s);
503
+ let i = this.slotPool.blockFor(v), a = this.slotPool.blockOffset(v);
504
+ if (i[a + P] === t && i[a + F] === n) {
505
+ let d = i[a + z], f = this.entityPool.blockFor(d), p = this.entityPool.blockOffset(d), m = f[p + V], h = f[p + U], g = f[p + H], v = f[p + W];
506
+ if (t === (r < m ? m : r > h ? h : r) && n === (o < g ? g : o > v ? v : o)) {
507
+ let t = this.entityBlockDistanceSquared(f, p, s, c);
508
+ t <= u && _(e, i[a + N], t, l);
505
509
  }
506
510
  }
507
- v = l[u + R];
511
+ v = i[a + R];
508
512
  }
509
- p(u, g);
513
+ a(f, g);
510
514
  }
511
515
  addEntityCandidate(e, t, n, r, i, a, o) {
512
516
  let s = this.entityDistanceSquared(n, r, i);
@@ -521,35 +525,34 @@ var b = 0, x = 1, S = 2, C = 3, w = 4, T = 5, E = 6, D = 8, ne = 10, O = 0, k =
521
525
  return y(n, r, i, a, i + e[t + q], a + e[t + J]);
522
526
  }
523
527
  unlinkFromBucket(e, t, n, r) {
524
- let i = this.nullIndex, a = this.getBucketSegment(e), o = this.bucketHeadSegments[e >>> j], s = this.bucketOffset(e), c = s + O;
525
- f(a, c);
526
- let l = s + k, u = this.slotPool, d = i, m = o[l];
527
- for (; m !== i;) {
528
- let e = u.blockFor(m), s = u.blockOffset(m), f = e[s + R];
529
- if (e[s + N] === t && e[s + P] === n && e[s + F] === r) return d === i ? o[l] = f : u.set(d, R, f), p(a, c), m;
530
- d = m, m = f;
528
+ let o = this.nullIndex, s = this.getBucketSegment(e), c = this.bucketHeadSegments[e >>> j], l = this.bucketOffset(e), u = l + O;
529
+ i(s, u);
530
+ let d = l + k, f = this.slotPool, p = o, m = c[d];
531
+ for (; m !== o;) {
532
+ let e = f.blockFor(m), i = f.blockOffset(m), l = e[i + R];
533
+ if (e[i + N] === t && e[i + P] === n && e[i + F] === r) return p === o ? c[d] = l : f.set(p, R, l), a(s, u), m;
534
+ p = m, m = l;
531
535
  }
532
- return p(a, c), i;
536
+ return a(s, u), o;
533
537
  }
534
538
  };
535
- function me(e) {
536
- let t = l(e ?? fe);
537
- if (t !== c.float32 && t !== c.float64) throw Error("SharedSpatialMap must be Float32Array or Float64Array");
538
- return t;
539
- }
540
539
  function he(e) {
541
- return u(e) === 2 ? Float64Array : Float32Array;
540
+ let t = m(e ?? pe);
541
+ if (t !== p.float32 && t !== p.float64) throw Error("SharedSpatialMap must be Float32Array or Float64Array");
542
+ return t;
542
543
  }
543
544
  function ge(e) {
544
- return e === c.float32 ? ie : ae;
545
+ return h(e) === 2 ? Float64Array : Float32Array;
546
+ }
547
+ function _e(e) {
548
+ return e === p.float32 ? ie : ae;
545
549
  }
546
550
  function $(e) {
547
551
  return e & e - 1 ? -1 : e - 1;
548
552
  }
549
- function _e(e) {
553
+ function ve(e) {
550
554
  return 2 ** Math.ceil(Math.log2(Math.max(1, e)));
551
555
  }
552
- //#endregion
553
- export { pe as default };
556
+ export { me as default };
554
557
 
555
558
  //# sourceMappingURL=shared-spatial-map.js.map