1 11 package org.eclipse.ui.dialogs; 12 13 import java.util.Comparator ; 14 15 import org.eclipse.core.runtime.Assert; 16 17 23 class TwoArrayQuickSorter { 24 25 private Comparator fComparator; 26 27 30 public static final class StringComparator implements Comparator { 31 private boolean fIgnoreCase; 32 33 StringComparator(boolean ignoreCase) { 34 fIgnoreCase = ignoreCase; 35 } 36 37 public int compare(Object left, Object right) { 38 return fIgnoreCase ? ((String ) left) 39 .compareToIgnoreCase((String ) right) : ((String ) left) 40 .compareTo((String ) right); 41 } 42 } 43 44 49 public TwoArrayQuickSorter(boolean ignoreCase) { 50 fComparator = new StringComparator(ignoreCase); 51 } 52 53 57 public TwoArrayQuickSorter(Comparator comparator) { 58 fComparator = comparator; 59 } 60 61 66 public void sort(Object [] keys, Object [] values) { 67 if ((keys == null) || (values == null)) { 68 Assert.isTrue(false, "Either keys or values in null"); return; 70 } 71 72 if (keys.length <= 1) { 73 return; 74 } 75 76 internalSort(keys, values, 0, keys.length - 1); 77 } 78 79 private void internalSort(Object [] keys, Object [] values, int left, 80 int right) { 81 int original_left = left; 82 int original_right = right; 83 84 Object mid = keys[(left + right) / 2]; 85 do { 86 while (fComparator.compare(keys[left], mid) < 0) { 87 left++; 88 } 89 90 while (fComparator.compare(mid, keys[right]) < 0) { 91 right--; 92 } 93 94 if (left <= right) { 95 swap(keys, left, right); 96 swap(values, left, right); 97 left++; 98 right--; 99 } 100 } while (left <= right); 101 102 if (original_left < right) { 103 internalSort(keys, values, original_left, right); 104 } 105 106 if (left < original_right) { 107 internalSort(keys, values, left, original_right); 108 } 109 } 110 111 114 private static final void swap(Object x[], int a, int b) { 115 Object t = x[a]; 116 x[a] = x[b]; 117 x[b] = t; 118 } 119 120 } 121 | Popular Tags |