Source code: com/memoire/fu/FuVectorint.java
1 /**
2 * @modification $Date: 2002/12/16 18:56:26 $
3 * @statut unstable
4 * @file FuVectorint.java
5 * @version 0.36
6 * @author Guillaume Desnoix
7 * @email guillaume@desnoix.com
8 * @license GNU General Public License 2 (GPL2)
9 * @copyright 1998-2001 Guillaume Desnoix
10 */
11
12 package com.memoire.fu;
13
14 import com.memoire.fu.*;
15
16
17 import java.io.*;
18 import java.util.*;
19
20 /**
21 * A vector for int values.
22 * This a vector for primitive types.
23 * This source code for this class is generated from FuVector.jgen.
24 * Public fields needed for Yapod serialization.
25 */
26 public class FuVectorint
27 implements Cloneable, Serializable
28 {
29 public int data_[];
30 public int count_;
31 public int increment_;
32
33 public FuVectorint(int _capacity,int _increment)
34 {
35 super();
36 data_ =new int[_capacity];
37 count_ =0;
38 increment_=_increment;
39 }
40
41 public FuVectorint(int _capacity)
42 {
43 this(_capacity,0);
44 }
45
46 public FuVectorint()
47 {
48 this(11,0);
49 }
50
51 public FuVectorint(int[] _data)
52 {
53 super();
54 data_ =_data;
55 count_ =_data.length;
56 increment_=1;
57 }
58
59 public final synchronized void copyInto(int[] _array)
60 {
61 for(int i=0;i<count_;i++)
62 _array[i]=data_[i];
63 }
64
65 public final synchronized int[] toArray()
66 {
67 int[] r=new int[count_];
68
69 for(int i=0;i<count_;i++)
70 r[i]=data_[i];
71
72 return r;
73 }
74
75 public final synchronized void trimToSize()
76 {
77 int old_capacity=data_.length;
78 if(count_<old_capacity)
79 {
80 int[] old_data=data_;
81 data_=new int[count_];
82 System.arraycopy(old_data,0,data_,0,count_);
83 }
84 }
85
86 public final synchronized void ensureCapacity(int _min)
87 {
88 if(_min>data_.length)
89 resize0(_min);
90 }
91
92 private void resize0(int _min)
93 {
94 int old_capacity=data_.length;
95 int[] old_data =data_;
96
97 int new_capacity=
98 ( (increment_>0)
99 ? (old_capacity+increment_)
100 : (old_capacity*2));
101
102 if(new_capacity<_min)
103 new_capacity=_min;
104
105 data_=new int[new_capacity];
106 System.arraycopy(old_data,0,data_,0,count_);
107 }
108
109 public final synchronized void setSize(int _new)
110 {
111 if((_new>count_)&&(_new>data_.length))
112 resize0(_new);
113 else
114 for(int i=_new;i<count_;i++)
115 data_[i]=0;
116
117 count_=_new;
118 }
119
120 public final int capacity()
121 {
122 return data_.length;
123 }
124
125 public final int size()
126 {
127 return count_;
128 }
129
130 public final boolean isEmpty()
131 {
132 return count_==0;
133 }
134
135 public final synchronized Enumerator elements()
136 {
137 return new Enumerator(this);
138 }
139
140 public final boolean contains(int _o)
141 {
142 return (indexOf(_o,0)>=0);
143 }
144
145 public final int indexOf(int _o)
146 {
147 return indexOf(_o,0);
148 }
149
150 public final synchronized int indexOf(int _o, int _index)
151 {
152 for(int i=_index; i<count_; i++)
153 if(_o==data_[i]) // equals
154 return i;
155
156 return -1;
157 }
158
159 public final int lastIndexOf(int _o)
160 {
161 return lastIndexOf(_o,count_-1);
162 }
163
164 public final synchronized int lastIndexOf(int _o, int _index)
165 {
166 for(int i=_index; i>=0; i--)
167 if(_o==data_[i]) // equals
168 return i;
169
170 return -1;
171 }
172
173 public final synchronized int elementAt(int _index)
174 {
175 return data_[_index];
176 }
177
178 public final synchronized int firstElement()
179 {
180 if(count_==0) throw new NoSuchElementException();
181 return data_[0];
182 }
183
184 public final synchronized int lastElement()
185 {
186 if(count_==0) throw new NoSuchElementException();
187 return data_[count_-1];
188 }
189
190 public final synchronized void setElementAt(int _o,int _index)
191 {
192 if(_index>=count_) throw new ArrayIndexOutOfBoundsException();
193 data_[_index]=_o;
194 }
195
196 public final synchronized void removeElementAt(int _index)
197 {
198 int j=count_-_index-1;
199 if(j>0)
200 System.arraycopy(data_,_index+1,data_,_index,j);
201
202 count_--;
203 data_[count_]=0;
204 }
205
206 public final synchronized void insertElementAt(int _o, int _index)
207 {
208 if(_index==count_)
209 addElement(_o);
210 else
211 {
212 if(count_>=data_.length)
213 resize0(count_+1);
214 count_++;
215 System.arraycopy(data_,_index,data_,
216 _index+1,count_-1-_index);
217 data_[_index]=_o;
218 }
219 }
220
221 public final synchronized void addElement(int _o)
222 {
223 if(count_>=data_.length)
224 resize0(count_+1);
225 data_[count_]=_o;
226 count_++;
227 }
228
229 public final synchronized boolean removeElement(int _o)
230 {
231 int i=indexOf(_o);
232 if(i>=0)
233 {
234 removeElementAt(i);
235 return true;
236 }
237 return false;
238 }
239
240 public final synchronized void removeAllElements()
241 {
242 for(int i=0; i<count_; i++)
243 data_[i]=0;
244 count_=0;
245 }
246
247 public final synchronized Object clone()
248 {
249 FuVectorint r=null;
250
251 try
252 {
253 r=(FuVectorint)super.clone();
254 r.data_=new int[count_];
255 System.arraycopy(data_,0,r.data_,0,count_);
256 }
257 catch(CloneNotSupportedException ex)
258 {
259 throw new InternalError();
260 }
261
262 return r;
263 }
264
265 public final String toString()
266 {
267 return "FuVectorint("+count_+")";
268 }
269
270 public static final class Enumerator
271 {
272 private FuVectorint vector;
273 private int count;
274
275 Enumerator(FuVectorint _v)
276 {
277 vector=_v;
278 count =0;
279 }
280
281 public final boolean hasMoreElements()
282 {
283 return count<vector.count_;
284 }
285
286 public final int nextElement()
287 {
288 if(count<vector.count_)
289 return vector.data_[count++];
290 throw new NoSuchElementException("FuVectorint.Enumerator");
291 }
292 }
293 }