do a make tabbing
[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   public boolean isEmpty() {
19     return size==0;
20   }
21
22   public void clear() {
23     size=0;
24     array=new Object[10];
25   }
26
27   public int indexOf(Object elem) {
28     return indexOf(elem, 0);
29   }
30
31   public int indexOf(Object elem, int index) {
32     for(int i=index; i<size; i++) {
33       if (elem.equals(array[i]))
34         return i;
35     }
36     return -1;
37   }
38
39   public boolean contains(Object e) {
40     return indexOf(e)!=-1;
41   }
42
43   public void remove(Object o) {
44     int in=indexOf(o);
45     if (in!=-1)
46       removeElementAt(in);
47   }
48
49   public Object elementAt(int index) {
50     if (index<0 | index >=size) {
51       System.printString("Illegal Vector.elementAt\n");
52       System.exit(-1);
53       return null;
54     }
55     return array[index];
56   }
57
58   public void setElementAt(Object obj, int index) {
59     if (index <size)
60       array[index]=obj;
61     else {
62       System.printString("Illegal Vector.setElementAt\n");
63       System.exit(-1);
64     }
65   }
66
67   private ensureCapacity(int minCapacity) {
68     if (minCapacity>array.length) {
69       int newsize;
70       if (capacityIncrement<=0)
71         newsize=array.length*2;
72       else
73         newsize=array.length+capacityIncrement;
74       if (newsize<minCapacity)
75         newsize=minCapacity;
76       Object [] newarray=new Object[newsize];
77       for(int i=0; i<size; i++)
78         newarray[i]=array[i];
79       array=newarray;
80     }
81   }
82
83   public int size() {
84     return size;
85   }
86
87   public Enumeration elements() {
88     System.printString("Vector.elements not implemented\n");
89     System.exit(-1);
90   }
91
92   public void addElement(Object obj) {
93     if (size==array.length) {
94       ensureCapacity(size+1);
95     }
96     array[size++]=obj;
97   }
98
99   public void insertElementAt(Object obj, int index) {
100     if (index<0||index>size) {
101       System.printString("Illegal Vector.insertElementAt\n");
102       System.exit(-1);
103     }
104
105     if (size==array.length) {
106       ensureCapacity(size+1);
107     }
108     size++;
109     for(int i=size-1; i>index; --i) {
110       array[i] = array[i-1];
111     }
112     array[index] = obj;
113   }
114
115   public void removeElementAt(int index) {
116     if (index<0||index>=size) {
117       System.printString("Illegal Vector.removeElementAt\n");
118       System.exit(-1);
119     }
120     removeElement(array, index, size);
121     size--;
122     array[size]=null;
123   }
124
125   public static native void removeElement(Object[] array, int index, int size);
126
127   public void removeAllElements() {
128     int s = size;
129     for(int i = 0; i<s; ++i ) {
130       removeElementAt(0);
131     }
132   }
133 }