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() );
89 virtual void remove() {
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 ) {
106 if( this->current == list->end() ) {
107 this->list->insert( this->current, e );
109 *( this->current ) = e;
113 virtual bool hasPrevious()
const {
114 return ( this->current != this->list->begin() );
117 virtual E previous() {
118 if( this->current == this->list->begin() ) {
121 "List::ListIterator::previous - No Previous element." );
124 typename std::list<E>::const_iterator iter = this->current;
128 virtual int nextIndex()
const {
129 if( this->current == this->list->end() ) {
130 return (
int)this->list->size();
133 return (
int)std::distance( this->list->begin(), this->current );
136 virtual int previousIndex()
const {
137 if( this->current == this->list->begin() ) {
141 return (
int)std::distance( this->list->begin(), this->current ) - 1;
149 typename std::list<E>::const_iterator current;
150 typename std::list<E>::const_iterator prev;
151 const typename std::list<E>* list;
155 ConstStlListIterator(
const ConstStlListIterator& );
156 ConstStlListIterator
operator= (
const ConstStlListIterator& );
160 ConstStlListIterator(
const typename std::list<E>* list,
int index ) :
161 ListIterator<E>(), current( list->begin() ), prev( list->end() ), list( list ) {
163 if( index < (
int)list->size() ) {
164 std::advance( this->current, index );
166 this->current = list->end();
170 virtual ~ConstStlListIterator() {}
173 if( this->current == list->end() ) {
176 "List::Iterator::next - No more elements to return" );
179 this->prev = this->current;
180 return *( this->current++ );
183 virtual bool hasNext()
const {
184 return ( this->current != list->end() );
187 virtual void remove() {
191 "List::ListIterator::remove - Const Iterator." );
198 "List::ListIterator::add - Const Iterator." );
205 "List::ListIterator::set - Const Iterator." );
208 virtual bool hasPrevious()
const {
209 return ( this->current != this->list->begin() );
212 virtual E previous() {
213 if( this->current == this->list->begin() ) {
216 "List::ListIterator::previous - No Previous element." );
219 typename std::list<E>::const_iterator iter = this->current;
223 virtual int nextIndex()
const {
224 if( this->current == this->list->end() ) {
225 return (
int)this->list->size();
228 return (
int)std::distance( this->list->begin(), this->current );
231 virtual int previousIndex()
const {
232 if( this->current == this->list->begin() ) {
236 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++) );
438 if( index < 0 || index > this->
size() ) {
441 "List::addAll - Index greater than size()" );
448 std::vector<E> array = collection.
toArray();
449 typename std::vector<E>::const_iterator vecIter = array.begin();
451 std::auto_ptr< ListIterator<E> > iter( this->
listIterator( index ) );
453 while( vecIter != array.end() ) {
454 iter->add( *( vecIter++) );
463 virtual bool remove(
const E& value ) {
465 int origSize = this->
size();
466 this->values.remove( value );
467 return origSize != this->
size();
475 if( index < 0 || index >= this->
size() ) {
478 "List::removeAt - Index greater than size() or negative" );
482 typename std::list<E>::iterator iter = this->values.begin();
483 std::advance( iter, index );
485 this->values.erase( iter );
492 typename std::list<E>::const_iterator iter;
493 iter = std::find( values.begin(), values.end(), value );
495 if( iter == values.end() ) {
499 return (
int)std::distance( values.begin(), iter );
504 typename std::list<E>::const_reverse_iterator iter;
505 iter = std::find( values.rbegin(), values.rend(), value );
507 if( iter == values.rend() ) {
512 return (
int)( this->
size() - std::distance( values.rbegin(), iter ) - 1 );
516 typename std::list<E>::const_iterator iter;
517 iter = std::find( values.begin(), values.end(), value );
518 return iter != values.end();