atomic-fns 1.2.0 → 2.0.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 (216) hide show
  1. package/README.md +397 -91
  2. package/dist/array/index.d.ts +9 -0
  3. package/dist/array/index.js +9 -0
  4. package/dist/array/list.d.ts +99 -0
  5. package/dist/array/list.js +133 -0
  6. package/dist/array/sets.d.ts +130 -0
  7. package/dist/array/sets.js +213 -0
  8. package/dist/async/index.d.ts +6 -0
  9. package/dist/async/index.js +6 -0
  10. package/dist/async/retry.d.ts +38 -0
  11. package/dist/async/retry.js +56 -0
  12. package/dist/async/sleep.d.ts +42 -0
  13. package/dist/async/sleep.js +76 -0
  14. package/dist/collections/bisect.d.ts +103 -0
  15. package/dist/collections/bisect.js +157 -0
  16. package/dist/collections/deque.d.ts +67 -97
  17. package/dist/collections/deque.js +197 -256
  18. package/dist/collections/entries.d.ts +89 -0
  19. package/dist/collections/entries.js +131 -0
  20. package/dist/collections/frozen-set.d.ts +42 -0
  21. package/dist/collections/frozen-set.js +64 -0
  22. package/dist/collections/group.d.ts +141 -0
  23. package/dist/collections/group.js +241 -0
  24. package/dist/collections/heap.d.ts +64 -0
  25. package/dist/collections/heap.js +205 -0
  26. package/dist/collections/index.d.ts +15 -623
  27. package/dist/collections/index.js +14 -844
  28. package/dist/collections/internal/arrayKeys.d.ts +19 -0
  29. package/dist/collections/internal/arrayKeys.js +83 -0
  30. package/dist/collections/internal/invariants.d.ts +4 -0
  31. package/dist/collections/internal/invariants.js +12 -0
  32. package/dist/collections/internal/iterable.d.ts +25 -0
  33. package/dist/collections/internal/iterable.js +62 -0
  34. package/dist/collections/iterate.d.ts +162 -0
  35. package/dist/collections/iterate.js +188 -0
  36. package/dist/collections/lru-cache.d.ts +58 -0
  37. package/dist/collections/lru-cache.js +164 -0
  38. package/dist/collections/order.d.ts +19 -0
  39. package/dist/collections/order.js +38 -0
  40. package/dist/collections/shape.d.ts +32 -0
  41. package/dist/collections/shape.js +86 -0
  42. package/dist/collections/sorted-map.d.ts +171 -0
  43. package/dist/collections/sorted-map.js +845 -0
  44. package/dist/collections/sorted-set.d.ts +103 -0
  45. package/dist/collections/sorted-set.js +147 -0
  46. package/dist/collections/splay-tree.d.ts +82 -0
  47. package/dist/collections/splay-tree.js +354 -0
  48. package/dist/collections/trie.d.ts +58 -0
  49. package/dist/collections/trie.js +196 -0
  50. package/dist/decimal/currency.d.ts +211 -0
  51. package/dist/decimal/currency.js +321 -0
  52. package/dist/decimal/decimal.d.ts +299 -0
  53. package/dist/decimal/decimal.js +660 -0
  54. package/dist/decimal/index.d.ts +11 -148
  55. package/dist/decimal/index.js +10 -410
  56. package/dist/decimal/internal/bits.d.ts +2 -0
  57. package/dist/decimal/internal/bits.js +7 -0
  58. package/dist/decimal/internal/echo.d.ts +4 -0
  59. package/dist/decimal/internal/echo.js +6 -0
  60. package/dist/decimal/internal/isqrt.d.ts +9 -0
  61. package/dist/decimal/internal/isqrt.js +34 -0
  62. package/dist/decimal/internal/locale.d.ts +7 -0
  63. package/dist/decimal/internal/locale.js +24 -0
  64. package/dist/decimal/internal/minor-units.d.ts +2 -0
  65. package/dist/decimal/internal/minor-units.js +33 -0
  66. package/dist/decimal/internal/pow10.d.ts +24 -0
  67. package/dist/decimal/internal/pow10.js +96 -0
  68. package/dist/encoding/base64.d.ts +39 -0
  69. package/dist/encoding/base64.js +53 -0
  70. package/dist/encoding/hex.d.ts +23 -0
  71. package/dist/encoding/hex.js +34 -0
  72. package/dist/encoding/index.d.ts +6 -0
  73. package/dist/encoding/index.js +6 -0
  74. package/dist/function/debounce.d.ts +68 -0
  75. package/dist/function/debounce.js +203 -0
  76. package/dist/function/index.d.ts +6 -0
  77. package/dist/function/index.js +6 -0
  78. package/dist/function/shared.d.ts +2 -0
  79. package/dist/function/shared.js +5 -0
  80. package/dist/function/wrap.d.ts +130 -0
  81. package/dist/function/wrap.js +113 -0
  82. package/dist/hash/digest.d.ts +18 -0
  83. package/dist/hash/digest.js +26 -0
  84. package/dist/hash/hash.d.ts +22 -0
  85. package/dist/hash/hash.js +252 -0
  86. package/dist/hash/hashString.d.ts +13 -0
  87. package/dist/hash/hashString.js +53 -0
  88. package/dist/hash/index.d.ts +8 -0
  89. package/dist/hash/index.js +8 -0
  90. package/dist/hash/stableStringify.d.ts +19 -0
  91. package/dist/hash/stableStringify.js +102 -0
  92. package/dist/id/index.d.ts +7 -0
  93. package/dist/id/index.js +7 -0
  94. package/dist/id/nanoid.d.ts +16 -0
  95. package/dist/id/nanoid.js +65 -0
  96. package/dist/id/uniqueId.d.ts +14 -0
  97. package/dist/id/uniqueId.js +20 -0
  98. package/dist/id/uuid.d.ts +25 -0
  99. package/dist/id/uuid.js +64 -0
  100. package/dist/index.d.ts +15 -8
  101. package/dist/index.js +15 -8
  102. package/dist/internal/bytes.d.ts +11 -0
  103. package/dist/internal/bytes.js +33 -0
  104. package/dist/internal/codec.d.ts +11 -0
  105. package/dist/internal/codec.js +84 -0
  106. package/dist/internal/compare.d.ts +10 -0
  107. package/dist/internal/compare.js +26 -0
  108. package/dist/internal/crypto.d.ts +5 -0
  109. package/dist/internal/crypto.js +11 -0
  110. package/dist/internal/keys.d.ts +12 -0
  111. package/dist/internal/keys.js +23 -0
  112. package/dist/internal/kind.d.ts +44 -0
  113. package/dist/internal/kind.js +139 -0
  114. package/dist/internal/sparse.d.ts +51 -0
  115. package/dist/internal/sparse.js +104 -0
  116. package/dist/internal/text.d.ts +7 -0
  117. package/dist/internal/text.js +15 -0
  118. package/dist/internal/timer.d.ts +10 -0
  119. package/dist/internal/timer.js +17 -0
  120. package/dist/internal/truncate.d.ts +10 -0
  121. package/dist/internal/truncate.js +22 -0
  122. package/dist/internal/walk.d.ts +47 -0
  123. package/dist/internal/walk.js +100 -0
  124. package/dist/is/index.d.ts +8 -0
  125. package/dist/is/index.js +8 -0
  126. package/dist/is/isArrayLike.d.ts +18 -0
  127. package/dist/is/isArrayLike.js +25 -0
  128. package/dist/is/isError.d.ts +13 -0
  129. package/dist/is/isError.js +18 -0
  130. package/dist/is/isNil.d.ts +12 -0
  131. package/dist/is/isNil.js +14 -0
  132. package/dist/is/isPlainObject.d.ts +15 -0
  133. package/dist/is/isPlainObject.js +34 -0
  134. package/dist/number/index.d.ts +7 -0
  135. package/dist/number/index.js +7 -0
  136. package/dist/number/round.d.ts +86 -0
  137. package/dist/number/round.js +130 -0
  138. package/dist/number/stats.d.ts +52 -0
  139. package/dist/number/stats.js +115 -0
  140. package/dist/object/cloneDeep.d.ts +28 -0
  141. package/dist/object/cloneDeep.js +200 -0
  142. package/dist/object/index.d.ts +14 -0
  143. package/dist/object/index.js +13 -0
  144. package/dist/object/isEqual.d.ts +64 -0
  145. package/dist/object/isEqual.js +289 -0
  146. package/dist/object/matches.d.ts +40 -0
  147. package/dist/object/matches.js +284 -0
  148. package/dist/object/merge.d.ts +66 -0
  149. package/dist/object/merge.js +272 -0
  150. package/dist/object/parsePath.d.ts +22 -0
  151. package/dist/object/parsePath.js +111 -0
  152. package/dist/object/path.d.ts +85 -0
  153. package/dist/object/path.js +208 -0
  154. package/dist/object/pick.d.ts +91 -0
  155. package/dist/object/pick.js +180 -0
  156. package/dist/object/reshape.d.ts +73 -0
  157. package/dist/object/reshape.js +144 -0
  158. package/dist/random/index.d.ts +96 -32
  159. package/dist/random/index.js +215 -53
  160. package/dist/string/case.d.ts +71 -0
  161. package/dist/string/case.js +152 -0
  162. package/dist/string/fold.d.ts +35 -0
  163. package/dist/string/fold.js +100 -0
  164. package/dist/string/index.d.ts +6 -187
  165. package/dist/string/index.js +6 -224
  166. package/dist/string/truncate.d.ts +25 -0
  167. package/dist/string/truncate.js +44 -0
  168. package/dist/string/words.d.ts +27 -0
  169. package/dist/string/words.js +241 -0
  170. package/package.json +99 -51
  171. package/dist/collections/BTree.d.ts +0 -230
  172. package/dist/collections/BTree.js +0 -1143
  173. package/dist/collections/Heap.d.ts +0 -69
  174. package/dist/collections/Heap.js +0 -181
  175. package/dist/collections/LRUCache.d.ts +0 -83
  176. package/dist/collections/LRUCache.js +0 -139
  177. package/dist/collections/SortedMap.d.ts +0 -92
  178. package/dist/collections/SortedMap.js +0 -131
  179. package/dist/collections/SortedSet.d.ts +0 -69
  180. package/dist/collections/SortedSet.js +0 -99
  181. package/dist/collections/SortedTree.d.ts +0 -103
  182. package/dist/collections/SortedTree.js +0 -142
  183. package/dist/collections/SplayTree.d.ts +0 -107
  184. package/dist/collections/SplayTree.js +0 -351
  185. package/dist/collections/Trie.d.ts +0 -42
  186. package/dist/collections/Trie.js +0 -104
  187. package/dist/collections/abc.d.ts +0 -152
  188. package/dist/collections/abc.js +0 -39
  189. package/dist/collections/frozenset.d.ts +0 -19
  190. package/dist/collections/frozenset.js +0 -33
  191. package/dist/functools/index.d.ts +0 -106
  192. package/dist/functools/index.js +0 -203
  193. package/dist/globals/index.d.ts +0 -268
  194. package/dist/globals/index.js +0 -447
  195. package/dist/i18n/currency.d.ts +0 -27
  196. package/dist/i18n/currency.js +0 -100
  197. package/dist/i18n/date/index.d.ts +0 -210
  198. package/dist/i18n/date/index.js +0 -552
  199. package/dist/i18n/date/utils.d.ts +0 -64
  200. package/dist/i18n/date/utils.js +0 -192
  201. package/dist/i18n/duration.d.ts +0 -113
  202. package/dist/i18n/duration.js +0 -398
  203. package/dist/i18n/format.d.ts +0 -34
  204. package/dist/i18n/format.js +0 -378
  205. package/dist/i18n/index.d.ts +0 -9
  206. package/dist/i18n/index.js +0 -8
  207. package/dist/itertools/index.d.ts +0 -251
  208. package/dist/itertools/index.js +0 -434
  209. package/dist/math/index.d.ts +0 -79
  210. package/dist/math/index.js +0 -81
  211. package/dist/mod.d.ts +0 -1
  212. package/dist/mod.js +0 -1
  213. package/dist/operators/index.d.ts +0 -148
  214. package/dist/operators/index.js +0 -325
  215. package/dist/types.d.ts +0 -19
  216. package/dist/types.js +0 -1
@@ -1,69 +0,0 @@
1
- import type { Comparer } from '../operators/index.js';
2
- import { compare } from '../operators/index.js';
3
- import { Collection } from './abc.js';
4
- export declare class Heap<T> extends Collection {
5
- private readonly heap;
6
- private readonly compare;
7
- private count;
8
- /**
9
- * Initializes a new Heap instance.
10
- *
11
- * **Note:** When constructing the heap from an array, it will operate directly on this array. For other iterables, it will create a new array.
12
- *
13
- * @param {Iterable<T>} [container=[]] The initial values.
14
- * @param {Comparer} [cmp=compare] Compare function. Defaults to smaller values first.
15
- */
16
- constructor(container?: Iterable<T>, cmp?: Comparer);
17
- at(n: number): T;
18
- get size(): number;
19
- clear(): void;
20
- /**
21
- * Push element into a container in order.
22
- * @param item The element to push.
23
- */
24
- add(item: T): void;
25
- /**
26
- * Removes the top element.
27
- */
28
- pop(): T | undefined;
29
- /**
30
- * Accesses the top element.
31
- */
32
- top(): T | undefined;
33
- /**
34
- * Check if element is in heap.
35
- * @param item The item want to find.
36
- * @return `true` if element exists.
37
- */
38
- contains(item: T): boolean;
39
- /**
40
- * Remove specified item from heap.
41
- * @param item The item want to remove.
42
- * @return `true` if the item was removed.
43
- */
44
- remove(item: T): boolean;
45
- /**
46
- * Returns an iterable with all the values in the heap.
47
- * @returns {Iterable<T>} The values in the heap.
48
- */
49
- values(): IterableIterator<T>;
50
- }
51
- /**
52
- * Transform any array into a heap, in-place, in linear time.
53
- * @param {Array} heap
54
- * @param {Comparer} [compareFn=compare] Custom compare function
55
- */
56
- export declare function heapify(heap: any, compareFn?: typeof compare): void;
57
- /**
58
- * Push the value `item` onto the `heap`, maintaining the heap invariant.
59
- * @param {Array} heap
60
- * @param {*} item
61
- * @param {Comparer} [compareFn=compare] Custom compare function
62
- */
63
- export declare function heappush<T>(heap: T[], item: T, compareFn?: typeof compare): void;
64
- /**
65
- * Pop and return the smallest item from the `heap`, maintaining the heap invariant.
66
- * @param {Array} heap
67
- * @param {Comparer} [compareFn=compare] Custom compare function
68
- */
69
- export declare function heappop<T>(heap: T[], compareFn?: typeof compare): T;
@@ -1,181 +0,0 @@
1
- import { compare } from '../operators/index.js';
2
- import { Collection } from './abc.js';
3
- export class Heap extends Collection {
4
- heap;
5
- compare;
6
- count = 0;
7
- /**
8
- * Initializes a new Heap instance.
9
- *
10
- * **Note:** When constructing the heap from an array, it will operate directly on this array. For other iterables, it will create a new array.
11
- *
12
- * @param {Iterable<T>} [container=[]] The initial values.
13
- * @param {Comparer} [cmp=compare] Compare function. Defaults to smaller values first.
14
- */
15
- constructor(container = [], cmp = compare) {
16
- super();
17
- this.compare = cmp;
18
- if (Array.isArray(container)) {
19
- // use the provided array to avoid copying.
20
- this.heap = container;
21
- }
22
- else {
23
- this.heap = Array.from(container);
24
- }
25
- this.count = this.heap.length;
26
- const halfLength = this.count >> 1;
27
- // Heapify the items
28
- for (let parent = (this.count - 1) >> 1; parent >= 0; --parent) {
29
- heapifyDown(this.heap, parent, halfLength, this.compare);
30
- }
31
- }
32
- at(n) {
33
- return this.heap.at(n);
34
- }
35
- get size() {
36
- return this.count;
37
- }
38
- clear() {
39
- this.count = 0;
40
- this.heap.length = 0;
41
- }
42
- /**
43
- * Push element into a container in order.
44
- * @param item The element to push.
45
- */
46
- add(item) {
47
- this.heap.push(item);
48
- heapifyUp(this.heap, this.count, this.compare);
49
- this.count += 1;
50
- }
51
- /**
52
- * Removes the top element.
53
- */
54
- pop() {
55
- if (!this.count)
56
- return;
57
- const value = this.heap[0];
58
- const last = this.heap.pop();
59
- this.count -= 1;
60
- if (this.count) {
61
- this.heap[0] = last;
62
- heapifyDown(this.heap, 0, this.count >> 1, this.compare);
63
- }
64
- return value;
65
- }
66
- /**
67
- * Accesses the top element.
68
- */
69
- top() {
70
- if (!this.count)
71
- return;
72
- return this.heap[0];
73
- }
74
- /**
75
- * Check if element is in heap.
76
- * @param item The item want to find.
77
- * @return `true` if element exists.
78
- */
79
- contains(item) {
80
- if (!this.count)
81
- return false;
82
- return this.heap.includes(item);
83
- }
84
- /**
85
- * Remove specified item from heap.
86
- * @param item The item want to remove.
87
- * @return `true` if the item was removed.
88
- */
89
- remove(item) {
90
- const index = Number.isNaN(item) ? this.heap.findIndex(Number.isNaN) : this.heap.indexOf(item);
91
- if (index < 0)
92
- return false;
93
- if (index === 0) {
94
- this.pop();
95
- }
96
- else if (index === this.count - 1) {
97
- this.heap.pop();
98
- this.count -= 1;
99
- }
100
- else {
101
- this.heap.splice(index, 1, this.heap.pop());
102
- this.count -= 1;
103
- heapifyUp(this.heap, index, this.compare);
104
- heapifyDown(this.heap, index, this.count >> 1, this.compare);
105
- }
106
- return true;
107
- }
108
- /**
109
- * Returns an iterable with all the values in the heap.
110
- * @returns {Iterable<T>} The values in the heap.
111
- */
112
- values() {
113
- return this.heap[Symbol.iterator]();
114
- }
115
- }
116
- function heapifyDown(heap, i, halfLength, compareFn = compare) {
117
- const item = heap[i];
118
- while (i < halfLength) {
119
- let left = (i << 1) | 1;
120
- const right = left + 1;
121
- let minItem = heap[left];
122
- if (right < heap.length && compareFn(minItem, heap[right]) > 0) {
123
- left = right;
124
- minItem = heap[right];
125
- }
126
- if (compareFn(minItem, item) >= 0)
127
- break;
128
- heap[i] = minItem;
129
- i = left;
130
- }
131
- heap[i] = item;
132
- }
133
- function heapifyUp(heap, i, compareFn) {
134
- const item = heap[i];
135
- while (i > 0) {
136
- const parent = (i - 1) >> 1;
137
- const parentItem = heap[parent];
138
- if (compareFn(parentItem, item) <= 0)
139
- break;
140
- heap[i] = parentItem;
141
- i = parent;
142
- }
143
- heap[i] = item;
144
- }
145
- /**
146
- * Transform any array into a heap, in-place, in linear time.
147
- * @param {Array} heap
148
- * @param {Comparer} [compareFn=compare] Custom compare function
149
- */
150
- export function heapify(heap, compareFn = compare) {
151
- const size = heap.length;
152
- const halfLength = size >> 1;
153
- for (let parent = (size - 1) >> 1; parent >= 0; --parent) {
154
- heapifyDown(heap, parent, halfLength, compareFn);
155
- }
156
- }
157
- /**
158
- * Push the value `item` onto the `heap`, maintaining the heap invariant.
159
- * @param {Array} heap
160
- * @param {*} item
161
- * @param {Comparer} [compareFn=compare] Custom compare function
162
- */
163
- export function heappush(heap, item, compareFn = compare) {
164
- heap.push(item);
165
- heapifyUp(heap, heap.length - 1, compareFn);
166
- }
167
- /**
168
- * Pop and return the smallest item from the `heap`, maintaining the heap invariant.
169
- * @param {Array} heap
170
- * @param {Comparer} [compareFn=compare] Custom compare function
171
- */
172
- export function heappop(heap, compareFn = compare) {
173
- const last = heap.pop();
174
- if (heap.length) {
175
- const item = heap[0];
176
- heap[0] = last;
177
- heapifyDown(heap, 0, heap.length >> 1, compareFn);
178
- return item;
179
- }
180
- return last;
181
- }
@@ -1,83 +0,0 @@
1
- import { Mapping } from './abc.js';
2
- /**
3
- * Implements a Least Recently Used fixed-capacity cache which supports updating, removing, and accessing keys in O(1).
4
- * @template K, V
5
- */
6
- export declare class LRUCache<K = any, V = any> extends Mapping<K, V> {
7
- protected items: Map<K, V>;
8
- protected maxSize: number;
9
- private eviction;
10
- /**
11
- * Creates a new instance of fixed size.
12
- * @param {number} capacity
13
- */
14
- constructor(capacity?: number);
15
- /**
16
- * Adds a new element with a specified `key` and `value` to the cache. If an element with the same key already exists, the element will be updated.
17
- * @param {K} key
18
- * @param {V} value
19
- * @returns {this}
20
- */
21
- set(key: K, value: V): this;
22
- /** An idle Map iterator pins every hash table the Map has outgrown, so it cannot live forever. */
23
- private wrote;
24
- /**
25
- * This is just an alias of {@link LRUCache.set}.
26
- * @param {K} key The key to add (note value will be `undefined`)
27
- * @returns {this}
28
- */
29
- add(key: K): this;
30
- /**
31
- * Returns `true` if the specified `key` is found on the cache.
32
- * @param {K} key
33
- * @returns {boolean}
34
- */
35
- contains(key: K): boolean;
36
- /**
37
- * Get the value associated with the specified `key`, or `undefined` it not found.
38
- * @param {K} key
39
- * @returns {?V} The associated value or `undefined`
40
- */
41
- get(key: K): V | undefined;
42
- /**
43
- * Removes all keys and values from the cache.
44
- * @returns {this}
45
- */
46
- clear(): this;
47
- /**
48
- * Removes a key from the cache and returns `true` if the key existed or `false` otherwise.
49
- * @param {K} key
50
- * @returns {boolean} `true` if the key existed and was removed
51
- */
52
- delete(key: K): boolean;
53
- /**
54
- * Removes the value associated with the specified `key` from the cache and returns the removed value if the key existed.
55
- * @param {K} key
56
- * @returns {?V} The value if it was removed or `undefined`.
57
- */
58
- remove(key: K): V | undefined;
59
- /**
60
- * Returns the total number of elements in the cache.
61
- */
62
- get size(): number;
63
- /**
64
- * Returns the total capacity of the cache.
65
- */
66
- get capacity(): number;
67
- /**
68
- * Returns an iterable of all the keys in the cache, from least to most recently used.
69
- * @returns {IterableIterator<K>}
70
- */
71
- keys(): IterableIterator<K>;
72
- /**
73
- * Returns an iterable of all the values in the cache, from least to most recently used.
74
- * @returns {IterableIterator<V>}
75
- */
76
- values(): IterableIterator<V>;
77
- /**
78
- * Returns an iterable of all the [key, value] pairs in the cache, from least to most recently used.
79
- * @returns {IterableIterator<[K, V]>}
80
- */
81
- entries(): IterableIterator<[K, V]>;
82
- [Symbol.iterator](): IterableIterator<[K, V]>;
83
- }
@@ -1,139 +0,0 @@
1
- import { Mapping } from './abc.js';
2
- /**
3
- * Implements a Least Recently Used fixed-capacity cache which supports updating, removing, and accessing keys in O(1).
4
- * @template K, V
5
- */
6
- export class LRUCache extends Mapping {
7
- items = new Map();
8
- maxSize;
9
- /**
10
- * Creates a new instance of fixed size.
11
- * @param {number} capacity
12
- */
13
- constructor(capacity = 1024) {
14
- super();
15
- this.maxSize = capacity;
16
- // Not enumerable, so two caches compare by their items
17
- Object.defineProperty(this, 'eviction', { value: { keys: this.items.keys(), writes: 0 } });
18
- }
19
- /**
20
- * Adds a new element with a specified `key` and `value` to the cache. If an element with the same key already exists, the element will be updated.
21
- * @param {K} key
22
- * @param {V} value
23
- * @returns {this}
24
- */
25
- set(key, value) {
26
- // Delete the previous key to update the new insertion order
27
- this.items.delete(key);
28
- this.items.set(key, value);
29
- this.wrote();
30
- if (this.items.size > this.maxSize) {
31
- // Because ES6 maps remember insertion order, we can evict the first key
32
- const lastKey = this.eviction.keys.next().value;
33
- this.items.delete(lastKey);
34
- }
35
- return this;
36
- }
37
- /** An idle Map iterator pins every hash table the Map has outgrown, so it cannot live forever. */
38
- wrote() {
39
- const { eviction } = this;
40
- if (++eviction.writes >= this.items.size) {
41
- eviction.writes = 0;
42
- eviction.keys = this.items.keys();
43
- }
44
- }
45
- /**
46
- * This is just an alias of {@link LRUCache.set}.
47
- * @param {K} key The key to add (note value will be `undefined`)
48
- * @returns {this}
49
- */
50
- add(key) {
51
- return this.set(key, undefined);
52
- }
53
- /**
54
- * Returns `true` if the specified `key` is found on the cache.
55
- * @param {K} key
56
- * @returns {boolean}
57
- */
58
- contains(key) {
59
- return this.items.has(key);
60
- }
61
- /**
62
- * Get the value associated with the specified `key`, or `undefined` it not found.
63
- * @param {K} key
64
- * @returns {?V} The associated value or `undefined`
65
- */
66
- get(key) {
67
- if (this.items.has(key)) {
68
- const value = this.items.get(key);
69
- // Delete the previous key to update the new insertion order
70
- this.items.delete(key);
71
- this.items.set(key, value);
72
- this.wrote();
73
- return value;
74
- }
75
- }
76
- /**
77
- * Removes all keys and values from the cache.
78
- * @returns {this}
79
- */
80
- clear() {
81
- this.items.clear();
82
- return this;
83
- }
84
- /**
85
- * Removes a key from the cache and returns `true` if the key existed or `false` otherwise.
86
- * @param {K} key
87
- * @returns {boolean} `true` if the key existed and was removed
88
- */
89
- delete(key) {
90
- return this.items.delete(key);
91
- }
92
- /**
93
- * Removes the value associated with the specified `key` from the cache and returns the removed value if the key existed.
94
- * @param {K} key
95
- * @returns {?V} The value if it was removed or `undefined`.
96
- */
97
- remove(key) {
98
- const value = this.items.get(key);
99
- this.items.delete(key);
100
- return value;
101
- }
102
- /**
103
- * Returns the total number of elements in the cache.
104
- */
105
- get size() {
106
- return this.items.size;
107
- }
108
- /**
109
- * Returns the total capacity of the cache.
110
- */
111
- get capacity() {
112
- return this.maxSize;
113
- }
114
- /**
115
- * Returns an iterable of all the keys in the cache, from least to most recently used.
116
- * @returns {IterableIterator<K>}
117
- */
118
- keys() {
119
- // A snapshot: get and set re-insert keys, so a live Map iterator would never reach the end
120
- return [...this.items.keys()].values();
121
- }
122
- /**
123
- * Returns an iterable of all the values in the cache, from least to most recently used.
124
- * @returns {IterableIterator<V>}
125
- */
126
- values() {
127
- return [...this.items.values()].values();
128
- }
129
- /**
130
- * Returns an iterable of all the [key, value] pairs in the cache, from least to most recently used.
131
- * @returns {IterableIterator<[K, V]>}
132
- */
133
- entries() {
134
- return [...this.items.entries()].values();
135
- }
136
- [Symbol.iterator]() {
137
- return this.entries();
138
- }
139
- }
@@ -1,92 +0,0 @@
1
- import { Mapping } from './abc.js';
2
- /**
3
- * A `SortedMap` is a map that holds key value pairs ordered by keys. Any primitive value (and objects that implement {@link Comparable} or {@link Ordered} interfaces) can be used as keys or values.
4
- * @see {@link operators.compare}
5
- * @see {@link operators.lt}
6
- * @template K, V
7
- */
8
- export declare class SortedMap<K, V> extends Mapping<K, V> {
9
- private readonly store;
10
- /**
11
- * Creates a new `SortedMap` object.
12
- * @param {Iterable<[K, V]>} [iterable] Initialize the map with entries.
13
- * @param {Comparer} [compareFn] The `compare` function to sort keys.
14
- */
15
- constructor(iterable?: Iterable<[K, V]>, compareFn?: any);
16
- /**
17
- * Adds a new `key` with a `null` value, if the `key` doesn't exist.
18
- * @param {K} key
19
- * @returns {this} Returns the Map object.
20
- */
21
- add(key: K): this;
22
- /**
23
- * Returns the value associated with the `key` or `undefined`.
24
- * @param {K} key
25
- * @returns {?V} The key value or `undefined`.
26
- */
27
- get(key: K): V | undefined;
28
- /**
29
- * Sets the value for the given `key`.
30
- * @param {K} key
31
- * @param {V} value
32
- * @returns {this} Returns the Map object.
33
- */
34
- set(key: K, value: V): this;
35
- /**
36
- * Returns the smallest key in the tree.
37
- * @returns {?K}
38
- */
39
- min(): K | undefined;
40
- /**
41
- * Returns the largest key in the tree.
42
- * @returns {?K}
43
- */
44
- max(): K | undefined;
45
- /**
46
- * Returns `true` if the `key` existed in the map and was removed.
47
- * @param {K} key
48
- * @returns {boolean} `true` if key was removed, otherwise `false`.
49
- */
50
- delete(item: K): boolean;
51
- /**
52
- * Removes all key/value pairs in the tree.
53
- */
54
- clear(): void;
55
- /**
56
- * Returns the total key/value pairs in the tree.
57
- * @returns {number}
58
- */
59
- get size(): number;
60
- /**
61
- * Check if there are no key/value pairs in the tree.
62
- * @returns {boolean} `true` if is empty.
63
- */
64
- empty(): boolean;
65
- /**
66
- * Returns `true` if the `key` exists in the map.
67
- * @param {K} key
68
- * @returns {boolean} `true` if key is found, otherwise `false`.
69
- */
70
- contains(key: K): boolean;
71
- /**
72
- * Returns `true` if the `key` exists in the map.
73
- * @param {K} key
74
- * @returns {boolean} `true` if key is found, otherwise `false`.
75
- */
76
- has(key: K): boolean;
77
- /**
78
- * Returns a new iterator of all **ordered** keys in the map.
79
- * @returns {Iterable<K>}
80
- */
81
- keys(): Iterable<K>;
82
- /**
83
- * Returns a new iterator of all values **ordered** by their keys.
84
- * @returns {Iterable<V>}
85
- */
86
- values(): Iterable<V>;
87
- /**
88
- * Returns a new iterator of all key value pairs **ordered** by their keys.
89
- * @returns {Iterable<[K, V]>}
90
- */
91
- entries(): Iterable<[K, V]>;
92
- }
@@ -1,131 +0,0 @@
1
- import { Mapping } from './abc.js';
2
- import { SplayTree } from './SplayTree.js';
3
- /**
4
- * A `SortedMap` is a map that holds key value pairs ordered by keys. Any primitive value (and objects that implement {@link Comparable} or {@link Ordered} interfaces) can be used as keys or values.
5
- * @see {@link operators.compare}
6
- * @see {@link operators.lt}
7
- * @template K, V
8
- */
9
- export class SortedMap extends Mapping {
10
- store;
11
- /**
12
- * Creates a new `SortedMap` object.
13
- * @param {Iterable<[K, V]>} [iterable] Initialize the map with entries.
14
- * @param {Comparer} [compareFn] The `compare` function to sort keys.
15
- */
16
- constructor(iterable, compareFn) {
17
- super();
18
- this.store = new SplayTree(compareFn);
19
- if (iterable) {
20
- for (const pair of iterable) {
21
- this.store.set(pair[0], pair[1] ?? null);
22
- }
23
- }
24
- }
25
- /**
26
- * Adds a new `key` with a `null` value, if the `key` doesn't exist.
27
- * @param {K} key
28
- * @returns {this} Returns the Map object.
29
- */
30
- add(key) {
31
- this.store.add(key);
32
- return this;
33
- }
34
- /**
35
- * Returns the value associated with the `key` or `undefined`.
36
- * @param {K} key
37
- * @returns {?V} The key value or `undefined`.
38
- */
39
- get(key) {
40
- return this.store.get(key);
41
- }
42
- /**
43
- * Sets the value for the given `key`.
44
- * @param {K} key
45
- * @param {V} value
46
- * @returns {this} Returns the Map object.
47
- */
48
- set(key, value) {
49
- this.store.set(key, value);
50
- return this;
51
- }
52
- /**
53
- * Returns the smallest key in the tree.
54
- * @returns {?K}
55
- */
56
- min() {
57
- return this.store.min();
58
- }
59
- /**
60
- * Returns the largest key in the tree.
61
- * @returns {?K}
62
- */
63
- max() {
64
- return this.store.max();
65
- }
66
- /**
67
- * Returns `true` if the `key` existed in the map and was removed.
68
- * @param {K} key
69
- * @returns {boolean} `true` if key was removed, otherwise `false`.
70
- */
71
- delete(item) {
72
- return this.store.delete(item);
73
- }
74
- /**
75
- * Removes all key/value pairs in the tree.
76
- */
77
- clear() {
78
- return this.store.clear();
79
- }
80
- /**
81
- * Returns the total key/value pairs in the tree.
82
- * @returns {number}
83
- */
84
- get size() {
85
- return this.store.size;
86
- }
87
- /**
88
- * Check if there are no key/value pairs in the tree.
89
- * @returns {boolean} `true` if is empty.
90
- */
91
- empty() {
92
- return this.store.empty();
93
- }
94
- /**
95
- * Returns `true` if the `key` exists in the map.
96
- * @param {K} key
97
- * @returns {boolean} `true` if key is found, otherwise `false`.
98
- */
99
- contains(key) {
100
- return this.store.contains(key);
101
- }
102
- /**
103
- * Returns `true` if the `key` exists in the map.
104
- * @param {K} key
105
- * @returns {boolean} `true` if key is found, otherwise `false`.
106
- */
107
- has(key) {
108
- return this.store.contains(key);
109
- }
110
- /**
111
- * Returns a new iterator of all **ordered** keys in the map.
112
- * @returns {Iterable<K>}
113
- */
114
- keys() {
115
- return this.store.keys();
116
- }
117
- /**
118
- * Returns a new iterator of all values **ordered** by their keys.
119
- * @returns {Iterable<V>}
120
- */
121
- values() {
122
- return this.store.values();
123
- }
124
- /**
125
- * Returns a new iterator of all key value pairs **ordered** by their keys.
126
- * @returns {Iterable<[K, V]>}
127
- */
128
- entries() {
129
- return this.store.entries();
130
- }
131
- }