@procedures/collections 0.0.0-stage → 0.2.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.
- package/CHANGELOG.md +9 -0
- package/LICENSE +21 -0
- package/README.md +394 -2
- package/dist/.tsbuildinfo +1 -0
- package/dist/async/async-queue.d.ts +155 -0
- package/dist/async/async-queue.d.ts.map +1 -0
- package/dist/async/async-queue.js +342 -0
- package/dist/async/async-queue.js.map +1 -0
- package/dist/async/channels.d.ts +188 -0
- package/dist/async/channels.d.ts.map +1 -0
- package/dist/async/channels.js +314 -0
- package/dist/async/channels.js.map +1 -0
- package/dist/behaviors/bounded.d.ts +79 -0
- package/dist/behaviors/bounded.d.ts.map +1 -0
- package/dist/behaviors/bounded.js +384 -0
- package/dist/behaviors/bounded.js.map +1 -0
- package/dist/behaviors/composite.d.ts +17 -0
- package/dist/behaviors/composite.d.ts.map +1 -0
- package/dist/behaviors/composite.js +93 -0
- package/dist/behaviors/composite.js.map +1 -0
- package/dist/behaviors/evented.d.ts +154 -0
- package/dist/behaviors/evented.d.ts.map +1 -0
- package/dist/behaviors/evented.js +299 -0
- package/dist/behaviors/evented.js.map +1 -0
- package/dist/behaviors/lru.d.ts +84 -0
- package/dist/behaviors/lru.d.ts.map +1 -0
- package/dist/behaviors/lru.js +376 -0
- package/dist/behaviors/lru.js.map +1 -0
- package/dist/behaviors/readonly.d.ts +73 -0
- package/dist/behaviors/readonly.d.ts.map +1 -0
- package/dist/behaviors/readonly.js +177 -0
- package/dist/behaviors/readonly.js.map +1 -0
- package/dist/behaviors/safe.d.ts +163 -0
- package/dist/behaviors/safe.d.ts.map +1 -0
- package/dist/behaviors/safe.js +176 -0
- package/dist/behaviors/safe.js.map +1 -0
- package/dist/behaviors/synchronized.d.ts +88 -0
- package/dist/behaviors/synchronized.d.ts.map +1 -0
- package/dist/behaviors/synchronized.js +285 -0
- package/dist/behaviors/synchronized.js.map +1 -0
- package/dist/behaviors/ttl.d.ts +118 -0
- package/dist/behaviors/ttl.d.ts.map +1 -0
- package/dist/behaviors/ttl.js +354 -0
- package/dist/behaviors/ttl.js.map +1 -0
- package/dist/contracts/model.d.ts +29 -0
- package/dist/contracts/model.d.ts.map +1 -0
- package/dist/contracts/model.js +26 -0
- package/dist/contracts/model.js.map +1 -0
- package/dist/core/effects.d.ts +176 -0
- package/dist/core/effects.d.ts.map +1 -0
- package/dist/core/effects.js +180 -0
- package/dist/core/effects.js.map +1 -0
- package/dist/core/events.d.ts +192 -0
- package/dist/core/events.d.ts.map +1 -0
- package/dist/core/events.js +291 -0
- package/dist/core/events.js.map +1 -0
- package/dist/core/middleware.d.ts +231 -0
- package/dist/core/middleware.d.ts.map +1 -0
- package/dist/core/middleware.js +161 -0
- package/dist/core/middleware.js.map +1 -0
- package/dist/core/policies.d.ts +220 -0
- package/dist/core/policies.d.ts.map +1 -0
- package/dist/core/policies.js +48 -0
- package/dist/core/policies.js.map +1 -0
- package/dist/core/traits.d.ts +142 -0
- package/dist/core/traits.d.ts.map +1 -0
- package/dist/core/traits.js +8 -0
- package/dist/core/traits.js.map +1 -0
- package/dist/fx/collectors.d.ts +193 -0
- package/dist/fx/collectors.d.ts.map +1 -0
- package/dist/fx/collectors.js +375 -0
- package/dist/fx/collectors.js.map +1 -0
- package/dist/fx/iter.d.ts +166 -0
- package/dist/fx/iter.d.ts.map +1 -0
- package/dist/fx/iter.js +421 -0
- package/dist/fx/iter.js.map +1 -0
- package/dist/impl/array-deque.d.ts +112 -0
- package/dist/impl/array-deque.d.ts.map +1 -0
- package/dist/impl/array-deque.js +380 -0
- package/dist/impl/array-deque.js.map +1 -0
- package/dist/impl/array-list.d.ts +101 -0
- package/dist/impl/array-list.d.ts.map +1 -0
- package/dist/impl/array-list.js +387 -0
- package/dist/impl/array-list.js.map +1 -0
- package/dist/impl/hash-map.d.ts +149 -0
- package/dist/impl/hash-map.d.ts.map +1 -0
- package/dist/impl/hash-map.js +473 -0
- package/dist/impl/hash-map.js.map +1 -0
- package/dist/impl/hash-set.d.ts +125 -0
- package/dist/impl/hash-set.d.ts.map +1 -0
- package/dist/impl/hash-set.js +264 -0
- package/dist/impl/hash-set.js.map +1 -0
- package/dist/impl/linked-hash-map.d.ts +141 -0
- package/dist/impl/linked-hash-map.d.ts.map +1 -0
- package/dist/impl/linked-hash-map.js +442 -0
- package/dist/impl/linked-hash-map.js.map +1 -0
- package/dist/impl/linked-list.d.ts +109 -0
- package/dist/impl/linked-list.d.ts.map +1 -0
- package/dist/impl/linked-list.js +494 -0
- package/dist/impl/linked-list.js.map +1 -0
- package/dist/impl/priority-queue.d.ts +139 -0
- package/dist/impl/priority-queue.d.ts.map +1 -0
- package/dist/impl/priority-queue.js +343 -0
- package/dist/impl/priority-queue.js.map +1 -0
- package/dist/impl/tree-map.d.ts +153 -0
- package/dist/impl/tree-map.d.ts.map +1 -0
- package/dist/impl/tree-map.js +788 -0
- package/dist/impl/tree-map.js.map +1 -0
- package/dist/impl/tree-set.d.ts +112 -0
- package/dist/impl/tree-set.d.ts.map +1 -0
- package/dist/impl/tree-set.js +374 -0
- package/dist/impl/tree-set.js.map +1 -0
- package/dist/index.d.ts +106 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +139 -0
- package/dist/index.js.map +1 -0
- package/dist/interfaces/collection.d.ts +119 -0
- package/dist/interfaces/collection.d.ts.map +1 -0
- package/dist/interfaces/collection.js +8 -0
- package/dist/interfaces/collection.js.map +1 -0
- package/dist/interfaces/list.d.ts +247 -0
- package/dist/interfaces/list.d.ts.map +1 -0
- package/dist/interfaces/list.js +8 -0
- package/dist/interfaces/list.js.map +1 -0
- package/dist/interfaces/map.d.ts +344 -0
- package/dist/interfaces/map.d.ts.map +1 -0
- package/dist/interfaces/map.js +8 -0
- package/dist/interfaces/map.js.map +1 -0
- package/dist/interfaces/queue.d.ts +304 -0
- package/dist/interfaces/queue.d.ts.map +1 -0
- package/dist/interfaces/queue.js +8 -0
- package/dist/interfaces/queue.js.map +1 -0
- package/dist/interfaces/set.d.ts +137 -0
- package/dist/interfaces/set.d.ts.map +1 -0
- package/dist/interfaces/set.js +8 -0
- package/dist/interfaces/set.js.map +1 -0
- package/dist/register.d.ts +8 -0
- package/dist/register.d.ts.map +1 -0
- package/dist/register.js +8 -0
- package/dist/register.js.map +1 -0
- package/dist/storage/index.d.ts +15 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/index.js +14 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/storage/interface.d.ts +141 -0
- package/dist/storage/interface.d.ts.map +1 -0
- package/dist/storage/interface.js +23 -0
- package/dist/storage/interface.js.map +1 -0
- package/dist/storage/memory.d.ts +68 -0
- package/dist/storage/memory.d.ts.map +1 -0
- package/dist/storage/memory.js +154 -0
- package/dist/storage/memory.js.map +1 -0
- package/dist/utils/defaults.d.ts +135 -0
- package/dist/utils/defaults.d.ts.map +1 -0
- package/dist/utils/defaults.js +322 -0
- package/dist/utils/defaults.js.map +1 -0
- package/dist/utils/factories.d.ts +144 -0
- package/dist/utils/factories.d.ts.map +1 -0
- package/dist/utils/factories.js +293 -0
- package/dist/utils/factories.js.map +1 -0
- package/dist/utils/helpers.d.ts +137 -0
- package/dist/utils/helpers.d.ts.map +1 -0
- package/dist/utils/helpers.js +221 -0
- package/dist/utils/helpers.js.map +1 -0
- package/package.json +68 -3
- package/src/async/async-queue.ts +417 -0
- package/src/async/channels.ts +374 -0
- package/src/behaviors/bounded.ts +477 -0
- package/src/behaviors/composite.ts +99 -0
- package/src/behaviors/evented.ts +464 -0
- package/src/behaviors/lru.ts +447 -0
- package/src/behaviors/readonly.ts +224 -0
- package/src/behaviors/safe.ts +372 -0
- package/src/behaviors/synchronized.ts +316 -0
- package/src/behaviors/ttl.ts +462 -0
- package/src/contracts/model.ts +44 -0
- package/src/core/effects.ts +323 -0
- package/src/core/events.ts +356 -0
- package/src/core/middleware.ts +415 -0
- package/src/core/policies.ts +316 -0
- package/src/core/traits.ts +164 -0
- package/src/fx/collectors.ts +500 -0
- package/src/fx/iter.ts +536 -0
- package/src/impl/array-deque.ts +463 -0
- package/src/impl/array-list.ts +490 -0
- package/src/impl/hash-map.ts +623 -0
- package/src/impl/hash-set.ts +342 -0
- package/src/impl/linked-hash-map.ts +572 -0
- package/src/impl/linked-list.ts +606 -0
- package/src/impl/priority-queue.ts +448 -0
- package/src/impl/tree-map.ts +912 -0
- package/src/impl/tree-set.ts +469 -0
- package/src/index.ts +277 -0
- package/src/interfaces/collection.ts +141 -0
- package/src/interfaces/list.ts +281 -0
- package/src/interfaces/map.ts +405 -0
- package/src/interfaces/queue.ts +353 -0
- package/src/interfaces/set.ts +158 -0
- package/src/register.ts +8 -0
- package/src/storage/index.ts +16 -0
- package/src/storage/interface.ts +179 -0
- package/src/storage/memory.ts +180 -0
- package/src/utils/defaults.ts +345 -0
- package/src/utils/factories.ts +357 -0
- package/src/utils/helpers.ts +248 -0
|
@@ -0,0 +1,572 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LinkedHashMap - HashMap that maintains insertion order.
|
|
3
|
+
*
|
|
4
|
+
* Combines a hash table with a doubly-linked list to maintain predictable
|
|
5
|
+
* iteration order. Mirrors java.util.LinkedHashMap.
|
|
6
|
+
*
|
|
7
|
+
* Key features:
|
|
8
|
+
* - O(1) access, insert, delete (like HashMap)
|
|
9
|
+
* - Predictable iteration order (insertion order or access order)
|
|
10
|
+
* - Perfect for LRU caches when accessOrder is true
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const map = linkedHashMap<string, number>()
|
|
14
|
+
* map.set('a', 1)
|
|
15
|
+
* map.set('b', 2)
|
|
16
|
+
* map.set('c', 3)
|
|
17
|
+
* // Iteration order: a, b, c (insertion order)
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* // LRU cache with access order
|
|
21
|
+
* const lru = linkedHashMap<string, number>({ accessOrder: true })
|
|
22
|
+
* lru.set('a', 1)
|
|
23
|
+
* lru.set('b', 2)
|
|
24
|
+
* lru.get('a') // Moves 'a' to end
|
|
25
|
+
* // Iteration order: b, a (access order)
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import type { MapLike, Entry } from "../interfaces/map.js";
|
|
29
|
+
import type { Eq, Hash } from "../core/traits.js";
|
|
30
|
+
import { defaultEq, defaultHash } from "../utils/defaults.js";
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* An entry. It is in two lists: the chain of its hash bucket (`next`) and the order list
|
|
34
|
+
* (`before`, `after`). Before, one `next` field served both lists, so the first collision or
|
|
35
|
+
* resize corrupted the map (BUGS-2026-07 C10).
|
|
36
|
+
*/
|
|
37
|
+
interface LinkedNode<K, V> {
|
|
38
|
+
key: K;
|
|
39
|
+
value: V;
|
|
40
|
+
hash: number;
|
|
41
|
+
/** The next entry of the same hash bucket */
|
|
42
|
+
next: LinkedNode<K, V> | null;
|
|
43
|
+
/** The entry before this one in the iteration order */
|
|
44
|
+
before: LinkedNode<K, V> | null;
|
|
45
|
+
/** The entry after this one in the iteration order */
|
|
46
|
+
after: LinkedNode<K, V> | null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Options for creating a LinkedHashMap.
|
|
51
|
+
*/
|
|
52
|
+
export interface LinkedHashMapOptions<K, V> {
|
|
53
|
+
/**
|
|
54
|
+
* Initial capacity of the hash table.
|
|
55
|
+
* @default 16
|
|
56
|
+
*/
|
|
57
|
+
initialCapacity?: number;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Load factor before resizing.
|
|
61
|
+
* @default 0.75
|
|
62
|
+
*/
|
|
63
|
+
loadFactor?: number;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Equality function for keys.
|
|
67
|
+
* @default defaultEq
|
|
68
|
+
*/
|
|
69
|
+
eq?: Eq<K>;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Hash function for keys.
|
|
73
|
+
* @default defaultHash
|
|
74
|
+
*/
|
|
75
|
+
hash?: Hash<K>;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Equality function for values.
|
|
79
|
+
* @default defaultEq
|
|
80
|
+
*/
|
|
81
|
+
valueEq?: Eq<V>;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* If true, maintain access order instead of insertion order.
|
|
85
|
+
* Useful for LRU caches.
|
|
86
|
+
* @default false
|
|
87
|
+
*/
|
|
88
|
+
accessOrder?: boolean;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* LinkedHashMap<K, V> - Hash map with predictable iteration order.
|
|
93
|
+
*
|
|
94
|
+
* Maintains a doubly-linked list of entries to preserve insertion order
|
|
95
|
+
* (or access order if accessOrder is true).
|
|
96
|
+
*/
|
|
97
|
+
export class LinkedHashMap<K, V> implements MapLike<K, V> {
|
|
98
|
+
private _buckets: (LinkedNode<K, V> | null)[];
|
|
99
|
+
private _size: number = 0;
|
|
100
|
+
private readonly _loadFactor: number;
|
|
101
|
+
private readonly _eq: Eq<K>;
|
|
102
|
+
private readonly _hash: Hash<K>;
|
|
103
|
+
private readonly _valueEq: Eq<V>;
|
|
104
|
+
private readonly _accessOrder: boolean;
|
|
105
|
+
|
|
106
|
+
// Doubly-linked list sentinels
|
|
107
|
+
private _head: LinkedNode<K, V> | null = null;
|
|
108
|
+
private _tail: LinkedNode<K, V> | null = null;
|
|
109
|
+
|
|
110
|
+
constructor(options: LinkedHashMapOptions<K, V> = {}) {
|
|
111
|
+
const {
|
|
112
|
+
initialCapacity = 16,
|
|
113
|
+
loadFactor = 0.75,
|
|
114
|
+
eq = defaultEq,
|
|
115
|
+
hash = defaultHash,
|
|
116
|
+
valueEq = defaultEq,
|
|
117
|
+
accessOrder = false,
|
|
118
|
+
} = options;
|
|
119
|
+
|
|
120
|
+
this._buckets = new Array(Math.max(1, initialCapacity)).fill(null);
|
|
121
|
+
this._loadFactor = loadFactor;
|
|
122
|
+
this._eq = eq;
|
|
123
|
+
this._hash = hash;
|
|
124
|
+
this._valueEq = valueEq;
|
|
125
|
+
this._accessOrder = accessOrder;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// ========================================================================
|
|
129
|
+
// Size and state
|
|
130
|
+
// ========================================================================
|
|
131
|
+
|
|
132
|
+
get size(): number {
|
|
133
|
+
return this._size;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
get isEmpty(): boolean {
|
|
137
|
+
return this._size === 0;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
get eq(): Eq<K> {
|
|
141
|
+
return this._eq;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
get keyEq(): Eq<K> {
|
|
145
|
+
return this._eq;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
get valueEq(): Eq<V> {
|
|
149
|
+
return this._valueEq;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
get hash(): Hash<K> {
|
|
153
|
+
return this._hash;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// ========================================================================
|
|
157
|
+
// Lookup operations
|
|
158
|
+
// ========================================================================
|
|
159
|
+
|
|
160
|
+
has(key: K): boolean {
|
|
161
|
+
return this.getNode(key) !== null;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
containsKey(key: K): boolean {
|
|
165
|
+
return this.has(key);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
containsValue(value: V): boolean {
|
|
169
|
+
let node = this._head;
|
|
170
|
+
while (node !== null) {
|
|
171
|
+
if (this._valueEq(node.value, value)) {
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
node = node.after;
|
|
175
|
+
}
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
get(key: K): V {
|
|
180
|
+
const node = this.getNode(key);
|
|
181
|
+
if (node === null) {
|
|
182
|
+
throw new Error("Key not found");
|
|
183
|
+
}
|
|
184
|
+
if (this._accessOrder) {
|
|
185
|
+
this.moveToEnd(node);
|
|
186
|
+
}
|
|
187
|
+
return node.value;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
getOrUndefined(key: K): V | undefined {
|
|
191
|
+
const node = this.getNode(key);
|
|
192
|
+
if (node === null) return undefined;
|
|
193
|
+
if (this._accessOrder) {
|
|
194
|
+
this.moveToEnd(node);
|
|
195
|
+
}
|
|
196
|
+
return node.value;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
getOrDefault(key: K, defaultValue: V): V {
|
|
200
|
+
const node = this.getNode(key);
|
|
201
|
+
if (node === null) return defaultValue;
|
|
202
|
+
if (this._accessOrder) {
|
|
203
|
+
this.moveToEnd(node);
|
|
204
|
+
}
|
|
205
|
+
return node.value;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// ========================================================================
|
|
209
|
+
// Modification operations
|
|
210
|
+
// ========================================================================
|
|
211
|
+
|
|
212
|
+
set(key: K, value: V): V | undefined {
|
|
213
|
+
const hashCode = this._hash(key);
|
|
214
|
+
const index = this.getBucketIndex(hashCode);
|
|
215
|
+
let node = this._buckets[index] ?? null;
|
|
216
|
+
|
|
217
|
+
// Check if key already exists
|
|
218
|
+
while (node !== null) {
|
|
219
|
+
if (node.hash === hashCode && this._eq(node.key, key)) {
|
|
220
|
+
const oldValue = node.value;
|
|
221
|
+
node.value = value;
|
|
222
|
+
if (this._accessOrder) {
|
|
223
|
+
this.moveToEnd(node);
|
|
224
|
+
}
|
|
225
|
+
return oldValue;
|
|
226
|
+
}
|
|
227
|
+
node = node.next;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Key doesn't exist, add new entry
|
|
231
|
+
const newNode: LinkedNode<K, V> = {
|
|
232
|
+
key,
|
|
233
|
+
value,
|
|
234
|
+
hash: hashCode,
|
|
235
|
+
next: this._buckets[index] ?? null,
|
|
236
|
+
before: null,
|
|
237
|
+
after: null,
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
this._buckets[index] = newNode;
|
|
241
|
+
this._size++;
|
|
242
|
+
|
|
243
|
+
// Add to linked list
|
|
244
|
+
this.addToEnd(newNode);
|
|
245
|
+
|
|
246
|
+
// Resize if needed
|
|
247
|
+
if (this._size > this._buckets.length * this._loadFactor) {
|
|
248
|
+
this.resize();
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
return undefined;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
setIfAbsent(key: K, value: V): V {
|
|
255
|
+
const existing = this.getNode(key);
|
|
256
|
+
if (existing !== null) {
|
|
257
|
+
if (this._accessOrder) {
|
|
258
|
+
this.moveToEnd(existing);
|
|
259
|
+
}
|
|
260
|
+
return existing.value;
|
|
261
|
+
}
|
|
262
|
+
this.set(key, value);
|
|
263
|
+
return value;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
replace(key: K, value: V): V | undefined {
|
|
267
|
+
const node = this.getNode(key);
|
|
268
|
+
if (node === null) return undefined;
|
|
269
|
+
const oldValue = node.value;
|
|
270
|
+
node.value = value;
|
|
271
|
+
if (this._accessOrder) {
|
|
272
|
+
this.moveToEnd(node);
|
|
273
|
+
}
|
|
274
|
+
return oldValue;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
replaceEntry(key: K, oldValue: V, newValue: V): boolean {
|
|
278
|
+
const node = this.getNode(key);
|
|
279
|
+
if (node === null || !this._valueEq(node.value, oldValue)) {
|
|
280
|
+
return false;
|
|
281
|
+
}
|
|
282
|
+
node.value = newValue;
|
|
283
|
+
if (this._accessOrder) {
|
|
284
|
+
this.moveToEnd(node);
|
|
285
|
+
}
|
|
286
|
+
return true;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
delete(key: K): V | undefined {
|
|
290
|
+
const node = this.getNode(key);
|
|
291
|
+
if (node === null) return undefined;
|
|
292
|
+
|
|
293
|
+
const oldValue = node.value;
|
|
294
|
+
|
|
295
|
+
// Remove from bucket
|
|
296
|
+
this.removeFromBucket(node);
|
|
297
|
+
|
|
298
|
+
// Remove from linked list
|
|
299
|
+
this.removeFromList(node);
|
|
300
|
+
|
|
301
|
+
this._size--;
|
|
302
|
+
return oldValue;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
deleteEntry(key: K, value: V): boolean {
|
|
306
|
+
const node = this.getNode(key);
|
|
307
|
+
if (node === null || !this._valueEq(node.value, value)) {
|
|
308
|
+
return false;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// Remove from bucket
|
|
312
|
+
this.removeFromBucket(node);
|
|
313
|
+
|
|
314
|
+
// Remove from linked list
|
|
315
|
+
this.removeFromList(node);
|
|
316
|
+
|
|
317
|
+
this._size--;
|
|
318
|
+
return true;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
clear(): void {
|
|
322
|
+
this._buckets = new Array(this._buckets.length).fill(null);
|
|
323
|
+
this._size = 0;
|
|
324
|
+
this._head = null;
|
|
325
|
+
this._tail = null;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// ========================================================================
|
|
329
|
+
// Bulk operations
|
|
330
|
+
// ========================================================================
|
|
331
|
+
|
|
332
|
+
putAll(other: MapLike<K, V> | Iterable<Entry<K, V>>): void {
|
|
333
|
+
if ('entries' in other && typeof other.entries === 'function') {
|
|
334
|
+
for (const entry of other.entries()) {
|
|
335
|
+
this.set(entry.key, entry.value);
|
|
336
|
+
}
|
|
337
|
+
} else {
|
|
338
|
+
for (const entry of other as Iterable<Entry<K, V>>) {
|
|
339
|
+
this.set(entry.key, entry.value);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// ========================================================================
|
|
345
|
+
// Computed operations
|
|
346
|
+
// ========================================================================
|
|
347
|
+
|
|
348
|
+
computeIfAbsent(key: K, mappingFunction: (key: K) => V): V {
|
|
349
|
+
const existing = this.getNode(key);
|
|
350
|
+
if (existing !== null) {
|
|
351
|
+
if (this._accessOrder) {
|
|
352
|
+
this.moveToEnd(existing);
|
|
353
|
+
}
|
|
354
|
+
return existing.value;
|
|
355
|
+
}
|
|
356
|
+
const value = mappingFunction(key);
|
|
357
|
+
this.set(key, value);
|
|
358
|
+
return value;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
computeIfPresent(key: K, remappingFunction: (key: K, value: V) => V | undefined): V | undefined {
|
|
362
|
+
const existing = this.getNode(key);
|
|
363
|
+
if (existing === null) return undefined;
|
|
364
|
+
|
|
365
|
+
const newValue = remappingFunction(key, existing.value);
|
|
366
|
+
if (newValue === undefined) {
|
|
367
|
+
this.delete(key);
|
|
368
|
+
return undefined;
|
|
369
|
+
}
|
|
370
|
+
existing.value = newValue;
|
|
371
|
+
if (this._accessOrder) {
|
|
372
|
+
this.moveToEnd(existing);
|
|
373
|
+
}
|
|
374
|
+
return newValue;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
compute(key: K, remappingFunction: (key: K, value: V | undefined) => V | undefined): V | undefined {
|
|
378
|
+
const existing = this.getNode(key);
|
|
379
|
+
const newValue = remappingFunction(key, existing?.value);
|
|
380
|
+
|
|
381
|
+
if (newValue === undefined) {
|
|
382
|
+
if (existing !== null) {
|
|
383
|
+
this.delete(key);
|
|
384
|
+
}
|
|
385
|
+
return undefined;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
this.set(key, newValue);
|
|
389
|
+
return newValue;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
merge(key: K, value: V, remappingFunction: (oldValue: V, newValue: V) => V | undefined): V | undefined {
|
|
393
|
+
const existing = this.getNode(key);
|
|
394
|
+
|
|
395
|
+
if (existing === null) {
|
|
396
|
+
this.set(key, value);
|
|
397
|
+
return value;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
const merged = remappingFunction(existing.value, value);
|
|
401
|
+
if (merged === undefined) {
|
|
402
|
+
this.delete(key);
|
|
403
|
+
return undefined;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
existing.value = merged;
|
|
407
|
+
if (this._accessOrder) {
|
|
408
|
+
this.moveToEnd(existing);
|
|
409
|
+
}
|
|
410
|
+
return merged;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// ========================================================================
|
|
414
|
+
// View operations
|
|
415
|
+
// ========================================================================
|
|
416
|
+
|
|
417
|
+
*keys(): IterableIterator<K> {
|
|
418
|
+
let node = this._head;
|
|
419
|
+
while (node !== null) {
|
|
420
|
+
yield node.key;
|
|
421
|
+
node = node.after;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
*values(): IterableIterator<V> {
|
|
426
|
+
let node = this._head;
|
|
427
|
+
while (node !== null) {
|
|
428
|
+
yield node.value;
|
|
429
|
+
node = node.after;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
*entries(): IterableIterator<Entry<K, V>> {
|
|
434
|
+
let node = this._head;
|
|
435
|
+
while (node !== null) {
|
|
436
|
+
yield { key: node.key, value: node.value };
|
|
437
|
+
node = node.after;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
*[Symbol.iterator](): Iterator<Entry<K, V>> {
|
|
442
|
+
yield* this.entries();
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
forEach(action: (value: V, key: K, map: this) => void): void {
|
|
446
|
+
for (const entry of this.entries()) {
|
|
447
|
+
action(entry.value, entry.key, this);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
toArray(): Entry<K, V>[] {
|
|
452
|
+
return Array.from(this.entries());
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// ========================================================================
|
|
456
|
+
// Private helpers
|
|
457
|
+
// ========================================================================
|
|
458
|
+
|
|
459
|
+
private getNode(key: K): LinkedNode<K, V> | null {
|
|
460
|
+
const hashCode = this._hash(key);
|
|
461
|
+
const index = this.getBucketIndex(hashCode);
|
|
462
|
+
let node = this._buckets[index] ?? null;
|
|
463
|
+
|
|
464
|
+
while (node !== null) {
|
|
465
|
+
if (node.hash === hashCode && this._eq(node.key, key)) {
|
|
466
|
+
return node;
|
|
467
|
+
}
|
|
468
|
+
node = node.next;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
return null;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
private getBucketIndex(hashCode: number): number {
|
|
475
|
+
return Math.abs(hashCode) % this._buckets.length;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
private removeFromBucket(node: LinkedNode<K, V>): void {
|
|
479
|
+
const index = this.getBucketIndex(node.hash);
|
|
480
|
+
let current = this._buckets[index] ?? null;
|
|
481
|
+
let prev: LinkedNode<K, V> | null = null;
|
|
482
|
+
|
|
483
|
+
while (current !== null) {
|
|
484
|
+
if (current === node) {
|
|
485
|
+
if (prev === null) {
|
|
486
|
+
this._buckets[index] = current.next;
|
|
487
|
+
} else {
|
|
488
|
+
prev.next = current.next;
|
|
489
|
+
}
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
prev = current;
|
|
493
|
+
current = current.next;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
private addToEnd(node: LinkedNode<K, V>): void {
|
|
498
|
+
node.before = this._tail;
|
|
499
|
+
node.after = null;
|
|
500
|
+
if (this._tail === null) this._head = node;
|
|
501
|
+
else this._tail.after = node;
|
|
502
|
+
this._tail = node;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
private removeFromList(node: LinkedNode<K, V>): void {
|
|
506
|
+
if (node.before !== null) node.before.after = node.after;
|
|
507
|
+
else this._head = node.after;
|
|
508
|
+
if (node.after !== null) node.after.before = node.before;
|
|
509
|
+
else this._tail = node.before;
|
|
510
|
+
node.before = node.after = null;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
private moveToEnd(node: LinkedNode<K, V>): void {
|
|
514
|
+
if (node === this._tail) return; // Already at end
|
|
515
|
+
this.removeFromList(node);
|
|
516
|
+
this.addToEnd(node);
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
private resize(): void {
|
|
520
|
+
this._buckets = new Array(this._buckets.length * 2).fill(null);
|
|
521
|
+
// Rebuild the bucket chains (`next`) from the order list (`after`)
|
|
522
|
+
for (let node = this._head; node !== null; node = node.after) {
|
|
523
|
+
const index = this.getBucketIndex(node.hash);
|
|
524
|
+
node.next = this._buckets[index] ?? null;
|
|
525
|
+
this._buckets[index] = node;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
// ========================================================================
|
|
530
|
+
// Utility methods
|
|
531
|
+
// ========================================================================
|
|
532
|
+
|
|
533
|
+
toString(): string {
|
|
534
|
+
const entries = Array.from(this.entries())
|
|
535
|
+
.map(({ key, value }) => `${key}=${value}`)
|
|
536
|
+
.join(', ');
|
|
537
|
+
return `LinkedHashMap{${entries}}`;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* Factory function to create a LinkedHashMap.
|
|
543
|
+
*
|
|
544
|
+
* @example
|
|
545
|
+
* const map = linkedHashMap<string, number>()
|
|
546
|
+
* map.set('a', 1)
|
|
547
|
+
* map.set('b', 2)
|
|
548
|
+
*
|
|
549
|
+
* @example
|
|
550
|
+
* // With initial entries
|
|
551
|
+
* const map = linkedHashMap<string, number>([
|
|
552
|
+
* ['a', 1],
|
|
553
|
+
* ['b', 2]
|
|
554
|
+
* ])
|
|
555
|
+
*
|
|
556
|
+
* @example
|
|
557
|
+
* // LRU cache with access order
|
|
558
|
+
* const lru = linkedHashMap<string, number>({ accessOrder: true })
|
|
559
|
+
*/
|
|
560
|
+
export function linkedHashMap<K, V>(
|
|
561
|
+
optionsOrEntries?: LinkedHashMapOptions<K, V> | Iterable<[K, V]>
|
|
562
|
+
): LinkedHashMap<K, V> {
|
|
563
|
+
if (optionsOrEntries && Symbol.iterator in optionsOrEntries) {
|
|
564
|
+
const map = new LinkedHashMap<K, V>();
|
|
565
|
+
for (const [key, value] of optionsOrEntries as Iterable<[K, V]>) {
|
|
566
|
+
map.set(key, value);
|
|
567
|
+
}
|
|
568
|
+
return map;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
return new LinkedHashMap<K, V>(optionsOrEntries as LinkedHashMapOptions<K, V>);
|
|
572
|
+
}
|