18#ifndef _DECAF_UTIL_CONCURRENT_COPYONWRITEARRAYLIST_H_
19#define _DECAF_UTIL_CONCURRENT_COPYONWRITEARRAYLIST_H_
34 template<
typename E >
44 Array() :
size(0), capacity(0), elements(
NULL) {
47 Array(
int capacity) :
size(0), capacity(0), elements(
NULL) {
51 Array(
const Array& src,
int capacity) :
size(0), capacity(0), elements(
NULL) {
56 this->size = src.size;
63 void reserve(
int requested) {
64 if (capacity < requested) {
67 E* newbuf = newlen ?
new E[newlen] :
NULL;
69 if (this->elements !=
NULL) {
73 delete[] this->elements;
74 this->elements = newbuf;
82 Array operator= (
const Array&);
102 ArrayListIterator(
const ArrayListIterator&);
103 ArrayListIterator& operator=(
const ArrayListIterator&);
110 if (position < 0 || position > array->size) {
111 throw decaf::lang::exceptions::IndexOutOfBoundsException(
112 __FILE__, __LINE__,
"Iterator created with invalid index.");
121 if (position >= array->size) {
125 return this->array->elements[position++];
129 return this->position < array->size;
134 __FILE__, __LINE__,
"CopyOnWriteArrayList Iterator cannot remove elements.");
137 virtual void add(
const E& e DECAF_UNUSED ) {
139 __FILE__, __LINE__,
"CopyOnWriteArrayList Iterator cannot add elements.");
142 virtual void set(
const E& e DECAF_UNUSED ) {
144 __FILE__, __LINE__,
"CopyOnWriteArrayList Iterator cannot add elements.");
148 return this->position > 0;
156 return this->array->elements[position--];
160 return this->position;
164 return this->position - 1;
172 this->condition.
reset(arrayLock.writeLock().newCondition());
176 this->condition.
reset(arrayLock.writeLock().newCondition());
177 this->doCopyCollection(collection);
181 this->condition.
reset(arrayLock.writeLock().newCondition());
182 this->doCopyCollection(collection);
186 this->condition.
reset(arrayLock.writeLock().newCondition());
188 for (
int i = 0; i <
size; ++i) {
189 temp->elements[i] = array[i];
192 this->array.
swap(temp);
205 this->doCopyCollection(list);
207 this->writeLock.unlock();
219 this->doCopyCollection(list);
221 this->writeLock.unlock();
235 this->doCopyCollection(collection);
243 virtual bool add(
const E& value) {
247 int size = oldArray->size;
249 newArray->elements[
size] = value;
251 this->array.
swap(newArray);
264 int size = oldArray->size;
266 std::auto_ptr<Iterator<E> > iter(collection.
iterator());
267 while (iter->hasNext()) {
268 newArray->elements[newArray->size++] = iter->next();
270 this->array.
swap(newArray);
283 this->array.
swap(newArray);
296 current = this->array;
303 for (
int i = 0; i < current->size; ++i) {
304 if (current->elements[i] == value) {
313 std::auto_ptr<Iterator<E> > iter(collection.
iterator());
314 while (iter->hasNext()) {
315 E next = iter->next();
326 if ((
void*)
this == &collection) {
330 const List<E>* asList =
dynamic_cast<const List<E>*
> (&collection);
331 if (asList ==
NULL) {
335 if (this->
size() != asList->
size()) {
339 std::auto_ptr<Iterator<E> > thisIter(this->
iterator());
340 std::auto_ptr<Iterator<E> > otherIter(asList->
iterator());
342 while (thisIter->hasNext()) {
343 if (!otherIter->hasNext()) {
347 E myNext = thisIter->next();
348 E otherNext = otherIter->next();
350 if (myNext != otherNext) {
355 if (otherIter->hasNext()) {
366 current = this->array;
373 return current->size == 0;
379 int index = this->
indexOf(value);
401 int size = oldArray->size;
410 for (
int i = 0; i <
size; ++i) {
411 E value = oldArray->elements[i];
413 buffer->elements[count++] = value;
419 this->array.
reset(
new Array());
422 this->array.
swap(newArray);
438 int size = oldArray->size;
446 this->array.
reset(
new Array());
454 for (
int i = 0; i <
size; ++i) {
455 E value = oldArray->elements[i];
457 buffer->elements[count++] = value;
463 this->array.
reset(
new Array());
465 this->array.
swap(buffer);
480 current = this->array;
487 return current->size;
494 current = this->array;
500 std::vector<E> result((std::size_t) current->size);
501 for (
int i = 0; i < current->size; ++i) {
502 result[i] = current->elements[i];
514 current = this->array;
527 current = this->array;
543 current = this->array;
556 current = this->array;
570 current = this->array;
583 current = this->array;
597 current = this->array;
604 for (
int i = 0; i < current->size; ++i) {
605 if (current->elements[i] == value) {
617 current = this->array;
624 for (
int i = current->size - 1; i >= 0; --i) {
625 if (current->elements[i] == value) {
633 virtual E
get(
int index)
const {
637 current = this->array;
644 checkIndexExclusive(index, current->size);
645 return current->elements[index];
648 virtual E
set(
int index,
const E& element) {
652 int size = oldArray->size;
653 this->checkIndexExclusive(index,
size);
655 E old = newArray->elements[index];
656 newArray->elements[index] = element;
657 this->array.
swap(newArray);
666 virtual void add(
int index,
const E& element) {
670 int size = oldArray->size;
671 this->checkIndexInclusive(index,
size);
682 newArray->elements[index] = element;
683 newArray->size =
size + 1;
684 this->array.
swap(newArray);
696 int size = oldArray->size;
697 this->checkIndexInclusive(index,
size);
698 int csize = collection.
size();
710 std::auto_ptr<Iterator<E> > iter(collection.
iterator());
712 while (iter->hasNext()) {
713 newArray->elements[pos++] = iter->next();
719 newArray->size =
size + csize;
720 this->array.
swap(newArray);
733 int size = oldArray->size;
734 this->checkIndexExclusive(index,
size);
735 E old = oldArray->elements[index];
738 this->array.
reset(
new Array());
750 newArray->size =
size - 1;
751 this->array.
swap(newArray);
764 current = this->array;
790 int size = oldArray->size;
793 if (this->
indexOf(value) != -1) {
800 newArray->elements[
size] = value;
802 this->array.
swap(newArray);
823 if (collection.
size() == 0) {
830 int size = oldArray->size;
834 std::auto_ptr<Iterator<E> > iter(collection.
iterator());
835 while (iter->hasNext()) {
836 E value = iter->next();
837 if (this->
indexOf(value) == -1) {
838 buffer->elements[count++] = value;
845 newArray->size =
size + count;
846 this->array.
swap(newArray);
872 current = this->array;
880 if (index >= current->size) {
882 __FILE__, __LINE__,
"Index given %d, actual size %d", index, current->size);
885 for (
int i = index - 1; i >= 0; --i) {
886 if (current->elements[i] == value) {
907 int indexOf(
const E& value,
int index)
const {
911 current = this->array;
921 __FILE__, __LINE__,
"Index given %d, actual size %d", index, current->size);
924 for (
int i = index; i < current->size; ++i) {
925 if (current->elements[i] == value) {
948 this->condition->await();
951 virtual void wait(
long long millisecs) {
955 virtual void wait(
long long millisecs,
int nanos ) {
957 this->condition->awaitNanos(timeout);
961 this->condition->signal();
965 this->condition->signalAll();
972 if ((
void*)
this == &collection || collection.
isEmpty()) {
979 int size = oldArray->size;
982 std::auto_ptr<Iterator<E> > iter(collection.
iterator());
984 while (iter->hasNext()) {
985 buffer->elements[
size + index++] = iter->next();
989 this->array.
swap(buffer);
997 static void checkIndexInclusive(
int index,
int size) {
998 if (index < 0 || index >
size) {
999 throw decaf::lang::exceptions::IndexOutOfBoundsException(
1000 __FILE__, __LINE__,
"Index is %d, size is %d", index,
size);
1004 static void checkIndexExclusive(
int index,
int size) {
1005 if (index < 0 || index >=
size) {
1006 throw decaf::lang::exceptions::IndexOutOfBoundsException(
1007 __FILE__, __LINE__,
"Index is %d, size is %d", index,
size);
Definition Exception.h:38
virtual decaf::util::Iterator< E > * iterator()=0
static short max(short a, short b)
Returns the larger of two short values.
Definition Math.h:426
Decaf's implementation of a Smart Pointer that is a template on a Type and is Thread Safe if the defa...
Definition Pointer.h:53
void reset(T *value=NULL)
Resets the Pointer to hold the new value.
Definition Pointer.h:161
void swap(Pointer &value)
Exception Safe Swap Function.
Definition Pointer.h:198
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 ...
Definition IndexOutOfBoundsException.h:31
Definition UnsupportedOperationException.h:32
The root interface in the collection hierarchy.
Definition Collection.h:69
virtual bool contains(const E &value) const =0
Returns true if this collection contains the specified element.
virtual int size() const =0
Returns the number of elements in this collection.
virtual bool isEmpty() const =0
Defines an object that can be used to iterate over the elements of a collection.
Definition Iterator.h:34
List()
Definition List.h:57
An iterator for lists that allows the programmer to traverse the list in either direction,...
Definition ListIterator.h:38
Definition NoSuchElementException.h:31
Definition CopyOnWriteArrayList.h:94
virtual int previousIndex() const
Returns the index of the element that would be returned by a subsequent call to previous.
Definition CopyOnWriteArrayList.h:163
virtual bool hasPrevious() const
Returns true if this list iterator has more elements when traversing the list in the reverse directio...
Definition CopyOnWriteArrayList.h:147
ArrayListIterator(decaf::lang::Pointer< Array > array, int index)
Definition CopyOnWriteArrayList.h:107
virtual E next()
Returns the next element in the iteration.
Definition CopyOnWriteArrayList.h:120
virtual E previous()
Returns the previous element in the list.
Definition CopyOnWriteArrayList.h:151
virtual int nextIndex() const
Returns the index of the element that would be returned by a subsequent call to next.
Definition CopyOnWriteArrayList.h:159
virtual void remove()
Removes from the underlying collection the last element returned by the iterator (optional operation)...
Definition CopyOnWriteArrayList.h:132
virtual void add(const E &e DECAF_UNUSED)
Definition CopyOnWriteArrayList.h:137
virtual void set(const E &e DECAF_UNUSED)
Definition CopyOnWriteArrayList.h:142
virtual ~ArrayListIterator()
Definition CopyOnWriteArrayList.h:116
virtual bool hasNext() const
Returns true if the iteration has more elements.
Definition CopyOnWriteArrayList.h:128
int addAllAbsent(const Collection< E > &collection)
Every element in the given collection that is not already contained in this Collection is added to th...
Definition CopyOnWriteArrayList.h:821
virtual E removeAt(int index)
Removes the element at the specified position in this list.
Definition CopyOnWriteArrayList.h:729
virtual ListIterator< E > * listIterator() const
Definition CopyOnWriteArrayList.h:552
virtual E set(int index, const E &element)
Replaces the element at the specified position in this list with the specified element.
Definition CopyOnWriteArrayList.h:648
virtual bool add(const E &value)
Returns true if this collection changed as a result of the call.
Definition CopyOnWriteArrayList.h:243
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 CopyOnWriteArrayList.h:593
virtual bool contains(const E &value) const
Returns true if this collection contains the specified element.
Definition CopyOnWriteArrayList.h:292
virtual void wait()
Waits on a signal from this object, which is generated by a call to Notify.
Definition CopyOnWriteArrayList.h:947
virtual E get(int index) const
Gets the element contained at position passed.
Definition CopyOnWriteArrayList.h:633
virtual ListIterator< E > * listIterator(int index) const
Definition CopyOnWriteArrayList.h:579
virtual std::string toString() const
Definition CopyOnWriteArrayList.h:760
virtual void unlock()
Unlocks the object.
Definition CopyOnWriteArrayList.h:943
CopyOnWriteArrayList< E > & operator=(const CopyOnWriteArrayList< E > &list)
Definition CopyOnWriteArrayList.h:201
virtual bool containsAll(const Collection< E > &collection) const
Returns true if this collection contains all of the elements in the specified collection.
Definition CopyOnWriteArrayList.h:312
CopyOnWriteArrayList(const Collection< E > &collection)
Definition CopyOnWriteArrayList.h:175
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 CopyOnWriteArrayList.h:613
virtual bool equals(const Collection< E > &collection) const
Compares the passed collection to this one, if they contain the same elements, i.e.
Definition CopyOnWriteArrayList.h:324
CopyOnWriteArrayList< E > & operator=(const Collection< E > &list)
Definition CopyOnWriteArrayList.h:215
virtual decaf::util::Iterator< E > * iterator()
Definition CopyOnWriteArrayList.h:510
virtual void lock()
Locks the object.
Definition CopyOnWriteArrayList.h:935
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 CopyOnWriteArrayList.h:692
virtual bool remove(const E &value)
Removes a single instance of the specified element from the collection.
Definition CopyOnWriteArrayList.h:376
virtual void wait(long long millisecs)
Waits on a signal from this object, which is generated by a call to Notify.
Definition CopyOnWriteArrayList.h:951
virtual int size() const
Returns the number of elements in this collection.
Definition CopyOnWriteArrayList.h:476
virtual void clear()
Removes all of the elements from this collection (optional operation).
Definition CopyOnWriteArrayList.h:279
bool addIfAbsent(const E &value)
Adds the given value to the end of this List if it is not already contained in this List.
Definition CopyOnWriteArrayList.h:786
virtual void notify()
Signals a waiter on this object that it can now wake up and continue.
Definition CopyOnWriteArrayList.h:960
virtual void notifyAll()
Signals the waiters on this object that it can now wake up and continue.
Definition CopyOnWriteArrayList.h:964
virtual void add(int index, const E &element)
Inserts the specified element at the specified position in this list.
Definition CopyOnWriteArrayList.h:666
CopyOnWriteArrayList()
Definition CopyOnWriteArrayList.h:171
virtual ListIterator< E > * listIterator()
Definition CopyOnWriteArrayList.h:539
virtual bool retainAll(const Collection< E > &collection)
Retains only the elements in this collection that are contained in the specified collection (optional...
Definition CopyOnWriteArrayList.h:433
virtual bool isEmpty() const
Definition CopyOnWriteArrayList.h:362
virtual decaf::util::Iterator< E > * iterator() const
Definition CopyOnWriteArrayList.h:523
virtual void wait(long long millisecs, int nanos)
Waits on a signal from this object, which is generated by a call to Notify.
Definition CopyOnWriteArrayList.h:955
virtual bool addAll(const Collection< E > &collection)
Adds all of the elements in the specified collection to this collection.
Definition CopyOnWriteArrayList.h:260
virtual std::vector< E > toArray() const
Returns an array containing all of the elements in this collection.
Definition CopyOnWriteArrayList.h:490
CopyOnWriteArrayList(const CopyOnWriteArrayList< E > &collection)
Definition CopyOnWriteArrayList.h:180
virtual void copy(const Collection< E > &collection)
Renders this Collection as a Copy of the given Collection.
Definition CopyOnWriteArrayList.h:231
int indexOf(const E &value, int index) const
Searches the List starting from the specified index and returns the index of the first item in the li...
Definition CopyOnWriteArrayList.h:907
virtual bool tryLock()
Attempts to Lock the object, if the lock is already held by another thread than this method returns f...
Definition CopyOnWriteArrayList.h:939
int lastIndexOf(const E &value, int index)
Searches backwards through the List for the given element starting at the index specified.
Definition CopyOnWriteArrayList.h:868
virtual ~CopyOnWriteArrayList()
Definition CopyOnWriteArrayList.h:195
virtual bool removeAll(const Collection< E > &collection)
Removes all this collection's elements that are also contained in the specified collection (optional ...
Definition CopyOnWriteArrayList.h:393
CopyOnWriteArrayList(const E *array, int size)
Definition CopyOnWriteArrayList.h:185
virtual ListIterator< E > * listIterator(int index)
Definition CopyOnWriteArrayList.h:566
static const TimeUnit MILLISECONDS
Definition TimeUnit.h:79
virtual bool tryLock()=0
Acquires the lock only if it is free at the time of invocation.
virtual void unlock()=0
Releases the lock.
virtual void lock()=0
Acquires the lock.
Definition ReentrantReadWriteLock.h:38
virtual decaf::util::concurrent::locks::Lock & writeLock()
Returns the lock used for writing.the lock used for writing.
virtual decaf::util::concurrent::locks::Lock & readLock()
Returns the lock used for reading.the lock used for reading.
#define NULL
Definition Config.h:33
Definition AbstractExecutorService.h:28
Definition AbstractCollection.h:33
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
Definition AprPool.h:25