| 866 | * min and max are both inclusive. If max < min, interval is empty. |
| 867 | */ |
| 868 | void check(NavigableSet<Integer> set, |
| 869 | final int min, final int max, final boolean ascending, |
| 870 | final BitSet bs) { |
| 871 | class ReferenceSet { |
| 872 | int lower(int element) { |
| 873 | return ascending ? |
| 874 | lowerAscending(element) : higherAscending(element); |
| 875 | } |
| 876 | int floor(int element) { |
| 877 | return ascending ? |
| 878 | floorAscending(element) : ceilingAscending(element); |
| 879 | } |
| 880 | int ceiling(int element) { |
| 881 | return ascending ? |
| 882 | ceilingAscending(element) : floorAscending(element); |
| 883 | } |
| 884 | int higher(int element) { |
| 885 | return ascending ? |
| 886 | higherAscending(element) : lowerAscending(element); |
| 887 | } |
| 888 | int first() { |
| 889 | return ascending ? firstAscending() : lastAscending(); |
| 890 | } |
| 891 | int last() { |
| 892 | return ascending ? lastAscending() : firstAscending(); |
| 893 | } |
| 894 | int lowerAscending(int element) { |
| 895 | return floorAscending(element - 1); |
| 896 | } |
| 897 | int floorAscending(int element) { |
| 898 | if (element < min) |
| 899 | return -1; |
| 900 | else if (element > max) |
| 901 | element = max; |
| 902 | |
| 903 | // BitSet should support this! Test would run much faster |
| 904 | while (element >= min) { |
| 905 | if (bs.get(element)) |
| 906 | return element; |
| 907 | element--; |
| 908 | } |
| 909 | return -1; |
| 910 | } |
| 911 | int ceilingAscending(int element) { |
| 912 | if (element < min) |
| 913 | element = min; |
| 914 | else if (element > max) |
| 915 | return -1; |
| 916 | int result = bs.nextSetBit(element); |
| 917 | return result > max ? -1 : result; |
| 918 | } |
| 919 | int higherAscending(int element) { |
| 920 | return ceilingAscending(element + 1); |
| 921 | } |
| 922 | private int firstAscending() { |
| 923 | int result = ceilingAscending(min); |
| 924 | return result > max ? -1 : result; |
| 925 | } |