* Performs a range query using binary search - O(log n + m)
(options: RangeQueryOptions = {})
| 252 | * Performs a range query using binary search - O(log n + m) |
| 253 | */ |
| 254 | rangeQuery(options: RangeQueryOptions = {}): Set<TKey> { |
| 255 | const { from, to, fromInclusive = true, toInclusive = true } = options |
| 256 | const result = new Set<TKey>() |
| 257 | |
| 258 | if (this.sortedValues.length === 0) { |
| 259 | return result |
| 260 | } |
| 261 | |
| 262 | const normalizedFrom = normalizeValue(from) |
| 263 | const normalizedTo = normalizeValue(to) |
| 264 | |
| 265 | // Find start index |
| 266 | let startIdx = 0 |
| 267 | if (normalizedFrom !== undefined) { |
| 268 | startIdx = findInsertPositionInArray( |
| 269 | this.sortedValues, |
| 270 | normalizedFrom, |
| 271 | this.compareFn, |
| 272 | ) |
| 273 | // If not inclusive and we found exact match, skip it |
| 274 | if ( |
| 275 | !fromInclusive && |
| 276 | startIdx < this.sortedValues.length && |
| 277 | this.compareFn(this.sortedValues[startIdx], normalizedFrom) === 0 |
| 278 | ) { |
| 279 | startIdx++ |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | // Find end index |
| 284 | let endIdx = this.sortedValues.length |
| 285 | if (normalizedTo !== undefined) { |
| 286 | endIdx = findInsertPositionInArray( |
| 287 | this.sortedValues, |
| 288 | normalizedTo, |
| 289 | this.compareFn, |
| 290 | ) |
| 291 | // If inclusive and we found the value, include it |
| 292 | if ( |
| 293 | toInclusive && |
| 294 | endIdx < this.sortedValues.length && |
| 295 | this.compareFn(this.sortedValues[endIdx], normalizedTo) === 0 |
| 296 | ) { |
| 297 | endIdx++ |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | // Collect all keys in range |
| 302 | for (let i = startIdx; i < endIdx; i++) { |
| 303 | const keys = this.valueMap.get(this.sortedValues[i]) |
| 304 | if (keys) { |
| 305 | keys.forEach((key) => result.add(key)) |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | return result |
| 310 | } |
| 311 |
no test coverage detected