18 #ifndef _DECAF_UTIL_CONCURRENT_COPYONWRITEARRAYLIST_H_ 19 #define _DECAF_UTIL_CONCURRENT_COPYONWRITEARRAYLIST_H_ 32 namespace concurrent {
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) {
54 decaf::lang::System::arraycopy<E>(src.elements, 0, this->elements, 0, src.size);
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) {
70 decaf::lang::System::arraycopy<E>(this->elements, 0, newbuf, 0,
size);
73 delete[] this->elements;
74 this->elements = newbuf;
110 if (position < 0 || position > array->size) {
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;
132 virtual void remove() {
134 __FILE__, __LINE__,
"CopyOnWriteArrayList Iterator cannot remove elements.");
139 __FILE__, __LINE__,
"CopyOnWriteArrayList Iterator cannot add elements.");
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;
177 this->doCopyCollection(collection);
182 this->doCopyCollection(collection);
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;
376 virtual bool remove(
const E& value) {
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( 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);
675 decaf::lang::System::arraycopy<E>(oldArray->elements, 0, newArray->elements, 0, index);
679 decaf::lang::System::arraycopy<E>(oldArray->elements, index, newArray->elements, index + 1, size - index);
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());
744 decaf::lang::System::arraycopy<E>(oldArray->elements, 0, newArray->elements, 0, index);
747 decaf::lang::System::arraycopy<E>(oldArray->elements, index + 1, newArray->elements, index, size - index - 1);
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 ) {
961 this->condition->
signal();
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) {
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) {
1007 __FILE__, __LINE__,
"Index is %d, size is %d", index, size);
virtual std::vector< E > toArray() const
Returns an array containing all of the elements in this collection.
Definition: CopyOnWriteArrayList.h:490
void reset(T *value=NULL)
Resets the Pointer to hold the new value.
Definition: Pointer.h:161
virtual ListIterator< E > * listIterator(int index)
Definition: CopyOnWriteArrayList.h:566
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
The root interface in the collection hierarchy.
Definition: Collection.h:68
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 E next()
Returns the next element in the iteration.
Definition: CopyOnWriteArrayList.h:120
virtual ListIterator< E > * listIterator()
Definition: CopyOnWriteArrayList.h:539
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
long long toNanos(long long duration) const
Equivalent to NANOSECONDS.convert(duration, this).
Definition: TimeUnit.h:126
virtual void notifyAll()
Signals the waiters on this object that it can now wake up and continue.
Definition: CopyOnWriteArrayList.h:964
ArrayListIterator(decaf::lang::Pointer< Array > array, int index)
Definition: CopyOnWriteArrayList.h:107
virtual int size() const
Returns the number of elements in this collection.
Definition: CopyOnWriteArrayList.h:476
CopyOnWriteArrayList(const CopyOnWriteArrayList< E > &collection)
Definition: CopyOnWriteArrayList.h:180
virtual std::string toString() const
Definition: CopyOnWriteArrayList.h:760
Definition: CopyOnWriteArrayList.h:94
virtual void wait()
Waits on a signal from this object, which is generated by a call to Notify.
Definition: CopyOnWriteArrayList.h:947
#define NULL
Definition: Config.h:33
virtual bool isEmpty() const =0
CopyOnWriteArrayList< E > & operator=(const CopyOnWriteArrayList< E > &list)
Definition: CopyOnWriteArrayList.h:201
virtual decaf::util::concurrent::locks::Lock & readLock()
Returns the lock used for reading.the lock used for reading.
virtual E previous()
Returns the previous element in the list.
Definition: CopyOnWriteArrayList.h:151
virtual void signal()=0
Wakes up one waiting thread.
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
Definition: CopyOnWriteArrayList.h:35
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 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
An iterator for lists that allows the programmer to traverse the list in either direction, modify the list during iteration, and obtain the iterator's current position in the list.
Definition: ListIterator.h:38
virtual int size() const =0
Returns the number of elements in this collection.
CopyOnWriteArrayList< E > & operator=(const Collection< E > &list)
Definition: CopyOnWriteArrayList.h:215
virtual void add(const E &e DECAF_UNUSED)
Definition: CopyOnWriteArrayList.h:137
virtual ListIterator< E > * listIterator(int index) const
Definition: CopyOnWriteArrayList.h:579
virtual bool addAll(const Collection< E > &collection)
Adds all of the elements in the specified collection to this collection.
Definition: CopyOnWriteArrayList.h:260
Defines an object that can be used to iterate over the elements of a collection.
Definition: Iterator.h:34
virtual bool hasNext() const
Returns true if the iteration has more elements.
Definition: CopyOnWriteArrayList.h:128
virtual int previousIndex() const
Returns the index of the element that would be returned by a subsequent call to previous.
Definition: CopyOnWriteArrayList.h:163
Definition: IndexOutOfBoundsException.h:31
Definition: UnsupportedOperationException.h:32
virtual bool tryLock()=0
Acquires the lock only if it is free at the time of invocation.
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
static const TimeUnit MILLISECONDS
Definition: TimeUnit.h:79
virtual decaf::util::Iterator< E > * iterator()
Definition: CopyOnWriteArrayList.h:510
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 contains(const E &value) const =0
Returns true if this collection contains the specified element.
virtual void lock()=0
Acquires the lock.
virtual void clear()
Removes all of the elements from this collection (optional operation).
Definition: CopyOnWriteArrayList.h:279
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
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 decaf::util::Iterator< E > * iterator() const
Definition: CopyOnWriteArrayList.h:523
virtual decaf::util::Iterator< E > * iterator()=0
virtual bool add(const E &value)
Returns true if this collection changed as a result of the call.
Definition: CopyOnWriteArrayList.h:243
virtual void add(int index, const E &element)
Inserts the specified element at the specified position in this list.
Definition: CopyOnWriteArrayList.h:666
virtual E removeAt(int index)
Removes the element at the specified position in this list.
Definition: CopyOnWriteArrayList.h:729
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
static short max(short a, short b)
Returns the larger of two short values.
Definition: Math.h:426
virtual ~ArrayListIterator()
Definition: CopyOnWriteArrayList.h:116
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
virtual Condition * newCondition()=0
Returns a new Condition instance that is bound to this Lock instance.
virtual long long awaitNanos(long long nanosTimeout)=0
Causes the current thread to wait until it is signaled or interrupted, or the specified waiting time ...
virtual void lock()
Locks the object.
Definition: CopyOnWriteArrayList.h:935
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
virtual void unlock()=0
Releases the lock.
virtual void copy(const Collection< E > &collection)
Renders this Collection as a Copy of the given Collection.
Definition: CopyOnWriteArrayList.h:231
virtual bool contains(const E &value) const
Returns true if this collection contains the specified element.
Definition: CopyOnWriteArrayList.h:292
virtual void notify()
Signals a waiter on this object that it can now wake up and continue.
Definition: CopyOnWriteArrayList.h:960
virtual ~CopyOnWriteArrayList()
Definition: CopyOnWriteArrayList.h:195
CopyOnWriteArrayList(const E *array, int size)
Definition: CopyOnWriteArrayList.h:185
#define DECAF_UNUSED
Definition: Config.h:160
Definition: NoSuchElementException.h:31
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
CopyOnWriteArrayList()
Definition: CopyOnWriteArrayList.h:171
CopyOnWriteArrayList(const Collection< E > &collection)
Definition: CopyOnWriteArrayList.h:175
Definition: Exception.h:38
virtual decaf::util::concurrent::locks::Lock & writeLock()
Returns the lock used for writing.the lock used for writing.
virtual ListIterator< E > * listIterator() const
Definition: CopyOnWriteArrayList.h:552
virtual void signalAll()=0
Wakes up all waiting threads.
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
An ordered collection (also known as a sequence).
Definition: List.h:47
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 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
Definition: ReentrantReadWriteLock.h:38
virtual void await()=0
Causes the current thread to wait until it is signaled or interrupted.
virtual void unlock()
Unlocks the object.
Definition: CopyOnWriteArrayList.h:943
void swap(Pointer &value)
Exception Safe Swap Function.
Definition: Pointer.h:198
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: AprPool.h:25
virtual bool isEmpty() const
Definition: CopyOnWriteArrayList.h:362