initial checkin
[org.ibex.nanogoat.git] / src / org / ibex / util / Vec.java
1 // Copyright (C) 2003 Adam Megacz <adam@ibex.org> all rights reserved.
2 //
3 // You may modify, copy, and redistribute this code under the terms of
4 // the GNU Library Public License version 2.1, with the exception of
5 // the portion of clause 6a after the semicolon (aka the "obnoxious
6 // relink clause")
7
8 package org.ibex.util;
9
10 import java.io.*;
11
12 /** 
13  *  An unsynchronized Vector implementation; same semantics as
14  *  java.util.Vector. Useful for JDK1.1 platforms that don't have
15  *  java.util.ArrayList.
16  *
17  *  May contain nulls.
18  *
19  *  @see java.util.Vector
20  */
21 public final class Vec implements Serializable {
22     
23     // FIXME: ommitting this crashes jc1
24     private final static boolean NANOGOAT_KEEP_ALL_METHODS = true;
25
26     private Object[] store;
27     private int size = 0;
28     
29     public Vec() { this(10); }
30     public Vec(int i) { store = new Object[i]; }
31     public Vec(int i, Object[] store) { size = i; this.store = store; }
32     
33     private void grow() { grow(store.length * 2); }
34     private void grow(int newsize) {
35         Object[] newstore = new Object[newsize];
36         System.arraycopy(store, 0, newstore, 0, size);
37         store = newstore;
38     }
39
40     public void removeAllElements() {
41         for(int i=0; i<size; i++) store[i] = null;
42         size = 0;
43     }
44
45     public void toArray(Object[] o) {
46         for(int i=0; i<size; i++)
47             o[i] = store[i];
48     }
49     
50     public int indexOf(Object o) {
51         for(int i=0; i<size; i++)
52             if (o == null ? store[i] == null : store[i].equals(o)) return i;
53         
54         return -1;
55     }
56     
57     public void addElement(Object o) {
58         if (size >= store.length - 1) grow();
59         store[size++] = o;
60     }
61
62     public Object peek() {
63         return lastElement();
64     }
65
66     public Object elementAt(int i) {
67         return store[i];
68     }
69     
70     public Object lastElement() {
71         if (size == 0) return null;
72         return store[size - 1];
73     }
74
75     public void push(Object o) { addElement(o); }
76     public Object pop() {
77         Object ret = lastElement();
78         if (size > 0) store[size--] = null;
79         return ret;
80     }
81
82     public int size() { return size; }
83
84     public void setSize(int newSize) {
85         if (newSize < 0) throw new RuntimeException("tried to set size to negative value");
86         if (newSize > store.length) grow(newSize * 2);
87         if (newSize < size)
88             for(int i=newSize; i<size; i++)
89                 store[i] = null;
90         size = newSize;
91     }
92
93     public void copyInto(Object[] out) {
94         for(int i=0; i<size; i++)
95             out[i] = store[i];
96     }
97
98     public void fromArray(Object[] in) {
99         setSize(in.length);
100         for(int i=0; i<size; i++)
101             store[i] = in[i];
102     }
103
104     public void removeElementAt(int i) {
105         if (i >= size || i < 0) throw new RuntimeException("tried to remove an element outside the vector's limits");
106         for(int j=i; j<size - 1; j++)
107             store[j] = store[j + 1];
108         setSize(size - 1);
109     }
110
111     public void setElementAt(Object o, int i) {
112         if (i >= size) setSize(i);
113         store[i] = o;
114     }
115
116     public void removeElement(Object o) {
117         int idx = indexOf(o);
118         if (idx != -1) removeElementAt(idx);
119     }
120
121     public void insertElementAt(Object o, int at) {
122         if (size == store.length) grow();
123         for(int i=size; i>at; i--)
124             store[i] = store[i-1];
125         store[at] = o;
126         size++;
127     }
128
129     public interface CompareFunc {
130         public int compare(Object a, Object b);
131     }
132
133     public void sort(CompareFunc c) {
134         sort(this, null, c, 0, size-1);
135     }
136
137     public static void sort(Vec a, Vec b, CompareFunc c) {
138         if (b != null && a.size != b.size) throw new IllegalArgumentException("Vec a and b must be of equal size");
139         sort(a, b, c, 0, a.size-1);
140     }
141
142     private static final void sort(Vec a, Vec b, CompareFunc c, int start, int end) {
143         Object tmpa, tmpb = null;
144         if(start >= end) return;
145         if(end-start <= 6) {
146             for(int i=start+1;i<=end;i++) {
147                 tmpa = a.store[i];
148                 if (b != null) tmpb = b.store[i];
149                 int j;
150                 for(j=i-1;j>=start;j--) {
151                     if(c.compare(a.store[j],tmpa) <= 0) break;
152                     a.store[j+1] = a.store[j];
153                     if (b != null) b.store[j+1] = b.store[j];
154                 }
155                 a.store[j+1] = tmpa;
156                 if (b != null) b.store[j+1] = tmpb;
157             }
158             return;
159         }
160
161         Object pivot = a.store[end];
162         int lo = start - 1;
163         int hi = end;
164
165         do {
166             while(c.compare(a.store[++lo],pivot) < 0) { }
167             while((hi > lo) && c.compare(a.store[--hi],pivot) > 0) { }
168             swap(a, lo,hi);
169             if (b != null) swap(b, lo,hi);
170         } while(lo < hi);
171
172         swap(a, lo,end);
173         if (b != null) swap(b, lo,end);
174
175         sort(a, b, c, start, lo-1);
176         sort(a, b, c, lo+1, end);
177     }
178
179     private static final void swap(Vec vec, int a, int b) {
180         if(a != b) {
181             Object tmp = vec.store[a];
182             vec.store[a] = vec.store[b];
183             vec.store[b] = tmp;
184         }
185     }
186 }