18 #ifndef _DECAF_UTIL_LINKEDLIST_H_
19 #define _DECAF_UTIL_LINKEDLIST_H_
54 template<
typename E >
58 template<
typename U >
68 ListNode(
const ListNode&);
73 ListNode() : value(), prev(
NULL), next(
NULL) {}
75 ListNode(
const U& value) : value(value), prev(
NULL), next(
NULL) {}
77 ListNode(ListNode<U>* prev, ListNode<U>* next,
const U& value) :
78 value(value), prev(prev), next(next) {
93 this->head.next = &this->tail;
94 this->tail.prev = &this->head;
99 this->head.next = &this->tail;
100 this->tail.prev = &this->head;
102 this->addAllAtLocation(0, list);
107 this->head.next = &this->tail;
108 this->tail.prev = &this->head;
110 this->addAllAtLocation(0, collection);
123 this->addAllAtLocation(0, list);
129 this->addAllAtLocation(0, collection);
134 return this->
equals(other);
138 return !this->
equals(other);
143 virtual E
get(
int index)
const {
145 if (index < 0 || index >= this->listSize) {
147 __FILE__, __LINE__,
"Index given is outside bounds of this list {%d}", index );
150 const ListNode<E>* location =
NULL;
152 if (index < this->listSize / 2) {
153 location = &this->head;
154 for (
int i = 0; i <= index; ++i) {
155 location = location->next;
158 location = &this->tail;
159 for (
int i = this->listSize; i > index; --i) {
160 location = location->prev;
164 return location->value;
169 if (index < 0 || index >= this->listSize) {
171 __FILE__, __LINE__,
"Index given is outside bounds of this list {%d}", index );
174 ListNode<E>* location =
NULL;
176 if (index < this->listSize / 2) {
177 location = &this->head;
178 for (
int i = 0; i <= index; ++i) {
179 location = location->next;
182 location = &this->tail;
183 for (
int i = this->listSize; i > index; --i) {
184 location = location->prev;
188 E oldValue = location->value;
194 virtual bool add(
const E& value) {
195 this->addToEnd(value);
199 virtual void add(
int index,
const E& value) {
201 if (index < 0 || index > this->listSize) {
203 __FILE__, __LINE__,
"Index given is outside bounds of this list {%d}", index );
206 this->addAtLocation(index, value);
210 return this->addAllAtLocation(this->listSize, collection);
214 return this->addAllAtLocation(index, collection);
219 this->addAllAtLocation(0, collection);
222 virtual bool remove(
const E& value) {
227 return this->listSize == 0;
231 return this->listSize;
236 this->head.next = &this->tail;
237 this->tail.prev = &this->head;
243 return this->
indexOf(value) != -1;
248 if (this->listSize == 0) {
252 const ListNode<E>* location = this->head.next;
254 for (
int i = 0; location != &this->tail; ++i, location = location->next) {
255 if (location->value == value) {
265 if (this->listSize == 0) {
269 const ListNode<E>* location = this->tail.prev;
271 for (
int i = this->listSize - 1; location != &this->head; --i, location = location->prev) {
272 if (location->value == value) {
282 std::vector<E> result;
283 result.reserve(this->listSize);
285 const ListNode<E>* current = this->head.next;
287 while (current != &this->tail) {
288 result.push_back(current->value);
289 current = current->next;
297 virtual bool offer(
const E& value) {
304 if (this->listSize == 0) {
308 result = this->head.next->value;
309 this->removeAtFront();
314 return this->removeAtFront();
317 virtual bool peek(E& result)
const {
319 if (this->listSize == 0) {
323 result = this->head.next->value;
328 if (this->listSize == 0) {
332 return this->head.next->value;
336 this->addToFront(value);
340 this->addToEnd(value);
344 if (this->listSize == 0) {
348 return this->head.next->value;
352 if (this->listSize == 0) {
356 return this->head.next->value;
360 if (this->listSize == 0) {
364 return this->tail.prev->value;
368 if (this->listSize == 0) {
372 return this->tail.prev->value;
376 this->addToFront(element);
381 this->addToEnd(element);
386 return this->removeAtFront();
390 return this->removeAtEnd();
394 if (this->listSize == 0) {
398 result = this->head.next->value;
399 this->removeAtFront();
404 if (this->listSize == 0) {
408 result = this->tail.prev->value;
414 if (this->listSize == 0) {
418 result = this->head.next->value;
423 if (this->listSize == 0) {
427 result = this->tail.prev->value;
432 return this->removeAtFront();
436 this->addToFront(element);
440 std::auto_ptr<Iterator<E> > iter(this->
iterator());
441 while (iter->hasNext()) {
442 if (iter->next() == value) {
453 while (iter->hasNext()) {
454 if (iter->next() == value) {
469 ListNode<E>* current;
470 ListNode<E>* lastReturned;
472 int expectedModCount;
476 LinkedListIterator(
const LinkedListIterator&);
477 LinkedListIterator
operator=(
const LinkedListIterator&);
482 ListIterator<E>(), list(list), current(
NULL), lastReturned(
NULL), index(-1), expectedModCount(0) {
486 __FILE__, __LINE__,
"Parent LinkedList pointer was Null." );
489 if (index < 0 || index > list->listSize) {
491 __FILE__, __LINE__,
"Given index {%d} is out of range.", index );
494 this->expectedModCount = list->
modCount;
500 if (index < this->list->listSize / 2) {
501 this->current = &this->list->head;
502 for (this->index = -1; this->index + 1 < index; ++this->index) {
503 this->current = this->current->next;
506 this->current = &this->list->tail;
507 for (this->index = this->list->listSize; this->index >= index; --this->index) {
508 this->current = this->current->prev;
513 virtual ~LinkedListIterator() {}
517 if (this->expectedModCount != this->list->
modCount) {
518 throw ConcurrentModificationException(
519 __FILE__, __LINE__,
"List modified outside of this Iterator." );
522 if (this->current->next == &(this->list->tail)) {
523 throw NoSuchElementException(
524 __FILE__, __LINE__,
"No more elements to return from next()" );
527 this->current = this->current->next;
528 this->lastReturned = this->current;
531 return this->current->value;
534 virtual bool hasNext()
const {
535 return (this->current->next != &this->list->tail);
539 virtual E previous() {
540 if (this->expectedModCount != this->list->
modCount) {
541 throw ConcurrentModificationException(
542 __FILE__, __LINE__,
"List modified outside of this Iterator." );
545 if (this->current == &(this->list->head)) {
548 "No previous element, must call next() before calling previous()." );
551 this->lastReturned = this->current;
552 this->current = this->current->prev;
555 return this->lastReturned->value;
558 virtual bool hasPrevious()
const {
559 return (this->current != &this->list->head);
562 virtual void remove() {
563 if (this->expectedModCount != this->list->
modCount) {
564 throw ConcurrentModificationException(
565 __FILE__, __LINE__,
"List modified outside of this Iterator." );
568 if (this->lastReturned ==
NULL) {
569 throw lang::exceptions::IllegalStateException(
571 "Invalid State to call remove, must call next() before remove()" );
574 ListNode<E>* next = this->lastReturned->next;
575 ListNode<E>* previous = this->lastReturned->prev;
577 next->prev = previous;
578 previous->next = next;
581 if (this->current == this->lastReturned) {
584 this->current = previous;
586 delete this->lastReturned;
587 this->lastReturned =
NULL;
589 this->list->listSize--;
592 this->expectedModCount++;
595 virtual void add(
const E& e) {
596 if (this->expectedModCount != this->list->
modCount) {
597 throw ConcurrentModificationException(
598 __FILE__, __LINE__,
"List modified outside of this Iterator." );
601 ListNode<E>* newNode =
new ListNode<E>( this->current, this->current->next, e );
603 this->current->next->prev = newNode;
604 this->current->next = newNode;
606 this->current = newNode;
607 this->lastReturned =
NULL;
610 this->expectedModCount++;
612 this->list->listSize++;
615 virtual void set(
const E& e) {
617 if (this->expectedModCount != this->list->
modCount) {
618 throw ConcurrentModificationException(
619 __FILE__, __LINE__,
"List modified outside of this Iterator." );
622 if (this->lastReturned !=
NULL) {
623 this->lastReturned->value = e;
626 __FILE__, __LINE__,
"Iterator next has not been called." );
630 virtual int nextIndex()
const {
631 return this->index + 1;
634 virtual int previousIndex()
const {
639 class ConstLinkedListIterator :
public ListIterator<E> {
642 const LinkedList<E>* list;
643 const ListNode<E>* current;
644 const ListNode<E>* lastReturned;
649 ConstLinkedListIterator(
const ConstLinkedListIterator&);
650 ConstLinkedListIterator
operator=(
const ConstLinkedListIterator&);
654 ConstLinkedListIterator(
const LinkedList<E>* list,
int index) :
655 ListIterator<E>(), list(list), current(
NULL), lastReturned(
NULL), index(-1) {
659 __FILE__, __LINE__,
"Parent LinkedList pointer was Null." );
662 if (index < 0 || index > list->listSize) {
664 __FILE__, __LINE__,
"Given index {%d} is out of range.", index );
671 if (index < this->list->listSize / 2) {
672 this->current = &this->list->head;
673 for (this->index = -1; this->index + 1 < index; ++this->index) {
674 this->current = this->current->next;
677 this->current = &this->list->tail;
678 for (this->index = this->list->listSize; this->index >= index; --this->index) {
679 this->current = this->current->prev;
684 virtual ~ConstLinkedListIterator() {}
688 if (this->current->next == &(this->list->tail)) {
689 throw NoSuchElementException(
690 __FILE__, __LINE__,
"No more elements to return from this ListIterator" );
693 this->current = this->current->next;
694 this->lastReturned = this->current;
697 return this->current->value;
700 virtual bool hasNext()
const {
701 return (this->current->next != &(this->list->tail));
704 virtual E previous() {
706 if (this->current == &(this->list->head)) {
709 "No previous element, must call next() before calling previous()." );
712 this->lastReturned = this->current;
713 this->current = this->current->prev;
716 return this->lastReturned->value;
719 virtual bool hasPrevious()
const {
720 return (this->current != &(this->list->head));
723 virtual void remove() {
724 throw lang::exceptions::UnsupportedOperationException(
725 __FILE__, __LINE__,
"Cannot write to a const ListIterator." );
729 throw lang::exceptions::UnsupportedOperationException(
730 __FILE__, __LINE__,
"Cannot write to a const ListIterator." );
734 throw lang::exceptions::UnsupportedOperationException(
735 __FILE__, __LINE__,
"Cannot write to a const ListIterator." );
738 virtual int nextIndex()
const {
739 return this->index + 1;
742 virtual int previousIndex()
const {
747 class ReverseIterator :
public Iterator<E> {
751 ListNode<E>* current;
752 int expectedModCount;
757 ReverseIterator(
const ReverseIterator&);
758 ReverseIterator
operator=(
const ReverseIterator&);
762 ReverseIterator(LinkedList<E>* list) :
763 Iterator<E>(), list(list), current(
NULL), expectedModCount(0), canRemove(false) {
767 __FILE__, __LINE__,
"Parent LinkedList pointer was Null." );
770 this->expectedModCount = this->list->modCount;
771 this->current = &list->tail;
774 virtual ~ReverseIterator() {}
776 virtual bool hasNext()
const {
777 return this->current->prev != &(this->list->head);
782 if (this->expectedModCount != this->list->modCount) {
783 throw ConcurrentModificationException(
784 __FILE__, __LINE__,
"List modified outside of this Iterator." );
787 if (this->current->prev == &(this->list->head)) {
788 throw NoSuchElementException(
789 __FILE__, __LINE__,
"No more elements to return from next()" );
792 this->current = this->current->prev;
793 this->canRemove =
true;
795 return this->current->value;
798 virtual void remove() {
799 if (this->expectedModCount != this->list->modCount) {
800 throw ConcurrentModificationException(
801 __FILE__, __LINE__,
"List modified outside of this Iterator." );
804 if (!this->canRemove) {
805 throw lang::exceptions::IllegalStateException(
807 "Invalid State to call remove, must call next() before remove()" );
810 ListNode<E>* next = this->current->prev;
811 ListNode<E>* prev = this->current->next;
816 delete this->current;
818 this->current = prev;
820 this->list->listSize--;
821 this->list->modCount++;
822 this->expectedModCount++;
823 this->canRemove =
false;
827 class ConstReverseIterator :
public Iterator<E> {
830 const LinkedList<E>* list;
831 const ListNode<E>* current;
835 ConstReverseIterator(
const ConstReverseIterator&);
836 ConstReverseIterator
operator=(
const ConstReverseIterator&);
840 ConstReverseIterator(
const LinkedList<E>* list) : Iterator<E>(), list(list), current(
NULL) {
844 __FILE__, __LINE__,
"Parent LinkedList pointer was Null." );
847 this->current = &list->tail;
850 virtual ~ConstReverseIterator() {}
852 virtual bool hasNext()
const {
853 return this->current->prev != &(this->list->head);
858 if (this->current->prev == &(this->list->head)) {
859 throw NoSuchElementException(
860 __FILE__, __LINE__,
"No more elements to return from next()" );
863 this->current = this->current->prev;
865 return this->current->value;
868 virtual void remove() {
869 throw lang::exceptions::UnsupportedOperationException(
870 __FILE__, __LINE__,
"Cannot write to a const Iterator." );
876 using AbstractSequentialList<E>::listIterator;
879 return new LinkedListIterator(
this, index);
882 return new ConstLinkedListIterator(
this, index);
886 return new ReverseIterator(
this);
889 return new ConstReverseIterator(
this);
896 if (this->head.next == &this->tail) {
898 __FILE__, __LINE__,
"The Collection is empty." );
901 ListNode<E>* oldNode = this->head.next;
902 E result = oldNode->value;
904 this->head.next = oldNode->next;
905 this->head.next->prev = &this->head;
910 AbstractList<E>::modCount++;
917 if (this->head.next == &this->tail) {
918 throw NoSuchElementException(
919 __FILE__, __LINE__,
"The Collection is empty." );
922 ListNode<E>* oldNode = this->tail.prev;
923 E result = oldNode->value;
925 this->tail.prev = oldNode->prev;
926 this->tail.prev->next = &this->tail;
931 AbstractList<E>::modCount++;
936 void addToFront(
const E& value) {
938 ListNode<E>* newHead =
new ListNode<E> (&this->head, this->head.next, value);
940 (this->head.next)->prev = newHead;
941 this->head.next = newHead;
944 AbstractList<E>::modCount++;
947 void addToEnd(
const E& value) {
949 ListNode<E>* newTail =
new ListNode<E> (this->tail.prev, &this->tail, value);
951 (this->tail.prev)->next = newTail;
952 this->tail.prev = newTail;
955 AbstractList<E>::modCount++;
958 void addAtLocation(
int index,
const E& value) {
960 ListNode<E>* location =
NULL;
962 if (index <= this->listSize / 2) {
963 location = this->head.next;
964 for (
int i = 0; i < index; ++i) {
965 location = location->next;
968 location = &this->tail;
969 for (
int i = this->listSize; i > index; --i) {
970 location = location->prev;
974 ListNode<E>* newNode =
new ListNode<E> (location->prev, location, value);
976 (location->prev)->next = newNode;
977 location->prev = newNode;
980 AbstractList<E>::modCount++;
983 bool addAllAtLocation(
int index,
const Collection<E>& collection) {
985 if (index < 0 || index > this->listSize) {
987 __FILE__, __LINE__,
"Index for add is outside bounds of this LinkedList." );
990 int csize = collection.size();
995 std::auto_ptr<ArrayList<E> >
copy;
996 std::auto_ptr<Iterator<E> > iter;
998 if (
this == &collection) {
999 copy.reset(
new ArrayList<E> (collection));
1000 iter.reset(copy->iterator());
1002 iter.reset(collection.iterator());
1005 ListNode<E>* newNode =
NULL;
1006 ListNode<E>* previous =
NULL;
1008 if (index < this->listSize / 2) {
1009 previous = &this->head;
1010 for (
int i = 0; i < index; ++i) {
1011 previous = previous->next;
1014 previous = &this->tail;
1015 for (
int i = this->listSize; i >= index; --i) {
1016 previous = previous->prev;
1020 while (iter->hasNext()) {
1021 newNode =
new ListNode<E> (previous, previous->next, iter->next());
1022 previous->next->prev = newNode;
1023 previous->next = newNode;
1027 this->listSize += csize;
1028 AbstractList<E>::modCount++;
1034 ListNode<E>* current = this->head.next;
1035 ListNode<E>* temp =
NULL;
1036 while (current != &this->tail) {
1038 current = current->next;
virtual bool removeLastOccurrence(const E &value)
Removes the last occurrence of the specified element from this Deque.
Definition: LinkedList.h:451
virtual E pop()
Treats this Deque as a stack and attempts to pop an element off the top.
Definition: LinkedList.h:431
virtual bool poll(E &result)
Gets and removes the element in the head of the queue.
Definition: LinkedList.h:302
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: LinkedList.h:263
The root interface in the collection hierarchy.
Definition: Collection.h:68
virtual Iterator< E > * descendingIterator() const
Definition: LinkedList.h:888
virtual bool removeFirstOccurrence(const E &value)
Removes the first occurrence of the specified element from this Deque.
Definition: LinkedList.h:439
LinkedList(const LinkedList< E > &list)
Definition: LinkedList.h:97
virtual void addFirst(const E &value)
Inserts an element onto the front of the Deque if possible without violating the implementations capa...
Definition: LinkedList.h:335
virtual ~LinkedList()
Definition: LinkedList.h:113
virtual const E & getLast() const
Definition: LinkedList.h:367
#define NULL
Definition: Config.h:33
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: LinkedList.h:209
virtual ListIterator< E > * listIterator(int index) const
Definition: LinkedList.h:881
virtual bool peek(E &result) const
Gets but not removes the element in the head of the queue.
Definition: LinkedList.h:317
virtual bool offerFirst(const E &element)
This method attempts to insert the given element into the Deque at the front end. ...
Definition: LinkedList.h:375
virtual bool contains(const E &value) const
Returns true if this collection contains the specified element.More formally, returns true if and onl...
Definition: LinkedList.h:242
virtual void addLast(const E &value)
Inserts an element onto the end of the Deque if possible without violating the implementations capaci...
Definition: LinkedList.h:339
virtual void clear()
Removes all of the elements from this collection (optional operation).
Definition: LinkedList.h:234
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
int modCount
Definition: AbstractList.h:69
LinkedList(const Collection< E > &collection)
Definition: LinkedList.h:105
bool operator==(const LinkedList< E > &other) const
Definition: LinkedList.h:133
virtual Iterator< E > * iterator()
Definition: AbstractSequentialList.h:69
virtual bool add(const E &value)
Returns true if this collection changed as a result of the call.
Definition: LinkedList.h:194
Defines an object that can be used to iterate over the elements of a collection.
Definition: Iterator.h:34
Definition: IndexOutOfBoundsException.h:31
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: LinkedList.h:246
LinkedList()
Definition: LinkedList.h:91
virtual bool offerLast(const E &element)
This method attempts to insert the given element into the Deque at the end.
Definition: LinkedList.h:380
virtual E set(int index, const E &element)
Replaces the element at the specified position in this list with the specified element.The index of the element to replace. The element to be stored at the specified position.the element previously at the specified position.if the index given is less than zero or greater than the List size. if this is an unmodifiable collection. if the Collection is a container of pointers and does not allow NULL values. if some property of the element prevents it from being added to this collection if the element cannot be added at this time due to insertion restrictions.This implementation first gets a list iterator pointing to the indexed element (with listIterator(index)). Then, it gets the current element using ListIterator.next and replaces it with ListIterator.set.
Definition: LinkedList.h:167
virtual Iterator< E > * descendingIterator()
Provides an Iterator over this Collection that traverses the element in reverse order.
Definition: LinkedList.h:885
virtual bool peekLast(E &result) const
Retrieves the last element contained in this Deque and assigns its value to the reference value passe...
Definition: LinkedList.h:422
virtual bool pollFirst(E &result)
Removes the first element from the Deque assigns it to the element reference passed.
Definition: LinkedList.h:393
virtual const E & getFirst() const
Definition: LinkedList.h:351
virtual int size() const
Returns the number of elements in this collection.
Definition: LinkedList.h:230
LinkedList< E > & operator=(const Collection< E > &collection)
Definition: LinkedList.h:127
virtual void push(const E &element)
Pushes an element onto the stack represented by this deque (in other words, at the head of this deque...
Definition: LinkedList.h:435
virtual bool offer(const E &value)
Inserts the specified element into the queue provided that the condition allows such an operation...
Definition: LinkedList.h:297
Definition: IllegalStateException.h:32
This class provides a skeletal implementation of the List interface to minimize the effort required t...
Definition: AbstractList.h:65
LinkedList< E > & operator=(const LinkedList< E > &list)
Definition: LinkedList.h:121
Definition: NullPointerException.h:32
#define DECAF_UNUSED
Definition: Config.h:160
Definition: NoSuchElementException.h:31
virtual E removeFirst()
Removes the topmost element from the Deque and returns it.
Definition: LinkedList.h:385
This class provides a skeletal implementation of the List interface to minimize the effort required t...
Definition: AbstractSequentialList.h:59
virtual void copy(const Collection< E > &collection)
Renders this Collection as a Copy of the given Collection.
Definition: LinkedList.h:217
virtual bool peekFirst(E &result) const
Retrieves the first element contained in this Deque and assigns its value to the reference value pass...
Definition: LinkedList.h:413
The System class provides static methods for accessing system level resources and performing some sys...
Definition: System.h:41
virtual void add(int index, const E &value)
Inserts the specified element at the specified position in this list.Shifts the element currently at ...
Definition: LinkedList.h:199
virtual bool isEmpty() const
Returns true if this collection contains no elements.
Definition: LinkedList.h:226
virtual bool pollLast(E &result)
Removes the last element from the Deque assigns it to the element reference passed.
Definition: LinkedList.h:403
virtual E & getLast()
Attempts to fetch a reference to the last element in the Deque.
Definition: LinkedList.h:359
bool operator!=(const LinkedList< E > &other) const
Definition: LinkedList.h:137
Defines a 'Double ended Queue' interface that allows for insertion and removal of elements from both ...
Definition: Deque.h:42
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: LinkedList.h:213
virtual E removeLast()
Removes the last element from the Deque and returns it.
Definition: LinkedList.h:389
virtual E & getFirst()
Attempts to fetch a reference to the first element in the Deque.
Definition: LinkedList.h:343
virtual std::vector< E > toArray() const
Answers an STL vector containing copies of all elements contained in this Collection.
Definition: LinkedList.h:280
virtual ListIterator< E > * listIterator(int index)
Definition: LinkedList.h:878
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
virtual E element() const
Gets but not removes the element in the head of the queue.
Definition: LinkedList.h:327
A complete implementation of the List interface using a doubly linked list data structure.
Definition: LinkedList.h:55