18#ifndef _DECAF_UTIL_STLLIST_H_
19#define _DECAF_UTIL_STLLIST_H_
51 typename std::list<E>::iterator current;
52 typename std::list<E>::iterator prev;
53 typename std::list<E>* list;
57 StlListIterator(
const StlListIterator&);
58 StlListIterator operator= (
const StlListIterator&);
62 StlListIterator(
typename std::list<E>* list,
int index) :
63 current(list->begin()), prev(list->end()), list(list) {
65 if (index < (
int) list->size()) {
66 std::advance(this->current, index);
68 this->current = list->end();
72 virtual ~StlListIterator() {}
75 if (this->current == list->end()) {
78 "List::Iterator::next - No more elements to return");
81 this->prev = this->current;
82 return *(this->current++);
85 virtual bool hasNext()
const {
86 return ( this->current != list->end() );
90 if (this->prev == list->end()) {
93 "List::Iterator::remove - Invalid State to call remove");
96 this->list->erase(this->prev);
97 this->prev = this->list->end();
100 virtual void add(
const E& e) {
101 this->list->insert(this->current, e);
104 virtual void set(
const E& e) {
105 if (this->current == list->end()) {
106 this->list->insert(this->current, e);
108 *(this->current) = e;
112 virtual bool hasPrevious()
const {
113 return (this->current != this->list->begin());
116 virtual E previous() {
117 if (this->current == this->list->begin()) {
120 "List::ListIterator::previous - No Previous element." );
123 typename std::list<E>::const_iterator iter = this->current;
127 virtual int nextIndex()
const {
128 if (this->current == this->list->end()) {
129 return (
int) this->list->size();
132 return (
int) std::distance(this->list->begin(), this->current);
135 virtual int previousIndex()
const {
136 if (this->current == this->list->begin()) {
140 return (
int) std::distance(this->list->begin(), this->current) - 1;
147 typename std::list<E>::const_iterator current;
148 typename std::list<E>::const_iterator prev;
149 const typename std::list<E>* list;
153 ConstStlListIterator(
const ConstStlListIterator&);
154 ConstStlListIterator operator= (
const ConstStlListIterator&);
158 ConstStlListIterator(
const typename std::list<E>* list,
int index) :
159 ListIterator<E>(), current(list->begin()), prev(list->end()), list( list) {
161 if (index < (
int) list->size()) {
162 std::advance(this->current, index);
164 this->current = list->end();
168 virtual ~ConstStlListIterator() {}
171 if (this->current == list->end()) {
174 "List::Iterator::next - No more elements to return");
177 this->prev = this->current;
178 return *(this->current++);
181 virtual bool hasNext()
const {
182 return (this->current != list->end());
189 "List::ListIterator::remove - Const Iterator.");
192 virtual void add(
const E& e DECAF_UNUSED) {
196 "List::ListIterator::add - Const Iterator.");
199 virtual void set(
const E& e DECAF_UNUSED) {
203 "List::ListIterator::set - Const Iterator.");
206 virtual bool hasPrevious()
const {
207 return (this->current != this->list->begin());
210 virtual E previous() {
211 if (this->current == this->list->begin()) {
214 "List::ListIterator::previous - No Previous element.");
217 typename std::list<E>::const_iterator iter = this->current;
221 virtual int nextIndex()
const {
222 if (this->current == this->list->end()) {
223 return (
int) this->list->size();
226 return (
int) std::distance(this->list->begin(), this->current);
229 virtual int previousIndex()
const {
230 if (this->current == this->list->begin()) {
234 return (
int) std::distance(this->list->begin(), this->current) - 1;
271 if (listptr ==
NULL) {
275 return this->values == listptr->values;
284 if (listptr ==
NULL) {
289 this->values.clear();
290 this->values = listptr->values;
297 return new StlListIterator(&values, 0);
300 return new ConstStlListIterator(&values, 0);
307 return new StlListIterator(&values, 0);
310 return new ConstStlListIterator(&values, 0);
318 if (index < 0 || index > this->
size()) {
321 "List::listIterator - Index greater than size() or negative");
324 return new StlListIterator(&values, index);
328 if (index < 0 || index > this->
size()) {
331 "List::listIterator - Index greater than size() or negative");
334 return new ConstStlListIterator(&values, index);
348 return values.empty();
355 return (
int)values.size();
361 virtual E
get(
int index)
const {
363 if( index < 0 || index >= this->
size() ) {
366 "List::get - Index greater than size() or negative" );
370 typename std::list<E>::const_iterator iter = this->values.begin();
371 std::advance( iter, index );
378 virtual E
set(
int index,
const E& element) {
380 if (index < 0 || index >= this->
size()) {
383 "List::get - Index greater than size() or negative");
388 typename std::list<E>::iterator iter = this->values.begin();
389 std::advance(iter, index);
396 virtual void add(
int index,
const E& element) {
398 if (index < 0 || index > this->
size()) {
401 "List::add - Index greater than size()");
405 typename std::list<E>::iterator iter = this->values.begin();
406 std::advance(iter, index);
407 this->values.insert(iter, element);
410 virtual bool add(
const E& value) {
411 values.insert(values.end(), value);
421 std::vector<E> array = collection.
toArray();
422 typename std::vector<E>::const_iterator vecIter = array.begin();
424 std::auto_ptr<ListIterator<E> > iter(this->
listIterator((
int) this->values.size()));
426 while (vecIter != array.end()) {
427 iter->add(*(vecIter++));
435 if (index < 0 || index > this->
size()) {
438 "List::addAll - Index greater than size()");
445 std::vector<E> array = collection.
toArray();
446 typename std::vector<E>::const_iterator vecIter = array.begin();
448 std::auto_ptr<ListIterator<E> > iter(this->
listIterator(index));
450 while (vecIter != array.end()) {
451 iter->add(*(vecIter++));
458 int origSize = this->
size();
459 this->values.remove(value);
460 return origSize != this->
size();
465 if (index < 0 || index >= this->
size()) {
468 "List::removeAt - Index greater than size() or negative");
472 typename std::list<E>::iterator iter = this->values.begin();
473 std::advance(iter, index);
475 this->values.erase(iter);
482 typename std::list<E>::const_iterator iter;
483 iter = std::find(values.begin(), values.end(), value);
485 if (iter == values.end()) {
489 return (
int) std::distance(values.begin(), iter);
494 typename std::list<E>::const_reverse_iterator iter;
495 iter = std::find(values.rbegin(), values.rend(), value);
497 if (iter == values.rend()) {
502 return (
int) (this->
size() - std::distance(values.rbegin(), iter) - 1);
506 typename std::list<E>::const_iterator iter;
507 iter = std::find(values.begin(), values.end(), value);
508 return iter != values.end();
Definition IllegalStateException.h:32
Definition IndexOutOfBoundsException.h:31
Definition UnsupportedOperationException.h:32
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
virtual void copy(const Collection< E > &collection)
Renders this Collection as a Copy of the given Collection.
Definition AbstractCollection.h:198
This class provides a skeletal implementation of the List interface to minimize the effort required t...
Definition AbstractList.h:66
AbstractList()
Definition AbstractList.h:341
The root interface in the collection hierarchy.
Definition Collection.h:69
virtual std::vector< E > toArray() const =0
Returns an array containing all of the 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
An iterator for lists that allows the programmer to traverse the list in either direction,...
Definition ListIterator.h:38
Definition NoSuchElementException.h:31
virtual bool add(const E &value)
Returns true if this collection changed as a result of the call.
Definition StlList.h:410
virtual Iterator< E > * iterator()
an iterator over a set of elements of type T.
Definition StlList.h:296
virtual bool remove(const E &value)
Removes a single instance of the specified element from the collection.
Definition StlList.h:457
virtual ListIterator< E > * listIterator()
a list iterator over the elements in this list (in proper sequence).
Definition StlList.h:306
virtual bool equals(const Collection< E > &collection) const
Compares the passed collection to this one, if they contain the same elements, i.e....
Definition StlList.h:268
virtual void add(int index, const E &element)
Inserts the specified element at the specified position in this list.
Definition StlList.h:396
virtual ListIterator< E > * listIterator(int index)
a list iterator of the elements in this list (in proper sequence), starting at the specified position...
Definition StlList.h:316
virtual bool isEmpty() const
true if this collection contains no elements.
Definition StlList.h:347
virtual ListIterator< E > * listIterator() const
Definition StlList.h:309
virtual E removeAt(int index)
Removes the element at the specified position in this list.
Definition StlList.h:463
StlList()
Default constructor - does nothing.
Definition StlList.h:243
virtual void copy(const Collection< E > &collection)
Renders this Collection as a Copy of the given Collection.
Definition StlList.h:281
StlList(const StlList &source)
Copy constructor - copies the content of the given set into this one.
Definition StlList.h:250
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 StlList.h:480
virtual bool addAll(const Collection< E > &collection)
Adds all of the elements in the specified collection to this collection.
Definition StlList.h:415
virtual bool contains(const E &value) const
Returns true if this collection contains the specified element.
Definition StlList.h:505
virtual Iterator< E > * iterator() const
Definition StlList.h:299
virtual void clear()
Removes all of the elements from this collection (optional operation).This collection will be empty a...
Definition StlList.h:340
virtual ListIterator< E > * listIterator(int index) const
Definition StlList.h:326
virtual int size() const
Returns the number of elements in this collection.If this collection contains more than Integer::MAX_...
Definition StlList.h:354
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 StlList.h:492
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 StlList.h:433
virtual E get(int index) const
Gets the element contained at position passed.value at index specified.
Definition StlList.h:361
StlList(const Collection< E > &source)
Copy constructor - copies the content of the given set into this one.
Definition StlList.h:259
virtual E set(int index, const E &element)
Replaces the element at the specified position in this list with the specified element....
Definition StlList.h:378
virtual ~StlList()
Definition StlList.h:263
#define NULL
Definition Config.h:33
Definition AbstractCollection.h:33
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
Definition AprPool.h:25