From 5c44cf5727874965cede92b8fc8c0f81db79c221 Mon Sep 17 00:00:00 2001 From: adam Date: Wed, 23 Jun 2004 02:18:01 +0000 Subject: [PATCH] added sorting methods for ints and floats to Vec.java darcs-hash:20040623021801-5007d-ad80e6b0fb6d2398ff4e6defa647e8b7c9593828.gz --- src/org/ibex/util/Vec.java | 238 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 237 insertions(+), 1 deletion(-) diff --git a/src/org/ibex/util/Vec.java b/src/org/ibex/util/Vec.java index 8bf8556..ffe7cfd 100644 --- a/src/org/ibex/util/Vec.java +++ b/src/org/ibex/util/Vec.java @@ -6,7 +6,6 @@ // relink clause") package org.ibex.util; - import java.io.*; /** @@ -215,4 +214,241 @@ 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; + } + } + } + + + + public static final void sortFloats(float[] a, int start, int end) { + float 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; + } + float pivot = a[end]; + int lo = start - 1; + int hi = end; + do { + while(a[++lo] < pivot) { } + while((hi > lo) && a[--hi] > pivot) { } + swapFloats(a, lo, hi); + } while(lo < hi); + swapFloats(a, lo, end); + sortFloats(a, start, lo-1); + sortFloats(a, lo+1, end); + } + private static final void swapFloats(float[] vec, int a, int b) { + if(a != b) { + float tmp = vec[a]; + vec[a] = vec[b]; + vec[b] = tmp; + } + } } -- 1.7.10.4