/** * Returns an int to int hash map with old row numbers mapped to new row numbers, which defines a * sorted order for this column * * @param validRows row numbers from which the sorted order is retrieved. */ public VIntIntHashMap getNewOrder(int[] validRows) { Object[] sortedValues = getValuesInRange(validRows[0], validRows[validRows.length - 1]); Object[] rawValues = getValues(validRows); // will hold the new order, the returned value. int[] newOrder = new int[validRows.length]; // a flag array. occupiedIndices[j] == true mean that the row number // stored at validRows[j] was already mapped to a value in tempMap boolean[] ocuupiedIndices = new boolean[validRows.length]; // for each valid row validRows[i] for (int i = 0; i < validRows.length; i++) { // finding the index of its mapped Object in values int newRow = Arrays.binarySearch(sortedValues, rawValues[i]); // because binarySearch can return the same index for items that are identical // checking for this option too. if (ocuupiedIndices[newRow]) { // if newRow index was already used - finding an alternative index newRow = getNewRow(rawValues[i], sortedValues, newRow, ocuupiedIndices); // marking the associated flag as true } ocuupiedIndices[newRow] = true; newOrder[newRow] = validRows[i]; } // end of for return VHashService.getMappedOrder(validRows, newOrder); }
/** * Returns the valid values in rows <codE>begin</code> through <codE>end</code> * * @param begin row number from to begin retrieving of values * @param end last row number in the section from which values are retrieved. * @return only valid values from rows no. <code>begin</code> through <codE>end</code>, sorted. */ protected Object[] getValuesInRange(int begin, int end) { if (end < begin) { Object[] retVal = {}; return retVal; } int[] indices = VHashService.getIndicesInRange(begin, end, elements); Object[] values = new Object[indices.length]; for (int i = 0; i < indices.length; i++) { values[i] = elements.get(indices[i]); } toComparableArray(values); Arrays.sort(values); return values; }