@structs-arena/linked-list 0.0.0-stage → 1.0.0

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 (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +39 -3
  3. package/package.json +40 -6
  4. package/src/index.js +600 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Augusto Vasques
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,39 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
1
+ # @structs-arena/linked-list
2
+
3
+ A Balloc-backed singly linked list with cursor navigation, optional cyclic traversal, multiple cursors, and CBOR serialization.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @structs-arena/linked-list
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```js
14
+ import { LinkedList } from "@structs-arena/linked-list";
15
+
16
+ const list = new LinkedList();
17
+ list.insertTail("first");
18
+ list.insertTail("second");
19
+
20
+ console.log(list.current);
21
+ list.next();
22
+ console.log(list.current);
23
+ console.log(list.length); // 2
24
+ ```
25
+
26
+ ## API overview
27
+
28
+ - Insert: `insertHead`, `insertTail`, `insertNext`.
29
+ - Read or replace: `current`, `set`.
30
+ - Navigate: `next`, `peek`, `reset`.
31
+ - Remove: `deleteNext`, `deleteHead`.
32
+ - Cursors: `createCursor`, `deleteCursor`, `setCursor`, `cloneCursor`.
33
+ - State: `head`, `tail`, `isCyclic`, `length`.
34
+ - Serialization: `dump`, `LinkedList.load`.
35
+ - `LinkedListFactory` creates lists that share a Balloc instance.
36
+
37
+ ## License
38
+
39
+ MIT
package/package.json CHANGED
@@ -1,6 +1,40 @@
1
- {
2
- "name": "@structs-arena/linked-list",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
1
+ {
2
+ "name": "@structs-arena/linked-list",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "main": "./src/index.js",
6
+ "exports": {
7
+ ".": "./src/index.js"
8
+ },
9
+ "files": [
10
+ "src",
11
+ "LICENSE"
12
+ ],
13
+ "license": "MIT",
14
+ "description": "Balloc-backed singly linked list with cursor navigation and optional cyclic traversal.",
15
+ "keywords": [
16
+ "linked-list",
17
+ "data-structures",
18
+ "javascript",
19
+ "balloc"
20
+ ],
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "https://github.com/AugustoVasques75/structs-arena.git",
24
+ "directory": "packages/linked-list"
25
+ },
26
+ "bugs": {
27
+ "url": "https://github.com/AugustoVasques75/structs-arena/issues"
28
+ },
29
+ "scripts": {
30
+ "test": "vitest run",
31
+ "test:watch": "vitest"
32
+ },
33
+ "dependencies": {
34
+ "balloc": "^2.0.1",
35
+ "cbor-x": "^1.6.8"
36
+ },
37
+ "engines": {
38
+ "node": ">=20"
39
+ }
40
+ }
package/src/index.js ADDED
@@ -0,0 +1,600 @@
1
+ import Balloc from "balloc";
2
+ import { decode, encode } from 'cbor-x';
3
+
4
+ const UNKNOWN = 0xFFFFFFFF >>> 0;
5
+ const DISABLED = 0xFFFFFFFE >>> 0;
6
+
7
+ export class LinkedList {
8
+ #cyclic = false;
9
+ #head = UNKNOWN;
10
+ #tail = UNKNOWN;
11
+ #cursor = UNKNOWN;
12
+ #cursorIdx = 0;
13
+ static #token = Symbol("SingledLinkedList");
14
+
15
+ /**
16
+ * Creates a new list or restores a serialized instance.
17
+
18
+ * @overload
19
+ * @param {symbol}
20
+ * @param {Buffer | Uint8Array}
21
+ * @param {{cursors?: boolean}} [options={cursors:false}] Specifies whether the cursors stored in the package should be restored.
22
+ * @param {Balloc}
23
+ * @overload
24
+ * @param {boolean}
25
+ * @param {number}
26
+ * @param {string}
27
+ * @param {number}
28
+ * @param {Balloc}
29
+ * @param {*}
30
+ * @param {*}
31
+ * @param {*}
32
+ * @param {*}
33
+ * @param {*}
34
+ * @throws {TypeError}
35
+ * @throws {LinkedListDeserializationError}
36
+ */
37
+ constructor(arg1, arg2, arg3, arg4, arg5) {
38
+ if (arg1 === LinkedList.#token) {
39
+ try {
40
+ const pack = decode(arg2);
41
+ if (pack.format !== "LinkedList") throw new TypeError(`Unable to read the structure. Expected LinkedList, received v${pack.format ?? "null"}.`);
42
+ if (pack.version !== 1) throw new RangeError(`Invalid dump version. Expected v1, received v${pack.version ?? "null"}.`);
43
+
44
+ this.#cyclic = pack.cyclic;
45
+ this.#head = pack.head;
46
+ this.#tail = pack.tail;
47
+
48
+ const cursors = !pack.cursors ? false : arg3?.cursors ?? false;
49
+
50
+ this.#cursor = cursors ? pack.cursor : UNKNOWN;
51
+ this.#cursorIdx = cursors ? pack.cursorIdx : 0;
52
+
53
+ this.dataBus = arg4 ?? new Balloc(0x80000000, pack.type);
54
+ this.values = this.dataBus.load(pack.data.values);
55
+
56
+ this.idxBus = new Balloc(0x80000000, "32u");
57
+ this.idxNext = this.idxBus.load(pack.indexes.idxNext);
58
+ this.freeStack = this.idxBus.load(pack.indexes.freeStack);
59
+ this.cursorStack = cursors ? this.idxBus.load(pack.indexes.cursorStack) : this.idxBus.alloc();
60
+ this.cursorFreeStack = cursors ? this.idxBus.load(pack.indexes.cursorFreeStack) : this.idxBus.alloc();
61
+ if (!cursors) this.idxBus.push(this.cursorStack, UNKNOWN);
62
+ return;
63
+ } catch (e) {
64
+ throw new LinkedListDeserializationError("Unable to deserialize the data package. See cause for details.", { cause: e });
65
+ }
66
+ } else {
67
+ if (arg1 !== null && arg1 !== undefined && typeof arg1 !== "boolean") {
68
+ throw new TypeError("The argument must be a boolean, null, or undefined.");
69
+ }
70
+ this.dataBus = arg5 ?? new Balloc(arg2 ?? 0x80000000, arg3 ?? "32u");
71
+ this.idxBus = new Balloc(this.dataBus.maxSize, "32u");
72
+ this.#cyclic = arg1 ?? false;
73
+ this.values = this.dataBus.alloc();
74
+ this.idxNext = this.idxBus.alloc();
75
+ this.freeStack = this.idxBus.alloc();
76
+ this.cursorStack = this.idxBus.alloc();
77
+ this.cursorFreeStack = this.idxBus.alloc();
78
+ this.idxBus.push(this.cursorStack, UNKNOWN);
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Reuses a node index from the free-index stack when available.
84
+
85
+ * @private
86
+ * @returns {number}
87
+ */
88
+ #recycleNode() {
89
+ return this.idxBus.occupied(this.freeStack) > 0 ? this.idxBus.pop(this.freeStack) : UNKNOWN;
90
+ }
91
+
92
+ /**
93
+ * Allocates a node or reuses a previously released slot.
94
+
95
+ * @private
96
+ * @param {*}
97
+ * @returns {number}
98
+ */
99
+ #allocateNode(value) {
100
+ let idx = this.#recycleNode();
101
+ if (idx === UNKNOWN) {
102
+ if (this.dataBus.occupied(this.values) === this.dataBus.length(this.values)) {
103
+ this.dataBus.realloc(this.values);
104
+ this.idxBus.realloc(this.idxNext);
105
+ }
106
+ idx = this.idxBus.occupied(this.idxNext);
107
+ this.dataBus.push(this.values, value);
108
+ this.idxBus.push(this.idxNext, UNKNOWN);
109
+ } else {
110
+ this.dataBus.set(this.values, idx, value);
111
+ this.idxBus.set(this.idxNext, idx, UNKNOWN);
112
+ }
113
+ return idx;
114
+ }
115
+
116
+ /**
117
+ * Moves cursors that reference a removed node to the specified replacement node.
118
+
119
+ * @private
120
+ * @param {number}
121
+ * @param {number}
122
+ * @returns {void}
123
+ */
124
+ #updateCursors(idx, newIdx) {
125
+ const length = this.idxBus.occupied(this.cursorStack);
126
+ for (let i = 0; i < length; i++) {
127
+ const cursor = this.idxBus.get(this.cursorStack, i);
128
+ if (cursor === DISABLED) continue;
129
+ if (cursor === idx) this.idxBus.set(this.cursorStack, i, newIdx);
130
+ }
131
+ }
132
+
133
+ /**
134
+ * A singly linked list backed by Balloc-managed memory.
135
+
136
+ * @param {*}
137
+ * @returns {*}
138
+ */
139
+ insertNext(value) {
140
+ if (this.#cursor === UNKNOWN) {
141
+ return this.#head === UNKNOWN ? this.insertTail(value): undefined;
142
+ }
143
+ const idx = this.#allocateNode(value);
144
+ const next = this.idxBus.get(this.idxNext, this.#cursor);
145
+
146
+ this.idxBus.set(this.idxNext, idx, next);
147
+ this.idxBus.set(this.idxNext, this.#cursor, idx);
148
+
149
+ if (this.#tail === this.#cursor) {
150
+ this.#tail = idx;
151
+ }
152
+
153
+ return value;
154
+ }
155
+
156
+ /**
157
+ * A singly linked list backed by Balloc-managed memory.
158
+
159
+ * @param {*}
160
+ * @returns {*}
161
+ */
162
+ insertTail(value) {
163
+ const idx = this.#allocateNode(value);
164
+ if (this.#head === UNKNOWN) {
165
+ this.#head = this.#tail = idx;
166
+ } else {
167
+ this.idxBus.set(this.idxNext, this.#tail, idx);
168
+ this.#tail = idx;
169
+ }
170
+ return value;
171
+ }
172
+
173
+ /**
174
+ * A singly linked list backed by Balloc-managed memory.
175
+
176
+ * @param {*}
177
+ * @returns {*}
178
+ */
179
+ insertHead(value) {
180
+ if (this.#head === UNKNOWN) {
181
+ return this.insertTail(value);
182
+ }
183
+ const idx = this.#allocateNode(value);
184
+ this.idxBus.set(this.idxNext, idx, this.#head);
185
+ this.#head = idx;
186
+ return value;
187
+ }
188
+
189
+ /**
190
+ * Replaces the value at the active cursor.
191
+
192
+ * @param {*}
193
+ * @returns {boolean}
194
+ */
195
+ set(value) {
196
+ if (this.#cursor === UNKNOWN) return false;
197
+
198
+ this.dataBus.set(this.values, this.#cursor, value);
199
+
200
+ return true;
201
+ }
202
+
203
+ /**
204
+ * A singly linked list backed by Balloc-managed memory.
205
+
206
+ * @returns {*}
207
+ */
208
+ deleteNext() {
209
+ if (this.#cursor === UNKNOWN || this.#cursor === this.#tail) {
210
+ return undefined;
211
+ }
212
+
213
+ const idx = this.idxBus.get(this.idxNext, this.#cursor);
214
+ const value = this.dataBus.get(this.values, idx);
215
+ const next = this.idxBus.get(this.idxNext, idx);
216
+
217
+ this.#updateCursors(idx, this.#cyclic && next === this.#tail ? this.#head : next);
218
+
219
+ this.idxBus.set(this.idxNext, this.#cursor, next);
220
+
221
+ // Update the tail regardless of how the node is released.
222
+ if (this.#tail === idx) {
223
+ this.#tail = this.#cursor;
224
+ }
225
+
226
+ if (idx === this.idxBus.occupied(this.idxNext) - 1) {
227
+ this.dataBus.pop(this.values);
228
+ this.idxBus.pop(this.idxNext);
229
+ } else {
230
+ if (
231
+ this.idxBus.occupied(this.freeStack) ===
232
+ this.idxBus.length(this.freeStack)
233
+ ) {
234
+ this.idxBus.realloc(this.freeStack);
235
+ }
236
+
237
+ this.idxBus.push(this.freeStack, idx);
238
+ }
239
+
240
+ return value;
241
+ }
242
+
243
+ /**
244
+ * Removes the head node.
245
+
246
+ * @returns {*}
247
+ */
248
+ deleteHead() {
249
+ if (this.#head === UNKNOWN) {
250
+ return undefined;
251
+ }
252
+
253
+ const idx = this.#head;
254
+ const value = this.dataBus.get(this.values, idx);
255
+ const next = this.idxBus.get(this.idxNext, idx);
256
+
257
+ // Cursors that referenced the removed node are moved to its successor.
258
+ this.#updateCursors(idx, next);
259
+
260
+ if (idx === this.#tail) {
261
+ this.#head = UNKNOWN;
262
+ this.#tail = UNKNOWN;
263
+ this.#cursor = UNKNOWN;
264
+ } else {
265
+ this.#head = next;
266
+
267
+ if (this.#cursor === idx) {
268
+ this.#cursor = next;
269
+ }
270
+ }
271
+
272
+ if (idx === this.idxBus.occupied(this.idxNext) - 1) {
273
+ this.dataBus.pop(this.values);
274
+ this.idxBus.pop(this.idxNext);
275
+ } else {
276
+ if (
277
+ this.idxBus.occupied(this.freeStack) ===
278
+ this.idxBus.length(this.freeStack)
279
+ ) {
280
+ this.idxBus.realloc(this.freeStack);
281
+ }
282
+
283
+ this.idxBus.push(this.freeStack, idx);
284
+ }
285
+
286
+ return value;
287
+ }
288
+
289
+ /**
290
+ * Moves the active cursor to the next node.
291
+
292
+ * @returns {boolean}
293
+ */
294
+ next() {
295
+ if (this.#tail === this.#head && this.#head == UNKNOWN) return false;
296
+
297
+ if (this.#cursor === UNKNOWN) {
298
+ this.#cursor = this.#head;
299
+ } else {
300
+ if (this.#cyclic) {
301
+ if (this.#cursor === this.#tail) {
302
+ this.#cursor = this.#head;
303
+ } else {
304
+ this.#cursor = this.idxBus.get(this.idxNext, this.#cursor);
305
+ }
306
+ } else {
307
+ if (this.#cursor === this.#tail) {
308
+ return false;
309
+ } else {
310
+ this.#cursor = this.idxBus.get(this.idxNext, this.#cursor);
311
+ }
312
+ }
313
+ }
314
+
315
+ return true;
316
+ }
317
+
318
+ /**
319
+ * A singly linked list backed by Balloc-managed memory.
320
+
321
+ * @returns {*}
322
+ */
323
+ peek() {
324
+ if (this.#head === UNKNOWN) return undefined;
325
+
326
+ if (this.#cursor === UNKNOWN) {
327
+ return this.head;
328
+ }
329
+
330
+ if (this.#cursor === this.#tail) {
331
+ return this.#cyclic ? this.head : undefined;
332
+ }
333
+
334
+ const idx = this.idxBus.get(this.idxNext, this.#cursor);
335
+ return this.dataBus.get(this.values, idx);
336
+ }
337
+
338
+ /**
339
+ * Resets the active cursor to the unpositioned state.
340
+
341
+ * @returns {void}
342
+ */
343
+ reset() {
344
+ this.#cursor = UNKNOWN;
345
+ }
346
+
347
+ /**
348
+ * Removes all nodes and reinitializes the list's internal storage.
349
+
350
+ * @param {boolean}
351
+ * @returns {void}
352
+ */
353
+ clear(cyclic = this.#cyclic) {
354
+ this.#cyclic = cyclic;
355
+ this.#head = UNKNOWN;
356
+ this.#tail = UNKNOWN;
357
+ this.#cursor = UNKNOWN;
358
+ this.#cursorIdx = 0;
359
+ this.dataBus.free(this.values);
360
+ this.idxBus.free(this.idxNext);
361
+ this.idxBus.free(this.freeStack);
362
+ this.idxBus.free(this.cursorStack);
363
+ this.idxBus.free(this.cursorFreeStack);
364
+
365
+ this.values = this.dataBus.alloc();
366
+ this.idxNext = this.idxBus.alloc();
367
+ this.freeStack = this.idxBus.alloc();
368
+ this.cursorStack = this.idxBus.alloc();
369
+ this.cursorFreeStack = this.idxBus.alloc();
370
+ this.idxBus.push(this.cursorStack, UNKNOWN);
371
+ }
372
+
373
+ /**
374
+ * Creates an independent cursor.
375
+
376
+ * @returns {number}
377
+ */
378
+ createCursor() {
379
+ if (this.idxBus.occupied(this.cursorFreeStack) > 0) {
380
+ const cursor = this.idxBus.pop(this.cursorFreeStack);
381
+ this.idxBus.set(this.cursorStack, cursor, UNKNOWN);
382
+ return cursor;
383
+ }
384
+
385
+ const cursor = this.idxBus.occupied(this.cursorStack);
386
+ if (cursor === this.idxBus.length(this.cursorStack)) this.idxBus.realloc(this.cursorStack);
387
+ this.idxBus.push(this.cursorStack, UNKNOWN);
388
+ return cursor;
389
+ }
390
+
391
+ /**
392
+ * Disables an independent cursor and releases its identifier for reuse.
393
+
394
+ * @param {number}
395
+ * @returns {boolean}
396
+ */
397
+ deleteCursor(cursor) {
398
+ const length = this.idxBus.occupied(this.cursorStack);
399
+ if (!Number.isInteger(cursor) || cursor <= 0 || cursor >= length) return false;
400
+ if (this.idxBus.get(this.cursorStack, cursor) === DISABLED) return false;
401
+
402
+ this.idxBus.set(this.cursorStack, cursor, DISABLED);
403
+
404
+ if (this.#cursorIdx === cursor) {
405
+ this.#cursorIdx = 0;
406
+ this.#cursor = this.idxBus.get(this.cursorStack, 0);
407
+ }
408
+
409
+ if (cursor === length - 1) {
410
+ this.idxBus.pop(this.cursorStack);
411
+ } else {
412
+ if (this.idxBus.occupied(this.cursorFreeStack) === this.idxBus.length(this.cursorFreeStack)) {
413
+ this.idxBus.realloc(this.cursorFreeStack);
414
+ }
415
+ this.idxBus.push(this.cursorFreeStack, cursor);
416
+ }
417
+
418
+ return true;
419
+ }
420
+
421
+ /**
422
+ * Selects the cursor used by position-dependent operations.
423
+
424
+ * @param {number}
425
+ * @returns {boolean}
426
+ */
427
+ setCursor(cursor) {
428
+ if (cursor === this.#cursorIdx) return true;
429
+ const length = this.idxBus.occupied(this.cursorStack);
430
+ if (!Number.isInteger(cursor) || cursor < 0 || cursor >= length) return false;
431
+
432
+ const state = this.idxBus.get(this.cursorStack, cursor);
433
+ if (state === DISABLED) return false;
434
+
435
+ this.idxBus.set(this.cursorStack, this.#cursorIdx, this.#cursor);
436
+ this.#cursorIdx = cursor;
437
+ this.#cursor = state;
438
+
439
+ return true;
440
+ }
441
+
442
+ /**
443
+ * Creates an independent cursor at the same position as another cursor.
444
+
445
+ * @param {number}
446
+ * @returns {number | undefined}
447
+ */
448
+ cloneCursor(cursor) {
449
+ const length = this.idxBus.occupied(this.cursorStack);
450
+ if (!Number.isInteger(cursor) || cursor < 0 || cursor >= length) return undefined;
451
+ if (this.idxBus.get(this.cursorStack, cursor) === DISABLED) return undefined;
452
+
453
+ const state = cursor === this.#cursorIdx ? this.#cursor : this.idxBus.get(this.cursorStack, cursor);
454
+ const clone = this.createCursor();
455
+ this.idxBus.set(this.cursorStack, clone, state);
456
+ return clone;
457
+ }
458
+
459
+
460
+ /**
461
+ * Serializes the list state into a CBOR-encoded binary buffer.
462
+
463
+ * @param {{cursors?: boolean}} [options={cursors:false}] Specifies whether auxiliary cursor stacks should be included in the serialized data.
464
+ * @returns {Uint8Array}
465
+ */
466
+ dump(options = { cursors: false, }) {
467
+ this.idxBus.set(this.cursorStack, this.#cursorIdx, this.#cursor);
468
+ return encode({
469
+ format: "LinkedList",
470
+ version: 1,
471
+ cyclic: this.#cyclic,
472
+ type: this.dataBus.bucketSize,
473
+ head: this.#head,
474
+ tail: this.#tail,
475
+ cursors: options.cursors,
476
+ cursorIdx: options.cursors ? this.#cursorIdx : 0,
477
+ cursor: this.#cursor,
478
+ data: {
479
+ values: this.dataBus.dump(this.values)
480
+ },
481
+ indexes: {
482
+ idxNext: this.idxBus.dump(this.idxNext),
483
+ freeStack: this.idxBus.dump(this.freeStack),
484
+ ...(options.cursors && {
485
+ cursorStack: this.idxBus.dump(this.cursorStack),
486
+ cursorFreeStack: this.idxBus.dump(this.cursorFreeStack)
487
+ })
488
+ }
489
+ });
490
+ }
491
+
492
+ /**
493
+ * Restores a list from a CBOR-encoded binary buffer.
494
+
495
+ * @param {Buffer | Uint8Array}
496
+ * @param {{cursors?: boolean}} [options={cursors:false}] Specifies whether the exported cursor state should be restored.
497
+ * @param {Balloc}
498
+ * @returns {LinkedList}
499
+ * @throws {LinkedListDeserializationError}
500
+ */
501
+ static load(data, options = { cursors: false, }, memory) {
502
+ return new LinkedList(LinkedList.#token, data, options, memory);
503
+ }
504
+
505
+ /**
506
+ * Returns the value at the active cursor.
507
+
508
+ * @returns {*}
509
+ */
510
+ get current() {
511
+ return this.#cursor === UNKNOWN ? undefined : this.dataBus.get(this.values, this.#cursor);
512
+ }
513
+
514
+ /**
515
+ * A singly linked list backed by Balloc-managed memory.
516
+
517
+ * @returns {*}
518
+ */
519
+ get head() {
520
+ return this.#head === UNKNOWN ? undefined : this.dataBus.get(this.values, this.#head);
521
+ }
522
+
523
+ /**
524
+ * Returns the value stored at the tail node.
525
+
526
+ * @returns {*}
527
+ */
528
+ get tail() {
529
+ return this.#tail === UNKNOWN ? undefined : this.dataBus.get(this.values, this.#tail);
530
+ }
531
+
532
+ /**
533
+ * Indicates whether the list uses cyclic navigation.
534
+
535
+ * @returns {boolean}
536
+ */
537
+ get isCyclic() {
538
+ return this.#cyclic;
539
+ }
540
+
541
+ /**
542
+ * Returns the number of active nodes in the list.
543
+
544
+ * @returns {number}
545
+ */
546
+ get length() {
547
+ return this.idxBus.occupied(this.idxNext) - this.idxBus.occupied(this.freeStack);
548
+ }
549
+ }
550
+
551
+ export class LinkedListFactory {
552
+
553
+ /**
554
+ * Creates a new list or restores a serialized instance.
555
+
556
+ * @param {number}
557
+ * @param {string}
558
+ * @param {number}
559
+ */
560
+ constructor(memSize = 128, bucketSize = "32u", metadataSize = 64) {
561
+ this.memory = new Balloc(memSize, bucketSize);
562
+ }
563
+
564
+ /**
565
+ * Creates a new linked list using the factory's shared memory manager.
566
+
567
+ * @param {boolean}
568
+ * @returns {LinkedList}
569
+ */
570
+ createLinkedList(cyclic = false) {
571
+ return new LinkedList(cyclic, null, null, null, this.memory);
572
+ }
573
+
574
+ /**
575
+ * Restores a linked list using the factory's shared memory manager.
576
+
577
+ * @param {Buffer | Uint8Array}
578
+ * @param {{cursors?: boolean}} [options={cursors:false}] Specifies whether the cursor states stored in the package should be restored.
579
+ * @returns {LinkedList}
580
+ * @throws {LinkedListDeserializationError}
581
+ */
582
+ loadLinkedList(data, options = { cursors: false, }) {
583
+ return LinkedList.load(data, options, this.memory);
584
+ }
585
+ }
586
+
587
+ /**
588
+ * A singly linked list backed by Balloc-managed memory.
589
+ */
590
+ export class LinkedListDeserializationError extends Error {
591
+ /**
592
+ * Creates a new list or restores a serialized instance.
593
+
594
+ * @param {string}
595
+ * @param {ErrorOptions}
596
+ */
597
+ constructor(message, options) {
598
+ super(message, options);
599
+ }
600
+ }