Make SPECjbb compile again
[IRC.git] / Robust / src / ClassLibrary / MGC / Vector.java
1 import java.util.NoSuchElementException;
2
3 public class Vector implements Set {
4   Object[] array;
5   int size;
6   int capacityIncrement;
7
8   public Vector() {
9     capacityIncrement=0;
10     size=0;
11     array=new Object[10];
12   }
13
14   public Vector(int size) {
15     capacityIncrement=0;
16     this.size=0;
17     array=new Object[size];
18   }
19
20   public boolean isEmpty() {
21     return size==0;
22   }
23
24   public void clear() {
25     size=0;
26     array=new Object[10];
27   }
28
29   public int indexOf(Object elem) {
30     return indexOf(elem, 0);
31   }
32
33   public int indexOf(Object elem, int index) {
34     for(int i=index; i<size; i++) {
35       if (elem.equals(array[i]))
36         return i;
37     }
38     return -1;
39   }
40
41   public boolean contains(Object e) {
42     return indexOf(e)!=-1;
43   }
44
45   /*public boolean remove(Object o) {
46     int in=indexOf(o);
47     if (in!=-1) {
48       removeElementAt(in);
49       return true;
50     } else {
51       return false;
52     }
53   }*/
54   
55   public Object remove(int index) {
56     Object r = null;
57     if (index!=-1) {
58       r = array[index];
59       removeElementAt(index);
60     }
61     return r;
62   }
63
64   public Object elementAt(int index) {
65     if (index<0 | index >=size) {
66       System.printString("Illegal Vector.elementAt\n");
67       System.exit(-1);
68       return null;
69     }
70     return array[index];
71   }
72
73   public void setElementAt(Object obj, int index) {
74     if (index <size)
75       array[index]=obj;
76     else {
77       System.printString("Illegal Vector.setElementAt\n");
78       System.exit(-1);
79     }
80   }
81
82   private void ensureCapacity(int minCapacity) {
83     if (minCapacity>array.length) {
84       int newsize;
85       if (capacityIncrement<=0)
86         newsize=array.length*2;
87       else
88         newsize=array.length+capacityIncrement;
89       if (newsize<minCapacity)
90         newsize=minCapacity;
91       Object [] newarray=new Object[newsize];
92       for(int i=0; i<size; i++)
93         newarray[i]=array[i];
94       array=newarray;
95     }
96   }
97
98   public int size() {
99     return size;
100   }
101
102   public Enumeration elements() {
103     System.printString("Vector.elements not implemented\n");
104     System.exit(-1);
105   }
106
107   public void addElement(Object obj) {
108     if (size==array.length) {
109       ensureCapacity(size+1);
110     }
111     array[size++]=obj;
112   }
113   
114   public boolean add(Object obj) {
115     if (size==array.length) {
116       ensureCapacity(size+1);
117     }
118     array[size++]=obj;
119     return true;
120   }
121
122   public void insertElementAt(Object obj, int index) {
123     if (index<0||index>size) {
124       System.printString("Illegal Vector.insertElementAt\n");
125       System.exit(-1);
126     }
127
128     if (size==array.length) {
129       ensureCapacity(size+1);
130     }
131     size++;
132     for(int i=size-1; i>index; --i) {
133       array[i] = array[i-1];
134     }
135     array[index] = obj;
136   }
137
138   public void removeElementAt(int index) {
139     if (index<0||index>=size) {
140       System.printString("Illegal Vector.removeElementAt\n");
141       System.exit(-1);
142     }
143     removeElement(array, index, size);
144     size--;
145     array[size]=null;
146   }
147
148   public static native void removeElement(Object[] array, int index, int size);
149
150   public void removeAllElements() {
151     int s = size;
152     for(int i = 0; i<s; ++i ) {
153       removeElementAt(0);
154     }
155   }
156   
157   public Object[] toArray() {
158     Object[] tarray = new Object[this.size];
159     for(int i = 0; i < this.size; i++) {
160       tarray[i] = this.array[i];
161     }
162     return tarray;
163   }
164   
165   public Vector(Set s) {
166     Object[] sarray = s.toArray();
167     capacityIncrement=0;
168     this.size=sarray.length;
169     array=sarray;
170   }
171   
172   public synchronized Object firstElement() {
173     if (size == 0) {
174       throw new /*NoSuchElement*/Exception("NoSuchElement");
175     }
176     return array[0];
177   }
178 }