846473b7092d5cc2c2add32db777a4060caf986c
[org.ibex.core.git] / src / org / xwt / util / Vec.java
1 // Copyright (C) 2003 Adam Megacz <adam@xwt.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.xwt.util;
9
10 import java.util.*;
11 import java.io.*;
12
13 /** 
14  *  An unsynchronized Vector implementation; same semantics as
15  *  java.util.Vector. Useful for JDK1.1 platforms that don't have
16  *  java.util.ArrayList.
17  *  @see java.util.Vector
18  */
19 public final class Vec implements Serializable {
20     
21     private Object[] store;
22     private int size = 0;
23     
24     public Vec() { this(10); }
25     public Vec(int i) { store = new Object[i]; }
26     public Vec(int i, Object[] store) { size = i; this.store = store; }
27     
28     private void grow() { grow(store.length * 2); }
29     private void grow(int newsize) {
30         Object[] newstore = new Object[newsize];
31         System.arraycopy(store, 0, newstore, 0, size);
32         store = newstore;
33     }
34
35     public void removeAllElements() {
36         for(int i=0; i<size; i++) store[i] = null;
37         size = 0;
38     }
39
40     public void toArray(Object[] o) {
41         for(int i=0; i<size; i++)
42             o[i] = store[i];
43     }
44     
45     public int indexOf(Object o) {
46         for(int i=0; i<size; i++)
47             if (store[i] == o) return i;
48         return -1;
49     }
50     
51     public void addElement(Object o) {
52         if (size >= store.length - 1) grow();
53         store[size++] = o;
54     }
55
56     public Object peek() {
57         return lastElement();
58     }
59
60     public Object elementAt(int i) {
61         return store[i];
62     }
63     
64     public Object lastElement() {
65         if (size == 0) return null;
66         return store[size - 1];
67     }
68
69     public void push(Object o) { addElement(o); }
70     public Object pop() {
71         Object ret = lastElement();
72         if (size > 0) store[size--] = null;
73         return ret;
74     }
75
76     public int size() { return size; }
77
78     public void setSize(int newSize) {
79         if (newSize < 0) throw new RuntimeException("tried to set size to negative value");
80         if (newSize > store.length) grow(newSize * 2);
81         if (newSize < size)
82             for(int i=newSize; i<size; i++)
83                 store[i] = null;
84         size = newSize;
85     }
86
87     public void copyInto(Object[] out) {
88         for(int i=0; i<size; i++)
89             out[i] = store[i];
90     }
91
92     public void fromArray(Object[] in) {
93         setSize(in.length);
94         for(int i=0; i<size; i++)
95             store[i] = in[i];
96     }
97
98     public void removeElementAt(int i) {
99         if (i >= size || i < 0) throw new RuntimeException("tried to remove an element outside the vector's limits");
100         for(int j=i; j<size - 1; j++)
101             store[j] = store[j + 1];
102         setSize(size - 1);
103     }
104
105     public void setElementAt(Object o, int i) {
106         if (i >= size) setSize(i);
107         store[i] = o;
108     }
109
110     public void removeElement(Object o) {
111         for(int i=0; i<size; i++)
112             if (store[i] == o) {
113                 removeElementAt(i);
114                 return;
115             }
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 }