@creejs/commons-collection 2.0.3 → 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.
- package/dist/cjs/index-dev.cjs +656 -12
- package/dist/cjs/index-dev.cjs.map +1 -1
- package/dist/cjs/index-min.cjs +1 -1
- package/dist/cjs/index-min.cjs.map +1 -1
- package/dist/esm/index-dev.js +655 -13
- package/dist/esm/index-dev.js.map +1 -1
- package/dist/esm/index-min.js +1 -1
- package/dist/esm/index-min.js.map +1 -1
- package/dist/umd/index.dev.js +656 -12
- package/dist/umd/index.dev.js.map +1 -1
- package/dist/umd/index.min.js +1 -1
- package/dist/umd/index.min.js.map +1 -1
- package/package.json +1 -1
- package/types/fixed-linked-list.d.ts +59 -0
- package/types/hour24-time-wheel-cache.d.ts +6 -0
- package/types/index.d.ts +5 -1
- package/types/linked-list.d.ts +178 -0
- package/types/time-wheel-cache.d.ts +10 -3
|
@@ -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';
|
|
@@ -12,6 +12,11 @@ export default class Hour24TimeWheelCache extends EventEmitter {
|
|
|
12
12
|
Downgrade: string;
|
|
13
13
|
Expired: string;
|
|
14
14
|
};
|
|
15
|
+
/**
|
|
16
|
+
*
|
|
17
|
+
*/
|
|
18
|
+
constructor(autoStart?: boolean);
|
|
19
|
+
_autoStart: boolean;
|
|
15
20
|
/**
|
|
16
21
|
* Second Wheel:
|
|
17
22
|
* 1. 1 Tick Mark is 1 Second, is 1000 Milliseconds
|
|
@@ -53,6 +58,7 @@ export default class Hour24TimeWheelCache extends EventEmitter {
|
|
|
53
58
|
*/
|
|
54
59
|
get maxTtl(): number;
|
|
55
60
|
get autoEvictRunning(): boolean;
|
|
61
|
+
get autoStart(): boolean;
|
|
56
62
|
_init(): void;
|
|
57
63
|
start(): void;
|
|
58
64
|
stop(): void;
|
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
|
-
|
|
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
|
+
};
|
|
@@ -15,7 +15,7 @@ export default class TimeWheelCache extends EventEmitter {
|
|
|
15
15
|
Advance: string;
|
|
16
16
|
Expired: string;
|
|
17
17
|
};
|
|
18
|
-
options:
|
|
18
|
+
options: TimeWheelCacheOptions;
|
|
19
19
|
/**
|
|
20
20
|
* How many milliseconds does one Tick represent
|
|
21
21
|
*/
|
|
@@ -24,6 +24,11 @@ export default class TimeWheelCache extends EventEmitter {
|
|
|
24
24
|
* How many Ticks does the Wheel have
|
|
25
25
|
*/
|
|
26
26
|
_tickCount: number;
|
|
27
|
+
/**
|
|
28
|
+
* Whether auto start "AutoEvict"
|
|
29
|
+
* @type {boolean}
|
|
30
|
+
*/
|
|
31
|
+
_autoStart: boolean;
|
|
27
32
|
/**
|
|
28
33
|
* Slots, one Tick owns one Slot to store key and expire timestamp
|
|
29
34
|
* @type {Map<any, {value: any, slotIndex: number, expireTimestamp: number}>[]}
|
|
@@ -55,6 +60,7 @@ export default class TimeWheelCache extends EventEmitter {
|
|
|
55
60
|
_maxTtl: number | undefined;
|
|
56
61
|
assertStarted(): void;
|
|
57
62
|
get autoEvictRunning(): boolean;
|
|
63
|
+
get autoStart(): boolean;
|
|
58
64
|
_startAutoEvict(): void;
|
|
59
65
|
_stopAutoEvict(): void;
|
|
60
66
|
_advance(): void;
|
|
@@ -101,7 +107,8 @@ export default class TimeWheelCache extends EventEmitter {
|
|
|
101
107
|
}
|
|
102
108
|
export type Timestamp = number;
|
|
103
109
|
export type TimeWheelCacheOptions = {
|
|
104
|
-
|
|
105
|
-
|
|
110
|
+
autoStart?: boolean;
|
|
111
|
+
tickInterval?: number;
|
|
112
|
+
tickCount?: number;
|
|
106
113
|
};
|
|
107
114
|
import { EventEmitter } from '@creejs/commons-events';
|