* Sets a key-value pair in the map and maintains sort order * * @param key - The key to set * @param value - The value to associate with the key * @returns This SortedMap instance for chaining
(key: TKey, value: TValue)
| 90 | * @returns This SortedMap instance for chaining |
| 91 | */ |
| 92 | set(key: TKey, value: TValue): this { |
| 93 | if (this.map.has(key)) { |
| 94 | // Need to remove the old key from the sorted keys array |
| 95 | const oldValue = this.map.get(key)! |
| 96 | const oldIndex = this.indexOf(key, oldValue) |
| 97 | this.sortedKeys.splice(oldIndex, 1) |
| 98 | } |
| 99 | |
| 100 | // Insert the new key at the correct position |
| 101 | const index = this.indexOf(key, value) |
| 102 | this.sortedKeys.splice(index, 0, key) |
| 103 | |
| 104 | this.map.set(key, value) |
| 105 | |
| 106 | return this |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Gets a value by its key |