2
* This program is free software; you can redistribute it and/or modify
3
* it under the terms of the GNU General Public License as published by
4
* the Free Software Foundation; either version 2 of the License, or
5
* (at your option) any later version.
7
* This program is distributed in the hope that it will be useful,
8
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
* GNU General Public License for more details.
12
* You should have received a copy of the GNU General Public License
13
* along with this program; if not, write to the Free Software
14
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
* Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
25
import java.io.Serializable;
26
import java.util.Enumeration;
29
* Implements a fast vector class without synchronized
30
* methods. Replaces java.util.Vector. (Synchronized methods tend to
33
* @author Eibe Frank (eibe@cs.waikato.ac.nz)
34
* @version $Revision: 1.14 $
36
public class FastVector
37
implements Copyable, Serializable {
39
/** for serialization */
40
private static final long serialVersionUID = -2173635135622930169L;
43
* Class for enumerating the vector's elements.
45
public class FastVectorEnumeration implements Enumeration {
48
private int m_Counter;
49
// These JML commands say how m_Counter implements Enumeration
51
//@ private represents moreElements = m_Counter < m_Vector.size();
52
//@ private invariant 0 <= m_Counter && m_Counter <= m_Vector.size();
55
private /*@non_null@*/ FastVector m_Vector;
57
/** Special element. Skipped during enumeration. */
58
private int m_SpecialElement;
59
//@ private invariant -1 <= m_SpecialElement;
60
//@ private invariant m_SpecialElement < m_Vector.size();
61
//@ private invariant m_SpecialElement>=0 ==> m_Counter!=m_SpecialElement;
64
* Constructs an enumeration.
66
* @param vector the vector which is to be enumerated
68
public FastVectorEnumeration(/*@non_null@*/FastVector vector) {
72
m_SpecialElement = -1;
76
* Constructs an enumeration with a special element.
77
* The special element is skipped during the enumeration.
79
* @param vector the vector which is to be enumerated
80
* @param special the index of the special element
82
//@ requires 0 <= special && special < vector.size();
83
public FastVectorEnumeration(/*@non_null@*/FastVector vector, int special){
86
m_SpecialElement = special;
96
* Tests if there are any more elements to enumerate.
98
* @return true if there are some elements left
100
public final /*@pure@*/ boolean hasMoreElements() {
102
if (m_Counter < m_Vector.size()) {
109
* Returns the next element.
111
* @return the next element to be enumerated
113
//@ also requires hasMoreElements();
114
public final Object nextElement() {
116
Object result = m_Vector.elementAt(m_Counter);
119
if (m_Counter == m_SpecialElement) {
126
/** The array of objects. */
127
private /*@spec_public@*/ Object[] m_Objects;
128
//@ invariant m_Objects != null;
129
//@ invariant m_Objects.length >= 0;
131
/** The current size; */
132
private /*@spec_public@*/ int m_Size = 0;
133
//@ invariant 0 <= m_Size;
134
//@ invariant m_Size <= m_Objects.length;
136
/** The capacity increment */
137
private /*@spec_public@*/ int m_CapacityIncrement = 1;
138
//@ invariant 1 <= m_CapacityIncrement;
140
/** The capacity multiplier. */
141
private /*@spec_public@*/ int m_CapacityMultiplier = 2;
142
//@ invariant 1 <= m_CapacityMultiplier;
144
// Make sure the size will increase...
145
//@ invariant 3 <= m_CapacityMultiplier + m_CapacityIncrement;
148
* Constructs an empty vector with initial
151
public FastVector() {
153
m_Objects = new Object[0];
157
* Constructs a vector with the given capacity.
159
* @param capacity the vector's initial capacity
161
//@ requires capacity >= 0;
162
public FastVector(int capacity) {
164
m_Objects = new Object[capacity];
168
* Adds an element to this vector. Increases its
169
* capacity if its not large enough.
171
* @param element the element to add
173
public final void addElement(Object element) {
177
if (m_Size == m_Objects.length) {
178
newObjects = new Object[m_CapacityMultiplier *
180
m_CapacityIncrement)];
181
System.arraycopy(m_Objects, 0, newObjects, 0, m_Size);
182
m_Objects = newObjects;
184
m_Objects[m_Size] = element;
189
* Returns the capacity of the vector.
191
* @return the capacity of the vector
193
//@ ensures \result == m_Objects.length;
194
public final /*@pure@*/ int capacity() {
196
return m_Objects.length;
200
* Produces a shallow copy of this vector.
202
* @return the new vector
204
public final Object copy() {
206
FastVector copy = new FastVector(m_Objects.length);
208
copy.m_Size = m_Size;
209
copy.m_CapacityIncrement = m_CapacityIncrement;
210
copy.m_CapacityMultiplier = m_CapacityMultiplier;
211
System.arraycopy(m_Objects, 0, copy.m_Objects, 0, m_Size);
216
* Clones the vector and shallow copies all its elements.
217
* The elements have to implement the Copyable interface.
219
* @return the new vector
221
public final Object copyElements() {
223
FastVector copy = new FastVector(m_Objects.length);
225
copy.m_Size = m_Size;
226
copy.m_CapacityIncrement = m_CapacityIncrement;
227
copy.m_CapacityMultiplier = m_CapacityMultiplier;
228
for (int i = 0; i < m_Size; i++) {
229
copy.m_Objects[i] = ((Copyable)m_Objects[i]).copy();
235
* Returns the element at the given position.
237
* @param index the element's index
238
* @return the element with the given index
240
//@ requires 0 <= index;
241
//@ requires index < m_Objects.length;
242
public final /*@pure@*/ Object elementAt(int index) {
244
return m_Objects[index];
248
* Returns an enumeration of this vector.
250
* @return an enumeration of this vector
252
public final /*@pure@*/ Enumeration elements() {
254
return new FastVectorEnumeration(this);
258
* Returns an enumeration of this vector, skipping the
259
* element with the given index.
261
* @param index the element to skip
262
* @return an enumeration of this vector
264
//@ requires 0 <= index && index < size();
265
public final /*@pure@*/ Enumeration elements(int index) {
267
return new FastVectorEnumeration(this, index);
273
public /*@pure@*/ boolean contains(Object o) {
277
for(int i=0; i<m_Objects.length; i++)
278
if(o.equals(m_Objects[i]))
286
* Returns the first element of the vector.
288
* @return the first element of the vector
290
//@ requires m_Size > 0;
291
public final /*@pure@*/ Object firstElement() {
297
* Searches for the first occurence of the given argument,
298
* testing for equality using the equals method.
300
* @param element the element to be found
301
* @return the index of the first occurrence of the argument
302
* in this vector; returns -1 if the object is not found
304
public final /*@pure@*/ int indexOf(/*@non_null@*/ Object element) {
306
for (int i = 0; i < m_Size; i++) {
307
if (element.equals(m_Objects[i])) {
315
* Inserts an element at the given position.
317
* @param element the element to be inserted
318
* @param index the element's index
320
public final void insertElementAt(Object element, int index) {
324
if (m_Size < m_Objects.length) {
325
System.arraycopy(m_Objects, index, m_Objects, index + 1,
327
m_Objects[index] = element;
329
newObjects = new Object[m_CapacityMultiplier *
331
m_CapacityIncrement)];
332
System.arraycopy(m_Objects, 0, newObjects, 0, index);
333
newObjects[index] = element;
334
System.arraycopy(m_Objects, index, newObjects, index + 1,
336
m_Objects = newObjects;
342
* Returns the last element of the vector.
344
* @return the last element of the vector
346
//@ requires m_Size > 0;
347
public final /*@pure@*/ Object lastElement() {
349
return m_Objects[m_Size - 1];
353
* Deletes an element from this vector.
355
* @param index the index of the element to be deleted
357
//@ requires 0 <= index && index < m_Size;
358
public final void removeElementAt(int index) {
360
System.arraycopy(m_Objects, index + 1, m_Objects, index,
366
* Removes all components from this vector and sets its
369
public final void removeAllElements() {
371
m_Objects = new Object[m_Objects.length];
376
* Appends all elements of the supplied vector to this vector.
378
* @param toAppend the FastVector containing elements to append.
380
public final void appendElements(FastVector toAppend) {
382
setCapacity(size() + toAppend.size());
383
System.arraycopy(toAppend.m_Objects, 0, m_Objects, size(), toAppend.size());
384
m_Size = m_Objects.length;
388
* Returns all the elements of this vector as an array
390
* @return an array containing all the elements of this vector
392
public final Object [] toArray() {
394
Object [] newObjects = new Object[size()];
395
System.arraycopy(m_Objects, 0, newObjects, 0, size());
400
* Sets the vector's capacity to the given value.
402
* @param capacity the new capacity
404
public final void setCapacity(int capacity) {
406
Object[] newObjects = new Object[capacity];
408
System.arraycopy(m_Objects, 0, newObjects, 0, Math.min(capacity, m_Size));
409
m_Objects = newObjects;
410
if (m_Objects.length < m_Size)
411
m_Size = m_Objects.length;
415
* Sets the element at the given index.
417
* @param element the element to be put into the vector
418
* @param index the index at which the element is to be placed
420
//@ requires 0 <= index && index < size();
421
public final void setElementAt(Object element, int index) {
423
m_Objects[index] = element;
427
* Returns the vector's current size.
429
* @return the vector's current size
431
//@ ensures \result == m_Size;
432
public final /*@pure@*/ int size() {
438
* Swaps two elements in the vector.
440
* @param first index of the first element
441
* @param second index of the second element
443
//@ requires 0 <= first && first < size();
444
//@ requires 0 <= second && second < size();
445
public final void swap(int first, int second) {
447
Object help = m_Objects[first];
449
m_Objects[first] = m_Objects[second];
450
m_Objects[second] = help;
454
* Sets the vector's capacity to its size.
456
public final void trimToSize() {
458
Object[] newObjects = new Object[m_Size];
460
System.arraycopy(m_Objects, 0, newObjects, 0, m_Size);
461
m_Objects = newObjects;