@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.
Files changed (68) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +77 -1
  3. package/build/dag/index.d.ts +144 -0
  4. package/build/dag/index.d.ts.map +1 -0
  5. package/build/dag/index.js +294 -0
  6. package/build/dag/index.js.map +1 -0
  7. package/build/graph/index.d.ts +20 -0
  8. package/build/graph/index.d.ts.map +1 -0
  9. package/build/graph/index.js +48 -0
  10. package/build/graph/index.js.map +1 -0
  11. package/build/index.d.ts +4952 -0
  12. package/build/index.d.ts.map +1 -0
  13. package/build/index.js +7811 -0
  14. package/build/index.js.map +1 -0
  15. package/build/testing/index.d.ts +25 -0
  16. package/build/testing/index.d.ts.map +1 -0
  17. package/build/testing/index.js +72 -0
  18. package/build/testing/index.js.map +1 -0
  19. package/package.json +71 -4
  20. package/src/async/async-queue.ts +481 -0
  21. package/src/async/channels.ts +450 -0
  22. package/src/async/settled.ts +41 -0
  23. package/src/behaviors/bounded.ts +485 -0
  24. package/src/behaviors/composite.ts +99 -0
  25. package/src/behaviors/evented.ts +464 -0
  26. package/src/behaviors/lru.ts +449 -0
  27. package/src/behaviors/readonly.ts +236 -0
  28. package/src/behaviors/safe.ts +358 -0
  29. package/src/behaviors/synchronized.ts +316 -0
  30. package/src/behaviors/ttl.ts +464 -0
  31. package/src/core/effects.ts +327 -0
  32. package/src/core/events.ts +363 -0
  33. package/src/core/middleware.ts +413 -0
  34. package/src/core/policies.ts +316 -0
  35. package/src/core/traits.ts +164 -0
  36. package/src/dag/builder.ts +124 -0
  37. package/src/dag/executor.ts +190 -0
  38. package/src/dag/index.ts +14 -0
  39. package/src/dag/traversal.ts +129 -0
  40. package/src/dag/types.ts +87 -0
  41. package/src/fx/collectors.ts +500 -0
  42. package/src/fx/iter.ts +536 -0
  43. package/src/graph/hub-order.ts +53 -0
  44. package/src/graph/index.ts +5 -0
  45. package/src/impl/array-deque.ts +463 -0
  46. package/src/impl/array-list.ts +490 -0
  47. package/src/impl/hash-map.ts +623 -0
  48. package/src/impl/hash-set.ts +342 -0
  49. package/src/impl/linked-hash-map.ts +572 -0
  50. package/src/impl/linked-list.ts +610 -0
  51. package/src/impl/priority-queue.ts +448 -0
  52. package/src/impl/summary-tree.ts +594 -0
  53. package/src/impl/tree-map.ts +913 -0
  54. package/src/impl/tree-set.ts +469 -0
  55. package/src/index.ts +237 -0
  56. package/src/interfaces/collection.ts +141 -0
  57. package/src/interfaces/list.ts +281 -0
  58. package/src/interfaces/map.ts +405 -0
  59. package/src/interfaces/queue.ts +353 -0
  60. package/src/interfaces/set.ts +158 -0
  61. package/src/order/index.ts +6 -0
  62. package/src/order/rank.ts +271 -0
  63. package/src/storage/listeners.ts +72 -0
  64. package/src/testing/check.ts +81 -0
  65. package/src/testing/index.ts +7 -0
  66. package/src/utils/defaults.ts +345 -0
  67. package/src/utils/factories.ts +357 -0
  68. package/src/utils/helpers.ts +248 -0
@@ -0,0 +1,141 @@
1
+ /**
2
+ * Base collection interfaces that mirror Java Collections Framework.
3
+ *
4
+ * These form the root of the collection hierarchy and define the
5
+ * fundamental operations common to all collections.
6
+ */
7
+
8
+ import type { Eq } from "../core/traits.js";
9
+
10
+ /**
11
+ * ReadonlyCollection<T> - The root interface for readonly collections.
12
+ *
13
+ * Mirrors java.util.Collection but without mutation operations.
14
+ * All collections must extend this or a more specific readonly interface.
15
+ */
16
+ export interface ReadonlyCollection<T> extends Iterable<T> {
17
+ /**
18
+ * This value is the number of elements in this collection.
19
+ */
20
+ readonly size: number;
21
+
22
+ /**
23
+ * This value is true if this collection contains no elements.
24
+ */
25
+ readonly isEmpty: boolean;
26
+
27
+ /**
28
+ * This method gives true if this collection contains the specified element.
29
+ * This method uses the collection's equality function to compare elements.
30
+ *
31
+ * @param element The element to check for
32
+ */
33
+ contains(element: T): boolean;
34
+
35
+ /**
36
+ * This method gives true if this collection contains all elements in the specified collection.
37
+ *
38
+ * @param other The collection whose elements to check for
39
+ */
40
+ containsAll(other: Iterable<T>): boolean;
41
+
42
+ /**
43
+ * This method gives an array containing all elements in this collection.
44
+ * The given array is safe to change (it's a copy).
45
+ */
46
+ toArray(): T[];
47
+
48
+ /**
49
+ * This method does the given action for each element.
50
+ *
51
+ * @param action The action to do for each element
52
+ */
53
+ forEach(action: (element: T, index: number) => void): void;
54
+
55
+ /**
56
+ * This value is the equality function used by this collection.
57
+ */
58
+ readonly eq: Eq<T>;
59
+ }
60
+
61
+ /**
62
+ * Collection<T> - The root interface for mutable collections.
63
+ *
64
+ * Mirrors java.util.Collection with mutation operations.
65
+ * All mutable collections must extend this or a more specific interface.
66
+ */
67
+ export interface Collection<T> extends ReadonlyCollection<T> {
68
+ /**
69
+ * This method adds an element to this collection.
70
+ *
71
+ * This method gives true if the collection was changed as a result of this call.
72
+ * Some collections (like Set) can give false if the element already exists.
73
+ *
74
+ * @param element The element to add
75
+ * @returns true if the collection changed
76
+ * @throws Error if collection is full (bounded) and policy is 'throw'
77
+ */
78
+ add(element: T): boolean;
79
+
80
+ /**
81
+ * This method adds all elements from the specified collection to this collection.
82
+ *
83
+ * @param other The collection whose elements to add
84
+ * @returns true if the collection changed
85
+ */
86
+ addAll(other: Iterable<T>): boolean;
87
+
88
+ /**
89
+ * This method removes a single instance of the specified element from this collection.
90
+ *
91
+ * This method gives true if an element was removed as a result of this call.
92
+ *
93
+ * @param element The element to remove
94
+ * @returns true if an element was removed
95
+ */
96
+ remove(element: T): boolean;
97
+
98
+ /**
99
+ * This method removes all elements from this collection that are also in the specified collection.
100
+ *
101
+ * @param other The collection whose elements to remove
102
+ * @returns true if the collection changed
103
+ */
104
+ removeAll(other: Iterable<T>): boolean;
105
+
106
+ /**
107
+ * This method retains only the elements in this collection that are in the specified collection.
108
+ * In other words, removes all elements not in the specified collection.
109
+ *
110
+ * @param other The collection of elements to retain
111
+ * @returns true if the collection changed
112
+ */
113
+ retainAll(other: Iterable<T>): boolean;
114
+
115
+ /**
116
+ * This method removes all elements from this collection.
117
+ * The collection will be empty after this call gives.
118
+ */
119
+ clear(): void;
120
+ }
121
+
122
+ /**
123
+ * Utility type to check if a collection is readonly.
124
+ */
125
+ export type IsReadonly<C> = C extends ReadonlyCollection<any>
126
+ ? C extends Collection<any>
127
+ ? false
128
+ : true
129
+ : false;
130
+
131
+ /**
132
+ * Extract element type from a collection.
133
+ */
134
+ export type ElementOf<C> = C extends ReadonlyCollection<infer T> ? T : never;
135
+
136
+ /**
137
+ * Make a collection readonly by removing mutation methods.
138
+ */
139
+ export type Readonly<C extends Collection<any>> = C extends Collection<infer T>
140
+ ? ReadonlyCollection<T>
141
+ : never;
@@ -0,0 +1,281 @@
1
+ /**
2
+ * List interfaces that mirror Java's List interface.
3
+ *
4
+ * Lists are ordered collections that can hold duplicate elements
5
+ * and give positional (indexed) access to elements.
6
+ */
7
+
8
+ import type { Collection, ReadonlyCollection } from "./collection.js";
9
+ import type { Compare } from "../core/traits.js";
10
+
11
+ /**
12
+ * ReadonlyList<T> - Readonly ordered collection with indexed access.
13
+ *
14
+ * Mirrors java.util.List but without mutation operations.
15
+ * Elements maintain their insertion order and can be accessed by index.
16
+ */
17
+ export interface ReadonlyList<T> extends ReadonlyCollection<T> {
18
+ /**
19
+ * This method gives the element at the specified position.
20
+ *
21
+ * @param index The index of the element to give (0-based)
22
+ * @returns The element at the specified position
23
+ * @throws RangeError if index is out of bounds
24
+ */
25
+ get(index: number): T;
26
+
27
+ /**
28
+ * This method gives the index of the first occurrence of the specified element,
29
+ * or -1 if not found.
30
+ *
31
+ * @param element The element to search for
32
+ * @param fromIndex Optional starting index for the search
33
+ */
34
+ indexOf(element: T, fromIndex?: number): number;
35
+
36
+ /**
37
+ * This method gives the index of the last occurrence of the specified element,
38
+ * or -1 if not found.
39
+ *
40
+ * @param element The element to search for
41
+ * @param fromIndex Optional starting index for backwards search
42
+ */
43
+ lastIndexOf(element: T, fromIndex?: number): number;
44
+
45
+ /**
46
+ * This method gives a view of the portion of this list between fromIndex (inclusive)
47
+ * and toIndex (exclusive).
48
+ *
49
+ * Changes to the sublist are reflected in the original list.
50
+ *
51
+ * @param fromIndex Start index (inclusive)
52
+ * @param toIndex End index (exclusive)
53
+ */
54
+ subList(fromIndex: number, toIndex: number): ReadonlyList<T>;
55
+
56
+ /**
57
+ * This method gives the first element in the list.
58
+ *
59
+ * @throws Error if list is empty
60
+ */
61
+ first(): T;
62
+
63
+ /**
64
+ * This method gives the last element in the list.
65
+ *
66
+ * @throws Error if list is empty
67
+ */
68
+ last(): T;
69
+ }
70
+
71
+ /**
72
+ * List<T> - Mutable ordered collection with indexed access.
73
+ *
74
+ * Mirrors java.util.List with full mutation capabilities.
75
+ * This type gives methods to insert, replace, and remove elements by index.
76
+ */
77
+ export interface List<T> extends ReadonlyList<T>, Collection<T> {
78
+ /**
79
+ * This method replaces the element at the specified position.
80
+ *
81
+ * @param index The index of the element to replace
82
+ * @param element The new element
83
+ * @returns The element previously at the specified position
84
+ * @throws RangeError if index is out of bounds
85
+ */
86
+ set(index: number, element: T): T;
87
+
88
+ /**
89
+ * This method inserts an element at the specified position.
90
+ * Shifts elements at and after the position to the right.
91
+ *
92
+ * @param index The index at which to insert
93
+ * @param element The element to insert
94
+ * @throws RangeError if index is out of bounds (0 to size inclusive)
95
+ */
96
+ insert(index: number, element: T): void;
97
+
98
+ /**
99
+ * This method inserts all elements from the specified collection at the specified position.
100
+ *
101
+ * @param index The index at which to insert
102
+ * @param other The collection whose elements to insert
103
+ * @returns true if the list changed
104
+ */
105
+ insertAll(index: number, other: Iterable<T>): boolean;
106
+
107
+ /**
108
+ * This method removes the element at the specified position.
109
+ * Shifts elements after the position to the left.
110
+ *
111
+ * @param index The index of the element to remove
112
+ * @returns The element that was removed
113
+ * @throws RangeError if index is out of bounds
114
+ */
115
+ removeAt(index: number): T;
116
+
117
+ /**
118
+ * This method removes elements in the specified index range.
119
+ *
120
+ * @param fromIndex Start index (inclusive)
121
+ * @param toIndex End index (exclusive)
122
+ */
123
+ removeRange(fromIndex: number, toIndex: number): void;
124
+
125
+ /**
126
+ * This method sorts this list according to the specified comparator.
127
+ *
128
+ * @param compare The comparison function
129
+ */
130
+ sort(compare?: Compare<T>): void;
131
+
132
+ /**
133
+ * This method reverses the order of elements in this list.
134
+ */
135
+ reverse(): void;
136
+
137
+ /**
138
+ * This method gives a mutable sublist view.
139
+ *
140
+ * @param fromIndex Start index (inclusive)
141
+ * @param toIndex End index (exclusive)
142
+ */
143
+ subList(fromIndex: number, toIndex: number): List<T>;
144
+
145
+ // Stack-like operations
146
+
147
+ /**
148
+ * This method appends an element to the end of this list.
149
+ * Equivalent to add() but doesn't give boolean.
150
+ *
151
+ * @param element The element to append
152
+ */
153
+ push(element: T): void;
154
+
155
+ /**
156
+ * Removes and gives the last element in this list.
157
+ *
158
+ * @returns The last element
159
+ * @throws Error if list is empty
160
+ */
161
+ pop(): T;
162
+
163
+ /**
164
+ * Removes and gives the first element in this list.
165
+ *
166
+ * @returns The first element
167
+ * @throws Error if list is empty
168
+ */
169
+ shift(): T;
170
+
171
+ /**
172
+ * This method prepends an element to the beginning of this list.
173
+ *
174
+ * @param element The element to prepend
175
+ */
176
+ unshift(element: T): void;
177
+ }
178
+
179
+ /**
180
+ * RandomAccess - Marker interface for lists with O(1) indexed access.
181
+ *
182
+ * This type shows that the list gives fast (constant-time) random access.
183
+ * Examples: ArrayList (yes), LinkedList (no).
184
+ */
185
+ export interface RandomAccess {
186
+ /**
187
+ * This marker property shows random access capability.
188
+ * Always true for collections implementing this interface.
189
+ */
190
+ readonly supportsRandomAccess: true;
191
+ }
192
+
193
+ /**
194
+ * Stack<T> - LIFO (Last-In-First-Out) collection.
195
+ *
196
+ * A stack of elements with push/pop operations.
197
+ * A specialized list or a standalone class can implement this type.
198
+ */
199
+ export interface Stack<T> extends Iterable<T> {
200
+ /** Number of elements */
201
+ readonly size: number;
202
+
203
+ /** True if empty */
204
+ readonly isEmpty: boolean;
205
+
206
+ /**
207
+ * This method pushes an element onto the stack.
208
+ */
209
+ push(element: T): void;
210
+
211
+ /**
212
+ * Removes and gives the top element.
213
+ *
214
+ * @throws Error if stack is empty
215
+ */
216
+ pop(): T;
217
+
218
+ /**
219
+ * This method gives the top element without removing it.
220
+ *
221
+ * @throws Error if stack is empty
222
+ */
223
+ peek(): T;
224
+
225
+ /**
226
+ * This method removes all elements.
227
+ */
228
+ clear(): void;
229
+
230
+ /**
231
+ * This method searches for an element and gives its distance from the top.
232
+ * This method gives -1 if not found.
233
+ */
234
+ search(element: T): number;
235
+ }
236
+
237
+ /**
238
+ * Vector<T> - Synchronized growable array.
239
+ *
240
+ * Like ArrayList but with synchronized access.
241
+ * This interface is a legacy of Java. Use compose(synchronized())(arrayList()) instead.
242
+ */
243
+ export interface Vector<T> extends List<T> {
244
+ /**
245
+ * Current capacity of the underlying array.
246
+ */
247
+ readonly capacity: number;
248
+
249
+ /**
250
+ * Trims capacity to current size to minimize memory usage.
251
+ */
252
+ trimToSize(): void;
253
+
254
+ /**
255
+ * This method increases capacity to make sure that it can hold at least the specified number of elements.
256
+ */
257
+ ensureCapacity(minCapacity: number): void;
258
+ }
259
+
260
+ /**
261
+ * CircularList<T> - List with circular/ring buffer behavior.
262
+ *
263
+ * When capacity is reached, oldest elements are overwritten.
264
+ * Useful for fixed-size buffers like logs or history.
265
+ */
266
+ export interface CircularList<T> extends List<T> {
267
+ /**
268
+ * Maximum capacity of the circular list.
269
+ */
270
+ readonly capacity: number;
271
+
272
+ /**
273
+ * This value is true if the list has reached capacity.
274
+ */
275
+ readonly isFull: boolean;
276
+
277
+ /**
278
+ * Number of elements that have been overwritten.
279
+ */
280
+ readonly overwriteCount: number;
281
+ }