@creejs/commons-collection 2.0.4 → 2.0.5

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.
@@ -0,0 +1,59 @@
1
+ /**
2
+ * A fixed-capacity linked list that automatically removes elements when capacity is exceeded.
3
+ * When adding to the end (addLast), it removes the first element if capacity is full.
4
+ * When adding to the beginning (addFirst), it removes the last element if capacity is full.
5
+ *
6
+ * @class FixedLinkedList
7
+ * @extends LinkedList
8
+ */
9
+ export default class FixedLinkedList extends LinkedList {
10
+ /**
11
+ * Creates a new FixedLinkedList instance with a fixed capacity.
12
+ * @constructor
13
+ * @param {number} capacity - The maximum number of elements the list can hold (must be > 0)
14
+ * @throws {Error} If capacity is less than or equal to 0
15
+ */
16
+ constructor(capacity: number);
17
+ capacity: number;
18
+ /**
19
+ * Adds a value to the beginning of the linked list.
20
+ * If the list is at capacity, removes the last element before adding.
21
+ * @param {*} value - The value to add
22
+ * @returns {FixedLinkedList} This linked list instance for chaining
23
+ */
24
+ addFirst(value: any): FixedLinkedList;
25
+ /**
26
+ * Adds a value to the end of the linked list.
27
+ * If the list is at capacity, removes the first element before adding.
28
+ * @param {*} value - The value to add
29
+ * @returns {FixedLinkedList} This linked list instance for chaining
30
+ */
31
+ addLast(value: any): FixedLinkedList;
32
+ /**
33
+ * Adds a value to the end of the linked list.
34
+ * If the list is at capacity, removes the first element before adding.
35
+ * @param {*} value - The value to add
36
+ * @returns {FixedLinkedList} This linked list instance for chaining
37
+ */
38
+ add(value: any): FixedLinkedList;
39
+ /**
40
+ * Inserts a value at the specified index.
41
+ * If the list is at capacity and index is not at the end, removes the first element before adding.
42
+ * If the list is at capacity and index is at the end, removes the first element before adding.
43
+ * @param {number} index - The index at which to insert the value
44
+ * @param {*} value - The value to insert
45
+ * @returns {FixedLinkedList} This linked list instance for chaining
46
+ */
47
+ insertAt(index: number, value: any): FixedLinkedList;
48
+ /**
49
+ * Returns the remaining capacity of the list.
50
+ * @returns {number} The number of additional elements that can be added
51
+ */
52
+ get remainingCapacity(): number;
53
+ /**
54
+ * Checks if the list is at full capacity.
55
+ * @returns {boolean} True if the list is full, false otherwise
56
+ */
57
+ get isFull(): boolean;
58
+ }
59
+ import LinkedList from './linked-list.js';
package/types/index.d.ts CHANGED
@@ -2,9 +2,13 @@ declare namespace _default {
2
2
  export { CappedSet };
3
3
  export { TimeWheelCache };
4
4
  export { Hour24TimeWheelCache };
5
+ export { LinkedList };
6
+ export { FixedLinkedList };
5
7
  }
6
8
  export default _default;
7
9
  import CappedSet from './capped-set.js';
8
10
  import TimeWheelCache from './time-wheel-cache.js';
9
11
  import Hour24TimeWheelCache from './hour24-time-wheel-cache.js';
10
- export { CappedSet, TimeWheelCache, Hour24TimeWheelCache };
12
+ import LinkedList from './linked-list.js';
13
+ import FixedLinkedList from './fixed-linked-list.js';
14
+ export { CappedSet, TimeWheelCache, Hour24TimeWheelCache, LinkedList, FixedLinkedList };
@@ -0,0 +1,178 @@
1
+ /**
2
+ * A doubly linked list implementation.
3
+ *
4
+ * @class LinkedList
5
+ */
6
+ export default class LinkedList {
7
+ /**
8
+ * @type {Node|undefined}
9
+ */
10
+ _head: Node | undefined;
11
+ /**
12
+ * @type {Node|undefined}
13
+ */
14
+ _tail: Node | undefined;
15
+ /**
16
+ * @type {number}
17
+ */
18
+ _size: number;
19
+ get size(): number;
20
+ /**
21
+ * get the first element
22
+ */
23
+ get first(): any;
24
+ /**
25
+ * get the last element
26
+ */
27
+ get last(): any;
28
+ /**
29
+ * Adds a value to the beginning of the linked list.
30
+ * @param {*} value - The value to add
31
+ * @returns {LinkedList} This linked list instance for chaining
32
+ */
33
+ addFirst(value: any): LinkedList;
34
+ /**
35
+ * Adds a value to the end of the linked list.
36
+ * @param {*} value - The value to add
37
+ * @returns {LinkedList} This linked list instance for chaining
38
+ */
39
+ addLast(value: any): LinkedList;
40
+ /**
41
+ * Adds a value to the end of the linked list.
42
+ * @param {*} value - The value to add
43
+ * @returns {LinkedList} This linked list instance for chaining
44
+ */
45
+ add(value: any): LinkedList;
46
+ /**
47
+ * Removes and returns the first element from the linked list.
48
+ * @returns {*} The removed value, or undefined if the list is empty
49
+ */
50
+ removeFirst(): any;
51
+ /**
52
+ * Removes and returns the last element from the linked list.
53
+ * @returns {*} The removed value, or undefined if the list is empty
54
+ */
55
+ removeLast(): any;
56
+ /**
57
+ * Removes the first occurrence of the specified value from the linked list.
58
+ * @param {*} value - The value to remove
59
+ * @param {function(*, *): boolean} [comparator] - Optional comparison function (a, b) => boolean
60
+ * @returns {boolean} True if the value was found and removed, false otherwise
61
+ */
62
+ remove(value: any, comparator?: (arg0: any, arg1: any) => boolean): boolean;
63
+ /**
64
+ * Removes all occurrences of the specified value from the linked list.
65
+ * @param {*} value - The value to remove
66
+ * @param {function(*, *): boolean} [comparator] - Optional comparison function (a, b) => boolean
67
+ * @returns {number} The number of elements removed
68
+ */
69
+ removeAll(value: any, comparator?: (arg0: any, arg1: any) => boolean): number;
70
+ /**
71
+ * Removes the element at the specified index.
72
+ * @param {number} index - The index of the element to remove
73
+ * @returns {*} The removed value, or undefined if index is out of bounds
74
+ */
75
+ removeAt(index: number): any;
76
+ /**
77
+ * Checks if the linked list contains the specified value.
78
+ * @param {*} value - The value to check for
79
+ * @param {function(*, *): boolean} [comparator] - Optional comparison function (a, b) => boolean
80
+ * @returns {boolean} True if the value exists, false otherwise
81
+ */
82
+ contains(value: any, comparator?: (arg0: any, arg1: any) => boolean): boolean;
83
+ /**
84
+ * Returns the index of the first occurrence of the specified value.
85
+ * @param {*} value - The value to find
86
+ * @param {function(*, *): boolean} [comparator] - Optional comparison function (a, b) => boolean
87
+ * @returns {number} The index of the value, or -1 if not found
88
+ */
89
+ indexOf(value: any, comparator?: (arg0: any, arg1: any) => boolean): number;
90
+ /**
91
+ * Returns the index of the last occurrence of the specified value.
92
+ * @param {*} value - The value to find
93
+ * @param {function(*, *): boolean} [comparator] - Optional comparison function (a, b) => boolean
94
+ * @returns {number} The index of the last occurrence, or -1 if not found
95
+ */
96
+ indexLastOf(value: any, comparator?: (arg0: any, arg1: any) => boolean): number;
97
+ /**
98
+ * Gets the value at the specified index.
99
+ * @param {number} index - The index of the value to get
100
+ * @returns {*} The value at the index, or undefined if index is out of bounds
101
+ */
102
+ get(index: number): any;
103
+ /**
104
+ * Inserts a value at the specified index.
105
+ * @param {number} index - The index at which to insert the value
106
+ * @param {*} value - The value to insert
107
+ * @returns {LinkedList} This linked list instance for chaining
108
+ */
109
+ insertAt(index: number, value: any): LinkedList;
110
+ /**
111
+ * Removes all elements from the linked list.
112
+ */
113
+ clear(): void;
114
+ /**
115
+ * Returns an array containing all the values in the linked list.
116
+ * @returns {any[]} An array of all values
117
+ */
118
+ toArray(): any[];
119
+ /**
120
+ * Returns an iterator of the values in the linked list.
121
+ * @returns {Iterator<any>} An iterator object that yields the values of the linked list
122
+ */
123
+ values(): Iterator<any>;
124
+ /**
125
+ * Returns a slice of the linked list as an array.
126
+ * @param {number} [start=0] - The start index (inclusive). If negative, counts from the end.
127
+ * @param {number} [end=this.size] - The end index (exclusive). If negative, counts from the end.
128
+ * @returns {any[]} An array containing the sliced elements
129
+ */
130
+ slice(start?: number, end?: number): any[];
131
+ /**
132
+ * Returns a string representation of the FixedLinkedList.
133
+ * @returns {string} String representation
134
+ */
135
+ toString(): string;
136
+ /**
137
+ * Slice implementation for positive indices (iterates from head to tail)
138
+ * @param {number} start - Normalized start index (inclusive)
139
+ * @param {number} end - Normalized end index (exclusive)
140
+ * @returns {any[]} An array containing the sliced elements
141
+ * @private
142
+ */
143
+ private _slicePositive;
144
+ /**
145
+ * Slice implementation for negative indices (iterates from tail to head)
146
+ * @param {number} start - Normalized start index (inclusive)
147
+ * @param {number} end - Normalized end index (exclusive)
148
+ * @returns {any[]} An array containing the sliced elements
149
+ * @private
150
+ */
151
+ private _sliceNegative;
152
+ /**
153
+ * Removes a node from the linked list.
154
+ * @param {Node} node - The node to be removed
155
+ * @private
156
+ */
157
+ private _removeNode;
158
+ /**
159
+ * Default comparator for strict equality comparison.
160
+ * @param {*} a - First value to compare
161
+ * @param {*} b - Second value to compare
162
+ * @returns {boolean} True if values are strictly equal
163
+ * @private
164
+ */
165
+ private _defaultComparator;
166
+ [Symbol.iterator](): {
167
+ next(): {
168
+ value: any;
169
+ done: boolean;
170
+ };
171
+ [Symbol.iterator](): /*elided*/ any;
172
+ };
173
+ }
174
+ export type Node = {
175
+ value: any;
176
+ prev: Node | undefined;
177
+ next: Node | undefined;
178
+ };