@osmix/core 0.0.2 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +60 -0
- package/README.md +118 -107
- package/dist/src/entities.d.ts +103 -9
- package/dist/src/entities.d.ts.map +1 -1
- package/dist/src/entities.js +101 -11
- package/dist/src/entities.js.map +1 -1
- package/dist/src/ids.d.ts +82 -13
- package/dist/src/ids.d.ts.map +1 -1
- package/dist/src/ids.js +103 -31
- package/dist/src/ids.js.map +1 -1
- package/dist/src/index.d.ts +31 -5
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +33 -5
- package/dist/src/index.js.map +1 -1
- package/dist/src/nodes.d.ts +91 -21
- package/dist/src/nodes.d.ts.map +1 -1
- package/dist/src/nodes.js +191 -44
- package/dist/src/nodes.js.map +1 -1
- package/dist/src/osm.d.ts +99 -0
- package/dist/src/osm.d.ts.map +1 -0
- package/dist/src/osm.js +165 -0
- package/dist/src/osm.js.map +1 -0
- package/dist/src/relations.d.ts +112 -20
- package/dist/src/relations.d.ts.map +1 -1
- package/dist/src/relations.js +298 -35
- package/dist/src/relations.js.map +1 -1
- package/dist/src/stringtable.d.ts +78 -14
- package/dist/src/stringtable.d.ts.map +1 -1
- package/dist/src/stringtable.js +105 -26
- package/dist/src/stringtable.js.map +1 -1
- package/dist/src/tags.d.ts +117 -18
- package/dist/src/tags.d.ts.map +1 -1
- package/dist/src/tags.js +177 -30
- package/dist/src/tags.js.map +1 -1
- package/dist/src/typed-arrays.d.ts +85 -11
- package/dist/src/typed-arrays.d.ts.map +1 -1
- package/dist/src/typed-arrays.js +88 -9
- package/dist/src/typed-arrays.js.map +1 -1
- package/dist/src/utils.d.ts +35 -1
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/src/utils.js +69 -10
- package/dist/src/utils.js.map +1 -1
- package/dist/src/ways.d.ts +91 -27
- package/dist/src/ways.d.ts.map +1 -1
- package/dist/src/ways.js +229 -69
- package/dist/src/ways.js.map +1 -1
- package/dist/test/memory.test.d.ts +7 -0
- package/dist/test/memory.test.d.ts.map +1 -0
- package/dist/test/memory.test.js +29 -0
- package/dist/test/memory.test.js.map +1 -0
- package/dist/test/mock-osm.d.ts +3 -3
- package/dist/test/mock-osm.d.ts.map +1 -1
- package/dist/test/mock-osm.js +3 -3
- package/dist/test/mock-osm.js.map +1 -1
- package/dist/test/tags-search.test.d.ts +2 -0
- package/dist/test/tags-search.test.d.ts.map +1 -0
- package/dist/test/tags-search.test.js +56 -0
- package/dist/test/tags-search.test.js.map +1 -0
- package/dist/test/typed-arrays.test.js +19 -17
- package/dist/test/typed-arrays.test.js.map +1 -1
- package/package.json +11 -13
- package/src/entities.ts +147 -22
- package/src/ids.ts +126 -45
- package/src/index.ts +34 -5
- package/src/nodes.ts +248 -58
- package/src/osm.ts +222 -0
- package/src/relations.ts +396 -54
- package/src/stringtable.ts +123 -36
- package/src/tags.ts +220 -46
- package/src/typed-arrays.ts +97 -10
- package/src/types/geoflatbush.d.ts +37 -0
- package/src/types/geokdbush.d.ts +37 -0
- package/src/types/kdbush.d.ts +24 -6
- package/src/utils.ts +80 -14
- package/src/ways.ts +260 -84
- package/test/memory.test.ts +38 -0
- package/test/mock-osm.ts +8 -8
- package/test/tags-search.test.ts +60 -0
- package/test/typed-arrays.test.ts +5 -6
- package/dist/src/osmix.d.ts +0 -88
- package/dist/src/osmix.d.ts.map +0 -1
- package/dist/src/osmix.js +0 -438
- package/dist/src/osmix.js.map +0 -1
- package/dist/test/extract.bench.d.ts +0 -2
- package/dist/test/extract.bench.d.ts.map +0 -1
- package/dist/test/extract.bench.js +0 -23
- package/dist/test/extract.bench.js.map +0 -1
- package/dist/test/extract.test.d.ts +0 -2
- package/dist/test/extract.test.d.ts.map +0 -1
- package/dist/test/extract.test.js +0 -127
- package/dist/test/extract.test.js.map +0 -1
- package/dist/test/read.test.d.ts +0 -2
- package/dist/test/read.test.d.ts.map +0 -1
- package/dist/test/read.test.js +0 -17
- package/dist/test/read.test.js.map +0 -1
- package/dist/test/write.test.d.ts +0 -2
- package/dist/test/write.test.d.ts.map +0 -1
- package/dist/test/write.test.js +0 -55
- package/dist/test/write.test.js.map +0 -1
- package/src/osmix.ts +0 -556
- package/test/extract.bench.ts +0 -29
- package/test/extract.test.ts +0 -161
- package/test/read.test.ts +0 -22
- package/test/write.test.ts +0 -69
package/dist/src/tags.js
CHANGED
|
@@ -1,36 +1,78 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Tag storage and lookup.
|
|
3
|
+
*
|
|
4
|
+
* Stores key=value pairs using string table indices. Supports:
|
|
5
|
+
* 1. **Entity → Tags**: Retrieve tags for a given entity.
|
|
6
|
+
* 2. **Key → Entities**: Find entities with a specific tag key.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
import StringTable from "./stringtable";
|
|
11
|
+
import { ResizeableTypedArray as RTA } from "./typed-arrays";
|
|
12
|
+
/**
|
|
13
|
+
* Bidirectional tag storage.
|
|
14
|
+
*
|
|
15
|
+
* Limits: Max 255 tags per entity.
|
|
16
|
+
* Note: String indices reference a shared `StringTable`.
|
|
17
|
+
*/
|
|
2
18
|
export class Tags {
|
|
3
|
-
|
|
19
|
+
/** Reference to the shared string table for key/value storage */
|
|
20
|
+
stringTable = new StringTable();
|
|
21
|
+
// ─── Entity → Tag Lookup ───────────────────────────────────────────────────
|
|
22
|
+
/** Maps entity index → start position in tagKeys/tagVals */
|
|
4
23
|
tagStart;
|
|
24
|
+
/** Maps entity index → number of tags (max 255) */
|
|
5
25
|
tagCount;
|
|
26
|
+
/** All tag key string indices, concatenated */
|
|
6
27
|
tagKeys;
|
|
28
|
+
/** All tag value string indices, concatenated (parallel to tagKeys) */
|
|
7
29
|
tagVals;
|
|
30
|
+
// ─── Key → Entity Reverse Lookup ───────────────────────────────────────────
|
|
31
|
+
/**
|
|
32
|
+
* Flattened array of entity indices that have each tag key.
|
|
33
|
+
* Indexed via keyIndexStart and keyIndexCount.
|
|
34
|
+
*/
|
|
35
|
+
keyEntities;
|
|
36
|
+
/** Maps key string index → start position in keyEntities */
|
|
37
|
+
keyIndexStart;
|
|
38
|
+
/** Maps key string index → count of entities with that key */
|
|
39
|
+
keyIndexCount;
|
|
40
|
+
/**
|
|
41
|
+
* Temporary map used during ingestion to collect entity indices per key.
|
|
42
|
+
* Converted to flat arrays during buildIndex() and then cleared.
|
|
43
|
+
*/
|
|
44
|
+
keyEntityIndexBuilder = new Map();
|
|
45
|
+
/** Whether buildIndex() has been called */
|
|
8
46
|
indexBuilt = false;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
tagIndex.tagKeys = ResizeableTypedArray.from(Uint32Array, tagKeys);
|
|
14
|
-
tagIndex.tagVals = ResizeableTypedArray.from(Uint32Array, tagVals);
|
|
15
|
-
tagIndex.indexBuilt = true;
|
|
16
|
-
return tagIndex;
|
|
17
|
-
}
|
|
18
|
-
constructor(stringTable) {
|
|
47
|
+
/**
|
|
48
|
+
* Create a new Tags index.
|
|
49
|
+
*/
|
|
50
|
+
constructor(stringTable, transferables) {
|
|
19
51
|
this.stringTable = stringTable;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
52
|
+
if (transferables) {
|
|
53
|
+
this.tagStart = RTA.from(Uint32Array, transferables.tagStart);
|
|
54
|
+
this.tagCount = RTA.from(Uint8Array, transferables.tagCount);
|
|
55
|
+
this.tagKeys = RTA.from(Uint32Array, transferables.tagKeys);
|
|
56
|
+
this.tagVals = RTA.from(Uint32Array, transferables.tagVals);
|
|
57
|
+
this.keyEntities = RTA.from(Uint32Array, transferables.keyEntities);
|
|
58
|
+
this.keyIndexStart = RTA.from(Uint32Array, transferables.keyIndexStart);
|
|
59
|
+
this.keyIndexCount = RTA.from(Uint32Array, transferables.keyIndexCount);
|
|
60
|
+
this.indexBuilt = true;
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
this.tagStart = new RTA(Uint32Array);
|
|
64
|
+
this.tagCount = new RTA(Uint8Array);
|
|
65
|
+
this.tagKeys = new RTA(Uint32Array);
|
|
66
|
+
this.tagVals = new RTA(Uint32Array);
|
|
67
|
+
this.keyEntities = new RTA(Uint32Array);
|
|
68
|
+
this.keyIndexStart = new RTA(Uint32Array);
|
|
69
|
+
this.keyIndexCount = new RTA(Uint32Array);
|
|
70
|
+
}
|
|
32
71
|
}
|
|
33
|
-
|
|
72
|
+
/**
|
|
73
|
+
* Add tags to an entity.
|
|
74
|
+
*/
|
|
75
|
+
addTags(index, tags) {
|
|
34
76
|
const tagKeys = [];
|
|
35
77
|
const tagValues = [];
|
|
36
78
|
if (tags) {
|
|
@@ -39,27 +81,64 @@ export class Tags {
|
|
|
39
81
|
tagValues.push(this.stringTable.add(String(value)));
|
|
40
82
|
}
|
|
41
83
|
}
|
|
42
|
-
this.addTagKeysAndValues(tagKeys, tagValues);
|
|
84
|
+
this.addTagKeysAndValues(index, tagKeys, tagValues);
|
|
85
|
+
return [tagKeys, tagValues];
|
|
43
86
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
87
|
+
/**
|
|
88
|
+
* Add tags to an entity using key and value indexes.
|
|
89
|
+
*/
|
|
90
|
+
addTagKeysAndValues(index, keys, values) {
|
|
91
|
+
this.tagStart.set(index, this.tagKeys.length);
|
|
92
|
+
this.tagCount.set(index, keys.length);
|
|
47
93
|
this.tagKeys.pushMany(keys);
|
|
48
94
|
this.tagVals.pushMany(values);
|
|
95
|
+
keys.forEach((key) => {
|
|
96
|
+
const keyEntities = this.keyEntityIndexBuilder.get(key);
|
|
97
|
+
if (keyEntities) {
|
|
98
|
+
keyEntities.push(index);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
this.keyEntityIndexBuilder.set(key, [index]);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
49
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* Finalize the tag index.
|
|
107
|
+
*
|
|
108
|
+
* Compacts arrays and builds the reverse key→entity index.
|
|
109
|
+
* Must be called before `hasKey()`.
|
|
110
|
+
*/
|
|
50
111
|
buildIndex() {
|
|
51
112
|
this.tagStart.compact();
|
|
52
113
|
this.tagCount.compact();
|
|
53
114
|
this.tagKeys.compact();
|
|
54
115
|
this.tagVals.compact();
|
|
116
|
+
// Convert the builder map to flat arrays for the reverse index
|
|
117
|
+
for (const [keyIndex, entityIndexes] of this.keyEntityIndexBuilder) {
|
|
118
|
+
this.keyIndexStart.set(keyIndex, this.keyEntities.length);
|
|
119
|
+
this.keyIndexCount.set(keyIndex, entityIndexes.length);
|
|
120
|
+
this.keyEntities.pushMany(entityIndexes);
|
|
121
|
+
}
|
|
122
|
+
this.keyIndexStart.compact();
|
|
123
|
+
this.keyIndexCount.compact();
|
|
124
|
+
this.keyEntityIndexBuilder.clear();
|
|
55
125
|
this.indexBuilt = true;
|
|
56
126
|
}
|
|
57
|
-
|
|
127
|
+
/**
|
|
128
|
+
* Check if the index is built and ready for use.
|
|
129
|
+
*/
|
|
130
|
+
isReady() {
|
|
58
131
|
return this.indexBuilt;
|
|
59
132
|
}
|
|
133
|
+
/**
|
|
134
|
+
* Get the number of tags for an entity.
|
|
135
|
+
*/
|
|
60
136
|
cardinality(index) {
|
|
61
137
|
return this.tagCount.at(index) ?? 0;
|
|
62
138
|
}
|
|
139
|
+
/**
|
|
140
|
+
* Get the tags for an entity.
|
|
141
|
+
*/
|
|
63
142
|
getTags(index) {
|
|
64
143
|
const tagCount = this.tagCount.at(index) ?? 0;
|
|
65
144
|
if (tagCount === 0)
|
|
@@ -77,6 +156,9 @@ export class Tags {
|
|
|
77
156
|
}
|
|
78
157
|
return tags;
|
|
79
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* Get tags from key and value indexes.
|
|
161
|
+
*/
|
|
80
162
|
getTagsFromIndices(keys, values) {
|
|
81
163
|
const tags = {};
|
|
82
164
|
for (let i = 0; i < keys.length; i++) {
|
|
@@ -88,5 +170,70 @@ export class Tags {
|
|
|
88
170
|
}
|
|
89
171
|
return tags;
|
|
90
172
|
}
|
|
173
|
+
/**
|
|
174
|
+
* Find the index of a tag key.
|
|
175
|
+
*/
|
|
176
|
+
find(key) {
|
|
177
|
+
return this.stringTable.find(key);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Get all entity indexes that have a specific tag key.
|
|
181
|
+
*/
|
|
182
|
+
hasKey(keyIndex) {
|
|
183
|
+
if (keyIndex < 0)
|
|
184
|
+
return [];
|
|
185
|
+
const start = this.keyIndexStart.at(keyIndex) ?? 0;
|
|
186
|
+
const count = this.keyIndexCount.at(keyIndex) ?? 0;
|
|
187
|
+
return Array.from(this.keyEntities.array.subarray(start, start + count));
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Create a unique composite index for a key=value pair.
|
|
191
|
+
* Uses row-major indexing: `key * width + val`.
|
|
192
|
+
*/
|
|
193
|
+
kvToIndex(key, val) {
|
|
194
|
+
const width = this.stringTable.length;
|
|
195
|
+
return key * width + val;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Get transferable objects for passing to another thread.
|
|
199
|
+
*/
|
|
200
|
+
transferables() {
|
|
201
|
+
return {
|
|
202
|
+
tagStart: this.tagStart.array.buffer,
|
|
203
|
+
tagCount: this.tagCount.array.buffer,
|
|
204
|
+
tagKeys: this.tagKeys.array.buffer,
|
|
205
|
+
tagVals: this.tagVals.array.buffer,
|
|
206
|
+
keyEntities: this.keyEntities.array.buffer,
|
|
207
|
+
keyIndexStart: this.keyIndexStart.array.buffer,
|
|
208
|
+
keyIndexCount: this.keyIndexCount.array.buffer,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Get the approximate memory requirements for a given number of tags in bytes.
|
|
213
|
+
*/
|
|
214
|
+
static getBytesRequired(count) {
|
|
215
|
+
return (count * Uint32Array.BYTES_PER_ELEMENT + // tagStart
|
|
216
|
+
count * Uint8Array.BYTES_PER_ELEMENT // tagCount
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Reconstruct a Tags index from transferable objects.
|
|
221
|
+
*/
|
|
222
|
+
static fromTransferables(stringTable, transferables) {
|
|
223
|
+
const tagIndex = new Tags(stringTable, transferables);
|
|
224
|
+
tagIndex.indexBuilt = true;
|
|
225
|
+
return tagIndex;
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Update a ContentHasher with the tags data.
|
|
229
|
+
* Hashes tag keys and values for each entity.
|
|
230
|
+
*/
|
|
231
|
+
updateHash(hasher) {
|
|
232
|
+
return hasher
|
|
233
|
+
.update(this.tagStart.array)
|
|
234
|
+
.update(this.tagCount.array)
|
|
235
|
+
.update(this.tagKeys.array)
|
|
236
|
+
.update(this.tagVals.array);
|
|
237
|
+
}
|
|
91
238
|
}
|
|
92
239
|
//# sourceMappingURL=tags.js.map
|
package/dist/src/tags.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tags.js","sourceRoot":"","sources":["../../src/tags.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"tags.js","sourceRoot":"","sources":["../../src/tags.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,WAAW,MAAM,eAAe,CAAA;AACvC,OAAO,EAAmB,oBAAoB,IAAI,GAAG,EAAE,MAAM,gBAAgB,CAAA;AAwB7E;;;;;GAKG;AACH,MAAM,OAAO,IAAI;IAChB,iEAAiE;IACzD,WAAW,GAAgB,IAAI,WAAW,EAAE,CAAA;IAEpD,8EAA8E;IAC9E,4DAA4D;IACpD,QAAQ,CAAkB;IAClC,mDAAmD;IAC3C,QAAQ,CAAiB;IACjC,+CAA+C;IACvC,OAAO,CAAkB;IACjC,uEAA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AC,OAAO,CAAC,KAAK,CAAC,CAAA;IAC7B,CAAC;CACD"}
|
|
@@ -1,50 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resizable typed array utilities.
|
|
3
|
+
*
|
|
4
|
+
* Wraps typed arrays with automatic buffer expansion (ArrayList semantics).
|
|
5
|
+
* Uses SharedArrayBuffer (if available) for zero-copy worker transfer.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
1
9
|
/**
|
|
2
10
|
* Use SharedArrayBuffer if the runtime supports it, otherwise fall back to ArrayBuffer.
|
|
11
|
+
* SharedArrayBuffer enables zero-copy transfer between workers.
|
|
3
12
|
*/
|
|
4
|
-
export declare const BufferConstructor:
|
|
13
|
+
export declare const BufferConstructor: SharedArrayBufferConstructor | ArrayBufferConstructor;
|
|
14
|
+
/** The buffer type used by this runtime (SharedArrayBuffer or ArrayBuffer). */
|
|
5
15
|
export type BufferType = InstanceType<typeof BufferConstructor>;
|
|
16
|
+
/**
|
|
17
|
+
* Union of all standard typed array types.
|
|
18
|
+
* Generic over buffer type to preserve SharedArrayBuffer compatibility.
|
|
19
|
+
*/
|
|
6
20
|
export type TypedArray<B extends BufferType = BufferType> = Int8Array<B> | Uint8Array<B> | Uint8ClampedArray<B> | Int16Array<B> | Uint16Array<B> | Int32Array<B> | Uint32Array<B> | Float32Array<B> | Float64Array<B>;
|
|
21
|
+
/**
|
|
22
|
+
* Constructor interface for typed arrays.
|
|
23
|
+
*/
|
|
7
24
|
export interface TypedArrayConstructor<T extends TypedArray<BufferType> = TypedArray<BufferType>> {
|
|
8
25
|
new (buffer: BufferType): T;
|
|
9
26
|
readonly BYTES_PER_ELEMENT: number;
|
|
10
27
|
}
|
|
11
28
|
/**
|
|
12
|
-
*
|
|
29
|
+
* Float64Array for storing OSM IDs.
|
|
30
|
+
*
|
|
31
|
+
* OSM IDs are 64-bit integers. JavaScript's number type (IEEE 754 double)
|
|
32
|
+
* can exactly represent integers up to 2^53, which covers all current OSM IDs.
|
|
33
|
+
* Float64Array allows typed array operations while maintaining precision.
|
|
13
34
|
*/
|
|
14
35
|
export declare const IdArrayType: Float64ArrayConstructor;
|
|
15
36
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* When we are storing indexes into other arrays, we never need an index to exceed 2^32.
|
|
37
|
+
* Uint32Array for storing array indices.
|
|
38
|
+
*
|
|
39
|
+
* Internal indices into typed arrays never exceed 2^32 elements,
|
|
40
|
+
* so Uint32Array provides the best balance of range and memory efficiency.
|
|
22
41
|
*/
|
|
23
42
|
export declare const IndexArrayType: Uint32ArrayConstructor;
|
|
24
43
|
/**
|
|
25
|
-
*
|
|
44
|
+
* Initial buffer size for ResizeableTypedArray.
|
|
45
|
+
* 1 MiB provides reasonable initial capacity while avoiding excessive memory allocation.
|
|
26
46
|
*/
|
|
27
47
|
export declare const DEFAULT_BUFFER_SIZE: number;
|
|
48
|
+
/**
|
|
49
|
+
* Auto-expanding typed array wrapper.
|
|
50
|
+
*
|
|
51
|
+
* - `push()` appends elements, doubling buffer size as needed.
|
|
52
|
+
* - `compact()` shrinks buffer to fit data.
|
|
53
|
+
* - Supports growable SharedArrayBuffer and resizable ArrayBuffer.
|
|
54
|
+
*/
|
|
28
55
|
export declare class ResizeableTypedArray<TA extends TypedArray> {
|
|
56
|
+
/** The typed array constructor for this instance */
|
|
29
57
|
ArrayType: TypedArrayConstructor<TA>;
|
|
58
|
+
/** The current typed array view into the buffer */
|
|
30
59
|
array: TA;
|
|
60
|
+
/** Number of items actually stored (may be less than array.length) */
|
|
31
61
|
items: number;
|
|
62
|
+
/** The underlying ArrayBuffer or SharedArrayBuffer */
|
|
32
63
|
buffer: BufferType;
|
|
64
|
+
/** Current buffer size in bytes */
|
|
33
65
|
bufferSize: number;
|
|
66
|
+
/** Maximum byte length for growable buffers */
|
|
34
67
|
maxByteLength: number;
|
|
68
|
+
/** Buffer constructor (SharedArrayBuffer or ArrayBuffer) */
|
|
35
69
|
BC: SharedArrayBufferConstructor | ArrayBufferConstructor;
|
|
70
|
+
/**
|
|
71
|
+
* Reconstruct a ResizeableTypedArray from an existing buffer.
|
|
72
|
+
*
|
|
73
|
+
* Used after transferring buffers between workers. The resulting array
|
|
74
|
+
* is considered "compacted" with items = array.length.
|
|
75
|
+
*
|
|
76
|
+
* @param ArrayType - The typed array constructor.
|
|
77
|
+
* @param buffer - The existing buffer to wrap.
|
|
78
|
+
* @returns A new ResizeableTypedArray wrapping the buffer.
|
|
79
|
+
*/
|
|
36
80
|
static from<TA extends TypedArray>(ArrayType: TypedArrayConstructor<TA>, buffer: BufferType): ResizeableTypedArray<TA>;
|
|
81
|
+
/**
|
|
82
|
+
* Create a new ResizeableTypedArray with an empty buffer.
|
|
83
|
+
*
|
|
84
|
+
* @param ArrayType - The typed array constructor (e.g., Float64Array).
|
|
85
|
+
* @param BC - Buffer constructor to use (defaults to SharedArrayBuffer if available).
|
|
86
|
+
*/
|
|
37
87
|
constructor(ArrayType: TypedArrayConstructor<TA>, BC?: SharedArrayBufferConstructor | ArrayBufferConstructor);
|
|
88
|
+
/**
|
|
89
|
+
* Iterate over the array values.
|
|
90
|
+
*/
|
|
38
91
|
[Symbol.iterator](): ArrayIterator<number>;
|
|
92
|
+
/**
|
|
93
|
+
* Double the buffer capacity.
|
|
94
|
+
* Uses in-place grow/resize if supported, otherwise allocates new buffer and copies.
|
|
95
|
+
*/
|
|
39
96
|
expandArray(): void;
|
|
40
97
|
/**
|
|
41
|
-
* Handles negative
|
|
98
|
+
* Get the value at an index. Handles negative indices.
|
|
42
99
|
*/
|
|
43
100
|
at(index: number): number;
|
|
44
|
-
|
|
101
|
+
/**
|
|
102
|
+
* Get a slice of the array.
|
|
103
|
+
*/
|
|
104
|
+
slice(start: number, end: number): Int8Array<ArrayBuffer> | Uint8Array<ArrayBuffer> | Uint8ClampedArray<ArrayBuffer> | Int16Array<ArrayBuffer> | Uint16Array<ArrayBuffer> | Int32Array<ArrayBuffer> | Uint32Array<ArrayBuffer> | Float32Array<ArrayBuffer> | Float64Array<ArrayBuffer>;
|
|
45
105
|
get length(): number;
|
|
106
|
+
/**
|
|
107
|
+
* Push a value to the end of the array.
|
|
108
|
+
*/
|
|
46
109
|
push(value: number): number;
|
|
110
|
+
/**
|
|
111
|
+
* Set a value at a specific index. Expands array if needed.
|
|
112
|
+
*/
|
|
113
|
+
set(index: number, value: number): void;
|
|
114
|
+
/**
|
|
115
|
+
* Push multiple values to the end of the array.
|
|
116
|
+
*/
|
|
47
117
|
pushMany(values: number[] | TypedArray): void;
|
|
118
|
+
/**
|
|
119
|
+
* Shrink the buffer to exactly fit stored items.
|
|
120
|
+
* Buffer becomes fixed-length after compacting.
|
|
121
|
+
*/
|
|
48
122
|
compact(): TA;
|
|
49
123
|
}
|
|
50
124
|
//# sourceMappingURL=typed-arrays.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typed-arrays.d.ts","sourceRoot":"","sources":["../../src/typed-arrays.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"typed-arrays.d.ts","sourceRoot":"","sources":["../../src/typed-arrays.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH;;;GAGG;AACH,eAAO,MAAM,iBAAiB,uDAGa,CAAA;AAE3C,+EAA+E;AAC/E,MAAM,MAAM,UAAU,GAAG,YAAY,CAAC,OAAO,iBAAiB,CAAC,CAAA;AAE/D;;;GAGG;AACH,MAAM,MAAM,UAAU,CAAC,CAAC,SAAS,UAAU,GAAG,UAAU,IACrD,SAAS,CAAC,CAAC,CAAC,GACZ,UAAU,CAAC,CAAC,CAAC,GACb,iBAAiB,CAAC,CAAC,CAAC,GACpB,UAAU,CAAC,CAAC,CAAC,GACb,WAAW,CAAC,CAAC,CAAC,GACd,UAAU,CAAC,CAAC,CAAC,GACb,WAAW,CAAC,CAAC,CAAC,GACd,YAAY,CAAC,CAAC,CAAC,GACf,YAA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K,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM;IAIhC,IAAI,MAAM,WAET;IAED;;OAEG;IACH,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAQ3B;;OAEG;IACH,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;IAShC;;OAEG;IACH,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,UAAU;IAMtC;;;OAGG;IACH,OAAO;CAgBP"}
|
package/dist/src/typed-arrays.js
CHANGED
|
@@ -1,34 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resizable typed array utilities.
|
|
3
|
+
*
|
|
4
|
+
* Wraps typed arrays with automatic buffer expansion (ArrayList semantics).
|
|
5
|
+
* Uses SharedArrayBuffer (if available) for zero-copy worker transfer.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
1
9
|
/**
|
|
2
10
|
* Use SharedArrayBuffer if the runtime supports it, otherwise fall back to ArrayBuffer.
|
|
11
|
+
* SharedArrayBuffer enables zero-copy transfer between workers.
|
|
3
12
|
*/
|
|
4
13
|
export const BufferConstructor = typeof SharedArrayBuffer !== "undefined"
|
|
5
14
|
? SharedArrayBuffer
|
|
6
15
|
: ArrayBuffer;
|
|
7
16
|
/**
|
|
8
|
-
*
|
|
17
|
+
* Float64Array for storing OSM IDs.
|
|
18
|
+
*
|
|
19
|
+
* OSM IDs are 64-bit integers. JavaScript's number type (IEEE 754 double)
|
|
20
|
+
* can exactly represent integers up to 2^53, which covers all current OSM IDs.
|
|
21
|
+
* Float64Array allows typed array operations while maintaining precision.
|
|
9
22
|
*/
|
|
10
23
|
export const IdArrayType = Float64Array;
|
|
11
24
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* When we are storing indexes into other arrays, we never need an index to exceed 2^32.
|
|
25
|
+
* Uint32Array for storing array indices.
|
|
26
|
+
*
|
|
27
|
+
* Internal indices into typed arrays never exceed 2^32 elements,
|
|
28
|
+
* so Uint32Array provides the best balance of range and memory efficiency.
|
|
18
29
|
*/
|
|
19
30
|
export const IndexArrayType = Uint32Array;
|
|
20
31
|
/**
|
|
21
|
-
*
|
|
32
|
+
* Initial buffer size for ResizeableTypedArray.
|
|
33
|
+
* 1 MiB provides reasonable initial capacity while avoiding excessive memory allocation.
|
|
22
34
|
*/
|
|
23
35
|
export const DEFAULT_BUFFER_SIZE = 2 ** 20; // 1 MiB
|
|
36
|
+
/**
|
|
37
|
+
* Auto-expanding typed array wrapper.
|
|
38
|
+
*
|
|
39
|
+
* - `push()` appends elements, doubling buffer size as needed.
|
|
40
|
+
* - `compact()` shrinks buffer to fit data.
|
|
41
|
+
* - Supports growable SharedArrayBuffer and resizable ArrayBuffer.
|
|
42
|
+
*/
|
|
24
43
|
export class ResizeableTypedArray {
|
|
44
|
+
/** The typed array constructor for this instance */
|
|
25
45
|
ArrayType;
|
|
46
|
+
/** The current typed array view into the buffer */
|
|
26
47
|
array;
|
|
48
|
+
/** Number of items actually stored (may be less than array.length) */
|
|
27
49
|
items = 0;
|
|
50
|
+
/** The underlying ArrayBuffer or SharedArrayBuffer */
|
|
28
51
|
buffer;
|
|
52
|
+
/** Current buffer size in bytes */
|
|
29
53
|
bufferSize;
|
|
54
|
+
/** Maximum byte length for growable buffers */
|
|
30
55
|
maxByteLength;
|
|
56
|
+
/** Buffer constructor (SharedArrayBuffer or ArrayBuffer) */
|
|
31
57
|
BC;
|
|
58
|
+
/**
|
|
59
|
+
* Reconstruct a ResizeableTypedArray from an existing buffer.
|
|
60
|
+
*
|
|
61
|
+
* Used after transferring buffers between workers. The resulting array
|
|
62
|
+
* is considered "compacted" with items = array.length.
|
|
63
|
+
*
|
|
64
|
+
* @param ArrayType - The typed array constructor.
|
|
65
|
+
* @param buffer - The existing buffer to wrap.
|
|
66
|
+
* @returns A new ResizeableTypedArray wrapping the buffer.
|
|
67
|
+
*/
|
|
32
68
|
static from(ArrayType, buffer) {
|
|
33
69
|
const rta = new ResizeableTypedArray(ArrayType, buffer instanceof SharedArrayBuffer ? SharedArrayBuffer : ArrayBuffer);
|
|
34
70
|
rta.buffer = buffer;
|
|
@@ -36,6 +72,12 @@ export class ResizeableTypedArray {
|
|
|
36
72
|
rta.items = rta.array.length;
|
|
37
73
|
return rta;
|
|
38
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* Create a new ResizeableTypedArray with an empty buffer.
|
|
77
|
+
*
|
|
78
|
+
* @param ArrayType - The typed array constructor (e.g., Float64Array).
|
|
79
|
+
* @param BC - Buffer constructor to use (defaults to SharedArrayBuffer if available).
|
|
80
|
+
*/
|
|
39
81
|
constructor(ArrayType, BC = BufferConstructor) {
|
|
40
82
|
this.ArrayType = ArrayType;
|
|
41
83
|
this.bufferSize = DEFAULT_BUFFER_SIZE;
|
|
@@ -46,12 +88,20 @@ export class ResizeableTypedArray {
|
|
|
46
88
|
});
|
|
47
89
|
this.array = new this.ArrayType(this.buffer);
|
|
48
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* Iterate over the array values.
|
|
93
|
+
*/
|
|
49
94
|
[Symbol.iterator]() {
|
|
50
95
|
return this.array[Symbol.iterator]();
|
|
51
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* Double the buffer capacity.
|
|
99
|
+
* Uses in-place grow/resize if supported, otherwise allocates new buffer and copies.
|
|
100
|
+
*/
|
|
52
101
|
expandArray() {
|
|
53
102
|
this.bufferSize *= 2;
|
|
54
103
|
if (this.bufferSize > this.buffer.maxByteLength) {
|
|
104
|
+
// Need a completely new buffer with larger maxByteLength
|
|
55
105
|
this.maxByteLength *= 2;
|
|
56
106
|
const newBuffer = new this.BC(this.bufferSize, {
|
|
57
107
|
maxByteLength: this.maxByteLength,
|
|
@@ -62,6 +112,7 @@ export class ResizeableTypedArray {
|
|
|
62
112
|
this.array = newArray;
|
|
63
113
|
}
|
|
64
114
|
else {
|
|
115
|
+
// Can grow/resize the existing buffer in place
|
|
65
116
|
if (this.buffer instanceof SharedArrayBuffer && this.buffer.growable) {
|
|
66
117
|
this.buffer.grow(this.bufferSize);
|
|
67
118
|
}
|
|
@@ -74,7 +125,7 @@ export class ResizeableTypedArray {
|
|
|
74
125
|
}
|
|
75
126
|
}
|
|
76
127
|
/**
|
|
77
|
-
* Handles negative
|
|
128
|
+
* Get the value at an index. Handles negative indices.
|
|
78
129
|
*/
|
|
79
130
|
at(index) {
|
|
80
131
|
if (index < -this.length || index >= this.length)
|
|
@@ -86,12 +137,18 @@ export class ResizeableTypedArray {
|
|
|
86
137
|
throw Error(`No value at index: ${index}`);
|
|
87
138
|
return result;
|
|
88
139
|
}
|
|
140
|
+
/**
|
|
141
|
+
* Get a slice of the array.
|
|
142
|
+
*/
|
|
89
143
|
slice(start, end) {
|
|
90
144
|
return this.array.slice(start, end);
|
|
91
145
|
}
|
|
92
146
|
get length() {
|
|
93
147
|
return this.items;
|
|
94
148
|
}
|
|
149
|
+
/**
|
|
150
|
+
* Push a value to the end of the array.
|
|
151
|
+
*/
|
|
95
152
|
push(value) {
|
|
96
153
|
if (this.length >= this.array.length) {
|
|
97
154
|
this.expandArray();
|
|
@@ -99,17 +156,39 @@ export class ResizeableTypedArray {
|
|
|
99
156
|
this.array[this.items++] = value;
|
|
100
157
|
return this.length - 1;
|
|
101
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* Set a value at a specific index. Expands array if needed.
|
|
161
|
+
*/
|
|
162
|
+
set(index, value) {
|
|
163
|
+
if (index < 0)
|
|
164
|
+
throw Error("Index out of bounds");
|
|
165
|
+
if (index >= this.length) {
|
|
166
|
+
while (index >= this.array.length)
|
|
167
|
+
this.expandArray();
|
|
168
|
+
this.items = index + 1;
|
|
169
|
+
}
|
|
170
|
+
this.array[index] = value;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Push multiple values to the end of the array.
|
|
174
|
+
*/
|
|
102
175
|
pushMany(values) {
|
|
103
176
|
while (this.length + values.length > this.array.length)
|
|
104
177
|
this.expandArray();
|
|
105
178
|
this.array.set(values, this.length);
|
|
106
179
|
this.items += values.length;
|
|
107
180
|
}
|
|
181
|
+
/**
|
|
182
|
+
* Shrink the buffer to exactly fit stored items.
|
|
183
|
+
* Buffer becomes fixed-length after compacting.
|
|
184
|
+
*/
|
|
108
185
|
compact() {
|
|
109
186
|
if (this.buffer instanceof SharedArrayBuffer) {
|
|
187
|
+
// SharedArrayBuffer uses slice() to create a new fixed-size buffer
|
|
110
188
|
this.buffer = this.buffer.slice(0, this.length * this.ArrayType.BYTES_PER_ELEMENT);
|
|
111
189
|
}
|
|
112
190
|
else {
|
|
191
|
+
// ArrayBuffer uses transferToFixedLength() to detach and resize
|
|
113
192
|
this.buffer = this.buffer.transferToFixedLength(this.length * this.ArrayType.BYTES_PER_ELEMENT);
|
|
114
193
|
}
|
|
115
194
|
this.array = new this.ArrayType(this.buffer);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typed-arrays.js","sourceRoot":"","sources":["../../src/typed-arrays.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"typed-arrays.js","sourceRoot":"","sources":["../../src/typed-arrays.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH;;;GAGG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAC7B,OAAO,iBAAiB,KAAK,WAAW;IACvC,CAAC,CAAE,iBAAkD;IACrD,CAAC,CAAE,WAAsC,CAAA;AA8B3C;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,YAAY,CAAA;AAEvC;;;;;GAKG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,WAAW,CAAA;AAEzC;;;GAGG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,IAAI,EAAE,CAAA,CAAC,QAAQ;AAEnD;;;;;;GAMG;AACH,MAAM,OAAO,oBAAoB;IAChC,oDAAoD;IACpD,SAAS,CAA2B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