|
| 1 | +/** |
| 2 | + * Configuration object for creating a new HAMT map with custom hash and key comparison functions. |
| 3 | + */ |
| 4 | +export interface HamtConfig<K = any> { |
| 5 | + /** Custom hash function for keys */ |
| 6 | + hash?: ((key: K) => number) | undefined; |
| 7 | + /** Custom key equality comparison function */ |
| 8 | + keyEq?: ((a: K, b: K) => boolean) | undefined; |
| 9 | +} |
| 10 | + |
| 11 | +/** |
| 12 | + * A Hash Array Mapped Trie (HAMT) map implementation. |
| 13 | + * This is a persistent data structure that supports efficient updates and lookups. |
| 14 | + */ |
| 15 | +export interface HamtMap<K = any, V = any> extends Iterable<[K, V]> { |
| 16 | + /** Check if the map is empty */ |
| 17 | + isEmpty(): boolean; |
| 18 | + |
| 19 | + /** Get the number of entries in the map */ |
| 20 | + count(): number; |
| 21 | + |
| 22 | + /** Get the number of entries in the map (alias for count) */ |
| 23 | + readonly size: number; |
| 24 | + |
| 25 | + /** Get a value by key */ |
| 26 | + get(key: K): V | undefined; |
| 27 | + |
| 28 | + /** Get a value by key using a custom hash */ |
| 29 | + getHash(hash: number, key: K): V | undefined; |
| 30 | + |
| 31 | + /** Get a value by key, returning alt if not found */ |
| 32 | + tryGet(alt: V, key: K): V; |
| 33 | + |
| 34 | + /** Get a value by key using a custom hash, returning alt if not found */ |
| 35 | + tryGetHash(alt: V, hash: number, key: K): V; |
| 36 | + |
| 37 | + /** Check if a key exists in the map */ |
| 38 | + has(key: K): boolean; |
| 39 | + |
| 40 | + /** Check if a key exists in the map using a custom hash */ |
| 41 | + hasHash(hash: number, key: K): boolean; |
| 42 | + |
| 43 | + /** Set a value for a key, returning a new map */ |
| 44 | + set(key: K, value: V): HamtMap<K, V>; |
| 45 | + |
| 46 | + /** Set a value for a key using a custom hash, returning a new map */ |
| 47 | + setHash(hash: number, key: K, value: V): HamtMap<K, V>; |
| 48 | + |
| 49 | + /** Modify a value for a key using a function, returning a new map */ |
| 50 | + modify(key: K, f: (value: V | undefined) => V): HamtMap<K, V>; |
| 51 | + |
| 52 | + /** Modify a value for a key using a function and custom hash, returning a new map */ |
| 53 | + modifyHash(hash: number, key: K, f: (value: V | undefined) => V): HamtMap<K, V>; |
| 54 | + |
| 55 | + /** Remove a key from the map, returning a new map */ |
| 56 | + remove(key: K): HamtMap<K, V>; |
| 57 | + |
| 58 | + /** Remove a key from the map, returning a new map (alias for remove) */ |
| 59 | + delete(key: K): HamtMap<K, V>; |
| 60 | + |
| 61 | + /** Remove a key from the map using a custom hash, returning a new map */ |
| 62 | + removeHash(hash: number, key: K): HamtMap<K, V>; |
| 63 | + |
| 64 | + /** Remove a key from the map using a custom hash, returning a new map (alias for removeHash) */ |
| 65 | + deleteHash(hash: number, key: K): HamtMap<K, V>; |
| 66 | + |
| 67 | + /** Begin mutation mode for efficient batch operations */ |
| 68 | + beginMutation(): HamtMap<K, V>; |
| 69 | + |
| 70 | + /** End mutation mode */ |
| 71 | + endMutation(): HamtMap<K, V>; |
| 72 | + |
| 73 | + /** Execute a function in mutation context for efficient batch operations */ |
| 74 | + mutate(f: (map: HamtMap<K, V>) => HamtMap<K, V>): HamtMap<K, V>; |
| 75 | + |
| 76 | + /** Fold over all entries in the map */ |
| 77 | + fold<Z>(f: (acc: Z, value: V, key: K) => Z, initial: Z): Z; |
| 78 | + |
| 79 | + /** Execute a function for each entry in the map */ |
| 80 | + forEach(f: (value: V, key: K, map: HamtMap<K, V>) => void): void; |
| 81 | + |
| 82 | + /** Get an iterator for all entries [key, value] */ |
| 83 | + entries(): IterableIterator<[K, V]>; |
| 84 | + |
| 85 | + /** Get an iterator for all keys */ |
| 86 | + keys(): IterableIterator<K>; |
| 87 | + |
| 88 | + /** Get an iterator for all values */ |
| 89 | + values(): IterableIterator<V>; |
| 90 | +} |
| 91 | + |
| 92 | +/** A pre-created empty HAMT map */ |
| 93 | +export const empty: HamtMap<any, any>; |
| 94 | + |
| 95 | +/** Create a new empty HAMT map */ |
| 96 | +export function make<K = any>(config?: HamtConfig<K>): HamtMap<K, any>; |
| 97 | + |
| 98 | +/** Check if a map is empty */ |
| 99 | +export function isEmpty(map: HamtMap<any, any>): boolean; |
| 100 | + |
| 101 | +/** Get a value by key */ |
| 102 | +export function get<K, V>(key: K, map: HamtMap<K, V>): V | undefined; |
| 103 | + |
| 104 | +/** Get a value by key using a custom hash */ |
| 105 | +export function getHash<K, V>(hash: number, key: K, map: HamtMap<K, V>): V | undefined; |
| 106 | + |
| 107 | +/** Get a value by key, returning alt if not found */ |
| 108 | +export function tryGet<K, V>(alt: V, key: K, map: HamtMap<K, V>): V; |
| 109 | + |
| 110 | +/** Get a value by key using a custom hash, returning alt if not found */ |
| 111 | +export function tryGetHash<K, V>(alt: V, hash: number, key: K, map: HamtMap<K, V>): V; |
| 112 | + |
| 113 | +/** Check if a key exists in the map */ |
| 114 | +export function has<K>(key: K, map: HamtMap<K, any>): boolean; |
| 115 | + |
| 116 | +/** Set a value for a key, returning a new map */ |
| 117 | +export function set<K, V>(key: K, value: V, map: HamtMap<K, V>): HamtMap<K, V>; |
| 118 | + |
| 119 | +/** Set a value for a key using a custom hash, returning a new map */ |
| 120 | +export function setHash<K, V>(hash: number, key: K, value: V, map: HamtMap<K, V>): HamtMap<K, V>; |
| 121 | + |
| 122 | +/** Modify a value for a key using a function, returning a new map */ |
| 123 | +export function modify<K, V>(f: (value: V | undefined) => V, key: K, map: HamtMap<K, V>): HamtMap<K, V>; |
| 124 | + |
| 125 | +/** Modify a value for a key using a function and custom hash, returning a new map */ |
| 126 | +export function modifyHash<K, V>( |
| 127 | + f: (value: V | undefined) => V, |
| 128 | + hash: number, |
| 129 | + key: K, |
| 130 | + map: HamtMap<K, V>, |
| 131 | +): HamtMap<K, V>; |
| 132 | + |
| 133 | +/** Remove a key from the map, returning a new map */ |
| 134 | +export function remove<K, V>(key: K, map: HamtMap<K, V>): HamtMap<K, V>; |
| 135 | + |
| 136 | +/** Remove a key from the map using a custom hash, returning a new map */ |
| 137 | +export function removeHash<K, V>(hash: number, key: K, map: HamtMap<K, V>): HamtMap<K, V>; |
| 138 | + |
| 139 | +/** Begin mutation mode for efficient batch operations */ |
| 140 | +export function beginMutation<K, V>(map: HamtMap<K, V>): HamtMap<K, V>; |
| 141 | + |
| 142 | +/** End mutation mode */ |
| 143 | +export function endMutation<K, V>(map: HamtMap<K, V>): HamtMap<K, V>; |
| 144 | + |
| 145 | +/** Execute a function in mutation context for efficient batch operations */ |
| 146 | +export function mutate<K, V>(f: (map: HamtMap<K, V>) => HamtMap<K, V>, map: HamtMap<K, V>): HamtMap<K, V>; |
| 147 | + |
| 148 | +/** Get the number of entries in the map */ |
| 149 | +export function count(map: HamtMap<any, any>): number; |
| 150 | + |
| 151 | +/** Fold over all entries in the map */ |
| 152 | +export function fold<K, V, Z>(f: (acc: Z, value: V, key: K) => Z, initial: Z, map: HamtMap<K, V>): Z; |
| 153 | + |
| 154 | +/** Execute a function for each entry in the map */ |
| 155 | +export function forEach<K, V>(f: (value: V, key: K, map: HamtMap<K, V>) => void, map: HamtMap<K, V>): void; |
| 156 | + |
| 157 | +/** Get an iterator for all entries [key, value] */ |
| 158 | +export function entries<K, V>(map: HamtMap<K, V>): IterableIterator<[K, V]>; |
| 159 | + |
| 160 | +/** Get an iterator for all keys */ |
| 161 | +export function keys<K>(map: HamtMap<K, any>): IterableIterator<K>; |
| 162 | + |
| 163 | +/** Get an iterator for all values */ |
| 164 | +export function values<V>(map: HamtMap<any, V>): IterableIterator<V>; |
| 165 | + |
| 166 | +/** Compute a hash for a value */ |
| 167 | +export function hash(value: any): number; |
| 168 | + |
| 169 | +// Global/ambient declarations for DefinitelyTyped testing |
| 170 | +declare global { |
| 171 | + interface HamtConfig<K = any> { |
| 172 | + /** Custom hash function for keys */ |
| 173 | + hash?: ((key: K) => number) | undefined; |
| 174 | + /** Custom key equality comparison function */ |
| 175 | + keyEq?: ((a: K, b: K) => boolean) | undefined; |
| 176 | + } |
| 177 | + |
| 178 | + interface HamtMap<K = any, V = any> extends Iterable<[K, V]> { |
| 179 | + /** Check if the map is empty */ |
| 180 | + isEmpty(): boolean; |
| 181 | + |
| 182 | + /** Get the number of entries in the map */ |
| 183 | + count(): number; |
| 184 | + |
| 185 | + /** Get the number of entries in the map (alias for count) */ |
| 186 | + readonly size: number; |
| 187 | + |
| 188 | + /** Get a value by key */ |
| 189 | + get(key: K): V | undefined; |
| 190 | + |
| 191 | + /** Get a value by key using a custom hash */ |
| 192 | + getHash(hash: number, key: K): V | undefined; |
| 193 | + |
| 194 | + /** Get a value by key, returning alt if not found */ |
| 195 | + tryGet(alt: V, key: K): V; |
| 196 | + |
| 197 | + /** Get a value by key using a custom hash, returning alt if not found */ |
| 198 | + tryGetHash(alt: V, hash: number, key: K): V; |
| 199 | + |
| 200 | + /** Check if a key exists in the map */ |
| 201 | + has(key: K): boolean; |
| 202 | + |
| 203 | + /** Check if a key exists in the map using a custom hash */ |
| 204 | + hasHash(hash: number, key: K): boolean; |
| 205 | + |
| 206 | + /** Set a value for a key, returning a new map */ |
| 207 | + set(key: K, value: V): HamtMap<K, V>; |
| 208 | + |
| 209 | + /** Set a value for a key using a custom hash, returning a new map */ |
| 210 | + setHash(hash: number, key: K, value: V): HamtMap<K, V>; |
| 211 | + |
| 212 | + /** Modify a value for a key using a function, returning a new map */ |
| 213 | + modify(key: K, f: (value: V | undefined) => V): HamtMap<K, V>; |
| 214 | + |
| 215 | + /** Modify a value for a key using a function and custom hash, returning a new map */ |
| 216 | + modifyHash(hash: number, key: K, f: (value: V | undefined) => V): HamtMap<K, V>; |
| 217 | + |
| 218 | + /** Remove a key from the map, returning a new map */ |
| 219 | + remove(key: K): HamtMap<K, V>; |
| 220 | + |
| 221 | + /** Remove a key from the map, returning a new map (alias for remove) */ |
| 222 | + delete(key: K): HamtMap<K, V>; |
| 223 | + |
| 224 | + /** Remove a key from the map using a custom hash, returning a new map */ |
| 225 | + removeHash(hash: number, key: K): HamtMap<K, V>; |
| 226 | + |
| 227 | + /** Remove a key from the map using a custom hash, returning a new map (alias for removeHash) */ |
| 228 | + deleteHash(hash: number, key: K): HamtMap<K, V>; |
| 229 | + |
| 230 | + /** Begin mutation mode for efficient batch operations */ |
| 231 | + beginMutation(): HamtMap<K, V>; |
| 232 | + |
| 233 | + /** End mutation mode */ |
| 234 | + endMutation(): HamtMap<K, V>; |
| 235 | + |
| 236 | + /** Execute a function in mutation context for efficient batch operations */ |
| 237 | + mutate(f: (map: HamtMap<K, V>) => HamtMap<K, V>): HamtMap<K, V>; |
| 238 | + |
| 239 | + /** Fold over all entries in the map */ |
| 240 | + fold<Z>(f: (acc: Z, value: V, key: K) => Z, initial: Z): Z; |
| 241 | + |
| 242 | + /** Execute a function for each entry in the map */ |
| 243 | + forEach(f: (value: V, key: K, map: HamtMap<K, V>) => void): void; |
| 244 | + |
| 245 | + /** Get an iterator for all entries [key, value] */ |
| 246 | + entries(): IterableIterator<[K, V]>; |
| 247 | + |
| 248 | + /** Get an iterator for all keys */ |
| 249 | + keys(): IterableIterator<K>; |
| 250 | + |
| 251 | + /** Get an iterator for all values */ |
| 252 | + values(): IterableIterator<V>; |
| 253 | + } |
| 254 | + |
| 255 | + /** A pre-created empty HAMT map */ |
| 256 | + const empty: HamtMap<any, any>; |
| 257 | + |
| 258 | + /** Create a new empty HAMT map */ |
| 259 | + function make<K = any>(config?: HamtConfig<K>): HamtMap<K, any>; |
| 260 | + |
| 261 | + /** Check if a map is empty */ |
| 262 | + function isEmpty(map: HamtMap<any, any>): boolean; |
| 263 | + |
| 264 | + /** Get a value by key */ |
| 265 | + function get<K, V>(key: K, map: HamtMap<K, V>): V | undefined; |
| 266 | + |
| 267 | + /** Get a value by key using a custom hash */ |
| 268 | + function getHash<K, V>(hash: number, key: K, map: HamtMap<K, V>): V | undefined; |
| 269 | + |
| 270 | + /** Get a value by key, returning alt if not found */ |
| 271 | + function tryGet<K, V>(alt: V, key: K, map: HamtMap<K, V>): V; |
| 272 | + |
| 273 | + /** Get a value by key using a custom hash, returning alt if not found */ |
| 274 | + function tryGetHash<K, V>(alt: V, hash: number, key: K, map: HamtMap<K, V>): V; |
| 275 | + |
| 276 | + /** Check if a key exists in the map */ |
| 277 | + function has<K>(key: K, map: HamtMap<K, any>): boolean; |
| 278 | + |
| 279 | + /** Set a value for a key, returning a new map */ |
| 280 | + function set<K, V>(key: K, value: V, map: HamtMap<K, V>): HamtMap<K, V>; |
| 281 | + |
| 282 | + /** Set a value for a key using a custom hash, returning a new map */ |
| 283 | + function setHash<K, V>(hash: number, key: K, value: V, map: HamtMap<K, V>): HamtMap<K, V>; |
| 284 | + |
| 285 | + /** Modify a value for a key using a function, returning a new map */ |
| 286 | + function modify<K, V>(f: (value: V | undefined) => V, key: K, map: HamtMap<K, V>): HamtMap<K, V>; |
| 287 | + |
| 288 | + /** Modify a value for a key using a function and custom hash, returning a new map */ |
| 289 | + function modifyHash<K, V>(f: (value: V | undefined) => V, hash: number, key: K, map: HamtMap<K, V>): HamtMap<K, V>; |
| 290 | + |
| 291 | + /** Remove a key from the map, returning a new map */ |
| 292 | + function remove<K, V>(key: K, map: HamtMap<K, V>): HamtMap<K, V>; |
| 293 | + |
| 294 | + /** Remove a key from the map using a custom hash, returning a new map */ |
| 295 | + function removeHash<K, V>(hash: number, key: K, map: HamtMap<K, V>): HamtMap<K, V>; |
| 296 | + |
| 297 | + /** Begin mutation mode for efficient batch operations */ |
| 298 | + function beginMutation<K, V>(map: HamtMap<K, V>): HamtMap<K, V>; |
| 299 | + |
| 300 | + /** End mutation mode */ |
| 301 | + function endMutation<K, V>(map: HamtMap<K, V>): HamtMap<K, V>; |
| 302 | + |
| 303 | + /** Execute a function in mutation context for efficient batch operations */ |
| 304 | + function mutate<K, V>(f: (map: HamtMap<K, V>) => HamtMap<K, V>, map: HamtMap<K, V>): HamtMap<K, V>; |
| 305 | + |
| 306 | + /** Get the number of entries in the map */ |
| 307 | + function count(map: HamtMap<any, any>): number; |
| 308 | + |
| 309 | + /** Fold over all entries in the map */ |
| 310 | + function fold<K, V, Z>(f: (acc: Z, value: V, key: K) => Z, initial: Z, map: HamtMap<K, V>): Z; |
| 311 | + |
| 312 | + /** Execute a function for each entry in the map */ |
| 313 | + function forEach<K, V>(f: (value: V, key: K, map: HamtMap<K, V>) => void, map: HamtMap<K, V>): void; |
| 314 | + |
| 315 | + /** Get an iterator for all entries [key, value] */ |
| 316 | + function entries<K, V>(map: HamtMap<K, V>): IterableIterator<[K, V]>; |
| 317 | + |
| 318 | + /** Get an iterator for all keys */ |
| 319 | + function keys<K>(map: HamtMap<K, any>): IterableIterator<K>; |
| 320 | + |
| 321 | + /** Get an iterator for all values */ |
| 322 | + function values<V>(map: HamtMap<any, V>): IterableIterator<V>; |
| 323 | + |
| 324 | + /** Compute a hash for a value */ |
| 325 | + function hash(value: any): number; |
| 326 | +} |
0 commit comments