1 16 package net.sf.cglib.util; 17 18 import java.lang.reflect.*; 19 import java.util.Comparator ; 20 import net.sf.cglib.core.*; 21 import org.objectweb.asm.ClassVisitor; 22 23 48 abstract public class ParallelSorter extends SorterTemplate { 49 protected Object [] a; 50 private Comparer comparer; 51 52 protected ParallelSorter() { 53 } 54 55 abstract public ParallelSorter newInstance(Object [] arrays); 56 57 65 public static ParallelSorter create(Object [] arrays) { 66 Generator gen = new Generator(); 67 gen.setArrays(arrays); 68 return gen.create(); 69 } 70 71 private int len() { 72 return ((Object [])a[0]).length; 73 } 74 75 79 public void quickSort(int index) { 80 quickSort(index, 0, len(), null); 81 } 82 83 89 public void quickSort(int index, int lo, int hi) { 90 quickSort(index, lo, hi, null); 91 } 92 93 98 public void quickSort(int index, Comparator cmp) { 99 quickSort(index, 0, len(), cmp); 100 } 101 102 109 public void quickSort(int index, int lo, int hi, Comparator cmp) { 110 chooseComparer(index, cmp); 111 super.quickSort(lo, hi - 1); 112 } 113 114 117 public void mergeSort(int index) { 118 mergeSort(index, 0, len(), null); 119 } 120 121 127 public void mergeSort(int index, int lo, int hi) { 128 mergeSort(index, lo, hi, null); 129 } 130 131 137 public void mergeSort(int index, Comparator cmp) { 138 mergeSort(index, 0, len(), cmp); 139 } 140 141 148 public void mergeSort(int index, int lo, int hi, Comparator cmp) { 149 chooseComparer(index, cmp); 150 super.mergeSort(lo, hi - 1); 151 } 152 153 private void chooseComparer(int index, Comparator cmp) { 154 Object array = a[index]; 155 Class type = array.getClass().getComponentType(); 156 if (type.equals(Integer.TYPE)) { 157 comparer = new IntComparer((int[])array); 158 } else if (type.equals(Long.TYPE)) { 159 comparer = new LongComparer((long[])array); 160 } else if (type.equals(Double.TYPE)) { 161 comparer = new DoubleComparer((double[])array); 162 } else if (type.equals(Float.TYPE)) { 163 comparer = new FloatComparer((float[])array); 164 } else if (type.equals(Short.TYPE)) { 165 comparer = new ShortComparer((short[])array); 166 } else if (type.equals(Byte.TYPE)) { 167 comparer = new ByteComparer((byte[])array); 168 } else if (cmp != null) { 169 comparer = new ComparatorComparer((Object [])array, cmp); 170 } else { 171 comparer = new ObjectComparer((Object [])array); 172 } 173 } 174 175 protected int compare(int i, int j) { 176 return comparer.compare(i, j); 177 } 178 179 interface Comparer { 180 int compare(int i, int j); 181 } 182 183 static class ComparatorComparer implements Comparer { 184 private Object [] a; 185 private Comparator cmp; 186 187 public ComparatorComparer(Object [] a, Comparator cmp) { 188 this.a = a; 189 this.cmp = cmp; 190 } 191 192 public int compare(int i, int j) { 193 return cmp.compare(a[i], a[j]); 194 } 195 } 196 197 static class ObjectComparer implements Comparer { 198 private Object [] a; 199 public ObjectComparer(Object [] a) { this.a = a; } 200 public int compare(int i, int j) { 201 return ((Comparable )a[i]).compareTo(a[j]); 202 } 203 } 204 205 static class IntComparer implements Comparer { 206 private int[] a; 207 public IntComparer(int[] a) { this.a = a; } 208 public int compare(int i, int j) { return a[i] - a[j]; } 209 } 210 211 static class LongComparer implements Comparer { 212 private long[] a; 213 public LongComparer(long[] a) { this.a = a; } 214 public int compare(int i, int j) { 215 long vi = a[i]; 216 long vj = a[j]; 217 return (vi == vj) ? 0 : (vi > vj) ? 1 : -1; 218 } 219 } 220 221 static class FloatComparer implements Comparer { 222 private float[] a; 223 public FloatComparer(float[] a) { this.a = a; } 224 public int compare(int i, int j) { 225 float vi = a[i]; 226 float vj = a[j]; 227 return (vi == vj) ? 0 : (vi > vj) ? 1 : -1; 228 } 229 } 230 231 static class DoubleComparer implements Comparer { 232 private double[] a; 233 public DoubleComparer(double[] a) { this.a = a; } 234 public int compare(int i, int j) { 235 double vi = a[i]; 236 double vj = a[j]; 237 return (vi == vj) ? 0 : (vi > vj) ? 1 : -1; 238 } 239 } 240 241 static class ShortComparer implements Comparer { 242 private short[] a; 243 public ShortComparer(short[] a) { this.a = a; } 244 public int compare(int i, int j) { return a[i] - a[j]; } 245 } 246 247 static class ByteComparer implements Comparer { 248 private byte[] a; 249 public ByteComparer(byte[] a) { this.a = a; } 250 public int compare(int i, int j) { return a[i] - a[j]; } 251 } 252 253 public static class Generator extends AbstractClassGenerator { 254 private static final Source SOURCE = new Source(ParallelSorter.class.getName()); 255 256 private Object [] arrays; 257 258 public Generator() { 259 super(SOURCE); 260 } 261 262 protected ClassLoader getDefaultClassLoader() { 263 return null; } 265 266 public void setArrays(Object [] arrays) { 267 this.arrays = arrays; 268 } 269 270 public ParallelSorter create() { 271 return (ParallelSorter)super.create(ClassesKey.create(arrays)); 272 } 273 274 public void generateClass(ClassVisitor v) throws Exception { 275 if (arrays.length == 0) { 276 throw new IllegalArgumentException ("No arrays specified to sort"); 277 } 278 for (int i = 0; i < arrays.length; i++) { 279 if (!arrays[i].getClass().isArray()) { 280 throw new IllegalArgumentException (arrays[i].getClass() + " is not an array"); 281 } 282 } 283 new ParallelSorterEmitter(v, getClassName(), arrays); 284 } 285 286 protected Object firstInstance(Class type) { 287 return ((ParallelSorter)ReflectUtils.newInstance(type)).newInstance(arrays); 288 } 289 290 protected Object nextInstance(Object instance) { 291 return ((ParallelSorter)instance).newInstance(arrays); 292 } 293 } 294 } 295 | Popular Tags |