18 #ifndef _DECAF_UTIL_ARRAYLIST_H_ 19 #define _DECAF_UTIL_ARRAYLIST_H_ 38 template<
typename E >
56 this->capacity = collection.
size() + (collection.
size() / 10);
57 this->elements =
new E[this->capacity];
59 std::auto_ptr<Iterator<E> > iter(collection.
iterator());
60 while (iter->hasNext()) {
61 this->elements[this->head++] = iter->next();
69 this->capacity = arrayList.
size() + (arrayList.
size() / 10);
70 this->elements =
new E[this->capacity];
72 std::auto_ptr<Iterator<E> > iter(arrayList.
iterator());
73 while (iter->hasNext()) {
74 this->elements[this->head++] = iter->next();
80 AbstractList<E>(), elements(
NULL), capacity(initialCapacity), head(0), curSize(0) {
82 if (initialCapacity < 0) {
86 this->elements =
new E[this->capacity];
106 this->
addAll( 0, collection );
111 return this->
equals(other);
115 return !this->
equals(other);
131 if( minimumCapacity < 0 || this->capacity >= minimumCapacity ) {
135 int newCapacity = minimumCapacity == 0 ? 10 : minimumCapacity;
137 E* newElements =
new E[newCapacity];
138 if( this->curSize > 0 ) {
141 delete [] this->elements;
142 this->elements = newElements;
143 this->capacity = newCapacity;
153 if( this->curSize < this->capacity ) {
155 int newCapacity = this->curSize == 0 ? 10 : this->curSize;
157 E* newElements =
new E[newCapacity];
158 if( this->curSize > 0 ) {
162 delete [] this->elements;
163 this->elements = newElements;
164 this->capacity = newCapacity;
173 delete [] this->elements;
176 this->elements =
new E[this->capacity];
181 return this->curSize == 0;
185 return this->curSize;
188 virtual E
set(
int index,
const E& element ) {
190 if( index < 0 || index >= this->curSize ) {
193 "List::get - Index greater than size() or negative" );
196 E oldValue = this->elements[index];
197 this->elements[index] = element;
202 virtual E
get(
int index )
const {
204 if( index < 0 || index >= this->curSize ) {
207 "List::get - Index greater than size() or negative" );
210 return this->elements[index];
213 virtual bool add(
const E& value ) {
215 this->expandEnd( 1 );
216 this->elements[this->curSize++] = value;
222 virtual void add(
int index,
const E& element ) {
224 if( index < 0 || index > this->curSize ) {
227 "Index was negative or greater than size()" );
231 this->expandFront( 1 );
232 }
else if( index == this->curSize ) {
233 this->expandEnd( 1 );
235 this->expandMiddle( index, 1 );
238 this->elements[index] = element;
245 int csize = collection.
size();
250 std::vector<E> array = collection.
toArray();
252 this->expandEnd( csize );
254 for(
int i = 0; i < csize; ++i ) {
255 this->elements[this->curSize++] = array[i];
265 if( index < 0 || index > this->curSize ) {
268 "List::addAll - Index greater than size()" );
271 int csize = collection.
size();
276 std::vector<E> array = collection.
toArray();
279 this->expandFront( csize );
280 }
else if( index == this->curSize ) {
281 this->expandEnd( csize );
283 this->expandMiddle( index, csize );
286 for(
int i = 0; i < csize; ++i, ++this->curSize ) {
287 this->elements[index++] = array[i];
295 virtual bool remove(
const E& value ) {
308 if( index < 0 || index >= this->curSize ) {
311 "List::removeAt - Index greater than size() or negative" );
314 E old = this->elements[index];
318 if( this->curSize > index ) {
319 System::arraycopy( this->elements, index + 1, this->elements, index, this->curSize - index - 1 );
322 this->elements[--this->curSize] = E();
329 return this->
indexOf( value ) != -1;
334 for(
int i = 0; i < this->curSize; ++i ) {
335 if( this->elements[i] == value ) {
345 for(
int i = this->curSize - 1; i >= 0; --i ) {
346 if( this->elements[i] == value ) {
356 std::vector<E> result;
358 for(
int i = 0; i < this->curSize; ++i ) {
359 result.push_back( this->elements[i] );
369 result.append(
"decaf::util::ArrayList [ size = " );
371 result.append(
" ]");
378 void expandFront(
int amount ) {
384 E* previous = this->elements;
386 if( amount > this->capacity - this->curSize ) {
387 this->capacity = this->capacity + amount + 11;
388 this->elements =
new E[this->capacity];
391 if( this->curSize > 0 ) {
395 if( previous != this->elements ) {
400 void expandEnd(
int amount ) {
406 E* previous = this->elements;
408 if( amount > this->capacity - this->curSize ) {
409 this->capacity = this->capacity + amount + 11;
410 this->elements =
new E[this->capacity];
414 if( previous != this->elements ) {
419 void expandMiddle(
int index,
int amount ) {
425 E* previous = this->elements;
427 if( amount > this->capacity - this->curSize ) {
428 this->capacity = this->capacity + amount + 11;
429 this->elements =
new E[this->capacity];
432 if( this->curSize > 0 ) {
436 if( this->curSize > index ) {
437 System::arraycopy( previous, index, this->elements, index + amount, this->curSize - index );
440 if( previous != this->elements ) {
The root interface in the collection hierarchy.
Definition: Collection.h:68
bool operator!=(const ArrayList< E > &other) const
Definition: ArrayList.h:114
virtual int indexOf(const E &value) const
Returns the index of the first occurrence of the specified element in this list, or -1 if this list d...
Definition: ArrayList.h:332
ArrayList()
Definition: ArrayList.h:49
#define NULL
Definition: Config.h:33
#define DECAF_CATCHALL_NOTHROW()
A catch-all that does not throw an exception, one use would be to catch any exception in a destructor...
Definition: ExceptionDefines.h:62
virtual bool isEmpty() const
Returns true if this collection contains no elements.
Definition: ArrayList.h:180
static void arraycopy(const char *src, std::size_t srcPos, char *dest, std::size_t destPos, std::size_t length)
Copies the number of elements specified by length from the source array starting at the given source ...
virtual void clear()
Removes all of the elements from this collection (optional operation).
Definition: ArrayList.h:172
void trimToSize()
Trims the internal array to match the current size of the ArrayList.
Definition: ArrayList.h:151
virtual int size() const =0
Returns the number of elements in this collection.
virtual bool addAll(int index, const Collection< E > &collection)
Inserts all of the elements in the specified collection into this list at the specified position (opt...
Definition: ArrayList.h:263
Definition: IndexOutOfBoundsException.h:31
virtual bool equals(const Collection< E > &collection) const
Answers true if this Collection and the one given are the same size and if each element contained in ...
Definition: AbstractCollection.h:158
void ensureCapacity(int minimumCapacity)
Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least ...
Definition: ArrayList.h:129
virtual bool contains(const E &value) const
Returns true if this collection contains the specified element.More formally, returns true if and onl...
Definition: ArrayList.h:328
virtual decaf::util::Iterator< E > * iterator()=0
ArrayList(const Collection< E > &collection)
Definition: ArrayList.h:53
Definition: IllegalArgumentException.h:31
This class provides a skeletal implementation of the List interface to minimize the effort required t...
Definition: AbstractList.h:65
bool operator==(const ArrayList< E > &other) const
Definition: ArrayList.h:110
virtual E removeAt(int index)
Removes the element at the specified position in this list.
Definition: ArrayList.h:306
virtual std::vector< E > toArray() const
Answers an STL vector containing copies of all elements contained in this Collection.
Definition: ArrayList.h:354
std::string toString() const
virtual bool addAll(const Collection< E > &collection)
Adds all of the elements in the specified collection to this collection.The behavior of this operatio...
Definition: ArrayList.h:243
virtual bool add(const E &value)
Returns true if this collection changed as a result of the call.
Definition: ArrayList.h:213
virtual ~ArrayList()
Definition: ArrayList.h:89
Definition: ArrayList.h:39
ArrayList(const ArrayList< E > &arrayList)
Definition: ArrayList.h:66
virtual Iterator< E > * iterator()
Definition: AbstractList.h:345
The System class provides static methods for accessing system level resources and performing some sys...
Definition: System.h:41
virtual int size() const
Returns the number of elements in this collection.
Definition: ArrayList.h:184
virtual std::string toString() const
Definition: ArrayList.h:365
ArrayList< E > & operator=(const ArrayList< E > &list)
Definition: ArrayList.h:98
virtual std::vector< E > toArray() const =0
Returns an array containing all of the elements in this collection.
virtual int lastIndexOf(const E &value) const
Returns the index of the last occurrence of the specified element in this list, or -1 if this list do...
Definition: ArrayList.h:343
ArrayList(int initialCapacity)
Definition: ArrayList.h:79
virtual void add(int index, const E &element)
Inserts the specified element at the specified position in this list.
Definition: ArrayList.h:222
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: AprPool.h:25