Lines 824-887
Link Here
|
824 |
* the returned int[]s will be sorted in reverse order, and the order in |
824 |
* the returned int[]s will be sorted in reverse order, and the order in |
825 |
* which they are returned will also be from highest to lowest. */ |
825 |
* which they are returned will also be from highest to lowest. */ |
826 |
private static Object[] getContiguousIndexBlocks(int[] indices, boolean reverseOrder) { |
826 |
private static Object[] getContiguousIndexBlocks(int[] indices, boolean reverseOrder) { |
827 |
|
|
|
828 |
//Quick check if there's only one index |
827 |
//Quick check if there's only one index |
829 |
if (indices.length == 1) { |
828 |
if (indices.length == 1) { |
830 |
return new Object[] {indices}; |
829 |
return new Object[] {indices}; |
831 |
} |
830 |
} |
832 |
|
831 |
|
833 |
ArrayList<ArrayList<Integer>> al = new ArrayList<ArrayList<Integer>>(); |
|
|
834 |
|
835 |
//Sort the indices as requested |
832 |
//Sort the indices as requested |
836 |
if (reverseOrder) { |
833 |
if (reverseOrder) { |
837 |
inverseSort (indices); |
834 |
inverseSort(indices); |
838 |
} else { |
835 |
} else { |
839 |
Arrays.sort (indices); |
836 |
Arrays.sort(indices); |
840 |
} |
837 |
} |
841 |
|
838 |
|
|
|
839 |
final List<int[]> al = new ArrayList<int[]>(); |
840 |
int lastVal = -1; |
841 |
int startIndex = 0; |
842 |
|
842 |
|
843 |
//The starting block |
843 |
for (int i = 0; i < indices.length; i++) { |
844 |
ArrayList<Integer> currBlock = new ArrayList<Integer>(indices.length / 2); |
|
|
845 |
al.add(currBlock); |
846 |
|
847 |
//The value we'll check against the previous one to detect the |
848 |
//end of contiguous segment |
849 |
int lastVal = -1; |
850 |
|
851 |
//Iterate the indices |
852 |
for (int i=0; i < indices.length; i++) { |
853 |
if (i != 0) { |
844 |
if (i != 0) { |
854 |
//See if we've hit a discontinuity |
845 |
//See if we've hit a discontinuity |
855 |
boolean newBlock = reverseOrder ? indices[i] != lastVal - 1 : |
846 |
boolean newBlock = reverseOrder ? indices[i] != lastVal - 1 |
856 |
indices[i] != lastVal + 1; |
847 |
: indices[i] != lastVal + 1; |
857 |
|
848 |
|
858 |
if (newBlock) { |
849 |
if (newBlock) { |
859 |
currBlock = new ArrayList<Integer>(indices.length - 1); |
850 |
// new block detected |
860 |
al.add(currBlock); |
851 |
// copy the last contiguous block and add it to the result array |
|
|
852 |
int[] block = new int[i - startIndex]; |
853 |
System.arraycopy(indices, startIndex, block, 0, block.length); |
854 |
al.add(block); |
855 |
startIndex = i; |
861 |
} |
856 |
} |
862 |
} |
857 |
} |
863 |
currBlock.add (new Integer(indices[i])); |
|
|
864 |
lastVal = indices[i]; |
858 |
lastVal = indices[i]; |
865 |
} |
859 |
} |
866 |
|
860 |
|
867 |
ArrayList<int[]> res = new ArrayList<int[]>(al.size()); |
861 |
// add last (possibly only) block to the result array |
868 |
for (int i=0; i < al.size(); i++) { |
862 |
if (0 != indices.length - startIndex) { |
869 |
ArrayList<Integer> curr = al.get(i); |
863 |
int[] block = new int[indices.length - startIndex]; |
870 |
Integer[] ints = curr.toArray (new Integer[0]); |
864 |
System.arraycopy(indices, startIndex, block, 0, block.length); |
871 |
|
865 |
al.add(block); |
872 |
res.add(toArrayOfInt(ints)); |
|
|
873 |
} |
866 |
} |
874 |
|
867 |
|
875 |
return res.toArray(); |
868 |
return al.toArray(); |
876 |
} |
|
|
877 |
|
878 |
/** Converts an Integer[] to an int[] */ |
879 |
private static int[] toArrayOfInt (Integer[] ints) { |
880 |
int[] result = new int[ints.length]; |
881 |
for (int i=0; i < ints.length; i++) { |
882 |
result[i] = ints[i].intValue(); |
883 |
} |
884 |
return result; |
885 |
} |
869 |
} |
886 |
|
870 |
|
887 |
/** Converts an Integer[] to an int[] */ |
871 |
/** Converts an Integer[] to an int[] */ |