X-Git-Url: http://git.megacz.com/?p=org.ibex.core.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Futil%2FVec.java;h=8bf8556cadde0c0fdd593ead72d0db26e13bb90a;hp=43ff43d44085b46dbd509963067cf7bcc45c98ed;hb=b3920b34a332d6504bf744f641c59d38f7b45288;hpb=a7119244e6186390d6093426b43ed321653e3575 diff --git a/src/org/ibex/util/Vec.java b/src/org/ibex/util/Vec.java index 43ff43d..8bf8556 100644 --- a/src/org/ibex/util/Vec.java +++ b/src/org/ibex/util/Vec.java @@ -196,11 +196,9 @@ public final class Vec implements Serializable { } return; } - int pivot = a[end]; int lo = start - 1; int hi = end; - do { while(a[++lo] < pivot) { } while((hi > lo) && a[--hi] > pivot) { } @@ -210,7 +208,6 @@ public final class Vec implements Serializable { sortInts(a, start, lo-1); sortInts(a, lo+1, end); } - private static final void swapInts(int[] vec, int a, int b) { if(a != b) { int tmp = vec[a]; @@ -218,205 +215,4 @@ public final class Vec implements Serializable { vec[b] = tmp; } } - - - - // just a cut-and-paste copy of Vec with int storage - public static class Int { - private int[] store; - private int size = 0; - - public Int() { this(10); } - public Int(int i) { store = new int[i]; } - public Int(int i, int[] store) { size = i; this.store = store; } - - private void grow() { grow(store.length * 2); } - private void grow(int newsize) { - int[] newstore = new int[newsize]; - System.arraycopy(store, 0, newstore, 0, size); - store = newstore; - } - - public void removeAllElements() { - for(int i=0; i= store.length - 1) grow(); - store[size++] = o; - } - - public int peek() { - return lastElement(); - } - - public int elementAt(int i) { - return store[i]; - } - - public int lastElement() { - if (size == 0) return 0; - return store[size - 1]; - } - - public void push(int o) { addElement(o); } - public int pop() { - int ret = lastElement(); - if (size > 0) store[size--] = 0; - return ret; - } - - public int size() { return size; } - - public void setSize(int newSize) { - if (newSize < 0) throw new RuntimeException("tried to set size to negative value"); - if (newSize > store.length) grow(newSize * 2); - if (newSize < size) - for(int i=newSize; i= size || i < 0) throw new RuntimeException("tried to remove an element outside the vector's limits"); - for(int j=i; j= size) setSize(i); - store[i] = o; - } - - public void removeElement(int o) { - int idx = indexOf(o); - if (idx != -1) removeElementAt(idx); - } - - public void insertElementAt(int o, int at) { - if (size == store.length) grow(); - for(int i=size; i>at; i--) - store[i] = store[i-1]; - store[at] = o; - size++; - } - - public void sort() { sort(this, null, 0, size-1); } - - public static void sort(Vec.Int a, Vec.Int b) { - if (b != null && a.size != b.size) throw new IllegalArgumentException("Vec.Int a and b must be of equal size"); - sort(a, b, 0, a.size-1); - } - - private static final void sort(Vec.Int a, Vec.Int b, int start, int end) { - int tmpa, tmpb = 0; - if(start >= end) return; - if(end-start <= 6) { - for(int i=start+1;i<=end;i++) { - tmpa = a.store[i]; - if (b != null) tmpb = b.store[i]; - int j; - for(j=i-1;j>=start;j--) { - if((a.store[j]-tmpa) <= 0) break; - a.store[j+1] = a.store[j]; - if (b != null) b.store[j+1] = b.store[j]; - } - a.store[j+1] = tmpa; - if (b != null) b.store[j+1] = tmpb; - } - return; - } - - int pivot = a.store[end]; - int lo = start - 1; - int hi = end; - - do { - while((a.store[++lo]-pivot) < 0) { } - while((hi > lo) && (a.store[--hi]-pivot) > 0) { } - swap(a, lo,hi); - if (b != null) swap(b, lo,hi); - } while(lo < hi); - - swap(a, lo,end); - if (b != null) swap(b, lo,end); - - sort(a, b, start, lo-1); - sort(a, b, lo+1, end); - } - - private static final void swap(Vec.Int vec, int a, int b) { - if(a != b) { - int tmp = vec.store[a]; - vec.store[a] = vec.store[b]; - vec.store[b] = tmp; - } - } - - public static final void sortInts(int[] a, int start, int end) { - int tmpa; - if(start >= end) return; - if(end-start <= 6) { - for(int i=start+1;i<=end;i++) { - tmpa = a[i]; - int j; - for(j=i-1;j>=start;j--) { - if(a[j] <= tmpa) break; - a[j+1] = a[j]; - } - a[j+1] = tmpa; - } - return; - } - - int pivot = a[end]; - int lo = start - 1; - int hi = end; - - do { - while(a[++lo] < pivot) { } - while((hi > lo) && a[--hi] > pivot) { } - swapInts(a, lo, hi); - } while(lo < hi); - swapInts(a, lo, end); - sortInts(a, start, lo-1); - sortInts(a, lo+1, end); - } - - private static final void swapInts(int[] vec, int a, int b) { - if(a != b) { - int tmp = vec[a]; - vec[a] = vec[b]; - vec[b] = tmp; - } - } - } - }