Open History to see the last commit
1/**
2 * A Map implementation that keeps its entries sorted based on a comparator function
3 * @template TKey - The type of keys in the map
4 * @template TValue - The type of values in the map
5 */
6export class SortedMap<TKey, TValue> {
7 private map: Map<TKey, TValue>
8 private sortedKeys: Array<TKey>
9 private comparator: (a: TValue, b: TValue) => number
10
11 /**
12 * Creates a new SortedMap instance
13 *
14 * @param comparator - Optional function to compare values for sorting
15 */
16 constructor(comparator?: (a: TValue, b: TValue) => number) {
17 this.map = new Map<TKey, TValue>()
18 this.sortedKeys = []
19 this.comparator = comparator || this.defaultComparator
20 }
21
22 /**
23 * Default comparator function used when none is provided
24 *
25 * @param a - First value to compare
26 * @param b - Second value to compare
27 * @returns -1 if a < b, 1 if a > b, 0 if equal
28 */
29 private defaultComparator(a: TValue, b: TValue): number {
30 if (a < b) return -1
31 if (a > b) return 1
32 return 0
33 }
34
35 /**
36 * Sets a key-value pair in the map and maintains sort order
37 *
38 * @param key - The key to set
39 * @param value - The value to associate with the key
40 * @returns This SortedMap instance for chaining
41 */
42 set(key: TKey, value: TValue): this {
43 this.map.set(key, value)
44
45 if (!this.sortedKeys.includes(key)) {
46 this.sortedKeys.push(key)
47 }
48
49 // Re-sort keys based on values
50 this.sortedKeys.sort((a, b) => {
51 const valueA = this.map.get(a)!
52 const valueB = this.map.get(b)!
53 return this.comparator(valueA, valueB)
54 })
55
56 return this
57 }
58
59 /**
60 * Gets a value by its key
61 *
62 * @param key - The key to look up
63 * @returns The value associated with the key, or undefined if not found
64 */
65 get(key: TKey): TValue | undefined {
66 return this.map.get(key)
67 }
68
69 /**
70 * Removes a key-value pair from the map
71 *
72 * @param key - The key to remove
73 * @returns True if the key was found and removed, false otherwise
74 */
75 delete(key: TKey): boolean {
76 if (this.map.delete(key)) {
77 const index = this.sortedKeys.indexOf(key)
78 this.sortedKeys.splice(index, 1)
79 return true
80 }
81 return false
82 }
83
84 /**
85 * Checks if a key exists in the map
86 *
87 * @param key - The key to check
88 * @returns True if the key exists, false otherwise
89 */
90 has(key: TKey): boolean {
91 return this.map.has(key)
92 }
93
94 /**
95 * Removes all key-value pairs from the map
96 */
97 clear(): void {
98 this.map.clear()
99 this.sortedKeys = []
100 }
101
102 /**
103 * Gets the number of key-value pairs in the map
104 */
105 get size(): number {
106 return this.map.size
107 }
108
109 /**
110 * Default iterator that returns entries in sorted order
111 *
112 * @returns An iterator for the map's entries
113 */
114 *[Symbol.iterator](): IterableIterator<[TKey, TValue]> {
115 for (const key of this.sortedKeys) {
116 yield [key, this.map.get(key)!] as [TKey, TValue]
117 }
118 }
119
120 /**
121 * Returns an iterator for the map's entries in sorted order
122 *
123 * @returns An iterator for the map's entries
124 */
125 entries(): IterableIterator<[TKey, TValue]> {
126 return this[Symbol.iterator]()
127 }
128
129 /**
130 * Returns an iterator for the map's keys in sorted order
131 *
132 * @returns An iterator for the map's keys
133 */
134 keys(): IterableIterator<TKey> {
135 return this.sortedKeys[Symbol.iterator]()
136 }
137
138 /**
139 * Returns an iterator for the map's values in sorted order
140 *
141 * @returns An iterator for the map's values
142 */
143 values(): IterableIterator<TValue> {
144 return function* (this: SortedMap<TKey, TValue>) {
145 for (const key of this.sortedKeys) {
146 yield this.map.get(key)!
147 }
148 }.call(this)
149 }
150
151 /**
152 * Executes a callback function for each key-value pair in the map in sorted order
153 *
154 * @param callbackfn - Function to execute for each entry
155 */
156 forEach(
157 callbackfn: (value: TValue, key: TKey, map: Map<TKey, TValue>) => void
158 ): void {
159 for (const key of this.sortedKeys) {
160 callbackfn(this.map.get(key)!, key, this.map)
161 }
162 }
163}