|
| 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 | + */ |
| 6 | +export declare class SortedMap<TKey, TValue> { |
| 7 | + private map; |
| 8 | + private sortedKeys; |
| 9 | + private comparator; |
| 10 | + /** |
| 11 | + * Creates a new SortedMap instance |
| 12 | + * |
| 13 | + * @param comparator - Optional function to compare values for sorting |
| 14 | + */ |
| 15 | + constructor(comparator?: (a: TValue, b: TValue) => number); |
| 16 | + /** |
| 17 | + * Default comparator function used when none is provided |
| 18 | + * |
| 19 | + * @param a - First value to compare |
| 20 | + * @param b - Second value to compare |
| 21 | + * @returns -1 if a < b, 1 if a > b, 0 if equal |
| 22 | + */ |
| 23 | + private defaultComparator; |
| 24 | + /** |
| 25 | + * Finds the index where a key-value pair should be inserted to maintain sort order. |
| 26 | + * Uses binary search to find the correct position based on the value. |
| 27 | + * Hence, it is in O(log n) time. |
| 28 | + * |
| 29 | + * @param key - The key to find position for |
| 30 | + * @param value - The value to compare against |
| 31 | + * @returns The index where the key should be inserted |
| 32 | + */ |
| 33 | + private indexOf; |
| 34 | + /** |
| 35 | + * Sets a key-value pair in the map and maintains sort order |
| 36 | + * |
| 37 | + * @param key - The key to set |
| 38 | + * @param value - The value to associate with the key |
| 39 | + * @returns This SortedMap instance for chaining |
| 40 | + */ |
| 41 | + set(key: TKey, value: TValue): this; |
| 42 | + /** |
| 43 | + * Gets a value by its key |
| 44 | + * |
| 45 | + * @param key - The key to look up |
| 46 | + * @returns The value associated with the key, or undefined if not found |
| 47 | + */ |
| 48 | + get(key: TKey): TValue | undefined; |
| 49 | + /** |
| 50 | + * Removes a key-value pair from the map |
| 51 | + * |
| 52 | + * @param key - The key to remove |
| 53 | + * @returns True if the key was found and removed, false otherwise |
| 54 | + */ |
| 55 | + delete(key: TKey): boolean; |
| 56 | + /** |
| 57 | + * Checks if a key exists in the map |
| 58 | + * |
| 59 | + * @param key - The key to check |
| 60 | + * @returns True if the key exists, false otherwise |
| 61 | + */ |
| 62 | + has(key: TKey): boolean; |
| 63 | + /** |
| 64 | + * Removes all key-value pairs from the map |
| 65 | + */ |
| 66 | + clear(): void; |
| 67 | + /** |
| 68 | + * Gets the number of key-value pairs in the map |
| 69 | + */ |
| 70 | + get size(): number; |
| 71 | + /** |
| 72 | + * Default iterator that returns entries in sorted order |
| 73 | + * |
| 74 | + * @returns An iterator for the map's entries |
| 75 | + */ |
| 76 | + [Symbol.iterator](): IterableIterator<[TKey, TValue]>; |
| 77 | + /** |
| 78 | + * Returns an iterator for the map's entries in sorted order |
| 79 | + * |
| 80 | + * @returns An iterator for the map's entries |
| 81 | + */ |
| 82 | + entries(): IterableIterator<[TKey, TValue]>; |
| 83 | + /** |
| 84 | + * Returns an iterator for the map's keys in sorted order |
| 85 | + * |
| 86 | + * @returns An iterator for the map's keys |
| 87 | + */ |
| 88 | + keys(): IterableIterator<TKey>; |
| 89 | + /** |
| 90 | + * Returns an iterator for the map's values in sorted order |
| 91 | + * |
| 92 | + * @returns An iterator for the map's values |
| 93 | + */ |
| 94 | + values(): IterableIterator<TValue>; |
| 95 | + /** |
| 96 | + * Executes a callback function for each key-value pair in the map in sorted order |
| 97 | + * |
| 98 | + * @param callbackfn - Function to execute for each entry |
| 99 | + */ |
| 100 | + forEach(callbackfn: (value: TValue, key: TKey, map: Map<TKey, TValue>) => void): void; |
| 101 | +} |
0 commit comments