@mark1russell7/collections 0.0.0-bootstrap.0 → 0.1.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/LICENSE +21 -0
- package/README.md +77 -1
- package/build/dag/index.d.ts +144 -0
- package/build/dag/index.d.ts.map +1 -0
- package/build/dag/index.js +294 -0
- package/build/dag/index.js.map +1 -0
- package/build/graph/index.d.ts +20 -0
- package/build/graph/index.d.ts.map +1 -0
- package/build/graph/index.js +48 -0
- package/build/graph/index.js.map +1 -0
- package/build/index.d.ts +4952 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +7811 -0
- package/build/index.js.map +1 -0
- package/build/testing/index.d.ts +25 -0
- package/build/testing/index.d.ts.map +1 -0
- package/build/testing/index.js +72 -0
- package/build/testing/index.js.map +1 -0
- package/package.json +71 -4
- package/src/async/async-queue.ts +481 -0
- package/src/async/channels.ts +450 -0
- package/src/async/settled.ts +41 -0
- package/src/behaviors/bounded.ts +485 -0
- package/src/behaviors/composite.ts +99 -0
- package/src/behaviors/evented.ts +464 -0
- package/src/behaviors/lru.ts +449 -0
- package/src/behaviors/readonly.ts +236 -0
- package/src/behaviors/safe.ts +358 -0
- package/src/behaviors/synchronized.ts +316 -0
- package/src/behaviors/ttl.ts +464 -0
- package/src/core/effects.ts +327 -0
- package/src/core/events.ts +363 -0
- package/src/core/middleware.ts +413 -0
- package/src/core/policies.ts +316 -0
- package/src/core/traits.ts +164 -0
- package/src/dag/builder.ts +124 -0
- package/src/dag/executor.ts +190 -0
- package/src/dag/index.ts +14 -0
- package/src/dag/traversal.ts +129 -0
- package/src/dag/types.ts +87 -0
- package/src/fx/collectors.ts +500 -0
- package/src/fx/iter.ts +536 -0
- package/src/graph/hub-order.ts +53 -0
- package/src/graph/index.ts +5 -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 +610 -0
- package/src/impl/priority-queue.ts +448 -0
- package/src/impl/summary-tree.ts +594 -0
- package/src/impl/tree-map.ts +913 -0
- package/src/impl/tree-set.ts +469 -0
- package/src/index.ts +237 -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/order/index.ts +6 -0
- package/src/order/rank.ts +271 -0
- package/src/storage/listeners.ts +72 -0
- package/src/testing/check.ts +81 -0
- package/src/testing/index.ts +7 -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,248 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Common helper utilities for collections.
|
|
3
|
+
*
|
|
4
|
+
* This module gives shared functionality to reduce code duplication across implementations.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { Eq } from "../core/traits.js";
|
|
8
|
+
|
|
9
|
+
// ============================================================================
|
|
10
|
+
// Bounds checking utilities
|
|
11
|
+
// ============================================================================
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Checks if an index is within valid bounds [0, size).
|
|
15
|
+
* This function throws RangeError if out of bounds.
|
|
16
|
+
*
|
|
17
|
+
* @param index The index to check
|
|
18
|
+
* @param size The size of the collection
|
|
19
|
+
* @param collectionName Optional name for error message
|
|
20
|
+
* @throws RangeError if index is out of bounds
|
|
21
|
+
*/
|
|
22
|
+
export function checkBounds(index: number, size: number, collectionName: string = "Collection"): void {
|
|
23
|
+
if (index < 0 || index >= size) {
|
|
24
|
+
throw new RangeError(`${collectionName}: Index ${index} out of bounds [0, ${size})`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Checks if an index is valid for insertion [0, size].
|
|
30
|
+
* In this function, index === size is correct for append operations.
|
|
31
|
+
*
|
|
32
|
+
* @param index The index to check
|
|
33
|
+
* @param size The size of the collection
|
|
34
|
+
* @param collectionName Optional name for error message
|
|
35
|
+
* @throws RangeError if index is out of bounds
|
|
36
|
+
*/
|
|
37
|
+
export function checkInsertBounds(index: number, size: number, collectionName: string = "Collection"): void {
|
|
38
|
+
if (index < 0 || index > size) {
|
|
39
|
+
throw new RangeError(`${collectionName}: Index ${index} out of bounds [0, ${size}]`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Checks if a range [start, end) is valid.
|
|
45
|
+
*
|
|
46
|
+
* @param start Start index (inclusive)
|
|
47
|
+
* @param end End index (exclusive)
|
|
48
|
+
* @param size The size of the collection
|
|
49
|
+
* @param collectionName Optional name for error message
|
|
50
|
+
* @throws RangeError if range is invalid
|
|
51
|
+
*/
|
|
52
|
+
export function checkRangeBounds(
|
|
53
|
+
start: number,
|
|
54
|
+
end: number,
|
|
55
|
+
size: number,
|
|
56
|
+
collectionName: string = "Collection"
|
|
57
|
+
): void {
|
|
58
|
+
if (start < 0 || end > size || start > end) {
|
|
59
|
+
throw new RangeError(
|
|
60
|
+
`${collectionName}: Invalid range [${start}, ${end}) for size ${size}`
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// ============================================================================
|
|
66
|
+
// Equality and search utilities
|
|
67
|
+
// ============================================================================
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* This function finds the index of an element in an array using custom equality.
|
|
71
|
+
*
|
|
72
|
+
* @param array The array to search
|
|
73
|
+
* @param element The element to find
|
|
74
|
+
* @param eq Equality function
|
|
75
|
+
* @returns Index of element, or -1 if not found
|
|
76
|
+
*/
|
|
77
|
+
export function findIndex<T>(array: T[], element: T, eq: Eq<T>): number {
|
|
78
|
+
for (let i = 0; i < array.length; i++) {
|
|
79
|
+
if (eq(array[i]!, element)) {
|
|
80
|
+
return i;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return -1;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* This function finds the last index of an element in an array using custom equality.
|
|
88
|
+
*
|
|
89
|
+
* @param array The array to search
|
|
90
|
+
* @param element The element to find
|
|
91
|
+
* @param eq Equality function
|
|
92
|
+
* @returns Last index of element, or -1 if not found
|
|
93
|
+
*/
|
|
94
|
+
export function findLastIndex<T>(array: T[], element: T, eq: Eq<T>): number {
|
|
95
|
+
for (let i = array.length - 1; i >= 0; i--) {
|
|
96
|
+
if (eq(array[i]!, element)) {
|
|
97
|
+
return i;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return -1;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Checks if an array contains an element using custom equality.
|
|
105
|
+
*
|
|
106
|
+
* @param array The array to search
|
|
107
|
+
* @param element The element to find
|
|
108
|
+
* @param eq Equality function
|
|
109
|
+
* @returns true if element is found
|
|
110
|
+
*/
|
|
111
|
+
export function arrayContains<T>(array: T[], element: T, eq: Eq<T>): boolean {
|
|
112
|
+
return findIndex(array, element, eq) !== -1;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* This function removes the first occurrence of an element from an array.
|
|
117
|
+
*
|
|
118
|
+
* @param array The array to change
|
|
119
|
+
* @param element The element to remove
|
|
120
|
+
* @param eq Equality function
|
|
121
|
+
* @returns true if element was found and removed
|
|
122
|
+
*/
|
|
123
|
+
export function arrayRemove<T>(array: T[], element: T, eq: Eq<T>): boolean {
|
|
124
|
+
const index = findIndex(array, element, eq);
|
|
125
|
+
if (index === -1) return false;
|
|
126
|
+
array.splice(index, 1);
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* This function removes all occurrences of an element from an array.
|
|
132
|
+
*
|
|
133
|
+
* @param array The array to change
|
|
134
|
+
* @param element The element to remove
|
|
135
|
+
* @param eq Equality function
|
|
136
|
+
* @returns Number of elements removed
|
|
137
|
+
*/
|
|
138
|
+
export function arrayRemoveAll<T>(array: T[], element: T, eq: Eq<T>): number {
|
|
139
|
+
let count = 0;
|
|
140
|
+
for (let i = array.length - 1; i >= 0; i--) {
|
|
141
|
+
if (eq(array[i]!, element)) {
|
|
142
|
+
array.splice(i, 1);
|
|
143
|
+
count++;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return count;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// ============================================================================
|
|
150
|
+
// Iterator utilities
|
|
151
|
+
// ============================================================================
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* This function makes a simple iterator for an array.
|
|
155
|
+
*
|
|
156
|
+
* @param array The array to iterate
|
|
157
|
+
* @returns Iterator over the array
|
|
158
|
+
*/
|
|
159
|
+
export function* arrayIterator<T>(array: T[]): Iterator<T> {
|
|
160
|
+
yield* array;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* This function makes a reversed iterator for an array.
|
|
165
|
+
*
|
|
166
|
+
* @param array The array to iterate
|
|
167
|
+
* @returns Reversed iterator over the array
|
|
168
|
+
*/
|
|
169
|
+
export function* reverseArrayIterator<T>(array: T[]): Iterator<T> {
|
|
170
|
+
for (let i = array.length - 1; i >= 0; i--) {
|
|
171
|
+
yield array[i]!;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* This function converts an iterable to an array.
|
|
177
|
+
*
|
|
178
|
+
* @param iterable The iterable to convert
|
|
179
|
+
* @returns Array containing all elements
|
|
180
|
+
*/
|
|
181
|
+
export function toArray<T>(iterable: Iterable<T>): T[] {
|
|
182
|
+
return Array.from(iterable);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// ============================================================================
|
|
186
|
+
// Validation utilities
|
|
187
|
+
// ============================================================================
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* This function makes sure that a value is not null or undefined.
|
|
191
|
+
*
|
|
192
|
+
* @param value The value to check
|
|
193
|
+
* @param message Error message
|
|
194
|
+
* @returns The non-null value
|
|
195
|
+
* @throws Error if value is null or undefined
|
|
196
|
+
*/
|
|
197
|
+
export function requireNonNull<T>(value: T | null | undefined, message: string = "Value cannot be null or undefined"): T {
|
|
198
|
+
if (value === null || value === undefined) {
|
|
199
|
+
throw new Error(message);
|
|
200
|
+
}
|
|
201
|
+
return value;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* This function makes sure that a collection is not empty.
|
|
206
|
+
*
|
|
207
|
+
* @param size Current size
|
|
208
|
+
* @param collectionName Optional name for error message
|
|
209
|
+
* @throws Error if collection is empty
|
|
210
|
+
*/
|
|
211
|
+
export function requireNonEmpty(size: number, collectionName: string = "Collection"): void {
|
|
212
|
+
if (size === 0) {
|
|
213
|
+
throw new Error(`${collectionName} is empty`);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// ============================================================================
|
|
218
|
+
// Math utilities
|
|
219
|
+
// ============================================================================
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* This function clamps a value between min and max.
|
|
223
|
+
*
|
|
224
|
+
* @param value The value to clamp
|
|
225
|
+
* @param min Minimum value
|
|
226
|
+
* @param max Maximum value
|
|
227
|
+
* @returns Clamped value
|
|
228
|
+
*/
|
|
229
|
+
export function clamp(value: number, min: number, max: number): number {
|
|
230
|
+
return Math.max(min, Math.min(max, value));
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* This function calculates hash code for a string.
|
|
235
|
+
* This function uses Java's String.hashCode() algorithm.
|
|
236
|
+
*
|
|
237
|
+
* @param str The string to hash
|
|
238
|
+
* @returns Hash code
|
|
239
|
+
*/
|
|
240
|
+
export function stringHashCode(str: string): number {
|
|
241
|
+
let hash = 0;
|
|
242
|
+
for (let i = 0; i < str.length; i++) {
|
|
243
|
+
const char = str.charCodeAt(i);
|
|
244
|
+
hash = ((hash << 5) - hash) + char;
|
|
245
|
+
hash = hash & hash; // Convert to 32-bit integer
|
|
246
|
+
}
|
|
247
|
+
return hash;
|
|
248
|
+
}
|