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