activemq-cpp-3.9.5
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  if (this->curSize > 0) {
174  delete [] this->elements;
175  this->curSize = 0;
176  this->capacity = 10;
177  this->elements = new E[this->capacity];
179  } else {
180  ensureCapacity(10);
181  }
182  }
183 
184  virtual bool isEmpty() const {
185  return this->curSize == 0;
186  }
187 
188  virtual int size() const {
189  return this->curSize;
190  }
191 
192  virtual E set(int index, const E& element) {
193 
194  if (index < 0 || index >= this->curSize) {
196  __FILE__, __LINE__, "Index greater than size() or negative");
197  }
198 
199  E oldValue = this->elements[index];
200  this->elements[index] = element;
201 
202  return oldValue;
203  }
204 
205  virtual E get(int index) const {
206 
207  if (index < 0 || index >= this->curSize) {
209  __FILE__, __LINE__, "Index greater than size() or negative");
210  }
211 
212  return this->elements[index];
213  }
214 
215  virtual bool add(const E& value) {
216 
217  this->expandEnd(1);
218  this->elements[this->curSize++] = value;
220 
221  return true;
222  }
223 
224  virtual void add(int index, const E& element) {
225 
226  if (index < 0 || index > this->curSize) {
228  __FILE__, __LINE__, "Index was negative or greater than size()");
229  }
230 
231  if (index == 0) {
232  this->expandFront(1);
233  } else if (index == this->curSize) {
234  this->expandEnd(1);
235  } else {
236  this->expandMiddle(index, 1);
237  }
238 
239  this->elements[index] = element;
240  this->curSize++;
242  }
243 
244  virtual bool addAll(const Collection<E>& collection) {
245 
246  int csize = collection.size();
247  if (csize == 0) {
248  return false;
249  }
250 
251  std::vector<E> array = collection.toArray();
252 
253  this->expandEnd(csize);
254 
255  for (int i = 0; i < csize; ++i) {
256  this->elements[this->curSize++] = array[i];
257  }
258 
260 
261  return true;
262  }
263 
264  virtual bool addAll(int index, const Collection<E>& collection) {
265 
266  if(index < 0 || index > this->curSize) {
268  __FILE__, __LINE__, "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__, "Index greater than size() or negative");
311  }
312 
313  E old = this->elements[index];
314 
315  System::arraycopy(this->elements, 0, this->elements, 0, index);
316 
317  if (this->curSize > index) {
318  System::arraycopy(this->elements, index + 1, this->elements, index, this->curSize - index - 1);
319  }
320 
321  this->elements[--this->curSize] = E();
323 
324  return old;
325  }
326 
327  virtual bool contains(const E& value) const {
328  return this->indexOf(value) != -1;
329  }
330 
331  virtual int indexOf(const E& value) const {
332 
333  for (int i = 0; i < this->curSize; ++i) {
334  if (this->elements[i] == value) {
335  return i;
336  }
337  }
338 
339  return -1;
340  }
341 
342  virtual int lastIndexOf(const E& value) const {
343 
344  for (int i = this->curSize - 1; i >= 0; --i) {
345  if (this->elements[i] == value) {
346  return i;
347  }
348  }
349 
350  return -1;
351  }
352 
353  virtual std::vector<E> toArray() const {
354 
355  std::vector<E> result;
356 
357  for (int i = 0; i < this->curSize; ++i) {
358  result.push_back(this->elements[i]);
359  }
360 
361  return result;
362  }
363 
364  virtual std::string toString() const {
365 
366  std::string result;
367 
368  result.append("decaf::util::ArrayList [ size = ");
369  result.append(decaf::lang::Integer::toString(this->curSize));
370  result.append(" ]");
371 
372  return result;
373  }
374 
375  private:
376 
377  void expandFront(int amount) {
378 
379  if (amount == 0) {
380  return;
381  }
382 
383  E* previous = this->elements;
384 
385  if (amount > this->capacity - this->curSize) {
386  this->capacity = this->capacity + amount + 11;
387  this->elements = new E[this->capacity];
388  }
389 
390  if (this->curSize > 0) {
391  System::arraycopy(previous, 0, this->elements, amount, this->curSize);
392  }
393 
394  if (previous != this->elements) {
395  delete [] previous;
396  }
397  }
398 
399  void expandEnd(int amount) {
400 
401  if (amount == 0) {
402  return;
403  }
404 
405  E* previous = this->elements;
406 
407  if (amount > this->capacity - this->curSize) {
408  this->capacity = this->capacity + amount + 11;
409  this->elements = new E[this->capacity];
410  System::arraycopy( previous, 0, this->elements, 0, this->curSize );
411  }
412 
413  if(previous != this->elements) {
414  delete [] previous;
415  }
416  }
417 
418  void expandMiddle(int index, int amount) {
419 
420  if (amount == 0) {
421  return;
422  }
423 
424  E* previous = this->elements;
425 
426  if (amount > this->capacity - this->curSize) {
427  this->capacity = this->capacity + amount + 11;
428  this->elements = new E[this->capacity];
429  }
430 
431  if (this->curSize > 0) {
432  System::arraycopy(previous, 0, this->elements, 0, index);
433  }
434 
435  if (this->curSize > index) {
436  System::arraycopy(previous, index, this->elements, index + amount, this->curSize - index);
437  }
438 
439  if (previous != this->elements) {
440  delete [] previous;
441  }
442  }
443  };
444 
445 }}
446 
447 #endif /* _DECAF_UTIL_ARRAYLIST_H_ */
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