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