Fix tabbing.... Please fix your editors so they do tabbing correctly!!! (Spaces...
[IRC.git] / Robust / src / ClassLibrary / Vector.java
1 public class Vector {
2   Object[] array;
3   int size;
4   int capacityIncrement;
5
6   public Vector() {
7     capacityIncrement=0;
8     size=0;
9     array=new Object[10];
10   }
11
12   public Vector(int size) {
13     capacityIncrement=0;
14     this.size=0;
15     array=new Object[size];
16   }
17
18   //used for internal cloning
19   private Vector(int size, int capacityIncrement, Object[] array) {
20     this.size = size;
21     this.capacityIncrement = capacityIncrement;
22     this.array = new Object[array.length];
23     System.arraycopy(array, 0, this.array, 0, size);
24   }
25
26   public Vector clone() {
27     return new Vector(size,capacityIncrement, array);
28   }
29
30   public boolean isEmpty() {
31     return size==0;
32   }
33
34   public void clear() {
35     size=0;
36     array=new Object[10];
37   }
38
39   public int indexOf(Object elem) {
40     return indexOf(elem, 0);
41   }
42
43   public int indexOf(Object elem, int index) {
44     for(int i=index; i<size; i++) {
45       if (elem.equals(array[i]))
46         return i;
47     }
48     return -1;
49   }
50
51   public boolean contains(Object e) {
52     return indexOf(e)!=-1;
53   }
54
55   public boolean  remove(Object o) {
56     int in=indexOf(o);
57     if (in!=-1) {
58       removeElementAt(in);
59       return true;
60     }
61
62     return false;
63   }
64
65   public Object elementAt(int index) {
66     if (index<0 | index >=size) {
67       System.printString("Illegal Vector.elementAt\n");
68       System.exit(-1);
69       return null;
70     }
71     return array[index];
72   }
73
74   public void setElementAt(Object obj, int index) {
75     if (index <size)
76       array[index]=obj;
77     else {
78       System.printString("Illegal Vector.setElementAt\n");
79       System.exit(-1);
80     }
81   }
82
83   private void ensureCapacity(int minCapacity) {
84     if (minCapacity>array.length) {
85       int newsize;
86       if (capacityIncrement<=0)
87         newsize=array.length*2;
88       else
89         newsize=array.length+capacityIncrement;
90       if (newsize<minCapacity)
91         newsize=minCapacity;
92       Object [] newarray=new Object[newsize];
93       for(int i=0; i<size; i++)
94         newarray[i]=array[i];
95       array=newarray;
96     }
97   }
98
99   public int size() {
100     return size;
101   }
102
103   public Enumeration elements() {
104     System.printString("Vector.elements not implemented\n");
105     System.exit(-1);
106   }
107
108   public void addElement(Object obj) {
109     if (size==array.length) {
110       ensureCapacity(size+1);
111     }
112     array[size++]=obj;
113   }
114
115   public void insertElementAt(Object obj, int index) {
116     if (index<0||index>size) {
117       System.printString("Illegal Vector.insertElementAt\n");
118       System.exit(-1);
119     }
120
121     if (size==array.length) {
122       ensureCapacity(size+1);
123     }
124     size++;
125     for(int i=size-1; i>index; --i) {
126       array[i] = array[i-1];
127     }
128     array[index] = obj;
129   }
130
131   public void removeElementAt(int index) {
132     if (index<0||index>=size) {
133       System.printString("Illegal Vector.removeElementAt\n");
134       System.exit(-1);
135     }
136     removeElement(array, index, size);
137     size--;
138     array[size]=null;
139   }
140
141   public static native void removeElement(Object[] array, int index, int size);
142
143   public void removeAllElements() {
144     int s = size;
145     for(int i = 0; i<s; ++i ) {
146       removeElementAt(0);
147     }
148   }
149 }