Example usage for org.apache.hadoop.util IndexedSortable compare

List of usage examples for org.apache.hadoop.util IndexedSortable compare

Introduction

In this page you can find the example usage for org.apache.hadoop.util IndexedSortable compare.

Prototype

int compare(int i, int j);

Source Link

Document

Compare items at the given addresses consistent with the semantics of java.util.Comparator#compare(Object,Object) .

Usage

From source file:com.ricemap.spateDB.util.MergeSorter.java

License:Apache License

@Override
public void sort(IndexedSortable s, final int l, final int r, Progressable rep) {
    for (int step = 2; step < (r - l) * 2; step *= 2) {
        for (int i = l; i < r; i += step) {
            // In this iteration, we need to merge the two sublists
            // [i, i + step / 2), [i + step / 2, i + step)

            int i1 = i;
            int j1 = i + step / 2;
            int j2 = Math.min(i + step, r);
            // In each iteration: Elements in the sublist [i, i1) are in their
            // correct position. i.e. each element in this sublist is less than
            // every element on the sublist [i1, j2)
            // The loop needs to merge the sublists [i1, j1), [j1, j2)

            while (j1 < j2) {
                // Step 1: Skip elements in the left list < minimum of right list
                while (i1 < j1 && s.compare(i1, j1) <= 0) {
                    i1++;/*from w  w w  .jav a  2  s.c o m*/
                }

                if (i1 == j1) {
                    // Range is already merged. Terminate the loop
                    break;
                }

                // Step 2: Find all elements starting from j1 that needs to be moved
                // to i1
                // Note: We don't need to compare [j1] as we know it's less than [i1]
                int old_j1 = j1++;
                while (j1 < j2 && s.compare(i1, j1) > 0) {
                    j1++;
                }

                // Step 3: Move the elements [old_j1, j1) to the position i1
                shift(s, old_j1, j1, i1);

                // Step 4: Update the indices.
                i1 += (j1 - old_j1);
                // I already compared [i1] and I know it's less than or equal to [j1]
                i1++;
                // Now the sublist [i, i1) is merged
            }
        }
    }
}