@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.
- package/dist/allocated-memory.js +1 -2
- package/dist/cached-item-list.js +1 -2
- package/dist/constant-string.js +13 -14
- package/dist/local-pool.js +5 -6
- package/dist/lock/read-write-lock.js +0 -1
- package/dist/lock/simple-lock.js +0 -1
- package/dist/memory-buffer.js +1 -2
- package/dist/memory-heap.js +24 -25
- package/dist/serialize-object.js +4 -5
- package/dist/shared-list.js +43 -44
- package/dist/shared-map.js +74 -75
- package/dist/shared-pointer-list.js +15 -16
- package/dist/shared-pool.d.ts +1 -0
- package/dist/shared-pool.js +36 -32
- package/dist/shared-pool.js.map +1 -1
- package/dist/shared-stack.js +33 -34
- package/dist/shared-string.js +19 -20
- package/dist/shared-vector.js +23 -24
- package/dist/spatial/shared-quadtree.js +81 -82
- package/dist/spatial/shared-quadtree.js.map +1 -1
- package/dist/spatial/shared-spatial-grid.js +104 -110
- package/dist/spatial/shared-spatial-grid.js.map +1 -1
- package/dist/spatial/shared-spatial-map.js +189 -190
- package/dist/spatial/shared-spatial-map.js.map +1 -1
- package/dist/spatial/spatial-entity.js +1 -2
- package/dist/spatial/spatial-neighbors.js +1 -2
- package/dist/utils/16-from-32-array.js +1 -2
- package/dist/utils/16-from-32-float.js +1 -2
- package/dist/utils/16-from-32-int.js +1 -2
- package/dist/utils/16-from-32-uint.js +1 -2
- package/dist/utils/16-from-64-array.js +1 -2
- package/dist/utils/16-from-64-float.js +1 -2
- package/dist/utils/16-from-64-int.js +1 -2
- package/dist/utils/16-from-64-uint.js +1 -2
- package/dist/utils/32-from-64-float.js +1 -2
- package/dist/utils/32-from-64-int.js +1 -2
- package/dist/utils/32-from-64-uint.js +1 -2
- package/dist/utils/array-type.js +1 -2
- package/dist/utils/atomic-math.js +1 -2
- package/dist/utils/float32-atomics.js +1 -2
- package/dist/utils/float64-atomics.js +1 -2
- package/dist/utils/hash.js +1 -2
- package/dist/utils/pointer.js +1 -2
- package/dist/utils/typedarray.js +1 -2
- package/package.json +11 -11
package/dist/shared-stack.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import n from "./
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
1
|
+
import e from "./allocated-memory.js";
|
|
2
|
+
import { lock as t, unlock as n } from "./lock/simple-lock.js";
|
|
3
|
+
import { getPointer as r, storePointer as i } from "./utils/pointer.js";
|
|
4
|
+
import { getArrayTypeCode as a, getByteMultipler as o, makeArrayView as s } from "./utils/array-type.js";
|
|
5
|
+
// src/shared-stack.ts
|
|
6
6
|
var c = 0, l = 1, u = 2, d = 3, f = 4, p = 5, m = 6, h = 7, g = 4, _ = 20, v = 22, y = (1 << v) - 1, b = 4194302, x = 1 << v, S = class v {
|
|
7
7
|
static BASE_ALLOCATE_COUNT = 8;
|
|
8
8
|
static DEFAULT_ALLOCATE_COUNT = this.BASE_ALLOCATE_COUNT + _;
|
|
@@ -75,28 +75,28 @@ var c = 0, l = 1, u = 2, d = 3, f = 4, p = 5, m = 6, h = 7, g = 4, _ = 20, v = 2
|
|
|
75
75
|
}
|
|
76
76
|
get usedMemory() {
|
|
77
77
|
let t = this.ownsFirstBlock ? this.firstBlock.usedMemory : 0;
|
|
78
|
-
for (let
|
|
79
|
-
let i =
|
|
80
|
-
t += new
|
|
78
|
+
for (let n = 0; n < this.segmentCount; n++) {
|
|
79
|
+
let i = r(Atomics.load(this.firstBlock.data, h + n), this.memory.positionBits);
|
|
80
|
+
t += new e(this.memory, i).usedMemory;
|
|
81
81
|
}
|
|
82
82
|
return t;
|
|
83
83
|
}
|
|
84
|
-
constructor(
|
|
85
|
-
if (this.memory =
|
|
86
|
-
if (
|
|
84
|
+
constructor(t, n) {
|
|
85
|
+
if (this.memory = t, n && "firstBlock" in n) {
|
|
86
|
+
if (n.firstBlock instanceof e) this.firstBlock = n.firstBlock;
|
|
87
87
|
else {
|
|
88
|
-
let r = new Uint32Array(
|
|
89
|
-
this.firstBlock = new
|
|
88
|
+
let r = new Uint32Array(t.buffers[n.firstBlock.bufferPosition].buf, n.firstBlock.bufferByteOffset + d * Uint32Array.BYTES_PER_ELEMENT, 1)[0];
|
|
89
|
+
this.firstBlock = new e(t, {
|
|
90
90
|
length: v.BASE_ALLOCATE_COUNT + r,
|
|
91
|
-
...
|
|
91
|
+
...n.firstBlock
|
|
92
92
|
});
|
|
93
93
|
}
|
|
94
|
-
this.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + l * Uint32Array.BYTES_PER_ELEMENT, 2), "type" in
|
|
94
|
+
this.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + l * Uint32Array.BYTES_PER_ELEMENT, 2), "type" in n || "baseSegmentLength" in n || "maxLength" in n ? (this.initialize(n), this.ownsFirstBlock = !1) : this.ownsFirstBlock = !0;
|
|
95
95
|
} else {
|
|
96
|
-
let
|
|
97
|
-
this.firstBlock =
|
|
96
|
+
let e = o(a(n?.type ?? Uint32Array)), r = n?.baseSegmentLength ?? g, i = this.resolveMaxSegmentLength(r, n?.maxSegmentLength, e), s = this.resolveMaxSegments(n?.maxLength, r, i);
|
|
97
|
+
this.firstBlock = t.allocUI32(v.BASE_ALLOCATE_COUNT + s), this.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + l * Uint32Array.BYTES_PER_ELEMENT, 2), this.ownsFirstBlock = !0, this.initialize(n);
|
|
98
98
|
}
|
|
99
|
-
if (this.growLock = new Int32Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + m * Uint32Array.BYTES_PER_ELEMENT, 1), this.cachedByteMultipler =
|
|
99
|
+
if (this.growLock = new Int32Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + m * Uint32Array.BYTES_PER_ELEMENT, 1), this.cachedByteMultipler = o(this.type), this.cachedBaseSegmentLength = this.baseSegmentLength, this.cachedMaxSegmentLength = this.maxSegmentLength, this.cachedCapSegmentCount = 32 - Math.clz32(this.cachedMaxSegmentLength / this.cachedBaseSegmentLength | 0), this.cachedGeometricCapacity = this.cachedBaseSegmentLength * ((1 << this.cachedCapSegmentCount) - 1), this.maxLength = v.getCapacity(this.maxSegments, this.cachedBaseSegmentLength, this.cachedMaxSegmentLength), this.maxLength > b) throw Error(`SharedStack maxLength ${this.maxLength} exceeds the ${b} the versioned length counter can address`);
|
|
100
100
|
}
|
|
101
101
|
resolveMaxSegmentLength(e, t, n = 1) {
|
|
102
102
|
let r = v.getMaxSegmentLength(this.memory.maxAllocationLength, e, n);
|
|
@@ -112,8 +112,8 @@ var c = 0, l = 1, u = 2, d = 3, f = 4, p = 5, m = 6, h = 7, g = 4, _ = 20, v = 2
|
|
|
112
112
|
return Math.min(r, i);
|
|
113
113
|
}
|
|
114
114
|
initialize(e) {
|
|
115
|
-
let
|
|
116
|
-
|
|
115
|
+
let t = a(e?.type ?? Uint32Array), n = o(t), r = e?.baseSegmentLength ?? g, s = this.resolveMaxSegmentLength(r, e?.maxSegmentLength, n), c = this.memory.allocUI32(r * (n + 1));
|
|
116
|
+
i(this.firstBlock.data, h, c.bufferPosition, c.bufferByteOffset, this.memory.positionBits), this.baseSegmentLength = r, this.maxSegmentLength = s, this.maxSegments = this.resolveMaxSegments(e?.maxLength, r, s), this.segmentCount = 1, this.type = t;
|
|
117
117
|
}
|
|
118
118
|
at(e) {
|
|
119
119
|
let t = this.length;
|
|
@@ -184,28 +184,28 @@ var c = 0, l = 1, u = 2, d = 3, f = 4, p = 5, m = 6, h = 7, g = 4, _ = 20, v = 2
|
|
|
184
184
|
let e = this.length, t = this.cachedMaxSegmentLength, n = this.baseSegmentLength, r = 0, i = this.getSegment(r), a = 0;
|
|
185
185
|
for (let o = 0; o < e; o++) yield i[a], a++, a >= n && (r++, i = this.getSegment(r), a = 0, n = Math.min(n * 2, t));
|
|
186
186
|
}
|
|
187
|
-
getSegment(
|
|
188
|
-
if (this.cachedSegments[
|
|
189
|
-
let
|
|
190
|
-
if (
|
|
191
|
-
|
|
187
|
+
getSegment(a) {
|
|
188
|
+
if (this.cachedSegments[a]) return this.cachedSegments[a];
|
|
189
|
+
let o = a < this.cachedCapSegmentCount ? this.cachedBaseSegmentLength << a : this.cachedMaxSegmentLength, c;
|
|
190
|
+
if (a >= this.segmentCount) {
|
|
191
|
+
t(this.growLock);
|
|
192
192
|
try {
|
|
193
|
-
|
|
193
|
+
a >= this.segmentCount && (c = this.memory.allocUI32(o * (this.cachedByteMultipler + 1)), i(this.firstBlock.data, h + a, c.bufferPosition, c.bufferByteOffset, this.memory.positionBits), Atomics.add(this.firstBlock.data, p, 1));
|
|
194
194
|
} finally {
|
|
195
|
-
|
|
195
|
+
n(this.growLock);
|
|
196
196
|
}
|
|
197
197
|
}
|
|
198
198
|
if (!c) {
|
|
199
|
-
let t = Atomics.load(this.firstBlock.data, h +
|
|
200
|
-
c = new
|
|
199
|
+
let t = Atomics.load(this.firstBlock.data, h + a), n = r(t, this.memory.positionBits);
|
|
200
|
+
c = new e(this.memory, n);
|
|
201
201
|
}
|
|
202
|
-
let l =
|
|
203
|
-
return this.cachedSequences[
|
|
202
|
+
let l = s(this.type, c.data.buffer, c.bufferByteOffset, o), u = c.bufferByteOffset + o * this.cachedByteMultipler * Uint32Array.BYTES_PER_ELEMENT;
|
|
203
|
+
return this.cachedSequences[a] = new Int32Array(c.data.buffer, u, o), this.cachedSegments[a] = l, l;
|
|
204
204
|
}
|
|
205
205
|
free() {
|
|
206
206
|
for (let t = 0; t < this.segmentCount; t++) {
|
|
207
|
-
let
|
|
208
|
-
new
|
|
207
|
+
let n = Atomics.load(this.firstBlock.data, h + t), i = r(n, this.memory.positionBits);
|
|
208
|
+
new e(this.memory, i).free();
|
|
209
209
|
}
|
|
210
210
|
this.ownsFirstBlock && this.firstBlock.free();
|
|
211
211
|
}
|
|
@@ -213,7 +213,6 @@ var c = 0, l = 1, u = 2, d = 3, f = 4, p = 5, m = 6, h = 7, g = 4, _ = 20, v = 2
|
|
|
213
213
|
return { firstBlock: this.firstBlock.getSharedMemory() };
|
|
214
214
|
}
|
|
215
215
|
};
|
|
216
|
-
//#endregion
|
|
217
216
|
export { S as default };
|
|
218
217
|
|
|
219
218
|
//# sourceMappingURL=shared-stack.js.map
|
package/dist/shared-string.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
|
|
1
|
+
import e from "./allocated-memory.js";
|
|
2
|
+
import { lock as t, unlock as n } from "./lock/simple-lock.js";
|
|
3
|
+
import { getPointer as r, loadPointer as i, loadRawPointer as a, storeRawPointer as o } from "./utils/pointer.js";
|
|
4
|
+
// src/shared-string.ts
|
|
5
5
|
var s = {
|
|
6
6
|
1: Uint8Array,
|
|
7
7
|
2: Uint16Array
|
|
@@ -12,12 +12,12 @@ var s = {
|
|
|
12
12
|
lock;
|
|
13
13
|
cachedPointer;
|
|
14
14
|
cachedString;
|
|
15
|
-
constructor(
|
|
16
|
-
this.memory =
|
|
15
|
+
constructor(t, n) {
|
|
16
|
+
this.memory = t, typeof n == "string" ? (this.allocatedMemory = this.memory.allocUI32(f.ALLOCATE_COUNT), this.lock = new Int32Array(this.allocatedMemory.data.buffer, this.allocatedMemory.bufferByteOffset + d * this.allocatedMemory.data.BYTES_PER_ELEMENT), this.updateString(n)) : (this.allocatedMemory = new e(t, n), this.lock = new Int32Array(this.allocatedMemory.data.buffer, this.allocatedMemory.bufferByteOffset + d * this.allocatedMemory.data.BYTES_PER_ELEMENT), "value" in n && this.updateString(n.value));
|
|
17
17
|
}
|
|
18
18
|
updateString(e) {
|
|
19
|
-
let
|
|
20
|
-
|
|
19
|
+
let r = this.createString(e);
|
|
20
|
+
t(this.lock), o(this.allocatedMemory.data, c, r.pointer), Atomics.store(this.allocatedMemory.data, l, e.length), Atomics.store(this.allocatedMemory.data, u, r.charType), n(this.lock), this.cachedPointer = r.pointer, this.cachedString = e;
|
|
21
21
|
}
|
|
22
22
|
createString(e) {
|
|
23
23
|
if (e === "") return {
|
|
@@ -37,17 +37,17 @@ var s = {
|
|
|
37
37
|
};
|
|
38
38
|
}
|
|
39
39
|
get value() {
|
|
40
|
-
let
|
|
41
|
-
if (this.cachedPointer ===
|
|
42
|
-
if (
|
|
43
|
-
let { bufferPosition:
|
|
44
|
-
|
|
45
|
-
let d = s[Atomics.load(this.allocatedMemory.data, u)], f = Atomics.load(this.allocatedMemory.data, l), m = p(new d(this.memory.buffers[
|
|
46
|
-
return
|
|
40
|
+
let e = a(this.allocatedMemory.data, c);
|
|
41
|
+
if (this.cachedPointer === e && this.cachedString !== void 0) return this.cachedString;
|
|
42
|
+
if (e === 0) return "";
|
|
43
|
+
let { bufferPosition: i, bufferByteOffset: o } = r(e, this.memory.positionBits);
|
|
44
|
+
t(this.lock);
|
|
45
|
+
let d = s[Atomics.load(this.allocatedMemory.data, u)], f = Atomics.load(this.allocatedMemory.data, l), m = p(new d(this.memory.buffers[i].buf, o, f));
|
|
46
|
+
return n(this.lock), this.cachedPointer = e, this.cachedString = m, m;
|
|
47
47
|
}
|
|
48
48
|
set value(e) {
|
|
49
|
-
let { bufferPosition:
|
|
50
|
-
this.updateString(e),
|
|
49
|
+
let { bufferPosition: t, bufferByteOffset: n } = i(this.allocatedMemory.data, c, this.memory.positionBits);
|
|
50
|
+
this.updateString(e), n && this.memory.buffers[t].free(n);
|
|
51
51
|
}
|
|
52
52
|
getSharedMemory() {
|
|
53
53
|
return this.allocatedMemory.getSharedMemory();
|
|
@@ -56,8 +56,8 @@ var s = {
|
|
|
56
56
|
return this.allocatedMemory.pointer;
|
|
57
57
|
}
|
|
58
58
|
free() {
|
|
59
|
-
let { bufferPosition: e, bufferByteOffset:
|
|
60
|
-
|
|
59
|
+
let { bufferPosition: e, bufferByteOffset: t } = i(this.allocatedMemory.data, c, this.memory.positionBits);
|
|
60
|
+
t && this.memory.buffers[e].free(t), this.allocatedMemory.free();
|
|
61
61
|
}
|
|
62
62
|
};
|
|
63
63
|
function p(e) {
|
|
@@ -67,7 +67,6 @@ function p(e) {
|
|
|
67
67
|
for (let r = 0; r < e.length; r += t) n += String.fromCharCode.apply(null, e.subarray(r, r + t));
|
|
68
68
|
return n;
|
|
69
69
|
}
|
|
70
|
-
//#endregion
|
|
71
70
|
export { f as default };
|
|
72
71
|
|
|
73
72
|
//# sourceMappingURL=shared-string.js.map
|
package/dist/shared-vector.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
import i from "./
|
|
1
|
+
import e from "./allocated-memory.js";
|
|
2
|
+
import { getPointer as t, loadPointer as n, storePointer as r, storeRawPointer as i } from "./utils/pointer.js";
|
|
3
3
|
import { getArrayTypeCode as a, getByteMultipler as o, makeArrayView as s } from "./utils/array-type.js";
|
|
4
|
-
|
|
4
|
+
// src/shared-vector.ts
|
|
5
5
|
var c = 0, l = 1, u = 2, d = 3, f = 4, p = class p {
|
|
6
6
|
static ALLOCATE_COUNT = 4;
|
|
7
7
|
memory;
|
|
@@ -36,24 +36,24 @@ var c = 0, l = 1, u = 2, d = 3, f = 4, p = class p {
|
|
|
36
36
|
return this.firstBlock.pointer;
|
|
37
37
|
}
|
|
38
38
|
get usedMemory() {
|
|
39
|
-
let
|
|
40
|
-
return this.firstBlock.usedMemory +
|
|
39
|
+
let t = new e(this.memory, n(this.firstBlock.data, c, this.memory.positionBits));
|
|
40
|
+
return this.firstBlock.usedMemory + t.usedMemory;
|
|
41
41
|
}
|
|
42
42
|
cachedFullDataBlock;
|
|
43
43
|
cachedPointer;
|
|
44
|
-
constructor(
|
|
45
|
-
if (this.memory =
|
|
46
|
-
if (this.firstBlock = new
|
|
44
|
+
constructor(t, n) {
|
|
45
|
+
if (this.memory = t, n && "firstBlock" in n) {
|
|
46
|
+
if (this.firstBlock = new e(t, {
|
|
47
47
|
length: p.ALLOCATE_COUNT,
|
|
48
|
-
...
|
|
49
|
-
}), this.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + d * Uint32Array.BYTES_PER_ELEMENT, 2), "type" in
|
|
50
|
-
let
|
|
51
|
-
|
|
48
|
+
...n.firstBlock
|
|
49
|
+
}), this.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + d * Uint32Array.BYTES_PER_ELEMENT, 2), "type" in n || "dataLength" in n) {
|
|
50
|
+
let e = a(n.type ?? Uint32Array), i = o(e), s = n.bufferLength ?? f, l = t.allocUI32(s * (n.dataLength ?? 1) * i);
|
|
51
|
+
r(this.firstBlock.data, c, l.bufferPosition, l.bufferByteOffset, t.positionBits), this.bufferLength = s, this.dataLength = n.dataLength ?? 1, this.type = e;
|
|
52
52
|
}
|
|
53
53
|
} else {
|
|
54
|
-
this.firstBlock =
|
|
55
|
-
let
|
|
56
|
-
|
|
54
|
+
this.firstBlock = t.allocUI32(p.ALLOCATE_COUNT), this.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + d * Uint32Array.BYTES_PER_ELEMENT, 2);
|
|
55
|
+
let e = n?.dataLength ?? 1, i = n?.bufferLength ?? f, s = a(n?.type ?? Uint32Array), l = t.allocUI32(i * e * o(s));
|
|
56
|
+
r(this.firstBlock.data, c, l.bufferPosition, l.bufferByteOffset, t.positionBits), this.bufferLength = i, this.type = s, this.dataLength = e;
|
|
57
57
|
}
|
|
58
58
|
this.cachedByteMultipler = o(this.type), this.cachedPointer = this.firstBlock.data[0], this.cachedFullDataBlock = this.getFullDataBlock();
|
|
59
59
|
}
|
|
@@ -102,30 +102,29 @@ var c = 0, l = 1, u = 2, d = 3, f = 4, p = class p {
|
|
|
102
102
|
return this.getFullDataBlock().subarray(0, this.length * this.dataLength);
|
|
103
103
|
}
|
|
104
104
|
getFullDataBlock() {
|
|
105
|
-
let
|
|
106
|
-
if (this.cachedPointer ===
|
|
107
|
-
let
|
|
108
|
-
return this.cachedPointer =
|
|
105
|
+
let n = Atomics.load(this.firstBlock.data, c);
|
|
106
|
+
if (this.cachedPointer === n && this.cachedFullDataBlock) return this.cachedFullDataBlock;
|
|
107
|
+
let r = t(n, this.memory.positionBits), i = new e(this.memory, r), a = s(this.type, i.data.buffer, i.bufferByteOffset, this.dataLength * this.bufferLength);
|
|
108
|
+
return this.cachedPointer = n, this.cachedFullDataBlock = a, a;
|
|
109
109
|
}
|
|
110
110
|
getDataBlock(e, t) {
|
|
111
111
|
let n = t * this.dataLength;
|
|
112
112
|
return e.subarray(n, n + this.dataLength);
|
|
113
113
|
}
|
|
114
114
|
growBuffer() {
|
|
115
|
-
let
|
|
115
|
+
let t = this.bufferLength * 2, r = this.dataLength, a = n(this.firstBlock.data, c, this.memory.positionBits), o = new e(this.memory, a), l = this.getFullDataBlock(), u = this.memory.allocUI32(t * r * this.cachedByteMultipler), d = s(this.type, u.data.buffer, u.bufferByteOffset, r * t);
|
|
116
116
|
d.set(l);
|
|
117
117
|
let f = u.pointer;
|
|
118
|
-
|
|
118
|
+
i(this.firstBlock.data, c, f), this.bufferLength = t, o.free(), this.cachedPointer = f, this.cachedFullDataBlock = d;
|
|
119
119
|
}
|
|
120
120
|
free() {
|
|
121
|
-
let
|
|
122
|
-
new
|
|
121
|
+
let t = n(this.firstBlock.data, c, this.memory.positionBits);
|
|
122
|
+
new e(this.memory, t).free(), this.firstBlock.free();
|
|
123
123
|
}
|
|
124
124
|
getSharedMemory() {
|
|
125
125
|
return { firstBlock: this.firstBlock.getSharedMemory() };
|
|
126
126
|
}
|
|
127
127
|
};
|
|
128
|
-
//#endregion
|
|
129
128
|
export { p as default };
|
|
130
129
|
|
|
131
130
|
//# sourceMappingURL=shared-vector.js.map
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import { lock as
|
|
5
|
-
import
|
|
6
|
-
import d from "../
|
|
1
|
+
import e from "../allocated-memory.js";
|
|
2
|
+
import t from "../shared-pool.js";
|
|
3
|
+
import n from "../shared-map.js";
|
|
4
|
+
import { lock as r, unlock as i } from "../lock/simple-lock.js";
|
|
5
|
+
import { getPointer as a, loadRawPointer as o, storeRawPointer as s } from "../utils/pointer.js";
|
|
6
|
+
import { ARRAY_TYPE as c, getArrayTypeCode as l, getByteMultipler as u, makeArrayView as d } from "../utils/array-type.js";
|
|
7
7
|
import { MinPriorityQueue as f, addCandidate as p, candidateLimit as m, distanceSquaredToRect as h } from "./spatial-neighbors.js";
|
|
8
8
|
import { getSpatialMapCapacity as g, getSpatialPoolChunkSize as _ } from "./spatial-entity.js";
|
|
9
|
-
|
|
10
|
-
var v = 0, y = 1, b = 2, x = 3, S = 4, C = 5, w = 6, T = 0, E = 1, D = 2, O = 0, k = 1, A = 2, j = 3, M = 4, N = 5, P = 6, F = 7, I = 16777216, L = I, R = 4294967295, z = 4, B = 1e6, V = Float32Array, H = class
|
|
9
|
+
// src/spatial/shared-quadtree.ts
|
|
10
|
+
var v = 0, y = 1, b = 2, x = 3, S = 4, C = 5, w = 6, T = 0, E = 1, D = 2, O = 0, k = 1, A = 2, j = 3, M = 4, N = 5, P = 6, F = 7, I = 16777216, L = I, R = 4294967295, z = 4, B = 1e6, V = Float32Array, H = class l {
|
|
11
11
|
static ALLOCATE_COUNT = 14;
|
|
12
12
|
memory;
|
|
13
13
|
firstBlock;
|
|
@@ -46,42 +46,42 @@ var v = 0, y = 1, b = 2, x = 3, S = 4, C = 5, w = 6, T = 0, E = 1, D = 2, O = 0,
|
|
|
46
46
|
get type() {
|
|
47
47
|
return this.coordType;
|
|
48
48
|
}
|
|
49
|
-
constructor(
|
|
50
|
-
if (this.memory =
|
|
49
|
+
constructor(r, i) {
|
|
50
|
+
if (this.memory = r, i && "firstBlock" in i) this.firstBlock = new e(r, i.firstBlock), this.coordType = this.firstBlock.data[C], this.pool = new t(r, { firstBlock: a(o(this.firstBlock.data, y), r.positionBits) }), this.idMap = new n(r, { firstBlock: a(o(this.firstBlock.data, b), r.positionBits) }), this.maxLevels = this.firstBlock.data[x], this.nodeCount = this.firstBlock.data[S], this.nullIndex = G(this.coordType);
|
|
51
51
|
else {
|
|
52
|
-
let e =
|
|
52
|
+
let e = i?.bounds ?? {
|
|
53
53
|
x: 0,
|
|
54
54
|
y: 0,
|
|
55
55
|
width: 0,
|
|
56
56
|
height: 0
|
|
57
|
-
},
|
|
58
|
-
if (this.maxLevels =
|
|
59
|
-
let
|
|
57
|
+
}, a = i?.maxEntities ?? B;
|
|
58
|
+
if (this.maxLevels = i?.maxLevels ?? z, this.nodeCount = l.getNodeCount(this.maxLevels), this.coordType = U(i?.type), this.nullIndex = G(this.coordType), this.coordType === c.float32 && a > I) throw Error(`Float32 SharedQuadtree supports at most ${I} entities; pass type: Float64Array for more`);
|
|
59
|
+
let o = {
|
|
60
60
|
type: W(this.coordType),
|
|
61
61
|
dataLength: F,
|
|
62
|
-
maxLength:
|
|
63
|
-
maxChunkSize: _(
|
|
64
|
-
},
|
|
65
|
-
this.firstBlock =
|
|
62
|
+
maxLength: a,
|
|
63
|
+
maxChunkSize: _(a, F, u(this.coordType), r.maxAllocationLength)
|
|
64
|
+
}, d = t.getAllocateCount(r, o), f = n.ALLOCATE_COUNT;
|
|
65
|
+
this.firstBlock = r.allocUI32(l.ALLOCATE_COUNT + d + f), this.firstBlock.data[x] = this.maxLevels, this.firstBlock.data[S] = this.nodeCount, this.firstBlock.data[C] = this.coordType;
|
|
66
66
|
let p = this.getBoundsView();
|
|
67
67
|
p[0] = e.x, p[1] = e.y, p[2] = e.width, p[3] = e.height;
|
|
68
|
-
let m =
|
|
68
|
+
let m = r.allocUI32(this.nodeCount * D), h = new Uint32Array(m.data.buffer, m.bufferByteOffset, this.nodeCount * D);
|
|
69
69
|
for (let e = 0; e < this.nodeCount; e++) h[e * D + E] = this.nullIndex;
|
|
70
|
-
|
|
71
|
-
let w = this.firstBlock.getSubAllocation(
|
|
72
|
-
this.pool = new
|
|
70
|
+
s(this.firstBlock.data, v, m.pointer);
|
|
71
|
+
let w = this.firstBlock.getSubAllocation(l.ALLOCATE_COUNT, d);
|
|
72
|
+
this.pool = new t(r, {
|
|
73
73
|
firstBlock: w,
|
|
74
|
-
...
|
|
75
|
-
}),
|
|
76
|
-
let T = this.firstBlock.getSubAllocation(
|
|
77
|
-
this.idMap = new
|
|
74
|
+
...o
|
|
75
|
+
}), s(this.firstBlock.data, y, w.pointer);
|
|
76
|
+
let T = this.firstBlock.getSubAllocation(l.ALLOCATE_COUNT + d, f);
|
|
77
|
+
this.idMap = new n(r, {
|
|
78
78
|
firstBlock: T,
|
|
79
|
-
capacity: g(
|
|
80
|
-
}),
|
|
79
|
+
capacity: g(a, r.maxAllocationLength)
|
|
80
|
+
}), s(this.firstBlock.data, b, T.pointer);
|
|
81
81
|
}
|
|
82
|
-
let
|
|
83
|
-
this.rootX =
|
|
84
|
-
let f = new r
|
|
82
|
+
let d = this.getBoundsView();
|
|
83
|
+
this.rootX = d[0], this.rootY = d[1], this.rootWidth = d[2], this.rootHeight = d[3];
|
|
84
|
+
let f = new e(r, a(o(this.firstBlock.data, v), r.positionBits));
|
|
85
85
|
this.nodeInts = new Int32Array(f.data.buffer, f.bufferByteOffset, this.nodeCount * D), this.nodeHeads = new Uint32Array(f.data.buffer, f.bufferByteOffset, this.nodeCount * D);
|
|
86
86
|
}
|
|
87
87
|
static getNodeCount(e) {
|
|
@@ -91,8 +91,8 @@ var v = 0, y = 1, b = 2, x = 3, S = 4, C = 5, w = 6, T = 0, E = 1, D = 2, O = 0,
|
|
|
91
91
|
return this.firstBlock.pointer;
|
|
92
92
|
}
|
|
93
93
|
get usedMemory() {
|
|
94
|
-
let
|
|
95
|
-
return this.firstBlock.usedMemory +
|
|
94
|
+
let t = new e(this.memory, a(o(this.firstBlock.data, v), this.memory.positionBits));
|
|
95
|
+
return this.firstBlock.usedMemory + t.usedMemory + this.pool.usedMemory + this.idMap.usedMemory;
|
|
96
96
|
}
|
|
97
97
|
insert(e, t, n, r = 0, i = 0) {
|
|
98
98
|
if (this.idMap.get(e) !== void 0) {
|
|
@@ -119,19 +119,19 @@ var v = 0, y = 1, b = 2, x = 3, S = 4, C = 5, w = 6, T = 0, E = 1, D = 2, O = 0,
|
|
|
119
119
|
}
|
|
120
120
|
this.idMap.setAll(i);
|
|
121
121
|
}
|
|
122
|
-
update(e, t, n,
|
|
123
|
-
let
|
|
124
|
-
if (
|
|
125
|
-
this.insert(e, t, n,
|
|
122
|
+
update(e, t, n, a = 0, o = 0) {
|
|
123
|
+
let s = this.idMap.get(e);
|
|
124
|
+
if (s === void 0) {
|
|
125
|
+
this.insert(e, t, n, a, o);
|
|
126
126
|
return;
|
|
127
127
|
}
|
|
128
|
-
let
|
|
128
|
+
let c = this.pool.blockFor(s), l = this.pool.blockOffset(s), u = c[l + N], d = this.locate(t, n, a, o);
|
|
129
129
|
if (u === d) {
|
|
130
|
-
|
|
130
|
+
r(this.nodeInts, u * D + T), c[l + k] = t, c[l + A] = n, c[l + j] = a, c[l + M] = o, i(this.nodeInts, u * D + T);
|
|
131
131
|
return;
|
|
132
132
|
}
|
|
133
133
|
let f = Math.min(u, d), p = Math.max(u, d);
|
|
134
|
-
|
|
134
|
+
r(this.nodeInts, f * D + T), r(this.nodeInts, p * D + T), this.unlinkNoLock(u, s), c[l + k] = t, c[l + A] = n, c[l + j] = a, c[l + M] = o, c[l + N] = d, this.linkNoLock(d, s), i(this.nodeInts, p * D + T), i(this.nodeInts, f * D + T);
|
|
135
135
|
}
|
|
136
136
|
remove(e) {
|
|
137
137
|
let t = this.idMap.get(e);
|
|
@@ -252,13 +252,13 @@ var v = 0, y = 1, b = 2, x = 3, S = 4, C = 5, w = 6, T = 0, E = 1, D = 2, O = 0,
|
|
|
252
252
|
for (let t of e) this.idMap.delete(t);
|
|
253
253
|
}
|
|
254
254
|
free() {
|
|
255
|
-
new
|
|
255
|
+
new e(this.memory, a(o(this.firstBlock.data, v), this.memory.positionBits)).free(), this.pool.free(), this.idMap.free(), this.firstBlock.free();
|
|
256
256
|
}
|
|
257
257
|
getSharedMemory() {
|
|
258
258
|
return { firstBlock: this.firstBlock.getSharedMemory() };
|
|
259
259
|
}
|
|
260
260
|
getBoundsView() {
|
|
261
|
-
return
|
|
261
|
+
return d(this.coordType, this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + w * Uint32Array.BYTES_PER_ELEMENT, 4);
|
|
262
262
|
}
|
|
263
263
|
locate(e, t, n, r) {
|
|
264
264
|
let i = 0, a = this.rootX, o = this.rootY, s = this.rootWidth, c = this.rootHeight;
|
|
@@ -272,52 +272,52 @@ var v = 0, y = 1, b = 2, x = 3, S = 4, C = 5, w = 6, T = 0, E = 1, D = 2, O = 0,
|
|
|
272
272
|
}
|
|
273
273
|
return i;
|
|
274
274
|
}
|
|
275
|
-
collect(e, t, n,
|
|
275
|
+
collect(e, t, n, a, o, s, c, l, u, d, f) {
|
|
276
276
|
let p = t * D + E;
|
|
277
277
|
if (this.nodeHeads[p] !== this.nullIndex) {
|
|
278
278
|
let n = t * D + T;
|
|
279
|
-
|
|
280
|
-
let
|
|
281
|
-
for (;
|
|
282
|
-
let t =
|
|
283
|
-
e.push(t[n + O]),
|
|
279
|
+
r(this.nodeInts, n);
|
|
280
|
+
let a = this.nodeHeads[p], o = this.pool;
|
|
281
|
+
for (; a !== this.nullIndex;) {
|
|
282
|
+
let t = o.blockFor(a), n = o.blockOffset(a);
|
|
283
|
+
e.push(t[n + O]), a = t[n + P];
|
|
284
284
|
}
|
|
285
|
-
|
|
285
|
+
i(this.nodeInts, n);
|
|
286
286
|
}
|
|
287
|
-
if (
|
|
288
|
-
let m = n +
|
|
289
|
-
b && v && this.collect(e, t * 4 + 1, n,
|
|
287
|
+
if (c >= this.maxLevels) return;
|
|
288
|
+
let m = n + o / 2, h = a + s / 2, g = o / 2, _ = s / 2, v = l < m, y = l + d > m, b = u < h, x = u + f > h;
|
|
289
|
+
b && v && this.collect(e, t * 4 + 1, n, a, g, _, c + 1, l, u, d, f), b && y && this.collect(e, t * 4 + 2, m, a, g, _, c + 1, l, u, d, f), x && v && this.collect(e, t * 4 + 3, n, h, g, _, c + 1, l, u, d, f), x && y && this.collect(e, t * 4 + 4, m, h, g, _, c + 1, l, u, d, f);
|
|
290
290
|
}
|
|
291
|
-
collectNeighborNode(e, t, n,
|
|
292
|
-
let
|
|
293
|
-
if (this.nodeHeads[
|
|
294
|
-
let
|
|
295
|
-
|
|
296
|
-
let
|
|
297
|
-
for (;
|
|
298
|
-
let t = this.pool.blockFor(
|
|
299
|
-
|
|
300
|
-
id:
|
|
301
|
-
distance:
|
|
302
|
-
}),
|
|
291
|
+
collectNeighborNode(e, t, n, a, o) {
|
|
292
|
+
let s = t * D + E;
|
|
293
|
+
if (this.nodeHeads[s] === this.nullIndex) return;
|
|
294
|
+
let c = t * D + T;
|
|
295
|
+
r(this.nodeInts, c);
|
|
296
|
+
let l = this.nodeHeads[s];
|
|
297
|
+
for (; l !== this.nullIndex;) {
|
|
298
|
+
let t = this.pool.blockFor(l), r = this.pool.blockOffset(l), i = t[r + O], s = h(n, a, t[r + k], t[r + A], t[r + k] + t[r + j], t[r + A] + t[r + M]);
|
|
299
|
+
s <= o && e.push({
|
|
300
|
+
id: i,
|
|
301
|
+
distance: s
|
|
302
|
+
}), l = t[r + P];
|
|
303
303
|
}
|
|
304
|
-
|
|
304
|
+
i(this.nodeInts, c);
|
|
305
305
|
}
|
|
306
|
-
collectNearestNode(e, t, n,
|
|
307
|
-
let
|
|
308
|
-
if (this.nodeHeads[
|
|
309
|
-
let
|
|
310
|
-
|
|
311
|
-
let
|
|
312
|
-
for (;
|
|
313
|
-
let t = this.pool.blockFor(
|
|
314
|
-
(
|
|
306
|
+
collectNearestNode(e, t, n, a) {
|
|
307
|
+
let o = t * D + E;
|
|
308
|
+
if (this.nodeHeads[o] === this.nullIndex) return;
|
|
309
|
+
let s = t * D + T;
|
|
310
|
+
r(this.nodeInts, s);
|
|
311
|
+
let c = this.nodeHeads[o];
|
|
312
|
+
for (; c !== this.nullIndex;) {
|
|
313
|
+
let t = this.pool.blockFor(c), r = this.pool.blockOffset(c), i = t[r + O], o = h(n, a, t[r + k], t[r + A], t[r + k] + t[r + j], t[r + A] + t[r + M]);
|
|
314
|
+
(o < e.distance || o === e.distance && (e.id === void 0 || i < e.id)) && (e.id = i, e.distance = o), c = t[r + P];
|
|
315
315
|
}
|
|
316
|
-
|
|
316
|
+
i(this.nodeInts, s);
|
|
317
317
|
}
|
|
318
318
|
linkIntoNode(e, t) {
|
|
319
319
|
let n = e * D + T;
|
|
320
|
-
|
|
320
|
+
r(this.nodeInts, n), this.linkNoLock(e, t), i(this.nodeInts, n);
|
|
321
321
|
}
|
|
322
322
|
linkNoLock(e, t) {
|
|
323
323
|
let n = e * D + E;
|
|
@@ -325,7 +325,7 @@ var v = 0, y = 1, b = 2, x = 3, S = 4, C = 5, w = 6, T = 0, E = 1, D = 2, O = 0,
|
|
|
325
325
|
}
|
|
326
326
|
unlinkFromNode(e, t) {
|
|
327
327
|
let n = e * D + T;
|
|
328
|
-
|
|
328
|
+
r(this.nodeInts, n), this.unlinkNoLock(e, t), i(this.nodeInts, n);
|
|
329
329
|
}
|
|
330
330
|
unlinkNoLock(e, t) {
|
|
331
331
|
let n = e * D + E, r = this.nodeHeads[n], i = this.pool.get(t, P);
|
|
@@ -345,17 +345,16 @@ var v = 0, y = 1, b = 2, x = 3, S = 4, C = 5, w = 6, T = 0, E = 1, D = 2, O = 0,
|
|
|
345
345
|
}
|
|
346
346
|
};
|
|
347
347
|
function U(e) {
|
|
348
|
-
let t =
|
|
349
|
-
if (t !==
|
|
348
|
+
let t = l(e ?? V);
|
|
349
|
+
if (t !== c.float32 && t !== c.float64) throw Error("SharedQuadtree must be Float32Array or Float64Array");
|
|
350
350
|
return t;
|
|
351
351
|
}
|
|
352
352
|
function W(e) {
|
|
353
|
-
return
|
|
353
|
+
return u(e) === 2 ? Float64Array : Float32Array;
|
|
354
354
|
}
|
|
355
355
|
function G(e) {
|
|
356
|
-
return e ===
|
|
356
|
+
return e === c.float32 ? L : R;
|
|
357
357
|
}
|
|
358
|
-
//#endregion
|
|
359
358
|
export { H as default };
|
|
360
359
|
|
|
361
360
|
//# sourceMappingURL=shared-quadtree.js.map
|