1 54 package org.logicalcobwebs.cglib.util; 55 56 import java.lang.reflect.*; 57 import java.util.Comparator ; 58 import org.logicalcobwebs.cglib.core.*; 59 import org.logicalcobwebs.asm.ClassVisitor; 60 61 86 abstract public class ParallelSorter extends SorterTemplate { 87 protected Object [] a; 88 private Comparer comparer; 89 90 protected ParallelSorter() { 91 } 92 93 abstract public ParallelSorter newInstance(Object [] arrays); 94 95 103 public static ParallelSorter create(Object [] arrays) { 104 Generator gen = new Generator(); 105 gen.setArrays(arrays); 106 return gen.create(); 107 } 108 109 private int len() { 110 return ((Object [])a[0]).length; 111 } 112 113 117 public void quickSort(int index) { 118 quickSort(index, 0, len(), null); 119 } 120 121 127 public void quickSort(int index, int lo, int hi) { 128 quickSort(index, lo, hi, null); 129 } 130 131 136 public void quickSort(int index, Comparator cmp) { 137 quickSort(index, 0, len(), cmp); 138 } 139 140 147 public void quickSort(int index, int lo, int hi, Comparator cmp) { 148 chooseComparer(index, cmp); 149 super.quickSort(lo, hi - 1); 150 } 151 152 155 public void mergeSort(int index) { 156 mergeSort(index, 0, len(), null); 157 } 158 159 165 public void mergeSort(int index, int lo, int hi) { 166 mergeSort(index, lo, hi, null); 167 } 168 169 175 public void mergeSort(int index, Comparator cmp) { 176 mergeSort(index, 0, len(), cmp); 177 } 178 179 186 public void mergeSort(int index, int lo, int hi, Comparator cmp) { 187 chooseComparer(index, cmp); 188 super.mergeSort(lo, hi - 1); 189 } 190 191 private void chooseComparer(int index, Comparator cmp) { 192 Object array = a[index]; 193 Class type = array.getClass().getComponentType(); 194 if (type.equals(Integer.TYPE)) { 195 comparer = new IntComparer((int[])array); 196 } else if (type.equals(Long.TYPE)) { 197 comparer = new LongComparer((long[])array); 198 } else if (type.equals(Double.TYPE)) { 199 comparer = new DoubleComparer((double[])array); 200 } else if (type.equals(Float.TYPE)) { 201 comparer = new FloatComparer((float[])array); 202 } else if (type.equals(Short.TYPE)) { 203 comparer = new ShortComparer((short[])array); 204 } else if (type.equals(Byte.TYPE)) { 205 comparer = new ByteComparer((byte[])array); 206 } else if (cmp != null) { 207 comparer = new ComparatorComparer((Object [])array, cmp); 208 } else { 209 comparer = new ObjectComparer((Object [])array); 210 } 211 } 212 213 protected int compare(int i, int j) { 214 return comparer.compare(i, j); 215 } 216 217 interface Comparer { 218 int compare(int i, int j); 219 } 220 221 static class ComparatorComparer implements Comparer { 222 private Object [] a; 223 private Comparator cmp; 224 225 public ComparatorComparer(Object [] a, Comparator cmp) { 226 this.a = a; 227 this.cmp = cmp; 228 } 229 230 public int compare(int i, int j) { 231 return cmp.compare(a[i], a[j]); 232 } 233 } 234 235 static class ObjectComparer implements Comparer { 236 private Object [] a; 237 public ObjectComparer(Object [] a) { this.a = a; } 238 public int compare(int i, int j) { 239 return ((Comparable )a[i]).compareTo(a[j]); 240 } 241 } 242 243 static class IntComparer implements Comparer { 244 private int[] a; 245 public IntComparer(int[] a) { this.a = a; } 246 public int compare(int i, int j) { return a[i] - a[j]; } 247 } 248 249 static class LongComparer implements Comparer { 250 private long[] a; 251 public LongComparer(long[] a) { this.a = a; } 252 public int compare(int i, int j) { 253 long vi = a[i]; 254 long vj = a[j]; 255 return (vi == vj) ? 0 : (vi > vj) ? 1 : -1; 256 } 257 } 258 259 static class FloatComparer implements Comparer { 260 private float[] a; 261 public FloatComparer(float[] a) { this.a = a; } 262 public int compare(int i, int j) { 263 float vi = a[i]; 264 float vj = a[j]; 265 return (vi == vj) ? 0 : (vi > vj) ? 1 : -1; 266 } 267 } 268 269 static class DoubleComparer implements Comparer { 270 private double[] a; 271 public DoubleComparer(double[] a) { this.a = a; } 272 public int compare(int i, int j) { 273 double vi = a[i]; 274 double vj = a[j]; 275 return (vi == vj) ? 0 : (vi > vj) ? 1 : -1; 276 } 277 } 278 279 static class ShortComparer implements Comparer { 280 private short[] a; 281 public ShortComparer(short[] a) { this.a = a; } 282 public int compare(int i, int j) { return a[i] - a[j]; } 283 } 284 285 static class ByteComparer implements Comparer { 286 private byte[] a; 287 public ByteComparer(byte[] a) { this.a = a; } 288 public int compare(int i, int j) { return a[i] - a[j]; } 289 } 290 291 public static class Generator extends AbstractClassGenerator { 292 private static final Source SOURCE = new Source(ParallelSorter.class.getName()); 293 294 private Object [] arrays; 295 296 public Generator() { 297 super(SOURCE); 298 } 299 300 protected ClassLoader getDefaultClassLoader() { 301 return null; } 303 304 public void setArrays(Object [] arrays) { 305 this.arrays = arrays; 306 } 307 308 public ParallelSorter create() { 309 return (ParallelSorter)super.create(ClassesKey.create(arrays)); 310 } 311 312 public void generateClass(ClassVisitor v) throws Exception { 313 if (arrays.length == 0) { 314 throw new IllegalArgumentException ("No arrays specified to sort"); 315 } 316 for (int i = 0; i < arrays.length; i++) { 317 if (!arrays[i].getClass().isArray()) { 318 throw new IllegalArgumentException (arrays[i].getClass() + " is not an array"); 319 } 320 } 321 new ParallelSorterEmitter(v, getClassName(), arrays); 322 } 323 324 protected Object firstInstance(Class type) { 325 return ((ParallelSorter)ReflectUtils.newInstance(type)).newInstance(arrays); 326 } 327 328 protected Object nextInstance(Object instance) { 329 return ((ParallelSorter)instance).newInstance(arrays); 330 } 331 } 332 } 333 | Popular Tags |