activemq-cpp-3.8.2
ArrayList.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef _DECAF_UTIL_ARRAYLIST_H_
19 #define _DECAF_UTIL_ARRAYLIST_H_
20 
21 #include <memory>
25 #include <decaf/lang/System.h>
26 #include <decaf/lang/Integer.h>
27 #include <decaf/util/Config.h>
28 #include <decaf/util/Iterator.h>
30 #include <decaf/util/List.h>
32 
33 namespace decaf {
34 namespace util {
35 
36  using decaf::lang::System;
37 
38  template< typename E >
40  private:
41 
42  E* elements;
43  int capacity;
44  int head;
45  int curSize;
46 
47  public:
48 
49  ArrayList() : AbstractList<E>(), elements(NULL), capacity(0), head(0), curSize(0) {
50  this->ensureCapacity(10);
51  }
52 
53  ArrayList(const Collection<E>& collection) :
54  AbstractList<E>(), elements(NULL), capacity(0), head(0), curSize(0) {
55 
56  this->capacity = collection.size() + (collection.size() / 10);
57  this->elements = new E[this->capacity];
58 
59  std::auto_ptr<Iterator<E> > iter(collection.iterator());
60  while (iter->hasNext()) {
61  this->elements[this->head++] = iter->next();
62  this->curSize++;
63  }
64  }
65 
66  ArrayList(const ArrayList<E>& arrayList) :
67  AbstractList<E>(), elements(NULL), capacity(0), head(0), curSize(0) {
68 
69  this->capacity = arrayList.size() + (arrayList.size() / 10);
70  this->elements = new E[this->capacity];
71 
72  std::auto_ptr<Iterator<E> > iter(arrayList.iterator());
73  while (iter->hasNext()) {
74  this->elements[this->head++] = iter->next();
75  this->curSize++;
76  }
77  }
78 
79  ArrayList(int initialCapacity) :
80  AbstractList<E>(), elements(NULL), capacity(initialCapacity), head(0), curSize(0) {
81 
82  if (initialCapacity < 0) {
83  throw decaf::lang::exceptions::IllegalArgumentException(__FILE__, __LINE__, "Initial Capacity argument cannot be negative.");
84  }
85 
86  this->elements = new E[this->capacity];
87  }
88 
89  virtual ~ArrayList() {
90  try {
91  delete[] elements;
92  }
94  }
95 
96  public:
97 
99  this->clear();
100  this->addAll( list );
101  return *this;
102  }
103 
104  ArrayList<E>& operator= ( const Collection<E>& collection ) {
105  this->clear();
106  this->addAll( 0, collection );
107  return *this;
108  }
109 
110  bool operator==(const ArrayList<E>& other) const {
111  return this->equals(other);
112  }
113 
114  bool operator!=(const ArrayList<E>& other) const {
115  return !this->equals(other);
116  }
117 
118  public:
119 
129  void ensureCapacity( int minimumCapacity ) {
130 
131  if( minimumCapacity < 0 || this->capacity >= minimumCapacity ) {
132  return;
133  }
134 
135  int newCapacity = minimumCapacity == 0 ? 10 : minimumCapacity;
136 
137  E* newElements = new E[newCapacity];
138  if( this->curSize > 0 ) {
139  decaf::lang::System::arraycopy( this->elements, this->head, newElements, 0, this->curSize );
140  }
141  delete [] this->elements;
142  this->elements = newElements;
143  this->capacity = newCapacity;
145  }
146 
151  void trimToSize() {
152 
153  if( this->curSize < this->capacity ) {
154 
155  int newCapacity = this->curSize == 0 ? 10 : this->curSize;
156 
157  E* newElements = new E[newCapacity];
158  if( this->curSize > 0 ) {
159  System::arraycopy( this->elements, 0, newElements, 0, this->curSize );
160  }
161 
162  delete [] this->elements;
163  this->elements = newElements;
164  this->capacity = newCapacity;
165  }
166 
168  }
169 
170  public:
171 
172  virtual void clear() {
173  delete [] this->elements;
174  this->curSize = 0;
175  this->capacity = 10;
176  this->elements = new E[this->capacity];
178  }
179 
180  virtual bool isEmpty() const {
181  return this->curSize == 0;
182  }
183 
184  virtual int size() const {
185  return this->curSize;
186  }
187 
188  virtual E set( int index, const E& element ) {
189 
190  if( index < 0 || index >= this->curSize ) {
192  __FILE__, __LINE__,
193  "List::get - Index greater than size() or negative" );
194  }
195 
196  E oldValue = this->elements[index];
197  this->elements[index] = element;
198 
199  return oldValue;
200  }
201 
202  virtual E get( int index ) const {
203 
204  if( index < 0 || index >= this->curSize ) {
206  __FILE__, __LINE__,
207  "List::get - Index greater than size() or negative" );
208  }
209 
210  return this->elements[index];
211  }
212 
213  virtual bool add( const E& value ) {
214 
215  this->expandEnd( 1 );
216  this->elements[this->curSize++] = value;
218 
219  return true;
220  }
221 
222  virtual void add( int index, const E& element ) {
223 
224  if( index < 0 || index > this->curSize ) {
226  __FILE__, __LINE__,
227  "Index was negative or greater than size()" );
228  }
229 
230  if( index == 0 ) {
231  this->expandFront( 1 );
232  } else if( index == this->curSize ) {
233  this->expandEnd( 1 );
234  } else {
235  this->expandMiddle( index, 1 );
236  }
237 
238  this->elements[index] = element;
239  this->curSize++;
241  }
242 
243  virtual bool addAll( const Collection<E>& collection ) {
244 
245  int csize = collection.size();
246  if( csize == 0 ) {
247  return false;
248  }
249 
250  std::vector<E> array = collection.toArray();
251 
252  this->expandEnd( csize );
253 
254  for( int i = 0; i < csize; ++i ) {
255  this->elements[this->curSize++] = array[i];
256  }
257 
259 
260  return true;
261  }
262 
263  virtual bool addAll( int index, const Collection<E>& collection ) {
264 
265  if( index < 0 || index > this->curSize ) {
267  __FILE__, __LINE__,
268  "List::addAll - Index greater than size()" );
269  }
270 
271  int csize = collection.size();
272  if( csize == 0 ) {
273  return false;
274  }
275 
276  std::vector<E> array = collection.toArray();
277 
278  if( index == 0 ) {
279  this->expandFront( csize );
280  } else if( index == this->curSize ) {
281  this->expandEnd( csize );
282  } else {
283  this->expandMiddle( index, csize );
284  }
285 
286  for( int i = 0; i < csize; ++i, ++this->curSize ) {
287  this->elements[index++] = array[i];
288  }
289 
291 
292  return true;
293  }
294 
295  virtual bool remove( const E& value ) {
296 
297  int result = indexOf( value );
298  if( result != -1 ) {
299  this->removeAt( result );
300  return true;
301  }
302 
303  return false;
304  }
305 
306  virtual E removeAt( int index ) {
307 
308  if( index < 0 || index >= this->curSize ) {
310  __FILE__, __LINE__,
311  "List::removeAt - Index greater than size() or negative" );
312  }
313 
314  E old = this->elements[index];
315 
316  System::arraycopy( this->elements, 0, this->elements, 0, index );
317 
318  if( this->curSize > index ) {
319  System::arraycopy( this->elements, index + 1, this->elements, index, this->curSize - index - 1 );
320  }
321 
322  this->elements[--this->curSize] = E();
324 
325  return old;
326  }
327 
328  virtual bool contains( const E& value ) const {
329  return this->indexOf( value ) != -1;
330  }
331 
332  virtual int indexOf( const E& value ) const {
333 
334  for( int i = 0; i < this->curSize; ++i ) {
335  if( this->elements[i] == value ) {
336  return i;
337  }
338  }
339 
340  return -1;
341  }
342 
343  virtual int lastIndexOf( const E& value ) const {
344 
345  for( int i = this->curSize - 1; i >= 0; --i ) {
346  if( this->elements[i] == value ) {
347  return i;
348  }
349  }
350 
351  return -1;
352  }
353 
354  virtual std::vector<E> toArray() const {
355 
356  std::vector<E> result;
357 
358  for( int i = 0; i < this->curSize; ++i ) {
359  result.push_back( this->elements[i] );
360  }
361 
362  return result;
363  }
364 
365  virtual std::string toString() const {
366 
367  std::string result;
368 
369  result.append( "decaf::util::ArrayList [ size = " );
370  result.append( decaf::lang::Integer::toString( this->curSize ) );
371  result.append( " ]");
372 
373  return result;
374  }
375 
376  private:
377 
378  void expandFront( int amount ) {
379 
380  if( amount == 0 ) {
381  return;
382  }
383 
384  E* previous = this->elements;
385 
386  if( amount > this->capacity - this->curSize ) {
387  this->capacity = this->capacity + amount + 11;
388  this->elements = new E[this->capacity];
389  }
390 
391  if( this->curSize > 0 ) {
392  System::arraycopy( previous, 0, this->elements, amount, this->curSize );
393  }
394 
395  if( previous != this->elements ) {
396  delete [] previous;
397  }
398  }
399 
400  void expandEnd( int amount ) {
401 
402  if( amount == 0 ) {
403  return;
404  }
405 
406  E* previous = this->elements;
407 
408  if( amount > this->capacity - this->curSize ) {
409  this->capacity = this->capacity + amount + 11;
410  this->elements = new E[this->capacity];
411  System::arraycopy( previous, 0, this->elements, 0, this->curSize );
412  }
413 
414  if( previous != this->elements ) {
415  delete [] previous;
416  }
417  }
418 
419  void expandMiddle( int index, int amount ) {
420 
421  if( amount == 0 ) {
422  return;
423  }
424 
425  E* previous = this->elements;
426 
427  if( amount > this->capacity - this->curSize ) {
428  this->capacity = this->capacity + amount + 11;
429  this->elements = new E[this->capacity];
430  }
431 
432  if( this->curSize > 0 ) {
433  System::arraycopy( previous, 0, this->elements, 0, index );
434  }
435 
436  if( this->curSize > index ) {
437  System::arraycopy( previous, index, this->elements, index + amount, this->curSize - index );
438  }
439 
440  if( previous != this->elements ) {
441  delete [] previous;
442  }
443  }
444 
445  };
446 
447 }}
448 
449 #endif /* _DECAF_UTIL_ARRAYLIST_H_ */
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