tikhomirov@307: /* tikhomirov@307: * Copyright (c) 2011 TMate Software Ltd tikhomirov@307: * tikhomirov@307: * This program is free software; you can redistribute it and/or modify tikhomirov@307: * it under the terms of the GNU General Public License as published by tikhomirov@307: * the Free Software Foundation; version 2 of the License. tikhomirov@307: * tikhomirov@307: * This program is distributed in the hope that it will be useful, tikhomirov@307: * but WITHOUT ANY WARRANTY; without even the implied warranty of tikhomirov@307: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the tikhomirov@307: * GNU General Public License for more details. tikhomirov@307: * tikhomirov@307: * For information on how to redistribute this software under tikhomirov@307: * the terms of a license other than GNU General Public License tikhomirov@307: * contact TMate Software at support@hg4j.com tikhomirov@307: */ tikhomirov@307: package org.tmatesoft.hg.internal; tikhomirov@307: tikhomirov@307: /** tikhomirov@307: * tikhomirov@307: * @author Artem Tikhomirov tikhomirov@307: * @author TMate Software Ltd. tikhomirov@307: */ tikhomirov@307: public class ArrayHelper { tikhomirov@307: private int[] reverse; tikhomirov@307: tikhomirov@307: @SuppressWarnings("unchecked") tikhomirov@307: public void sort(Comparable[] a) { tikhomirov@307: // Object[] aux = (Object[]) a.clone(); tikhomirov@307: reverse = new int[a.length]; tikhomirov@307: sort1((Comparable[])a, 0, a.length); tikhomirov@310: for (int i = 0; i < reverse.length; i++) { tikhomirov@310: // element that was not moved don't have an index in reverse. tikhomirov@310: // perhaps, can do it inside sort alg? tikhomirov@310: // TODO tests! tikhomirov@310: if (reverse[i] == 0) { tikhomirov@310: reverse[i] = i+1; tikhomirov@310: } tikhomirov@310: } tikhomirov@307: } tikhomirov@307: tikhomirov@307: private void sort1(Comparable x[], int off, int len) { tikhomirov@307: // Insertion sort on smallest arrays tikhomirov@307: if (len < 7) { tikhomirov@307: for (int i=off; ioff && x[j-1].compareTo(x[j]) > 0; j--) tikhomirov@307: swap(x, j, j-1); tikhomirov@307: return; tikhomirov@307: } tikhomirov@307: tikhomirov@307: // Choose a partition element, v tikhomirov@307: int m = off + (len >> 1); // Small arrays, middle element tikhomirov@307: if (len > 7) { tikhomirov@307: int l = off; tikhomirov@307: int n = off + len - 1; tikhomirov@307: if (len > 40) { // Big arrays, pseudomedian of 9 tikhomirov@307: int s = len/8; tikhomirov@307: l = med3(x, l, l+s, l+2*s); tikhomirov@307: m = med3(x, m-s, m, m+s); tikhomirov@307: n = med3(x, n-2*s, n-s, n); tikhomirov@307: } tikhomirov@307: m = med3(x, l, m, n); // Mid-size, med of 3 tikhomirov@307: } tikhomirov@307: Comparable v = x[m]; tikhomirov@307: tikhomirov@307: // Establish Invariant: v* (v)* v* tikhomirov@307: int a = off, b = a, c = off + len - 1, d = c; tikhomirov@307: while(true) { tikhomirov@307: while (b <= c && x[b].compareTo(v) <= 0) { tikhomirov@307: if (x[b] == v) tikhomirov@307: swap(x, a++, b); tikhomirov@307: b++; tikhomirov@307: } tikhomirov@307: while (c >= b && x[c].compareTo(v) >= 0) { tikhomirov@307: if (x[c] == v) tikhomirov@307: swap(x, c, d--); tikhomirov@307: c--; tikhomirov@307: } tikhomirov@307: if (b > c) tikhomirov@307: break; tikhomirov@307: swap(x, b++, c--); tikhomirov@307: } tikhomirov@307: tikhomirov@307: // Swap partition elements back to middle tikhomirov@307: int s, n = off + len; tikhomirov@307: s = Math.min(a-off, b-a ); vecswap(x, off, b-s, s); tikhomirov@307: s = Math.min(d-c, n-d-1); vecswap(x, b, n-s, s); tikhomirov@307: tikhomirov@307: // Recursively sort non-partition-elements tikhomirov@307: if ((s = b-a) > 1) tikhomirov@307: sort1(x, off, s); tikhomirov@307: if ((s = d-c) > 1) tikhomirov@307: sort1(x, n-s, s); tikhomirov@307: } tikhomirov@307: tikhomirov@307: /** tikhomirov@307: * Swaps x[a .. (a+n-1)] with x[b .. (b+n-1)]. tikhomirov@307: */ tikhomirov@307: private void vecswap(Object[] x, int a, int b, int n) { tikhomirov@307: for (int i=0; i[] x, int a, int b, int c) { tikhomirov@307: return (x[a].compareTo(x[b]) < 0 ? tikhomirov@307: (x[b].compareTo(x[c]) < 0 ? b : x[a].compareTo(x[c]) < 0 ? c : a) : tikhomirov@307: (x[b].compareTo(x[c]) > 0 ? b : x[a].compareTo(x[c]) > 0 ? c : a)); tikhomirov@307: } tikhomirov@307: tikhomirov@307: tikhomirov@307: /** tikhomirov@307: * @return the reverse tikhomirov@307: */ tikhomirov@307: public int[] getReverse() { tikhomirov@307: return reverse; tikhomirov@307: } tikhomirov@307: tikhomirov@307: /** tikhomirov@307: * Swaps x[a] with x[b]. tikhomirov@307: */ tikhomirov@307: private void swap(Object[] x, int a, int b) { tikhomirov@307: Object t = x[a]; tikhomirov@307: x[a] = x[b]; tikhomirov@307: x[b] = t; tikhomirov@307: int z1 = reverse[a] != 0 ? reverse[a] : a+1; tikhomirov@307: int z2 = reverse[b] != 0 ? reverse[b] : b+1; tikhomirov@307: reverse[b] = z1; tikhomirov@307: reverse[a] = z2; tikhomirov@307: } tikhomirov@307: }