43ff43d44085b46dbd509963067cf7bcc45c98ed
[org.ibex.core.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     private Object[] store;
24     private int size = 0;
25     
26     public Vec() { this(10); }
27     public Vec(int i) { store = new Object[i]; }
28     public Vec(int i, Object[] store) { size = i; this.store = store; }
29     
30     private void grow() { grow(store.length * 2); }
31     private void grow(int newsize) {
32         Object[] newstore = new Object[newsize];
33         System.arraycopy(store, 0, newstore, 0, size);
34         store = newstore;
35     }
36
37     public void removeAllElements() {
38         for(int i=0; i<size; i++) store[i] = null;
39         size = 0;
40     }
41
42     public void toArray(Object[] o) {
43         for(int i=0; i<size; i++)
44             o[i] = store[i];
45     }
46     
47     public int indexOf(Object o) {
48         for(int i=0; i<size; i++)
49             if (o == null ? store[i] == null : store[i].equals(o)) return i;
50         
51         return -1;
52     }
53     
54     public void addElement(Object o) {
55         if (size >= store.length - 1) grow();
56         store[size++] = o;
57     }
58
59     public Object peek() {
60         return lastElement();
61     }
62
63     public Object elementAt(int i) {
64         return store[i];
65     }
66     
67     public Object lastElement() {
68         if (size == 0) return null;
69         return store[size - 1];
70     }
71
72     public void push(Object o) { addElement(o); }
73     public Object pop() {
74         Object ret = lastElement();
75         if (size > 0) store[size--] = null;
76         return ret;
77     }
78
79     public int size() { return size; }
80
81     public void setSize(int newSize) {
82         if (newSize < 0) throw new RuntimeException("tried to set size to negative value");
83         if (newSize > store.length) grow(newSize * 2);
84         if (newSize < size)
85             for(int i=newSize; i<size; i++)
86                 store[i] = null;
87         size = newSize;
88     }
89
90     public void copyInto(Object[] out) {
91         for(int i=0; i<size; i++)
92             out[i] = store[i];
93     }
94
95     public void fromArray(Object[] in) {
96         setSize(in.length);
97         for(int i=0; i<size; i++)
98             store[i] = in[i];
99     }
100
101     public void removeElementAt(int i) {
102         if (i >= size || i < 0) throw new RuntimeException("tried to remove an element outside the vector's limits");
103         for(int j=i; j<size - 1; j++)
104             store[j] = store[j + 1];
105         setSize(size - 1);
106     }
107
108     public void setElementAt(Object o, int i) {
109         if (i >= size) setSize(i);
110         store[i] = o;
111     }
112
113     public void removeElement(Object o) {
114         int idx = indexOf(o);
115         if (idx != -1) removeElementAt(idx);
116     }
117
118     public void insertElementAt(Object o, int at) {
119         if (size == store.length) grow();
120         for(int i=size; i>at; i--)
121             store[i] = store[i-1];
122         store[at] = o;
123         size++;
124     }
125
126     public interface CompareFunc {
127         public int compare(Object a, Object b);
128     }
129
130     public void sort(CompareFunc c) {
131         sort(this, null, c, 0, size-1);
132     }
133
134     public static void sort(Vec a, Vec b, CompareFunc c) {
135         if (b != null && a.size != b.size) throw new IllegalArgumentException("Vec a and b must be of equal size");
136         sort(a, b, c, 0, a.size-1);
137     }
138
139     private static final void sort(Vec a, Vec b, CompareFunc c, int start, int end) {
140         Object tmpa, tmpb = null;
141         if(start >= end) return;
142         if(end-start <= 6) {
143             for(int i=start+1;i<=end;i++) {
144                 tmpa = a.store[i];
145                 if (b != null) tmpb = b.store[i];
146                 int j;
147                 for(j=i-1;j>=start;j--) {
148                     if(c.compare(a.store[j],tmpa) <= 0) break;
149                     a.store[j+1] = a.store[j];
150                     if (b != null) b.store[j+1] = b.store[j];
151                 }
152                 a.store[j+1] = tmpa;
153                 if (b != null) b.store[j+1] = tmpb;
154             }
155             return;
156         }
157
158         Object pivot = a.store[end];
159         int lo = start - 1;
160         int hi = end;
161
162         do {
163             while(c.compare(a.store[++lo],pivot) < 0) { }
164             while((hi > lo) && c.compare(a.store[--hi],pivot) > 0) { }
165             swap(a, lo,hi);
166             if (b != null) swap(b, lo,hi);
167         } while(lo < hi);
168
169         swap(a, lo,end);
170         if (b != null) swap(b, lo,end);
171
172         sort(a, b, c, start, lo-1);
173         sort(a, b, c, lo+1, end);
174     }
175
176     private static final void swap(Vec vec, int a, int b) {
177         if(a != b) {
178             Object tmp = vec.store[a];
179             vec.store[a] = vec.store[b];
180             vec.store[b] = tmp;
181         }
182     }
183
184     public static final void sortInts(int[] a, int start, int end) {
185         int tmpa;
186         if(start >= end) return;
187         if(end-start <= 6) {
188             for(int i=start+1;i<=end;i++) {
189                 tmpa = a[i];
190                 int j;
191                 for(j=i-1;j>=start;j--) {
192                     if(a[j] <= tmpa) break;
193                     a[j+1] = a[j];
194                 }
195                 a[j+1] = tmpa;
196             }
197             return;
198         }
199
200         int pivot = a[end];
201         int lo = start - 1;
202         int hi = end;
203
204         do {
205             while(a[++lo] < pivot) { }
206             while((hi > lo) && a[--hi] > pivot) { }
207             swapInts(a, lo, hi);
208         } while(lo < hi);
209         swapInts(a, lo, end);
210         sortInts(a, start, lo-1);
211         sortInts(a, lo+1, end);
212     }
213
214     private static final void swapInts(int[] vec, int a, int b) {
215         if(a != b) {
216             int tmp = vec[a];
217             vec[a] = vec[b];
218             vec[b] = tmp;
219         }
220     }
221
222
223
224     // just a cut-and-paste copy of Vec with int storage
225     public static class Int {
226         private int[] store;
227         private int size = 0;
228     
229         public Int() { this(10); }
230         public Int(int i) { store = new int[i]; }
231         public Int(int i, int[] store) { size = i; this.store = store; }
232     
233         private void grow() { grow(store.length * 2); }
234         private void grow(int newsize) {
235             int[] newstore = new int[newsize];
236             System.arraycopy(store, 0, newstore, 0, size);
237             store = newstore;
238         }
239
240         public void removeAllElements() {
241             for(int i=0; i<size; i++) store[i] = 0;
242             size = 0;
243         }
244
245         public void toArray(int[] o) {
246             for(int i=0; i<size; i++)
247                 o[i] = store[i];
248         }
249     
250         public int[] dump() { int[] o = new int[size]; toArray(o); return o; }
251     
252         public int indexOf(int o) {
253             for(int i=0; i<size; i++)
254                 if (o == store[i]) return i;
255         
256             return -1;
257         }
258     
259         public void addElement(int o) {
260             if (size >= store.length - 1) grow();
261             store[size++] = o;
262         }
263
264         public int peek() {
265             return lastElement();
266         }
267
268         public int elementAt(int i) {
269             return store[i];
270         }
271     
272         public int lastElement() {
273             if (size == 0) return 0;
274             return store[size - 1];
275         }
276
277         public void push(int o) { addElement(o); }
278         public int pop() {
279             int ret = lastElement();
280             if (size > 0) store[size--] = 0;
281             return ret;
282         }
283
284         public int size() { return size; }
285
286         public void setSize(int newSize) {
287             if (newSize < 0) throw new RuntimeException("tried to set size to negative value");
288             if (newSize > store.length) grow(newSize * 2);
289             if (newSize < size)
290                 for(int i=newSize; i<size; i++)
291                     store[i] = 0;
292             size = newSize;
293         }
294
295         public void copyInto(int[] out) {
296             for(int i=0; i<size; i++)
297                 out[i] = store[i];
298         }
299
300         public void fromArray(int[] in) {
301             setSize(in.length);
302             for(int i=0; i<size; i++)
303                 store[i] = in[i];
304         }
305
306         public void removeElementAt(int i) {
307             if (i >= size || i < 0) throw new RuntimeException("tried to remove an element outside the vector's limits");
308             for(int j=i; j<size - 1; j++)
309                 store[j] = store[j + 1];
310             setSize(size - 1);
311         }
312
313         public void setElementAt(int o, int i) {
314             if (i >= size) setSize(i);
315             store[i] = o;
316         }
317
318         public void removeElement(int o) {
319             int idx = indexOf(o);
320             if (idx != -1) removeElementAt(idx);
321         }
322
323         public void insertElementAt(int o, int at) {
324             if (size == store.length) grow();
325             for(int i=size; i>at; i--)
326                 store[i] = store[i-1];
327             store[at] = o;
328             size++;
329         }
330
331         public void sort() { sort(this, null, 0, size-1); }
332
333         public static void sort(Vec.Int a, Vec.Int b) {
334             if (b != null && a.size != b.size) throw new IllegalArgumentException("Vec.Int a and b must be of equal size");
335             sort(a, b, 0, a.size-1);
336         }
337
338         private static final void sort(Vec.Int a, Vec.Int b, int start, int end) {
339             int tmpa, tmpb = 0;
340             if(start >= end) return;
341             if(end-start <= 6) {
342                 for(int i=start+1;i<=end;i++) {
343                     tmpa = a.store[i];
344                     if (b != null) tmpb = b.store[i];
345                     int j;
346                     for(j=i-1;j>=start;j--) {
347                         if((a.store[j]-tmpa) <= 0) break;
348                         a.store[j+1] = a.store[j];
349                         if (b != null) b.store[j+1] = b.store[j];
350                     }
351                     a.store[j+1] = tmpa;
352                     if (b != null) b.store[j+1] = tmpb;
353                 }
354                 return;
355             }
356
357             int pivot = a.store[end];
358             int lo = start - 1;
359             int hi = end;
360
361             do {
362                 while((a.store[++lo]-pivot) < 0) { }
363                 while((hi > lo) && (a.store[--hi]-pivot) > 0) { }
364                 swap(a, lo,hi);
365                 if (b != null) swap(b, lo,hi);
366             } while(lo < hi);
367
368             swap(a, lo,end);
369             if (b != null) swap(b, lo,end);
370
371             sort(a, b, start, lo-1);
372             sort(a, b, lo+1, end);
373         }
374
375         private static final void swap(Vec.Int vec, int a, int b) {
376             if(a != b) {
377                 int tmp = vec.store[a];
378                 vec.store[a] = vec.store[b];
379                 vec.store[b] = tmp;
380             }
381         }
382
383         public static final void sortInts(int[] a, int start, int end) {
384             int tmpa;
385             if(start >= end) return;
386             if(end-start <= 6) {
387                 for(int i=start+1;i<=end;i++) {
388                     tmpa = a[i];
389                     int j;
390                     for(j=i-1;j>=start;j--) {
391                         if(a[j] <= tmpa) break;
392                         a[j+1] = a[j];
393                     }
394                     a[j+1] = tmpa;
395                 }
396                 return;
397             }
398
399             int pivot = a[end];
400             int lo = start - 1;
401             int hi = end;
402
403             do {
404                 while(a[++lo] < pivot) { }
405                 while((hi > lo) && a[--hi] > pivot) { }
406                 swapInts(a, lo, hi);
407             } while(lo < hi);
408             swapInts(a, lo, end);
409             sortInts(a, start, lo-1);
410             sortInts(a, lo+1, end);
411         }
412
413         private static final void swapInts(int[] vec, int a, int b) {
414             if(a != b) {
415                 int tmp = vec[a];
416                 vec[a] = vec[b];
417                 vec[b] = tmp;
418             }
419         }
420     }
421
422 }