nextValue returns either the `target` if found or the next larger value. If the target is in the interior or a run then `target` will be returned Ex: If our run structure resmembles [[a,c], [d,f]] with a <= target <= c then `target` will be returned. Ex: If c < target < d then d is returned. Ex: If
(target uint16)
| 2842 | // Ex: If target < a then a is returned |
| 2843 | // if the target > max, this is out of bounds and -1 is returned |
| 2844 | func (rc *runContainer16) nextValue(target uint16) int { |
| 2845 | if len(rc.iv) == 0 { |
| 2846 | return -1 |
| 2847 | } |
| 2848 | |
| 2849 | whichIndex, alreadyPresent, _ := rc.search(int(target)) |
| 2850 | |
| 2851 | if alreadyPresent { |
| 2852 | return int(target) |
| 2853 | } |
| 2854 | |
| 2855 | if whichIndex == -1 { |
| 2856 | return int(rc.iv[0].start) |
| 2857 | } |
| 2858 | |
| 2859 | if whichIndex == len(rc.iv)-1 { |
| 2860 | return -1 |
| 2861 | } |
| 2862 | |
| 2863 | // The if relies on the non-contiguous nature of runs. |
| 2864 | // If we have two runs [a,b] and another run [c,d] |
| 2865 | // We can rely on the invariant that b+1 < c |
| 2866 | // We will return c |
| 2867 | possibleNext := whichIndex + 1 |
| 2868 | if possibleNext < len(rc.iv) { |
| 2869 | return int(rc.iv[possibleNext].start) |
| 2870 | } |
| 2871 | |
| 2872 | return -1 |
| 2873 | } |
| 2874 | |
| 2875 | // nextAbsentValue returns the next absent value. |
| 2876 | // By construction the next absent value will be located between gaps in runs |