001/* 002 * To change this template, choose Tools | Templates 003 * and open the template in the editor. 004 */ 005package sec.sun.awt.geom; 006 007/** 008 * 009 * @author Michael Deutch An inexpensive hack to the Arrays class so that we can 010 * sort the curves. Instead of using a comparator object we are using a specific 011 * instance of its compare method as required for adding Areas. 012 */ 013public class Arrays { 014 015 private static final int INSERTIONSORT_THRESHOLD = 7; 016 017 public static void sort(Object[] a) { 018 Object[] aux = new Object[a.length]; 019 int n = a.length; 020 //for(int j=0;j<a.length;j++) 021 for (int j = 0; j < n; j++) { 022 aux[j] = a[j]; 023 } 024 025 mergeSort(aux, a, 0, a.length, 0); 026 } 027 028 private static void swap(Object[] x, int a, int b) { 029 Object t = x[a]; 030 x[a] = x[b]; 031 x[b] = t; 032 } 033 034 private static int compare(Object o1, Object o2) { 035 CurveObject c1 = ((Edge) o1).getCurve(); 036 CurveObject c2 = ((Edge) o2).getCurve(); 037 double v1, v2; 038 if ((v1 = c1.getYTop()) == (v2 = c2.getYTop())) { 039 if ((v1 = c1.getXTop()) == (v2 = c2.getXTop())) { 040 return 0; 041 } 042 } 043 if (v1 < v2) { 044 return -1; 045 } 046 return 1; 047 } 048 049 private static void mergeSort(Object[] src, 050 Object[] dest, 051 int low, int high, int off) { 052 //Comparator c) { 053 int length = high - low; 054 055 // Insertion sort on smallest arrays 056 if (length < INSERTIONSORT_THRESHOLD) { 057 for (int i = low; i < high; i++) { 058 for (int j = i; j > low && compare(dest[j - 1], dest[j]) > 0; j--) { 059 swap(dest, j, j - 1); 060 } 061 } 062 return; 063 } 064 065 // Recursively sort halves of dest into src 066 int destLow = low; 067 int destHigh = high; 068 low += off; 069 high += off; 070 int mid = (low + high) >>> 1; 071 //mergeSort(dest, src, low, mid, -off, c); 072 //mergeSort(dest, src, mid, high, -off, c); 073 mergeSort(dest, src, low, mid, -off); 074 mergeSort(dest, src, mid, high, -off); 075 076 // If list is already sorted, just copy from src to dest. This is an 077 // optimization that results in faster sorts for nearly ordered lists. 078 if (compare(src[mid - 1], src[mid]) <= 0) { 079 System.arraycopy(src, low, dest, destLow, length); 080 //arraycopy(src, low, dest, destLow, length); 081 //return; 082 } 083 084 // Merge sorted halves (now in src) into dest 085 for (int i = destLow, p = low, q = mid; i < destHigh; i++) { 086 if (q >= high || p < mid && compare(src[p], src[q]) <= 0) { 087 dest[i] = src[p++]; 088 } else { 089 dest[i] = src[q++]; 090 } 091 } 092 } 093 094 /** 095 * @param src the source array. 096 * @param srcPos starting position in the source array. 097 * @param dest the destination array. 098 * @param destPos starting position in the destination data. 099 * @param length the number of array elements to be copied. 100 * @exception IndexOutOfBoundsException if copying would cause access of 101 * data outside array bounds. 102 * @exception ArrayStoreException if an element in the <code>src</code> 103 * array could not be stored into the <code>dest</code> array because of a 104 * type mismatch. 105 * @exception NullPointerException if either <code>src</code> or 106 * <code>dest</code> is <code>null</code>. 107 */ 108 public static void arraycopy(Object[] src, int srcPos, 109 Object[] dest, int destPos, 110 int length) { 111 //int j=0; 112 for (int j = 0; j < length; j++) { 113 dest[j + destPos] = src[srcPos + j]; 114 } 115 } 116 117}