getCardinalityInRange returns the number of values in the half-open range [start, end).
(start, end uint)
| 2509 | |
| 2510 | // getCardinalityInRange returns the number of values in the half-open range [start, end). |
| 2511 | func (rc *runContainer16) getCardinalityInRange(start, end uint) int { |
| 2512 | if start >= end || len(rc.iv) == 0 { |
| 2513 | return 0 |
| 2514 | } |
| 2515 | // end is exclusive, so the last included value is end-1. |
| 2516 | last := end - 1 |
| 2517 | |
| 2518 | // Find the interval containing or just before 'start'. |
| 2519 | wStart, startInside, _ := rc.search(int(start)) |
| 2520 | // Find the interval containing or just before 'last'. |
| 2521 | wEnd, endInside, _ := rc.search(int(last)) |
| 2522 | |
| 2523 | if wStart < 0 && wEnd < 0 { |
| 2524 | // Both are before the first interval → nothing in range. |
| 2525 | return 0 |
| 2526 | } |
| 2527 | |
| 2528 | // Determine the effective first interval index to start counting from. |
| 2529 | firstIdx := wStart |
| 2530 | if !startInside { |
| 2531 | // start falls between intervals or before the first; next interval is wStart+1. |
| 2532 | firstIdx = wStart + 1 |
| 2533 | } |
| 2534 | // Determine the effective last interval index. |
| 2535 | lastIdx := wEnd |
| 2536 | if !endInside && wEnd >= 0 { |
| 2537 | // last falls between intervals; the last fully-before interval is wEnd, |
| 2538 | // but its values are all < start of next, and we need values <= last. |
| 2539 | // All of wEnd's values are < start (if wEnd < firstIdx), we handle below. |
| 2540 | lastIdx = wEnd |
| 2541 | } |
| 2542 | |
| 2543 | if firstIdx >= len(rc.iv) { |
| 2544 | return 0 |
| 2545 | } |
| 2546 | if lastIdx < 0 || lastIdx < firstIdx { |
| 2547 | return 0 |
| 2548 | } |
| 2549 | |
| 2550 | // If start and end land in the same interval (or there's only one relevant). |
| 2551 | if firstIdx == lastIdx && firstIdx >= 0 { |
| 2552 | ivStart := uint(rc.iv[firstIdx].start) |
| 2553 | ivEnd := uint(rc.iv[firstIdx].last()) |
| 2554 | // Clamp |
| 2555 | lo := start |
| 2556 | if ivStart > lo { |
| 2557 | lo = ivStart |
| 2558 | } |
| 2559 | hi := last |
| 2560 | if ivEnd < hi { |
| 2561 | hi = ivEnd |
| 2562 | } |
| 2563 | if lo > hi { |
| 2564 | return 0 |
| 2565 | } |
| 2566 | return int(hi-lo) + 1 |
| 2567 | } |
| 2568 |