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 if (this->curSize > 0) {
174 delete [] this->elements;
177 this->elements =
new E[this->capacity];
185 return this->curSize == 0;
189 return this->curSize;
192 virtual E
set(
int index,
const E& element) {
194 if (index < 0 || index >= this->curSize) {
196 __FILE__, __LINE__,
"Index greater than size() or negative");
199 E oldValue = this->elements[index];
200 this->elements[index] = element;
205 virtual E
get(
int index)
const {
207 if (index < 0 || index >= this->curSize) {
209 __FILE__, __LINE__,
"Index greater than size() or negative");
212 return this->elements[index];
215 virtual bool add(
const E& value) {
218 this->elements[this->curSize++] = value;
224 virtual void add(
int index,
const E& element) {
226 if (index < 0 || index > this->curSize) {
228 __FILE__, __LINE__,
"Index was negative or greater than size()");
232 this->expandFront(1);
233 }
else if (index == this->curSize) {
236 this->expandMiddle(index, 1);
239 this->elements[index] = element;
246 int csize = collection.
size();
251 std::vector<E> array = collection.
toArray();
253 this->expandEnd(csize);
255 for (
int i = 0; i < csize; ++i) {
256 this->elements[this->curSize++] = array[i];
266 if(index < 0 || index > this->curSize) {
268 __FILE__, __LINE__,
"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) {
310 __FILE__, __LINE__,
"Index greater than size() or negative");
313 E old = this->elements[index];
317 if (this->curSize > index) {
318 System::arraycopy(this->elements, index + 1, this->elements, index, this->curSize - index - 1);
321 this->elements[--this->curSize] = E();
328 return this->
indexOf(value) != -1;
333 for (
int i = 0; i < this->curSize; ++i) {
334 if (this->elements[i] == value) {
344 for (
int i = this->curSize - 1; i >= 0; --i) {
345 if (this->elements[i] == value) {
355 std::vector<E> result;
357 for (
int i = 0; i < this->curSize; ++i) {
358 result.push_back(this->elements[i]);
368 result.append(
"decaf::util::ArrayList [ size = ");
377 void expandFront(
int amount) {
383 E* previous = this->elements;
385 if (amount > this->capacity - this->curSize) {
386 this->capacity = this->capacity + amount + 11;
387 this->elements =
new E[this->capacity];
390 if (this->curSize > 0) {
394 if (previous != this->elements) {
399 void expandEnd(
int amount) {
405 E* previous = this->elements;
407 if (amount > this->capacity - this->curSize) {
408 this->capacity = this->capacity + amount + 11;
409 this->elements =
new E[this->capacity];
413 if(previous != this->elements) {
418 void expandMiddle(
int index,
int amount) {
424 E* previous = this->elements;
426 if (amount > this->capacity - this->curSize) {
427 this->capacity = this->capacity + amount + 11;
428 this->elements =
new E[this->capacity];
431 if (this->curSize > 0) {
435 if (this->curSize > index) {
436 System::arraycopy(previous, index, this->elements, index + amount, this->curSize - index);
439 if (previous != this->elements) {
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:331
The root interface in the collection hierarchy.
Definition: Collection.h:68
bool operator!=(const ArrayList< E > &other) const
Definition: ArrayList.h:114
virtual std::string toString() const
Definition: ArrayList.h:364
virtual int size() const
Returns the number of elements in this collection.
Definition: ArrayList.h:188
virtual E set(int index, const E &element)
Replaces the element at the specified position in this list with the specified element.
Definition: ArrayList.h:192
virtual std::vector< E > toArray() const
Answers an STL vector containing copies of all elements contained in this Collection.
Definition: ArrayList.h:353
ArrayList()
Definition: ArrayList.h:49
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:342
#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 contains(const E &value) const
Returns true if this collection contains the specified element.More formally, returns true if and onl...
Definition: ArrayList.h:327
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:264
Definition: IndexOutOfBoundsException.h:31
bool operator==(const ArrayList< E > &other) const
Definition: ArrayList.h:110
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 decaf::util::Iterator< E > * iterator()=0
ArrayList(const Collection< E > &collection)
Definition: ArrayList.h:53
Definition: IllegalArgumentException.h:31
std::string toString() const
This class provides a skeletal implementation of the List interface to minimize the effort required t...
Definition: AbstractList.h:65
virtual E removeAt(int index)
Removes the element at the specified position in this list.
Definition: ArrayList.h:306
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:244
virtual bool add(const E &value)
Returns true if this collection changed as a result of the call.
Definition: ArrayList.h:215
virtual ~ArrayList()
Definition: ArrayList.h:89
Definition: ArrayList.h:39
virtual bool isEmpty() const
Returns true if this collection contains no elements.
Definition: ArrayList.h:184
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
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.
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:224
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:172