min and max are both inclusive. If max < min, interval is empty.
(NavigableMap<Integer, Integer> map,
final int min, final int max, final boolean ascending)
| 963 | * min and max are both inclusive. If max < min, interval is empty. |
| 964 | */ |
| 965 | void check(NavigableMap<Integer, Integer> map, |
| 966 | final int min, final int max, final boolean ascending) { |
| 967 | class ReferenceSet { |
| 968 | int lower(int key) { |
| 969 | return ascending ? lowerAscending(key) : higherAscending(key); |
| 970 | } |
| 971 | int floor(int key) { |
| 972 | return ascending ? floorAscending(key) : ceilingAscending(key); |
| 973 | } |
| 974 | int ceiling(int key) { |
| 975 | return ascending ? ceilingAscending(key) : floorAscending(key); |
| 976 | } |
| 977 | int higher(int key) { |
| 978 | return ascending ? higherAscending(key) : lowerAscending(key); |
| 979 | } |
| 980 | int first() { |
| 981 | return ascending ? firstAscending() : lastAscending(); |
| 982 | } |
| 983 | int last() { |
| 984 | return ascending ? lastAscending() : firstAscending(); |
| 985 | } |
| 986 | int lowerAscending(int key) { |
| 987 | return floorAscending(key - 1); |
| 988 | } |
| 989 | int floorAscending(int key) { |
| 990 | if (key < min) |
| 991 | return -1; |
| 992 | else if (key > max) |
| 993 | key = max; |
| 994 | |
| 995 | // BitSet should support this! Test would run much faster |
| 996 | while (key >= min) { |
| 997 | if (bs.get(key)) |
| 998 | return key; |
| 999 | key--; |
| 1000 | } |
| 1001 | return -1; |
| 1002 | } |
| 1003 | int ceilingAscending(int key) { |
| 1004 | if (key < min) |
| 1005 | key = min; |
| 1006 | else if (key > max) |
| 1007 | return -1; |
| 1008 | int result = bs.nextSetBit(key); |
| 1009 | return result > max ? -1 : result; |
| 1010 | } |
| 1011 | int higherAscending(int key) { |
| 1012 | return ceilingAscending(key + 1); |
| 1013 | } |
| 1014 | private int firstAscending() { |
| 1015 | int result = ceilingAscending(min); |
| 1016 | return result > max ? -1 : result; |
| 1017 | } |
| 1018 | private int lastAscending() { |
| 1019 | int result = floorAscending(max); |
| 1020 | return result < min ? -1 : result; |
| 1021 | } |
| 1022 | } |