data-structure-typed 2.5.0 → 2.5.2
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/.vitepress/cache/deps_temp_51f5f1b0/chunk-7OIKW5WK.js +12984 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/package.json +3 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vue_devtools-api.js +4505 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vueuse_core.js +9731 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/vue.js +347 -0
- package/CHANGELOG.md +5 -1
- package/README.md +124 -29
- package/dist/cjs/binary-tree.cjs +26282 -0
- package/dist/cjs/graph.cjs +5422 -0
- package/dist/cjs/hash.cjs +1310 -0
- package/dist/cjs/heap.cjs +1602 -0
- package/dist/cjs/index.cjs +31257 -14673
- package/dist/cjs/linked-list.cjs +4576 -0
- package/dist/cjs/matrix.cjs +1080 -0
- package/dist/cjs/priority-queue.cjs +1376 -0
- package/dist/cjs/queue.cjs +4264 -0
- package/dist/cjs/stack.cjs +907 -0
- package/dist/cjs/trie.cjs +1223 -0
- package/dist/cjs-legacy/binary-tree.cjs +26319 -0
- package/dist/cjs-legacy/graph.cjs +5420 -0
- package/dist/cjs-legacy/hash.cjs +1310 -0
- package/dist/cjs-legacy/heap.cjs +1599 -0
- package/dist/cjs-legacy/index.cjs +31268 -14679
- package/dist/cjs-legacy/linked-list.cjs +4582 -0
- package/dist/cjs-legacy/matrix.cjs +1083 -0
- package/dist/cjs-legacy/priority-queue.cjs +1374 -0
- package/dist/cjs-legacy/queue.cjs +4262 -0
- package/dist/cjs-legacy/stack.cjs +907 -0
- package/dist/cjs-legacy/trie.cjs +1222 -0
- package/dist/esm/binary-tree.mjs +26267 -0
- package/dist/esm/graph.mjs +5409 -0
- package/dist/esm/hash.mjs +1307 -0
- package/dist/esm/heap.mjs +1596 -0
- package/dist/esm/index.mjs +31254 -14674
- package/dist/esm/linked-list.mjs +4569 -0
- package/dist/esm/matrix.mjs +1076 -0
- package/dist/esm/priority-queue.mjs +1372 -0
- package/dist/esm/queue.mjs +4260 -0
- package/dist/esm/stack.mjs +905 -0
- package/dist/esm/trie.mjs +1220 -0
- package/dist/esm-legacy/binary-tree.mjs +26304 -0
- package/dist/esm-legacy/graph.mjs +5407 -0
- package/dist/esm-legacy/hash.mjs +1307 -0
- package/dist/esm-legacy/heap.mjs +1593 -0
- package/dist/esm-legacy/index.mjs +31265 -14680
- package/dist/esm-legacy/linked-list.mjs +4575 -0
- package/dist/esm-legacy/matrix.mjs +1079 -0
- package/dist/esm-legacy/priority-queue.mjs +1370 -0
- package/dist/esm-legacy/queue.mjs +4258 -0
- package/dist/esm-legacy/stack.mjs +905 -0
- package/dist/esm-legacy/trie.mjs +1219 -0
- package/dist/types/common/error.d.ts +9 -0
- package/dist/types/common/index.d.ts +1 -1
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
- package/dist/types/data-structures/base/linear-base.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +288 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +336 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +618 -18
- package/dist/types/data-structures/binary-tree/bst.d.ts +676 -1
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +456 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +144 -1
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +3307 -399
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3285 -360
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2674 -325
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +3072 -287
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +240 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +216 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +274 -10
- package/dist/types/data-structures/heap/heap.d.ts +336 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +411 -3
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +363 -3
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +434 -2
- package/dist/types/data-structures/matrix/matrix.d.ts +192 -0
- package/dist/types/data-structures/queue/deque.d.ts +364 -4
- package/dist/types/data-structures/queue/queue.d.ts +288 -0
- package/dist/types/data-structures/stack/stack.d.ts +240 -0
- package/dist/types/data-structures/trie/trie.d.ts +292 -4
- package/dist/types/interfaces/graph.d.ts +1 -1
- package/dist/types/types/common.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
- package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
- package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/types/utils/validate-type.d.ts +4 -4
- package/dist/umd/data-structure-typed.js +31196 -14608
- package/dist/umd/data-structure-typed.min.js +11 -5
- package/docs-site-docusaurus/README.md +41 -0
- package/docs-site-docusaurus/docs/api/README.md +52 -0
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +6644 -0
- package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +282 -0
- package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +2266 -0
- package/docs-site-docusaurus/docs/api/classes/BST.md +6293 -0
- package/docs-site-docusaurus/docs/api/classes/BSTNode.md +333 -0
- package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +455 -0
- package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +4647 -0
- package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +331 -0
- package/docs-site-docusaurus/docs/api/classes/Deque.md +2767 -0
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +2999 -0
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +2685 -0
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +221 -0
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +253 -0
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +21 -0
- package/docs-site-docusaurus/docs/api/classes/HashMap.md +1333 -0
- package/docs-site-docusaurus/docs/api/classes/Heap.md +1881 -0
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +800 -0
- package/docs-site-docusaurus/docs/api/classes/IterableEntryBase.md +644 -0
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +1632 -0
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +1853 -0
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +1108 -0
- package/docs-site-docusaurus/docs/api/classes/LinkedListNode.md +156 -0
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +2824 -0
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +2929 -0
- package/docs-site-docusaurus/docs/api/classes/Matrix.md +1026 -0
- package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +1866 -0
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +1883 -0
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +1879 -0
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +1882 -0
- package/docs-site-docusaurus/docs/api/classes/Navigator.md +109 -0
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +1839 -0
- package/docs-site-docusaurus/docs/api/classes/Queue.md +2244 -0
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +6888 -0
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +372 -0
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +2897 -0
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +169 -0
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +1229 -0
- package/docs-site-docusaurus/docs/api/classes/Stack.md +1573 -0
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +1389 -0
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +1591 -0
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +1246 -0
- package/docs-site-docusaurus/docs/api/classes/Trie.md +1708 -0
- package/docs-site-docusaurus/docs/api/classes/TrieNode.md +199 -0
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +2979 -0
- package/docs-site-docusaurus/docs/guide/_category_.json +6 -0
- package/docs-site-docusaurus/docs/guide/architecture.md +615 -0
- package/docs-site-docusaurus/docs/guide/concepts.md +451 -0
- package/docs-site-docusaurus/docs/guide/faq.md +180 -0
- package/docs-site-docusaurus/docs/guide/guides.md +597 -0
- package/docs-site-docusaurus/docs/guide/installation.md +62 -0
- package/docs-site-docusaurus/docs/guide/integrations.md +825 -0
- package/docs-site-docusaurus/docs/guide/overview.md +645 -0
- package/docs-site-docusaurus/docs/guide/performance.md +835 -0
- package/docs-site-docusaurus/docs/guide/quick-start.md +104 -0
- package/docs-site-docusaurus/docs/guide/use-cases/_category_.json +6 -0
- package/docs-site-docusaurus/docs/guide/use-cases/array-sort-alternative.md +158 -0
- package/docs-site-docusaurus/docs/guide/use-cases/heap-vs-sorting.md +92 -0
- package/docs-site-docusaurus/docs/guide/use-cases/map-vs-treemap.md +151 -0
- package/docs-site-docusaurus/docs/guide/use-cases/priority-queue-typescript.md +113 -0
- package/docs-site-docusaurus/docs/guide/use-cases/treemap-javascript.md +151 -0
- package/docs-site-docusaurus/docusaurus.config.ts +159 -0
- package/docs-site-docusaurus/fix-mdx-generics.mjs +75 -0
- package/docs-site-docusaurus/package-lock.json +18667 -0
- package/docs-site-docusaurus/package.json +50 -0
- package/docs-site-docusaurus/prefix-class-to-methods.mjs +48 -0
- package/docs-site-docusaurus/sidebars.ts +23 -0
- package/docs-site-docusaurus/sort-protected.mjs +87 -0
- package/docs-site-docusaurus/src/css/custom.css +96 -0
- package/docs-site-docusaurus/src/pages/index.module.css +13 -0
- package/docs-site-docusaurus/src/pages/index.tsx +120 -0
- package/docs-site-docusaurus/src/pages/markdown-page.md +7 -0
- package/docs-site-docusaurus/src/theme/TOCItems/index.tsx +34 -0
- package/docs-site-docusaurus/static/.nojekyll +0 -0
- package/docs-site-docusaurus/static/img/docusaurus-social-card.jpg +0 -0
- package/docs-site-docusaurus/static/img/docusaurus.png +0 -0
- package/docs-site-docusaurus/static/img/favicon.ico +0 -0
- package/docs-site-docusaurus/static/img/favicon.png +0 -0
- package/docs-site-docusaurus/static/img/logo-180.png +0 -0
- package/docs-site-docusaurus/static/img/logo.jpg +0 -0
- package/docs-site-docusaurus/static/img/logo.png +0 -0
- package/docs-site-docusaurus/static/img/logo.svg +1 -0
- package/docs-site-docusaurus/static/img/og-image.png +0 -0
- package/docs-site-docusaurus/static/img/undraw_docusaurus_mountain.svg +171 -0
- package/docs-site-docusaurus/static/img/undraw_docusaurus_react.svg +170 -0
- package/docs-site-docusaurus/static/img/undraw_docusaurus_tree.svg +40 -0
- package/docs-site-docusaurus/static/llms.txt +37 -0
- package/docs-site-docusaurus/static/robots.txt +4 -0
- package/docs-site-docusaurus/typedoc.json +23 -0
- package/llms.txt +37 -0
- package/package.json +159 -55
- package/src/common/error.ts +19 -1
- package/src/common/index.ts +1 -1
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-element-base.ts +3 -2
- package/src/data-structures/base/iterable-entry-base.ts +8 -8
- package/src/data-structures/base/linear-base.ts +3 -3
- package/src/data-structures/binary-tree/avl-tree.ts +287 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +327 -5
- package/src/data-structures/binary-tree/binary-tree.ts +581 -6
- package/src/data-structures/binary-tree/bst.ts +922 -7
- package/src/data-structures/binary-tree/red-black-tree.ts +453 -0
- package/src/data-structures/binary-tree/segment-tree.ts +139 -2
- package/src/data-structures/binary-tree/tree-map.ts +3300 -495
- package/src/data-structures/binary-tree/tree-multi-map.ts +3384 -563
- package/src/data-structures/binary-tree/tree-multi-set.ts +2757 -493
- package/src/data-structures/binary-tree/tree-set.ts +3122 -440
- package/src/data-structures/graph/abstract-graph.ts +6 -6
- package/src/data-structures/graph/directed-graph.ts +230 -0
- package/src/data-structures/graph/undirected-graph.ts +207 -0
- package/src/data-structures/hash/hash-map.ts +270 -19
- package/src/data-structures/heap/heap.ts +326 -4
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +394 -3
- package/src/data-structures/linked-list/singly-linked-list.ts +348 -3
- package/src/data-structures/linked-list/skip-linked-list.ts +421 -7
- package/src/data-structures/matrix/matrix.ts +194 -10
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +350 -5
- package/src/data-structures/queue/queue.ts +276 -0
- package/src/data-structures/stack/stack.ts +230 -0
- package/src/data-structures/trie/trie.ts +283 -7
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- package/src/types/data-structures/binary-tree/bst.ts +1 -0
- package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
- package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
- package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/utils/validate-type.ts +4 -4
- package/vercel.json +6 -0
- package/dist/leetcode/avl-tree-counter.mjs +0 -2957
- package/dist/leetcode/avl-tree-multi-map.mjs +0 -2889
- package/dist/leetcode/avl-tree.mjs +0 -2720
- package/dist/leetcode/binary-tree.mjs +0 -1594
- package/dist/leetcode/bst.mjs +0 -2398
- package/dist/leetcode/deque.mjs +0 -683
- package/dist/leetcode/directed-graph.mjs +0 -1733
- package/dist/leetcode/doubly-linked-list.mjs +0 -709
- package/dist/leetcode/hash-map.mjs +0 -493
- package/dist/leetcode/heap.mjs +0 -542
- package/dist/leetcode/max-heap.mjs +0 -375
- package/dist/leetcode/max-priority-queue.mjs +0 -383
- package/dist/leetcode/min-heap.mjs +0 -363
- package/dist/leetcode/min-priority-queue.mjs +0 -371
- package/dist/leetcode/priority-queue.mjs +0 -363
- package/dist/leetcode/queue.mjs +0 -943
- package/dist/leetcode/red-black-tree.mjs +0 -2765
- package/dist/leetcode/singly-linked-list.mjs +0 -754
- package/dist/leetcode/stack.mjs +0 -217
- package/dist/leetcode/tree-counter.mjs +0 -3039
- package/dist/leetcode/tree-multi-map.mjs +0 -2913
- package/dist/leetcode/trie.mjs +0 -413
- package/dist/leetcode/undirected-graph.mjs +0 -1650
|
@@ -0,0 +1,1389 @@
|
|
|
1
|
+
[**data-structure-typed**](../README.md)
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
[data-structure-typed](../README.md) / TreeMap
|
|
6
|
+
|
|
7
|
+
# Class: TreeMap\<K, V, R\>
|
|
8
|
+
|
|
9
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:26](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L26)
|
|
10
|
+
|
|
11
|
+
An ordered Map backed by a red-black tree.
|
|
12
|
+
|
|
13
|
+
- Iteration order is ascending by key.
|
|
14
|
+
- No node exposure: all APIs use keys/values only.
|
|
15
|
+
|
|
16
|
+
## Example
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
// Set multiple key-value pairs
|
|
20
|
+
const tm = new TreeMap<number, string>();
|
|
21
|
+
tm.setMany([[1, 'a'], [2, 'b'], [3, 'c']]);
|
|
22
|
+
console.log(tm.size); // 3;
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Type Parameters
|
|
26
|
+
|
|
27
|
+
### K
|
|
28
|
+
|
|
29
|
+
`K` = `any`
|
|
30
|
+
|
|
31
|
+
### V
|
|
32
|
+
|
|
33
|
+
`V` = `any`
|
|
34
|
+
|
|
35
|
+
### R
|
|
36
|
+
|
|
37
|
+
`R` = \[`K`, `V`\]
|
|
38
|
+
|
|
39
|
+
## Implements
|
|
40
|
+
|
|
41
|
+
- `Iterable`\<\[`K`, `V` \| `undefined`\]\>
|
|
42
|
+
|
|
43
|
+
## Constructors
|
|
44
|
+
|
|
45
|
+
### Constructor
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
new TreeMap<K, V, R>(entries?, options?): TreeMap<K, V, R>;
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:46](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L46)
|
|
52
|
+
|
|
53
|
+
Create a TreeMap from an iterable of `[key, value]` entries or raw elements.
|
|
54
|
+
|
|
55
|
+
#### Parameters
|
|
56
|
+
|
|
57
|
+
##### entries?
|
|
58
|
+
|
|
59
|
+
\| `Iterable`\<\[`K`, `V` \| `undefined`\], `any`, `any`\>
|
|
60
|
+
\| `Iterable`\<`R`, `any`, `any`\>
|
|
61
|
+
|
|
62
|
+
Iterable of `[key, value]` tuples, or raw elements if `toEntryFn` is provided.
|
|
63
|
+
|
|
64
|
+
##### options?
|
|
65
|
+
|
|
66
|
+
`TreeMapOptions`\<`K`, `V`, `R`\> = `{}`
|
|
67
|
+
|
|
68
|
+
Configuration options including optional `toEntryFn` to transform raw elements.
|
|
69
|
+
|
|
70
|
+
#### Returns
|
|
71
|
+
|
|
72
|
+
`TreeMap`\<`K`, `V`, `R`\>
|
|
73
|
+
|
|
74
|
+
#### Throws
|
|
75
|
+
|
|
76
|
+
If any entry is not a 2-tuple-like value (when no toEntryFn), or when using
|
|
77
|
+
the default comparator and encountering unsupported/invalid keys (e.g. `NaN`, invalid `Date`).
|
|
78
|
+
|
|
79
|
+
#### Example
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
// Standard usage with entries
|
|
83
|
+
const map = new TreeMap([['a', 1], ['b', 2]]);
|
|
84
|
+
|
|
85
|
+
// Using toEntryFn to transform raw objects
|
|
86
|
+
const users = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
|
|
87
|
+
const map = new TreeMap<number, User, User>(users, { toEntryFn: u => [u.id, u] });
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Accessors
|
|
91
|
+
|
|
92
|
+
### size
|
|
93
|
+
|
|
94
|
+
#### Get Signature
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
get size(): number;
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:134](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L134)
|
|
101
|
+
|
|
102
|
+
Number of entries in the map.
|
|
103
|
+
|
|
104
|
+
##### Returns
|
|
105
|
+
|
|
106
|
+
`number`
|
|
107
|
+
|
|
108
|
+
## Methods
|
|
109
|
+
|
|
110
|
+
### ceiling()
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
ceiling(key): [K, V | undefined] | undefined;
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:3747](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L3747)
|
|
117
|
+
|
|
118
|
+
Smallest entry whose key is >= the given key.
|
|
119
|
+
|
|
120
|
+
*
|
|
121
|
+
|
|
122
|
+
#### Parameters
|
|
123
|
+
|
|
124
|
+
##### key
|
|
125
|
+
|
|
126
|
+
`K`
|
|
127
|
+
|
|
128
|
+
#### Returns
|
|
129
|
+
|
|
130
|
+
\[`K`, `V` \| `undefined`\] \| `undefined`
|
|
131
|
+
|
|
132
|
+
#### Example
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
// Event scheduler with time-based lookup
|
|
136
|
+
const events = new TreeMap<Date, string>();
|
|
137
|
+
|
|
138
|
+
const meeting = new Date('2024-01-15T10:00:00Z');
|
|
139
|
+
const lunch = new Date('2024-01-15T12:00:00Z');
|
|
140
|
+
const review = new Date('2024-01-15T15:00:00Z');
|
|
141
|
+
const standup = new Date('2024-01-15T09:00:00Z');
|
|
142
|
+
|
|
143
|
+
events.set(meeting, 'Team Meeting');
|
|
144
|
+
events.set(lunch, 'Lunch Break');
|
|
145
|
+
events.set(review, 'Code Review');
|
|
146
|
+
events.set(standup, 'Daily Standup');
|
|
147
|
+
|
|
148
|
+
// Events are sorted chronologically
|
|
149
|
+
console.log([...events.values()]); // [
|
|
150
|
+
// 'Daily Standup',
|
|
151
|
+
// 'Team Meeting',
|
|
152
|
+
// 'Lunch Break',
|
|
153
|
+
// 'Code Review'
|
|
154
|
+
// ];
|
|
155
|
+
|
|
156
|
+
// Next event after 11:00
|
|
157
|
+
const after11 = new Date('2024-01-15T11:00:00Z');
|
|
158
|
+
console.log(events.ceiling(after11)?.[1]); // 'Lunch Break';
|
|
159
|
+
|
|
160
|
+
// Events between 9:30 and 13:00
|
|
161
|
+
const from = new Date('2024-01-15T09:30:00Z');
|
|
162
|
+
const to = new Date('2024-01-15T13:00:00Z');
|
|
163
|
+
const window = events.rangeSearch([from, to]);
|
|
164
|
+
console.log(window.map(([, v]) => v)); // ['Team Meeting', 'Lunch Break'];
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
***
|
|
168
|
+
|
|
169
|
+
### clear()
|
|
170
|
+
|
|
171
|
+
```ts
|
|
172
|
+
clear(): void;
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:1238](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L1238)
|
|
176
|
+
|
|
177
|
+
Remove all entries.
|
|
178
|
+
|
|
179
|
+
*
|
|
180
|
+
|
|
181
|
+
#### Returns
|
|
182
|
+
|
|
183
|
+
`void`
|
|
184
|
+
|
|
185
|
+
#### Example
|
|
186
|
+
|
|
187
|
+
```ts
|
|
188
|
+
// Remove all
|
|
189
|
+
const tm = new TreeMap<number, string>([[1, 'a']]);
|
|
190
|
+
tm.clear();
|
|
191
|
+
console.log(tm.isEmpty()); // true;
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
***
|
|
195
|
+
|
|
196
|
+
### clone()
|
|
197
|
+
|
|
198
|
+
```ts
|
|
199
|
+
clone(): TreeMap<K, V>;
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:4652](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L4652)
|
|
203
|
+
|
|
204
|
+
Creates a shallow clone of this map.
|
|
205
|
+
|
|
206
|
+
#### Returns
|
|
207
|
+
|
|
208
|
+
`TreeMap`\<`K`, `V`\>
|
|
209
|
+
|
|
210
|
+
#### Remarks
|
|
211
|
+
|
|
212
|
+
Time O(n log n), Space O(n)
|
|
213
|
+
|
|
214
|
+
*
|
|
215
|
+
|
|
216
|
+
#### Example
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
// Deep clone
|
|
220
|
+
const tm = new TreeMap<number, string>([[1, 'a'], [2, 'b']]);
|
|
221
|
+
const copy = tm.clone();
|
|
222
|
+
copy.delete(1);
|
|
223
|
+
console.log(tm.has(1)); // true;
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
***
|
|
227
|
+
|
|
228
|
+
### delete()
|
|
229
|
+
|
|
230
|
+
```ts
|
|
231
|
+
delete(key): boolean;
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:1066](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L1066)
|
|
235
|
+
|
|
236
|
+
Delete a key.
|
|
237
|
+
|
|
238
|
+
#### Parameters
|
|
239
|
+
|
|
240
|
+
##### key
|
|
241
|
+
|
|
242
|
+
`K`
|
|
243
|
+
|
|
244
|
+
#### Returns
|
|
245
|
+
|
|
246
|
+
`boolean`
|
|
247
|
+
|
|
248
|
+
`true` if the key existed; otherwise `false`.
|
|
249
|
+
|
|
250
|
+
#### Remarks
|
|
251
|
+
|
|
252
|
+
Expected time O(log n)
|
|
253
|
+
|
|
254
|
+
*
|
|
255
|
+
|
|
256
|
+
#### Example
|
|
257
|
+
|
|
258
|
+
```ts
|
|
259
|
+
// Session management with expiry
|
|
260
|
+
const sessions = new TreeMap<string, number>([
|
|
261
|
+
['sess_abc', Date.now()],
|
|
262
|
+
['sess_def', Date.now()],
|
|
263
|
+
['sess_ghi', Date.now()]
|
|
264
|
+
]);
|
|
265
|
+
|
|
266
|
+
console.log(sessions.size); // 3;
|
|
267
|
+
sessions.delete('sess_def');
|
|
268
|
+
console.log(sessions.has('sess_def')); // false;
|
|
269
|
+
console.log(sessions.size); // 2;
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
***
|
|
273
|
+
|
|
274
|
+
### entries()
|
|
275
|
+
|
|
276
|
+
```ts
|
|
277
|
+
entries(): IterableIterator<[K, V | undefined]>;
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:1755](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L1755)
|
|
281
|
+
|
|
282
|
+
Iterate over `[key, value]` entries in ascending key order.
|
|
283
|
+
|
|
284
|
+
Note: values may be `undefined`.
|
|
285
|
+
|
|
286
|
+
*
|
|
287
|
+
|
|
288
|
+
#### Returns
|
|
289
|
+
|
|
290
|
+
`IterableIterator`\<\[`K`, `V` \| `undefined`\]\>
|
|
291
|
+
|
|
292
|
+
#### Example
|
|
293
|
+
|
|
294
|
+
```ts
|
|
295
|
+
// Iterate key-value pairs
|
|
296
|
+
const tm = new TreeMap<number, string>([[3, 'c'], [1, 'a'], [2, 'b']]);
|
|
297
|
+
console.log([...tm.entries()]); // [[1, 'a'], [2, 'b'], [3, 'c']];
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
***
|
|
301
|
+
|
|
302
|
+
### every()
|
|
303
|
+
|
|
304
|
+
```ts
|
|
305
|
+
every(callbackfn, thisArg?): boolean;
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:2637](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L2637)
|
|
309
|
+
|
|
310
|
+
Test whether all entries satisfy a predicate.
|
|
311
|
+
|
|
312
|
+
#### Parameters
|
|
313
|
+
|
|
314
|
+
##### callbackfn
|
|
315
|
+
|
|
316
|
+
`TreeMapEntryCallback`\<`K`, `V`, `boolean`, `TreeMap`\<`K`, `V`, \[`K`, `V`\]\>\>
|
|
317
|
+
|
|
318
|
+
##### thisArg?
|
|
319
|
+
|
|
320
|
+
`unknown`
|
|
321
|
+
|
|
322
|
+
#### Returns
|
|
323
|
+
|
|
324
|
+
`boolean`
|
|
325
|
+
|
|
326
|
+
#### Remarks
|
|
327
|
+
|
|
328
|
+
Time O(n), Space O(1)
|
|
329
|
+
|
|
330
|
+
*
|
|
331
|
+
|
|
332
|
+
#### Example
|
|
333
|
+
|
|
334
|
+
```ts
|
|
335
|
+
// Test all entries
|
|
336
|
+
const tm = new TreeMap<number, string>([[1, 'a'], [2, 'b']]);
|
|
337
|
+
console.log(tm.every((v, k) => k > 0)); // true;
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
***
|
|
341
|
+
|
|
342
|
+
### filter()
|
|
343
|
+
|
|
344
|
+
```ts
|
|
345
|
+
filter(callbackfn, thisArg?): TreeMap<K, V>;
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:2288](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L2288)
|
|
349
|
+
|
|
350
|
+
Create a new TreeMap containing only entries that satisfy the predicate.
|
|
351
|
+
|
|
352
|
+
#### Parameters
|
|
353
|
+
|
|
354
|
+
##### callbackfn
|
|
355
|
+
|
|
356
|
+
`TreeMapEntryCallback`\<`K`, `V`, `boolean`, `TreeMap`\<`K`, `V`, \[`K`, `V`\]\>\>
|
|
357
|
+
|
|
358
|
+
##### thisArg?
|
|
359
|
+
|
|
360
|
+
`unknown`
|
|
361
|
+
|
|
362
|
+
#### Returns
|
|
363
|
+
|
|
364
|
+
`TreeMap`\<`K`, `V`\>
|
|
365
|
+
|
|
366
|
+
#### Remarks
|
|
367
|
+
|
|
368
|
+
Time O(n log n) expected, Space O(n)
|
|
369
|
+
|
|
370
|
+
*
|
|
371
|
+
|
|
372
|
+
#### Example
|
|
373
|
+
|
|
374
|
+
```ts
|
|
375
|
+
// Filter entries
|
|
376
|
+
const tm = new TreeMap<number, string>([[1, 'a'], [2, 'b'], [3, 'c']]);
|
|
377
|
+
const filtered = tm.filter((v, k) => k > 1);
|
|
378
|
+
console.log([...filtered.keys()]); // [2, 3];
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
***
|
|
382
|
+
|
|
383
|
+
### find()
|
|
384
|
+
|
|
385
|
+
```ts
|
|
386
|
+
find(callbackfn, thisArg?): [K, V | undefined] | undefined;
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:2988](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L2988)
|
|
390
|
+
|
|
391
|
+
Find the first entry that satisfies a predicate.
|
|
392
|
+
|
|
393
|
+
#### Parameters
|
|
394
|
+
|
|
395
|
+
##### callbackfn
|
|
396
|
+
|
|
397
|
+
`TreeMapEntryCallback`\<`K`, `V`, `boolean`, `TreeMap`\<`K`, `V`, \[`K`, `V`\]\>\>
|
|
398
|
+
|
|
399
|
+
##### thisArg?
|
|
400
|
+
|
|
401
|
+
`unknown`
|
|
402
|
+
|
|
403
|
+
#### Returns
|
|
404
|
+
|
|
405
|
+
\[`K`, `V` \| `undefined`\] \| `undefined`
|
|
406
|
+
|
|
407
|
+
The first matching `[key, value]` tuple, or `undefined`.
|
|
408
|
+
|
|
409
|
+
#### Remarks
|
|
410
|
+
|
|
411
|
+
Time O(n), Space O(1)
|
|
412
|
+
|
|
413
|
+
*
|
|
414
|
+
|
|
415
|
+
#### Example
|
|
416
|
+
|
|
417
|
+
```ts
|
|
418
|
+
// Find matching entry
|
|
419
|
+
const tm = new TreeMap<number, string>([[1, 'a'], [2, 'b']]);
|
|
420
|
+
console.log(tm.find(v => v === 'b')?.[0]); // 2;
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
***
|
|
424
|
+
|
|
425
|
+
### first()
|
|
426
|
+
|
|
427
|
+
```ts
|
|
428
|
+
first(): [K, V | undefined] | undefined;
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:3407](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L3407)
|
|
432
|
+
|
|
433
|
+
Smallest entry by key.
|
|
434
|
+
|
|
435
|
+
*
|
|
436
|
+
|
|
437
|
+
#### Returns
|
|
438
|
+
|
|
439
|
+
\[`K`, `V` \| `undefined`\] \| `undefined`
|
|
440
|
+
|
|
441
|
+
#### Example
|
|
442
|
+
|
|
443
|
+
```ts
|
|
444
|
+
// Leaderboard with ranked scores
|
|
445
|
+
// Use score as key (descending), player name as value
|
|
446
|
+
const leaderboard = new TreeMap<number, string>([], {
|
|
447
|
+
comparator: (a, b) => b - a // descending
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
leaderboard.set(1500, 'Alice');
|
|
451
|
+
leaderboard.set(2200, 'Bob');
|
|
452
|
+
leaderboard.set(1800, 'Charlie');
|
|
453
|
+
leaderboard.set(2500, 'Diana');
|
|
454
|
+
|
|
455
|
+
// Top 3 players (first 3 in descending order)
|
|
456
|
+
const top3 = [...leaderboard.entries()].slice(0, 3);
|
|
457
|
+
console.log(top3); // [
|
|
458
|
+
// [2500, 'Diana'],
|
|
459
|
+
// [2200, 'Bob'],
|
|
460
|
+
// [1800, 'Charlie']
|
|
461
|
+
// ];
|
|
462
|
+
|
|
463
|
+
// Highest scorer
|
|
464
|
+
console.log(leaderboard.first()); // [2500, 'Diana'];
|
|
465
|
+
|
|
466
|
+
// Remove lowest scorer
|
|
467
|
+
console.log(leaderboard.pollLast()); // [1500, 'Alice'];
|
|
468
|
+
console.log(leaderboard.size); // 3;
|
|
469
|
+
```
|
|
470
|
+
|
|
471
|
+
***
|
|
472
|
+
|
|
473
|
+
### floor()
|
|
474
|
+
|
|
475
|
+
```ts
|
|
476
|
+
floor(key): [K, V | undefined] | undefined;
|
|
477
|
+
```
|
|
478
|
+
|
|
479
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:3905](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L3905)
|
|
480
|
+
|
|
481
|
+
Largest entry whose key is <= the given key.
|
|
482
|
+
|
|
483
|
+
*
|
|
484
|
+
|
|
485
|
+
#### Parameters
|
|
486
|
+
|
|
487
|
+
##### key
|
|
488
|
+
|
|
489
|
+
`K`
|
|
490
|
+
|
|
491
|
+
#### Returns
|
|
492
|
+
|
|
493
|
+
\[`K`, `V` \| `undefined`\] \| `undefined`
|
|
494
|
+
|
|
495
|
+
#### Example
|
|
496
|
+
|
|
497
|
+
```ts
|
|
498
|
+
// Find the largest key ≤ target
|
|
499
|
+
const versions = new TreeMap<number, string>([
|
|
500
|
+
[1, 'v1.0'],
|
|
501
|
+
[3, 'v3.0'],
|
|
502
|
+
[5, 'v5.0'],
|
|
503
|
+
[7, 'v7.0']
|
|
504
|
+
]);
|
|
505
|
+
|
|
506
|
+
// Largest version ≤ 4
|
|
507
|
+
console.log(versions.floor(4)); // [3, 'v3.0'];
|
|
508
|
+
// Largest version ≤ 5 (exact match)
|
|
509
|
+
console.log(versions.floor(5)); // [5, 'v5.0'];
|
|
510
|
+
// No version ≤ 0
|
|
511
|
+
console.log(versions.floor(0)); // undefined;
|
|
512
|
+
```
|
|
513
|
+
|
|
514
|
+
***
|
|
515
|
+
|
|
516
|
+
### forEach()
|
|
517
|
+
|
|
518
|
+
```ts
|
|
519
|
+
forEach(cb, thisArg?): void;
|
|
520
|
+
```
|
|
521
|
+
|
|
522
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:1932](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L1932)
|
|
523
|
+
|
|
524
|
+
Visit each entry in ascending key order.
|
|
525
|
+
|
|
526
|
+
Note: callback value may be `undefined`.
|
|
527
|
+
|
|
528
|
+
*
|
|
529
|
+
|
|
530
|
+
#### Parameters
|
|
531
|
+
|
|
532
|
+
##### cb
|
|
533
|
+
|
|
534
|
+
(`value`, `key`, `map`) => `void`
|
|
535
|
+
|
|
536
|
+
##### thisArg?
|
|
537
|
+
|
|
538
|
+
`unknown`
|
|
539
|
+
|
|
540
|
+
#### Returns
|
|
541
|
+
|
|
542
|
+
`void`
|
|
543
|
+
|
|
544
|
+
#### Example
|
|
545
|
+
|
|
546
|
+
```ts
|
|
547
|
+
// Execute for each entry
|
|
548
|
+
const tm = new TreeMap<number, string>([[1, 'a'], [2, 'b']]);
|
|
549
|
+
const pairs: string[] = [];
|
|
550
|
+
tm.forEach((v, k) => pairs.push(`${k}:${v}`));
|
|
551
|
+
console.log(pairs); // ['1:a', '2:b'];
|
|
552
|
+
```
|
|
553
|
+
|
|
554
|
+
***
|
|
555
|
+
|
|
556
|
+
### get()
|
|
557
|
+
|
|
558
|
+
```ts
|
|
559
|
+
get(key): V | undefined;
|
|
560
|
+
```
|
|
561
|
+
|
|
562
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:687](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L687)
|
|
563
|
+
|
|
564
|
+
Get the value under a key.
|
|
565
|
+
|
|
566
|
+
#### Parameters
|
|
567
|
+
|
|
568
|
+
##### key
|
|
569
|
+
|
|
570
|
+
`K`
|
|
571
|
+
|
|
572
|
+
#### Returns
|
|
573
|
+
|
|
574
|
+
`V` \| `undefined`
|
|
575
|
+
|
|
576
|
+
#### Remarks
|
|
577
|
+
|
|
578
|
+
Expected time O(log n)
|
|
579
|
+
|
|
580
|
+
*
|
|
581
|
+
|
|
582
|
+
#### Example
|
|
583
|
+
|
|
584
|
+
```ts
|
|
585
|
+
// Configuration registry with typed lookups
|
|
586
|
+
const config = new TreeMap<string, number>([
|
|
587
|
+
['maxRetries', 3],
|
|
588
|
+
['timeout', 5000],
|
|
589
|
+
['poolSize', 10]
|
|
590
|
+
]);
|
|
591
|
+
|
|
592
|
+
console.log(config.get('timeout')); // 5000;
|
|
593
|
+
console.log(config.get('missing')); // undefined;
|
|
594
|
+
console.log(config.size); // 3;
|
|
595
|
+
```
|
|
596
|
+
|
|
597
|
+
***
|
|
598
|
+
|
|
599
|
+
### getByRank()
|
|
600
|
+
|
|
601
|
+
```ts
|
|
602
|
+
getByRank(k): [K, V | undefined] | undefined;
|
|
603
|
+
```
|
|
604
|
+
|
|
605
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:4433](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L4433)
|
|
606
|
+
|
|
607
|
+
Returns the entry at the k-th position in tree order (0-indexed).
|
|
608
|
+
|
|
609
|
+
#### Parameters
|
|
610
|
+
|
|
611
|
+
##### k
|
|
612
|
+
|
|
613
|
+
`number`
|
|
614
|
+
|
|
615
|
+
#### Returns
|
|
616
|
+
|
|
617
|
+
\[`K`, `V` \| `undefined`\] \| `undefined`
|
|
618
|
+
|
|
619
|
+
#### Remarks
|
|
620
|
+
|
|
621
|
+
Time O(log n). Requires `enableOrderStatistic: true`.
|
|
622
|
+
|
|
623
|
+
*
|
|
624
|
+
|
|
625
|
+
#### Example
|
|
626
|
+
|
|
627
|
+
```ts
|
|
628
|
+
// Find k-th entry in a TreeMap
|
|
629
|
+
const map = new TreeMap<string, number>(
|
|
630
|
+
[['alice', 95], ['bob', 87], ['charlie', 92]],
|
|
631
|
+
{ enableOrderStatistic: true }
|
|
632
|
+
);
|
|
633
|
+
console.log(map.getByRank(0)); // 'alice';
|
|
634
|
+
console.log(map.getByRank(1)); // 'bob';
|
|
635
|
+
console.log(map.getByRank(2)); // 'charlie';
|
|
636
|
+
```
|
|
637
|
+
|
|
638
|
+
***
|
|
639
|
+
|
|
640
|
+
### getRank()
|
|
641
|
+
|
|
642
|
+
```ts
|
|
643
|
+
getRank(key): number;
|
|
644
|
+
```
|
|
645
|
+
|
|
646
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:4453](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L4453)
|
|
647
|
+
|
|
648
|
+
Returns the 0-based rank of a key (number of elements that precede it in tree order).
|
|
649
|
+
|
|
650
|
+
#### Parameters
|
|
651
|
+
|
|
652
|
+
##### key
|
|
653
|
+
|
|
654
|
+
`K`
|
|
655
|
+
|
|
656
|
+
#### Returns
|
|
657
|
+
|
|
658
|
+
`number`
|
|
659
|
+
|
|
660
|
+
#### Remarks
|
|
661
|
+
|
|
662
|
+
Time O(log n). Requires `enableOrderStatistic: true`.
|
|
663
|
+
*
|
|
664
|
+
|
|
665
|
+
#### Example
|
|
666
|
+
|
|
667
|
+
```ts
|
|
668
|
+
// Get the rank of a key in sorted order
|
|
669
|
+
const tree = new TreeMap<number>(
|
|
670
|
+
[10, 20, 30, 40, 50],
|
|
671
|
+
{ enableOrderStatistic: true }
|
|
672
|
+
);
|
|
673
|
+
console.log(tree.getRank(10)); // 0; // smallest → rank 0
|
|
674
|
+
console.log(tree.getRank(30)); // 2; // 2 elements before 30 in tree order
|
|
675
|
+
console.log(tree.getRank(50)); // 4; // largest → rank 4
|
|
676
|
+
console.log(tree.getRank(25)); // 2;
|
|
677
|
+
```
|
|
678
|
+
|
|
679
|
+
***
|
|
680
|
+
|
|
681
|
+
### has()
|
|
682
|
+
|
|
683
|
+
```ts
|
|
684
|
+
has(key): boolean;
|
|
685
|
+
```
|
|
686
|
+
|
|
687
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:875](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L875)
|
|
688
|
+
|
|
689
|
+
Test whether a key exists.
|
|
690
|
+
|
|
691
|
+
#### Parameters
|
|
692
|
+
|
|
693
|
+
##### key
|
|
694
|
+
|
|
695
|
+
`K`
|
|
696
|
+
|
|
697
|
+
#### Returns
|
|
698
|
+
|
|
699
|
+
`boolean`
|
|
700
|
+
|
|
701
|
+
#### Remarks
|
|
702
|
+
|
|
703
|
+
Expected time O(log n)
|
|
704
|
+
|
|
705
|
+
*
|
|
706
|
+
|
|
707
|
+
#### Example
|
|
708
|
+
|
|
709
|
+
```ts
|
|
710
|
+
// Feature flag checking
|
|
711
|
+
const flags = new TreeMap<string, boolean>([
|
|
712
|
+
['darkMode', true],
|
|
713
|
+
['betaFeature', false],
|
|
714
|
+
['notifications', true]
|
|
715
|
+
]);
|
|
716
|
+
|
|
717
|
+
console.log(flags.has('darkMode')); // true;
|
|
718
|
+
console.log(flags.has('unknownFlag')); // false;
|
|
719
|
+
```
|
|
720
|
+
|
|
721
|
+
***
|
|
722
|
+
|
|
723
|
+
### higher()
|
|
724
|
+
|
|
725
|
+
```ts
|
|
726
|
+
higher(key): [K, V | undefined] | undefined;
|
|
727
|
+
```
|
|
728
|
+
|
|
729
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:4063](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L4063)
|
|
730
|
+
|
|
731
|
+
Smallest entry whose key is > the given key.
|
|
732
|
+
|
|
733
|
+
*
|
|
734
|
+
|
|
735
|
+
#### Parameters
|
|
736
|
+
|
|
737
|
+
##### key
|
|
738
|
+
|
|
739
|
+
`K`
|
|
740
|
+
|
|
741
|
+
#### Returns
|
|
742
|
+
|
|
743
|
+
\[`K`, `V` \| `undefined`\] \| `undefined`
|
|
744
|
+
|
|
745
|
+
#### Example
|
|
746
|
+
|
|
747
|
+
```ts
|
|
748
|
+
// Find the smallest key strictly > target
|
|
749
|
+
const prices = new TreeMap<number, string>([
|
|
750
|
+
[10, 'Basic'],
|
|
751
|
+
[25, 'Standard'],
|
|
752
|
+
[50, 'Premium'],
|
|
753
|
+
[100, 'Enterprise']
|
|
754
|
+
]);
|
|
755
|
+
|
|
756
|
+
// Next tier above $25
|
|
757
|
+
console.log(prices.higher(25)); // [50, 'Premium'];
|
|
758
|
+
// Next tier above $99
|
|
759
|
+
console.log(prices.higher(99)); // [100, 'Enterprise'];
|
|
760
|
+
// Nothing above $100
|
|
761
|
+
console.log(prices.higher(100)); // undefined;
|
|
762
|
+
```
|
|
763
|
+
|
|
764
|
+
***
|
|
765
|
+
|
|
766
|
+
### isEmpty()
|
|
767
|
+
|
|
768
|
+
```ts
|
|
769
|
+
isEmpty(): boolean;
|
|
770
|
+
```
|
|
771
|
+
|
|
772
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:302](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L302)
|
|
773
|
+
|
|
774
|
+
Whether the map is empty.
|
|
775
|
+
|
|
776
|
+
*
|
|
777
|
+
|
|
778
|
+
#### Returns
|
|
779
|
+
|
|
780
|
+
`boolean`
|
|
781
|
+
|
|
782
|
+
#### Example
|
|
783
|
+
|
|
784
|
+
```ts
|
|
785
|
+
// Check empty
|
|
786
|
+
console.log(new TreeMap().isEmpty()); // true;
|
|
787
|
+
```
|
|
788
|
+
|
|
789
|
+
***
|
|
790
|
+
|
|
791
|
+
### keys()
|
|
792
|
+
|
|
793
|
+
```ts
|
|
794
|
+
keys(): IterableIterator<K>;
|
|
795
|
+
```
|
|
796
|
+
|
|
797
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:1407](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L1407)
|
|
798
|
+
|
|
799
|
+
Iterate over keys in ascending order.
|
|
800
|
+
|
|
801
|
+
*
|
|
802
|
+
|
|
803
|
+
#### Returns
|
|
804
|
+
|
|
805
|
+
`IterableIterator`\<`K`\>
|
|
806
|
+
|
|
807
|
+
#### Example
|
|
808
|
+
|
|
809
|
+
```ts
|
|
810
|
+
// Get sorted keys
|
|
811
|
+
const tm = new TreeMap<number, string>([[3, 'c'], [1, 'a']]);
|
|
812
|
+
console.log([...tm.keys()]); // [1, 3];
|
|
813
|
+
```
|
|
814
|
+
|
|
815
|
+
***
|
|
816
|
+
|
|
817
|
+
### last()
|
|
818
|
+
|
|
819
|
+
```ts
|
|
820
|
+
last(): [K, V | undefined] | undefined;
|
|
821
|
+
```
|
|
822
|
+
|
|
823
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:3460](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L3460)
|
|
824
|
+
|
|
825
|
+
Largest entry by key.
|
|
826
|
+
|
|
827
|
+
*
|
|
828
|
+
|
|
829
|
+
#### Returns
|
|
830
|
+
|
|
831
|
+
\[`K`, `V` \| `undefined`\] \| `undefined`
|
|
832
|
+
|
|
833
|
+
#### Example
|
|
834
|
+
|
|
835
|
+
```ts
|
|
836
|
+
// Access the maximum entry
|
|
837
|
+
const scores = new TreeMap<number, string>([
|
|
838
|
+
[85, 'Bob'],
|
|
839
|
+
[92, 'Alice'],
|
|
840
|
+
[78, 'Charlie']
|
|
841
|
+
]);
|
|
842
|
+
|
|
843
|
+
console.log(scores.last()); // [92, 'Alice'];
|
|
844
|
+
console.log(scores.first()); // [78, 'Charlie'];
|
|
845
|
+
```
|
|
846
|
+
|
|
847
|
+
***
|
|
848
|
+
|
|
849
|
+
### lower()
|
|
850
|
+
|
|
851
|
+
```ts
|
|
852
|
+
lower(key): [K, V | undefined] | undefined;
|
|
853
|
+
```
|
|
854
|
+
|
|
855
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:4219](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L4219)
|
|
856
|
+
|
|
857
|
+
Largest entry whose key is < the given key.
|
|
858
|
+
|
|
859
|
+
*
|
|
860
|
+
|
|
861
|
+
#### Parameters
|
|
862
|
+
|
|
863
|
+
##### key
|
|
864
|
+
|
|
865
|
+
`K`
|
|
866
|
+
|
|
867
|
+
#### Returns
|
|
868
|
+
|
|
869
|
+
\[`K`, `V` \| `undefined`\] \| `undefined`
|
|
870
|
+
|
|
871
|
+
#### Example
|
|
872
|
+
|
|
873
|
+
```ts
|
|
874
|
+
// Find the largest key strictly < target
|
|
875
|
+
const temps = new TreeMap<number, string>([
|
|
876
|
+
[0, 'Freezing'],
|
|
877
|
+
[20, 'Cool'],
|
|
878
|
+
[30, 'Warm'],
|
|
879
|
+
[40, 'Hot']
|
|
880
|
+
]);
|
|
881
|
+
|
|
882
|
+
// Largest reading below 30
|
|
883
|
+
console.log(temps.lower(30)); // [20, 'Cool'];
|
|
884
|
+
// Nothing below 0
|
|
885
|
+
console.log(temps.lower(0)); // undefined;
|
|
886
|
+
```
|
|
887
|
+
|
|
888
|
+
***
|
|
889
|
+
|
|
890
|
+
### map()
|
|
891
|
+
|
|
892
|
+
```ts
|
|
893
|
+
map<MK, MV>(
|
|
894
|
+
callbackfn,
|
|
895
|
+
options?,
|
|
896
|
+
thisArg?): TreeMap<MK, MV>;
|
|
897
|
+
```
|
|
898
|
+
|
|
899
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:2105](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L2105)
|
|
900
|
+
|
|
901
|
+
Create a new TreeMap by mapping each entry to a new `[key, value]` entry.
|
|
902
|
+
|
|
903
|
+
This mirrors `RedBlackTree.map`: mapping produces a new ordered container.
|
|
904
|
+
|
|
905
|
+
#### Type Parameters
|
|
906
|
+
|
|
907
|
+
##### MK
|
|
908
|
+
|
|
909
|
+
`MK`
|
|
910
|
+
|
|
911
|
+
##### MV
|
|
912
|
+
|
|
913
|
+
`MV`
|
|
914
|
+
|
|
915
|
+
#### Parameters
|
|
916
|
+
|
|
917
|
+
##### callbackfn
|
|
918
|
+
|
|
919
|
+
`TreeMapEntryCallback`\<`K`, `V`, \[`MK`, `MV`\], `TreeMap`\<`K`, `V`, \[`K`, `V`\]\>\>
|
|
920
|
+
|
|
921
|
+
##### options?
|
|
922
|
+
|
|
923
|
+
`Omit`\<`TreeMapOptions`\<`MK`, `MV`, \[`MK`, `MV`\]\>, `"toEntryFn"`\> & `object` = `{}`
|
|
924
|
+
|
|
925
|
+
##### thisArg?
|
|
926
|
+
|
|
927
|
+
`unknown`
|
|
928
|
+
|
|
929
|
+
#### Returns
|
|
930
|
+
|
|
931
|
+
`TreeMap`\<`MK`, `MV`\>
|
|
932
|
+
|
|
933
|
+
#### Remarks
|
|
934
|
+
|
|
935
|
+
Time O(n log n) expected, Space O(n)
|
|
936
|
+
|
|
937
|
+
*
|
|
938
|
+
|
|
939
|
+
#### Example
|
|
940
|
+
|
|
941
|
+
```ts
|
|
942
|
+
// Transform entries
|
|
943
|
+
const tm = new TreeMap<number, number>([[1, 10], [2, 20]]);
|
|
944
|
+
const doubled = tm.map((v, k) => [k, (v ?? 0) * 2] as [number, number]);
|
|
945
|
+
console.log([...doubled.values()]); // [20, 40];
|
|
946
|
+
```
|
|
947
|
+
|
|
948
|
+
***
|
|
949
|
+
|
|
950
|
+
### pollFirst()
|
|
951
|
+
|
|
952
|
+
```ts
|
|
953
|
+
pollFirst(): [K, V | undefined] | undefined;
|
|
954
|
+
```
|
|
955
|
+
|
|
956
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:3515](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L3515)
|
|
957
|
+
|
|
958
|
+
Remove and return the smallest entry.
|
|
959
|
+
|
|
960
|
+
*
|
|
961
|
+
|
|
962
|
+
#### Returns
|
|
963
|
+
|
|
964
|
+
\[`K`, `V` \| `undefined`\] \| `undefined`
|
|
965
|
+
|
|
966
|
+
#### Example
|
|
967
|
+
|
|
968
|
+
```ts
|
|
969
|
+
// Process items from lowest priority
|
|
970
|
+
const tasks = new TreeMap<number, string>([
|
|
971
|
+
[3, 'Low'],
|
|
972
|
+
[1, 'Critical'],
|
|
973
|
+
[2, 'Medium']
|
|
974
|
+
]);
|
|
975
|
+
|
|
976
|
+
// Process lowest priority first
|
|
977
|
+
console.log(tasks.pollFirst()); // [1, 'Critical'];
|
|
978
|
+
console.log(tasks.pollFirst()); // [2, 'Medium'];
|
|
979
|
+
console.log(tasks.size); // 1;
|
|
980
|
+
```
|
|
981
|
+
|
|
982
|
+
***
|
|
983
|
+
|
|
984
|
+
### pollLast()
|
|
985
|
+
|
|
986
|
+
```ts
|
|
987
|
+
pollLast(): [K, V | undefined] | undefined;
|
|
988
|
+
```
|
|
989
|
+
|
|
990
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:3572](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L3572)
|
|
991
|
+
|
|
992
|
+
Remove and return the largest entry.
|
|
993
|
+
|
|
994
|
+
*
|
|
995
|
+
|
|
996
|
+
#### Returns
|
|
997
|
+
|
|
998
|
+
\[`K`, `V` \| `undefined`\] \| `undefined`
|
|
999
|
+
|
|
1000
|
+
#### Example
|
|
1001
|
+
|
|
1002
|
+
```ts
|
|
1003
|
+
// Remove the maximum entry
|
|
1004
|
+
const bids = new TreeMap<number, string>([
|
|
1005
|
+
[100, 'Alice'],
|
|
1006
|
+
[150, 'Bob'],
|
|
1007
|
+
[120, 'Charlie']
|
|
1008
|
+
]);
|
|
1009
|
+
|
|
1010
|
+
// Remove highest bid
|
|
1011
|
+
console.log(bids.pollLast()); // [150, 'Bob'];
|
|
1012
|
+
console.log(bids.size); // 2;
|
|
1013
|
+
console.log(bids.last()); // [120, 'Charlie'];
|
|
1014
|
+
```
|
|
1015
|
+
|
|
1016
|
+
***
|
|
1017
|
+
|
|
1018
|
+
### print()
|
|
1019
|
+
|
|
1020
|
+
```ts
|
|
1021
|
+
print(): void;
|
|
1022
|
+
```
|
|
1023
|
+
|
|
1024
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:3335](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L3335)
|
|
1025
|
+
|
|
1026
|
+
Print a human-friendly representation.
|
|
1027
|
+
|
|
1028
|
+
#### Returns
|
|
1029
|
+
|
|
1030
|
+
`void`
|
|
1031
|
+
|
|
1032
|
+
#### Remarks
|
|
1033
|
+
|
|
1034
|
+
Time O(n), Space O(n)
|
|
1035
|
+
|
|
1036
|
+
*
|
|
1037
|
+
|
|
1038
|
+
#### Example
|
|
1039
|
+
|
|
1040
|
+
```ts
|
|
1041
|
+
// Display tree
|
|
1042
|
+
const tm = new TreeMap<number, string>([[1, 'a']]);
|
|
1043
|
+
expect(() => tm.print()).not.toThrow();
|
|
1044
|
+
```
|
|
1045
|
+
|
|
1046
|
+
***
|
|
1047
|
+
|
|
1048
|
+
### rangeByRank()
|
|
1049
|
+
|
|
1050
|
+
```ts
|
|
1051
|
+
rangeByRank(start, end): [K, V | undefined][];
|
|
1052
|
+
```
|
|
1053
|
+
|
|
1054
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:4476](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L4476)
|
|
1055
|
+
|
|
1056
|
+
Returns keys by rank range (0-indexed, inclusive on both ends).
|
|
1057
|
+
|
|
1058
|
+
#### Parameters
|
|
1059
|
+
|
|
1060
|
+
##### start
|
|
1061
|
+
|
|
1062
|
+
`number`
|
|
1063
|
+
|
|
1064
|
+
##### end
|
|
1065
|
+
|
|
1066
|
+
`number`
|
|
1067
|
+
|
|
1068
|
+
#### Returns
|
|
1069
|
+
|
|
1070
|
+
\[`K`, `V` \| `undefined`\][]
|
|
1071
|
+
|
|
1072
|
+
#### Remarks
|
|
1073
|
+
|
|
1074
|
+
Time O(log n + k). Requires `enableOrderStatistic: true`.
|
|
1075
|
+
|
|
1076
|
+
*
|
|
1077
|
+
|
|
1078
|
+
#### Example
|
|
1079
|
+
|
|
1080
|
+
```ts
|
|
1081
|
+
// Pagination with rangeByRank
|
|
1082
|
+
const tree = new TreeMap<number>(
|
|
1083
|
+
[10, 20, 30, 40, 50, 60, 70, 80, 90],
|
|
1084
|
+
{ enableOrderStatistic: true }
|
|
1085
|
+
);
|
|
1086
|
+
const pageSize = 3;
|
|
1087
|
+
|
|
1088
|
+
// Page 1
|
|
1089
|
+
console.log(tree.rangeByRank(0, pageSize - 1)); // [10, 20, 30];
|
|
1090
|
+
// Page 2
|
|
1091
|
+
console.log(tree.rangeByRank(pageSize, 2 * pageSize - 1)); // [40, 50, 60];
|
|
1092
|
+
// Page 3
|
|
1093
|
+
console.log(tree.rangeByRank(2 * pageSize, 3 * pageSize - 1)); // [70, 80, 90];
|
|
1094
|
+
```
|
|
1095
|
+
|
|
1096
|
+
***
|
|
1097
|
+
|
|
1098
|
+
### rangeSearch()
|
|
1099
|
+
|
|
1100
|
+
```ts
|
|
1101
|
+
rangeSearch(range, options?): [K, V | undefined][];
|
|
1102
|
+
```
|
|
1103
|
+
|
|
1104
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:4395](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L4395)
|
|
1105
|
+
|
|
1106
|
+
Return all entries in a given key range.
|
|
1107
|
+
|
|
1108
|
+
#### Parameters
|
|
1109
|
+
|
|
1110
|
+
##### range
|
|
1111
|
+
|
|
1112
|
+
\[`K`, `K`\]
|
|
1113
|
+
|
|
1114
|
+
`[low, high]`
|
|
1115
|
+
|
|
1116
|
+
##### options?
|
|
1117
|
+
|
|
1118
|
+
`TreeMapRangeOptions` = `{}`
|
|
1119
|
+
|
|
1120
|
+
Inclusive/exclusive bounds (defaults to inclusive).
|
|
1121
|
+
|
|
1122
|
+
*
|
|
1123
|
+
|
|
1124
|
+
#### Returns
|
|
1125
|
+
|
|
1126
|
+
\[`K`, `V` \| `undefined`\][]
|
|
1127
|
+
|
|
1128
|
+
#### Example
|
|
1129
|
+
|
|
1130
|
+
```ts
|
|
1131
|
+
// Inventory system with price-sorted products
|
|
1132
|
+
interface Product {
|
|
1133
|
+
name: string;
|
|
1134
|
+
price: number;
|
|
1135
|
+
stock: number;
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
const inventory = new TreeMap<string, Product, Product>(
|
|
1139
|
+
[
|
|
1140
|
+
{ name: 'Widget', price: 9.99, stock: 100 },
|
|
1141
|
+
{ name: 'Gadget', price: 24.99, stock: 50 },
|
|
1142
|
+
{ name: 'Doohickey', price: 4.99, stock: 200 }
|
|
1143
|
+
],
|
|
1144
|
+
{ toEntryFn: p => [p.name, p] }
|
|
1145
|
+
);
|
|
1146
|
+
|
|
1147
|
+
// Sorted alphabetically by product name
|
|
1148
|
+
console.log([...inventory.keys()]); // ['Doohickey', 'Gadget', 'Widget'];
|
|
1149
|
+
|
|
1150
|
+
// Filter high-stock items
|
|
1151
|
+
const highStock = inventory.filter(p => (p?.stock ?? 0) > 75);
|
|
1152
|
+
console.log([...highStock.keys()]); // ['Doohickey', 'Widget'];
|
|
1153
|
+
|
|
1154
|
+
// Calculate total inventory value
|
|
1155
|
+
const totalValue = inventory.reduce(
|
|
1156
|
+
(sum, p) => sum + (p ? p.price * p.stock : 0),
|
|
1157
|
+
0
|
|
1158
|
+
);
|
|
1159
|
+
console.log(totalValue); // toBeCloseTo;
|
|
1160
|
+
```
|
|
1161
|
+
|
|
1162
|
+
***
|
|
1163
|
+
|
|
1164
|
+
### reduce()
|
|
1165
|
+
|
|
1166
|
+
```ts
|
|
1167
|
+
reduce<A>(callbackfn, initialValue): A;
|
|
1168
|
+
```
|
|
1169
|
+
|
|
1170
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:2466](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L2466)
|
|
1171
|
+
|
|
1172
|
+
Reduce entries into a single accumulator.
|
|
1173
|
+
|
|
1174
|
+
#### Type Parameters
|
|
1175
|
+
|
|
1176
|
+
##### A
|
|
1177
|
+
|
|
1178
|
+
`A`
|
|
1179
|
+
|
|
1180
|
+
#### Parameters
|
|
1181
|
+
|
|
1182
|
+
##### callbackfn
|
|
1183
|
+
|
|
1184
|
+
`TreeMapReduceCallback`\<`K`, `V`, `A`, `TreeMap`\<`K`, `V`, \[`K`, `V`\]\>\>
|
|
1185
|
+
|
|
1186
|
+
##### initialValue
|
|
1187
|
+
|
|
1188
|
+
`A`
|
|
1189
|
+
|
|
1190
|
+
#### Returns
|
|
1191
|
+
|
|
1192
|
+
`A`
|
|
1193
|
+
|
|
1194
|
+
#### Remarks
|
|
1195
|
+
|
|
1196
|
+
Time O(n), Space O(1)
|
|
1197
|
+
|
|
1198
|
+
*
|
|
1199
|
+
|
|
1200
|
+
#### Example
|
|
1201
|
+
|
|
1202
|
+
```ts
|
|
1203
|
+
// Aggregate values
|
|
1204
|
+
const tm = new TreeMap<number, number>([[1, 10], [2, 20]]);
|
|
1205
|
+
console.log(tm.reduce((acc, v) => acc + (v ?? 0), 0)); // 30;
|
|
1206
|
+
```
|
|
1207
|
+
|
|
1208
|
+
***
|
|
1209
|
+
|
|
1210
|
+
### set()
|
|
1211
|
+
|
|
1212
|
+
```ts
|
|
1213
|
+
set(key, value): this;
|
|
1214
|
+
```
|
|
1215
|
+
|
|
1216
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:497](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L497)
|
|
1217
|
+
|
|
1218
|
+
Set or overwrite a value for a key.
|
|
1219
|
+
|
|
1220
|
+
#### Parameters
|
|
1221
|
+
|
|
1222
|
+
##### key
|
|
1223
|
+
|
|
1224
|
+
`K`
|
|
1225
|
+
|
|
1226
|
+
##### value
|
|
1227
|
+
|
|
1228
|
+
`V` \| `undefined`
|
|
1229
|
+
|
|
1230
|
+
#### Returns
|
|
1231
|
+
|
|
1232
|
+
`this`
|
|
1233
|
+
|
|
1234
|
+
#### Remarks
|
|
1235
|
+
|
|
1236
|
+
Expected time O(log n)
|
|
1237
|
+
|
|
1238
|
+
*
|
|
1239
|
+
|
|
1240
|
+
#### Example
|
|
1241
|
+
|
|
1242
|
+
```ts
|
|
1243
|
+
// Sorted dictionary for a contact book
|
|
1244
|
+
const contacts = new TreeMap<string, string>([
|
|
1245
|
+
['Bob', '555-0102'],
|
|
1246
|
+
['Alice', '555-0101'],
|
|
1247
|
+
['Charlie', '555-0103']
|
|
1248
|
+
]);
|
|
1249
|
+
|
|
1250
|
+
// Contacts are automatically sorted by name
|
|
1251
|
+
console.log([...contacts.keys()]); // ['Alice', 'Bob', 'Charlie'];
|
|
1252
|
+
console.log(contacts.get('Bob')); // '555-0102';
|
|
1253
|
+
|
|
1254
|
+
// Find the first contact alphabetically after 'B'
|
|
1255
|
+
console.log(contacts.ceiling('B')); // ['Bob', '555-0102'];
|
|
1256
|
+
|
|
1257
|
+
// Find contacts in range
|
|
1258
|
+
console.log(contacts.rangeSearch(['Alice', 'Bob'])); // [
|
|
1259
|
+
// ['Alice', '555-0101'],
|
|
1260
|
+
// ['Bob', '555-0102']
|
|
1261
|
+
// ];
|
|
1262
|
+
```
|
|
1263
|
+
|
|
1264
|
+
***
|
|
1265
|
+
|
|
1266
|
+
### some()
|
|
1267
|
+
|
|
1268
|
+
```ts
|
|
1269
|
+
some(callbackfn, thisArg?): boolean;
|
|
1270
|
+
```
|
|
1271
|
+
|
|
1272
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:2812](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L2812)
|
|
1273
|
+
|
|
1274
|
+
Test whether any entry satisfies a predicate.
|
|
1275
|
+
|
|
1276
|
+
#### Parameters
|
|
1277
|
+
|
|
1278
|
+
##### callbackfn
|
|
1279
|
+
|
|
1280
|
+
`TreeMapEntryCallback`\<`K`, `V`, `boolean`, `TreeMap`\<`K`, `V`, \[`K`, `V`\]\>\>
|
|
1281
|
+
|
|
1282
|
+
##### thisArg?
|
|
1283
|
+
|
|
1284
|
+
`unknown`
|
|
1285
|
+
|
|
1286
|
+
#### Returns
|
|
1287
|
+
|
|
1288
|
+
`boolean`
|
|
1289
|
+
|
|
1290
|
+
#### Remarks
|
|
1291
|
+
|
|
1292
|
+
Time O(n), Space O(1)
|
|
1293
|
+
|
|
1294
|
+
*
|
|
1295
|
+
|
|
1296
|
+
#### Example
|
|
1297
|
+
|
|
1298
|
+
```ts
|
|
1299
|
+
// Test any entry
|
|
1300
|
+
const tm = new TreeMap<number, string>([[1, 'a'], [2, 'b']]);
|
|
1301
|
+
console.log(tm.some((v, k) => k === 2)); // true;
|
|
1302
|
+
```
|
|
1303
|
+
|
|
1304
|
+
***
|
|
1305
|
+
|
|
1306
|
+
### toArray()
|
|
1307
|
+
|
|
1308
|
+
```ts
|
|
1309
|
+
toArray(): [K, V | undefined][];
|
|
1310
|
+
```
|
|
1311
|
+
|
|
1312
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:3165](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L3165)
|
|
1313
|
+
|
|
1314
|
+
Materialize the map into an array of `[key, value]` tuples.
|
|
1315
|
+
|
|
1316
|
+
#### Returns
|
|
1317
|
+
|
|
1318
|
+
\[`K`, `V` \| `undefined`\][]
|
|
1319
|
+
|
|
1320
|
+
#### Remarks
|
|
1321
|
+
|
|
1322
|
+
Time O(n), Space O(n)
|
|
1323
|
+
|
|
1324
|
+
*
|
|
1325
|
+
|
|
1326
|
+
#### Example
|
|
1327
|
+
|
|
1328
|
+
```ts
|
|
1329
|
+
// Convert to array
|
|
1330
|
+
const tm = new TreeMap<number, string>([[2, 'b'], [1, 'a']]);
|
|
1331
|
+
console.log(tm.toArray()); // [[1, 'a'], [2, 'b']];
|
|
1332
|
+
```
|
|
1333
|
+
|
|
1334
|
+
***
|
|
1335
|
+
|
|
1336
|
+
### values()
|
|
1337
|
+
|
|
1338
|
+
```ts
|
|
1339
|
+
values(): IterableIterator<V | undefined>;
|
|
1340
|
+
```
|
|
1341
|
+
|
|
1342
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:1584](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L1584)
|
|
1343
|
+
|
|
1344
|
+
Iterate over values in ascending key order.
|
|
1345
|
+
|
|
1346
|
+
Note: values may be `undefined` (TreeMap allows storing `undefined`, like native `Map`).
|
|
1347
|
+
|
|
1348
|
+
*
|
|
1349
|
+
|
|
1350
|
+
#### Returns
|
|
1351
|
+
|
|
1352
|
+
`IterableIterator`\<`V` \| `undefined`\>
|
|
1353
|
+
|
|
1354
|
+
#### Example
|
|
1355
|
+
|
|
1356
|
+
```ts
|
|
1357
|
+
// Get values in key order
|
|
1358
|
+
const tm = new TreeMap<number, string>([[2, 'b'], [1, 'a']]);
|
|
1359
|
+
console.log([...tm.values()]); // ['a', 'b'];
|
|
1360
|
+
```
|
|
1361
|
+
|
|
1362
|
+
***
|
|
1363
|
+
|
|
1364
|
+
### createDefaultComparator()
|
|
1365
|
+
|
|
1366
|
+
```ts
|
|
1367
|
+
static createDefaultComparator<K>(): Comparator<K>;
|
|
1368
|
+
```
|
|
1369
|
+
|
|
1370
|
+
Defined in: [data-structures/binary-tree/tree-map.ts:87](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/tree-map.ts#L87)
|
|
1371
|
+
|
|
1372
|
+
Create the strict default comparator.
|
|
1373
|
+
|
|
1374
|
+
Supports:
|
|
1375
|
+
- `number` (rejects `NaN`; treats `-0` and `0` as equal)
|
|
1376
|
+
- `string`
|
|
1377
|
+
- `Date` (orders by `getTime()`, rejects invalid dates)
|
|
1378
|
+
|
|
1379
|
+
For other key types, a custom comparator must be provided.
|
|
1380
|
+
|
|
1381
|
+
#### Type Parameters
|
|
1382
|
+
|
|
1383
|
+
##### K
|
|
1384
|
+
|
|
1385
|
+
`K`
|
|
1386
|
+
|
|
1387
|
+
#### Returns
|
|
1388
|
+
|
|
1389
|
+
`Comparator`\<`K`\>
|