18 #ifndef _DECAF_UTIL_ARRAYLIST_H_
19 #define _DECAF_UTIL_ARRAYLIST_H_
38 template<
typename E >
54 capacity( 0 ), head( 0 ), curSize( 0 ) {
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();
67 capacity( 0 ), head( 0 ), curSize( 0 ) {
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 capacity( initialCapacity ), head( 0 ), curSize( 0 ) {
82 if( initialCapacity < 0 ) {
84 __FILE__, __LINE__,
"Initial Capacity argument cannot be negative." );
87 this->elements =
new E[this->capacity];
107 this->
addAll( 0, collection );
124 if( minimumCapacity < 0 || this->capacity >= minimumCapacity ) {
128 int newCapacity = minimumCapacity == 0 ? 10 : minimumCapacity;
130 E* newElements =
new E[newCapacity];
131 if( this->curSize > 0 ) {
134 delete [] this->elements;
135 this->elements = newElements;
136 this->capacity = newCapacity;
146 if( this->curSize < this->capacity ) {
148 int newCapacity = this->curSize == 0 ? 10 : this->curSize;
150 E* newElements =
new E[newCapacity];
151 if( this->curSize > 0 ) {
155 delete [] this->elements;
156 this->elements = newElements;
157 this->capacity = newCapacity;
166 delete [] this->elements;
169 this->elements =
new E[this->capacity];
174 return this->curSize == 0;
178 return this->curSize;
181 virtual E
set(
int index,
const E& element ) {
183 if( index < 0 || index >= this->curSize ) {
186 "List::get - Index greater than size() or negative" );
189 E oldValue = this->elements[index];
190 this->elements[index] = element;
195 virtual E
get(
int index )
const {
197 if( index < 0 || index >= this->curSize ) {
200 "List::get - Index greater than size() or negative" );
203 return this->elements[index];
206 virtual bool add(
const E& value ) {
208 this->expandEnd( 1 );
209 this->elements[this->curSize++] = value;
215 virtual void add(
int index,
const E& element ) {
217 if( index < 0 || index > this->curSize ) {
220 "Index was negative or greater than size()" );
224 this->expandFront( 1 );
225 }
else if( index == this->curSize ) {
226 this->expandEnd( 1 );
228 this->expandMiddle( index, 1 );
231 this->elements[index] = element;
238 int csize = collection.
size();
243 std::vector<E> array = collection.
toArray();
245 this->expandEnd( csize );
247 for(
int i = 0; i < csize; ++i ) {
248 this->elements[this->curSize++] = array[i];
258 if( index < 0 || index > this->curSize ) {
261 "List::addAll - Index greater than size()" );
264 int csize = collection.
size();
269 std::vector<E> array = collection.
toArray();
272 this->expandFront( csize );
273 }
else if( index == this->curSize ) {
274 this->expandEnd( csize );
276 this->expandMiddle( index, csize );
279 for(
int i = 0; i < csize; ++i, ++this->curSize ) {
280 this->elements[index++] = array[i];
288 virtual bool remove(
const E& value ) {
301 if( index < 0 || index >= this->curSize ) {
304 "List::removeAt - Index greater than size() or negative" );
307 E old = this->elements[index];
311 if( this->curSize > index ) {
312 System::arraycopy( this->elements, index + 1, this->elements, index, this->curSize - index - 1 );
315 this->elements[--this->curSize] = E();
322 return this->
indexOf( value ) != -1;
327 for(
int i = 0; i < this->curSize; ++i ) {
328 if( this->elements[i] == value ) {
338 for(
int i = this->curSize - 1; i >= 0 ; --i ) {
339 if( this->elements[i] == value ) {
349 std::vector<E> result;
351 for(
int i = 0; i < this->curSize; ++i ) {
352 result.push_back( this->elements[i] );
362 result.append(
"decaf::util::ArrayList [ size = " );
364 result.append(
" ]");
371 void expandFront(
int amount ) {
377 E* previous = this->elements;
379 if( amount > this->capacity - this->curSize ) {
380 this->capacity = this->capacity + amount + 11;
381 this->elements =
new E[this->capacity];
384 if( this->curSize > 0 ) {
388 if( previous != this->elements ) {
393 void expandEnd(
int amount ) {
399 E* previous = this->elements;
401 if( amount > this->capacity - this->curSize ) {
402 this->capacity = this->capacity + amount + 11;
403 this->elements =
new E[this->capacity];
407 if( previous != this->elements ) {
412 void expandMiddle(
int index,
int amount ) {
418 E* previous = this->elements;
420 if( amount > this->capacity - this->curSize ) {
421 this->capacity = this->capacity + amount + 11;
422 this->elements =
new E[this->capacity];
425 if( this->curSize > 0 ) {
429 if( this->curSize > index ) {
430 System::arraycopy( previous, index, this->elements, index + amount, this->curSize - index );
433 if( previous != this->elements ) {