activemq-cpp-3.8.2
StlList.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef _DECAF_UTIL_STLLIST_H_
19 #define _DECAF_UTIL_STLLIST_H_
20 
21 #include <list>
22 #include <algorithm>
23 #include <memory>
27 #include <decaf/util/Config.h>
28 #include <decaf/util/Iterator.h>
30 #include <decaf/util/List.h>
32 
33 namespace decaf{
34 namespace util{
35 
40  template <typename E>
41  class StlList : public decaf::util::AbstractList<E> {
42  private:
43 
44  std::list<E> values;
45 
46  private:
47 
48  class StlListIterator : public ListIterator<E> {
49  private:
50 
51  typename std::list<E>::iterator current;
52  typename std::list<E>::iterator prev;
53  typename std::list<E>* list;
54 
55  private:
56 
57  StlListIterator( const StlListIterator& );
58  StlListIterator operator= ( const StlListIterator& );
59 
60  public:
61 
62  StlListIterator( typename std::list<E>* list, int index ) :
63  current( list->begin() ), prev( list->end() ), list( list ) {
64 
65  if( index < (int)list->size() ) {
66  std::advance( this->current, index );
67  } else {
68  this->current = list->end();
69  }
70  }
71 
72  virtual ~StlListIterator() {}
73 
74  virtual E next() {
75  if( this->current == list->end() ) {
77  __FILE__, __LINE__,
78  "List::Iterator::next - No more elements to return" );
79  }
80 
81  this->prev = this->current;
82  return *( this->current++ );
83  }
84 
85  virtual bool hasNext() const {
86  return ( this->current != list->end() );
87  }
88 
89  virtual void remove() {
90  if( this->prev == list->end() ) {
92  __FILE__, __LINE__,
93  "List::Iterator::remove - Invalid State to call remove" );
94  }
95 
96  this->list->erase( this->prev );
97  this->prev = this->list->end();
98  }
99 
100  virtual void add( const E& e ) {
101  this->list->insert( this->current, e );
102  }
103 
104  virtual void set( const E& e ) {
105 
106  if( this->current == list->end() ) {
107  this->list->insert( this->current, e );
108  } else {
109  *( this->current ) = e;
110  }
111  }
112 
113  virtual bool hasPrevious() const {
114  return ( this->current != this->list->begin() );
115  }
116 
117  virtual E previous() {
118  if( this->current == this->list->begin() ) {
120  __FILE__, __LINE__,
121  "List::ListIterator::previous - No Previous element." );
122  }
123 
124  typename std::list<E>::const_iterator iter = this->current;
125  return *( iter-- );
126  }
127 
128  virtual int nextIndex() const {
129  if( this->current == this->list->end() ) {
130  return (int)this->list->size();
131  }
132 
133  return (int)std::distance( this->list->begin(), this->current );
134  }
135 
136  virtual int previousIndex() const {
137  if( this->current == this->list->begin() ) {
138  return -1;
139  }
140 
141  return (int)std::distance( this->list->begin(), this->current ) - 1;
142  }
143 
144  };
145 
146  class ConstStlListIterator : public decaf::util::ListIterator<E> {
147  private:
148 
149  typename std::list<E>::const_iterator current;
150  typename std::list<E>::const_iterator prev;
151  const typename std::list<E>* list;
152 
153  private:
154 
155  ConstStlListIterator( const ConstStlListIterator& );
156  ConstStlListIterator operator= ( const ConstStlListIterator& );
157 
158  public:
159 
160  ConstStlListIterator( const typename std::list<E>* list, int index ) :
161  ListIterator<E>(), current( list->begin() ), prev( list->end() ), list( list ) {
162 
163  if( index < (int)list->size() ) {
164  std::advance( this->current, index );
165  } else {
166  this->current = list->end();
167  }
168  }
169 
170  virtual ~ConstStlListIterator() {}
171 
172  virtual E next() {
173  if( this->current == list->end() ) {
175  __FILE__, __LINE__,
176  "List::Iterator::next - No more elements to return" );
177  }
178 
179  this->prev = this->current;
180  return *( this->current++ );
181  }
182 
183  virtual bool hasNext() const {
184  return ( this->current != list->end() );
185  }
186 
187  virtual void remove() {
188 
190  __FILE__, __LINE__,
191  "List::ListIterator::remove - Const Iterator." );
192  }
193 
194  virtual void add( const E& e DECAF_UNUSED ) {
195 
197  __FILE__, __LINE__,
198  "List::ListIterator::add - Const Iterator." );
199  }
200 
201  virtual void set( const E& e DECAF_UNUSED ) {
202 
204  __FILE__, __LINE__,
205  "List::ListIterator::set - Const Iterator." );
206  }
207 
208  virtual bool hasPrevious() const {
209  return ( this->current != this->list->begin() );
210  }
211 
212  virtual E previous() {
213  if( this->current == this->list->begin() ) {
215  __FILE__, __LINE__,
216  "List::ListIterator::previous - No Previous element." );
217  }
218 
219  typename std::list<E>::const_iterator iter = this->current;
220  return *( iter-- );
221  }
222 
223  virtual int nextIndex() const {
224  if( this->current == this->list->end() ) {
225  return (int)this->list->size();
226  }
227 
228  return (int)std::distance( this->list->begin(), this->current );
229  }
230 
231  virtual int previousIndex() const {
232  if( this->current == this->list->begin() ) {
233  return -1;
234  }
235 
236  return (int)std::distance( this->list->begin(), this->current ) - 1;
237  }
238  };
239 
240  public:
241 
245  StlList() : AbstractList<E>(), values() {}
246 
252  StlList( const StlList& source ) : AbstractList<E>(), values() {
253  copy( source );
254  }
255 
261  StlList( const Collection<E>& source ) : AbstractList<E>(), values() {
262  AbstractList<E>::copy( source );
263  }
264 
268  virtual bool equals( const Collection<E>& collection ) const {
269 
270  const StlList<E>* listptr = dynamic_cast<const StlList<E>*>( &collection );
271  if( listptr == NULL ) {
272  return AbstractList<E>::equals( collection );
273  }
274 
275  return this->values == listptr->values;
276  }
277 
281  virtual void copy( const Collection<E>& collection ) {
282 
283  const StlList<E>* listptr = dynamic_cast<const StlList<E>*>( &collection );
284  if( listptr == NULL ) {
285  AbstractList<E>::copy( collection );
286  return;
287  }
288 
289  this->values.clear();
290  this->values = listptr->values;
291  }
292 
296  virtual Iterator<E>* iterator() {
297  return new StlListIterator( &values, 0 );
298  }
299  virtual Iterator<E>* iterator() const {
300  return new ConstStlListIterator( &values, 0 );
301  }
302 
307  return new StlListIterator( &values, 0 );
308  }
309  virtual ListIterator<E>* listIterator() const {
310  return new ConstStlListIterator( &values, 0 );
311  }
312 
316  virtual ListIterator<E>* listIterator( int index ) {
317 
318  if( index < 0 || index > this->size() ) {
320  __FILE__, __LINE__,
321  "List::listIterator - Index greater than size() or negative" );
322  }
323 
324  return new StlListIterator( &values, index );
325  }
326  virtual ListIterator<E>* listIterator( int index ) const {
327 
328  if( index < 0 || index > this->size() ) {
330  __FILE__, __LINE__,
331  "List::listIterator - Index greater than size() or negative" );
332  }
333 
334  return new ConstStlListIterator( &values, index );
335  }
336 
340  virtual void clear() {
341  values.clear();
342  }
343 
347  virtual bool isEmpty() const {
348  return values.empty();
349  }
350 
354  virtual int size() const {
355  return (int)values.size();
356  }
357 
361  virtual E get( int index ) const {
362 
363  if( index < 0 || index >= this->size() ) {
365  __FILE__, __LINE__,
366  "List::get - Index greater than size() or negative" );
367  }
368 
369  // Advance from begin and return the value at that location.
370  typename std::list<E>::const_iterator iter = this->values.begin();
371  std::advance( iter, index );
372  return *( iter );
373  }
374 
378  virtual E set( int index, const E& element ) {
379 
380  if( index < 0 || index >= this->size() ) {
382  __FILE__, __LINE__,
383  "List::get - Index greater than size() or negative" );
384  }
385 
386  // Advance from begin and return the value at that location
387  // after setting the value to the new value.
388  typename std::list<E>::iterator iter = this->values.begin();
389  std::advance( iter, index );
390  E oldValue = *iter;
391  *iter = element;
392 
393  return oldValue;
394  }
395 
396  virtual void add( int index, const E& element ) {
397 
398  if( index < 0 || index > this->size() ) {
400  __FILE__, __LINE__,
401  "List::add - Index greater than size()" );
402  }
403 
404  // Advance from begin and insert the value at that location
405  typename std::list<E>::iterator iter = this->values.begin();
406  std::advance( iter, index );
407  this->values.insert( iter, element );
408  }
409 
410  virtual bool add( const E& value ) {
411  values.insert( values.end(), value );
412  return true;
413  }
414 
415  virtual bool addAll( const Collection<E>& collection ) {
416 
417  if( collection.isEmpty() ) {
418  return false;
419  }
420 
421  std::vector<E> array = collection.toArray();
422  typename std::vector<E>::const_iterator vecIter = array.begin();
423 
424  std::auto_ptr< ListIterator<E> > iter( this->listIterator( (int)this->values.size() ) );
425 
426  while( vecIter != array.end() ) {
427  iter->add( *( vecIter++) );
428  }
429 
430  return true;
431  }
432 
436  virtual bool addAll( int index, const Collection<E>& collection ) {
437 
438  if( index < 0 || index > this->size() ) {
440  __FILE__, __LINE__,
441  "List::addAll - Index greater than size()" );
442  }
443 
444  if( collection.isEmpty() ) {
445  return false;
446  }
447 
448  std::vector<E> array = collection.toArray();
449  typename std::vector<E>::const_iterator vecIter = array.begin();
450 
451  std::auto_ptr< ListIterator<E> > iter( this->listIterator( index ) );
452 
453  while( vecIter != array.end() ) {
454  iter->add( *( vecIter++) );
455  }
456 
457  return true;
458  }
459 
463  virtual bool remove( const E& value ) {
464 
465  int origSize = this->size();
466  this->values.remove( value );
467  return origSize != this->size();
468  }
469 
473  virtual E removeAt( int index ) {
474 
475  if( index < 0 || index >= this->size() ) {
477  __FILE__, __LINE__,
478  "List::removeAt - Index greater than size() or negative" );
479  }
480 
481  // Advance from begin and insert the value at that location
482  typename std::list<E>::iterator iter = this->values.begin();
483  std::advance( iter, index );
484  E oldValue = *iter;
485  this->values.erase( iter );
486 
487  return oldValue;
488  }
489 
490  virtual int indexOf( const E& value ) const {
491 
492  typename std::list<E>::const_iterator iter;
493  iter = std::find( values.begin(), values.end(), value );
494 
495  if( iter == values.end() ) {
496  return -1;
497  }
498 
499  return (int)std::distance( values.begin(), iter );
500  }
501 
502  virtual int lastIndexOf( const E& value ) const {
503 
504  typename std::list<E>::const_reverse_iterator iter;
505  iter = std::find( values.rbegin(), values.rend(), value );
506 
507  if( iter == values.rend() ) {
508  return -1;
509  }
510 
511  // Now reverse the result to get the last index
512  return (int)( this->size() - std::distance( values.rbegin(), iter ) - 1 );
513  }
514 
515  virtual bool contains( const E& value ) const {
516  typename std::list<E>::const_iterator iter;
517  iter = std::find( values.begin(), values.end(), value );
518  return iter != values.end();
519  }
520 
521  };
522 
523 }}
524 
525 #endif /*_DECAF_UTIL_STLLIST_H_*/
StlList()
Default constructor - does nothing.
Definition: StlList.h:245
The root interface in the collection hierarchy.
Definition: Collection.h:68
List class that wraps the STL list object to provide a simpler interface and additional methods not p...
Definition: StlList.h:41
#define NULL
Definition: Config.h:33
virtual bool isEmpty() const =0
virtual void clear()
Removes all of the elements from this collection (optional operation).The collection will be empty af...
Definition: StlList.h:340
virtual int size() const
Returns the number of elements in this collection.If this collection contains more than Integer::MAX_...
Definition: StlList.h:354
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 bool addAll(const Collection< E > &collection)
Adds all of the elements in the specified collection to this collection.The behavior of this operatio...
Definition: StlList.h:415
virtual void copy(const Collection< E > &collection)
Renders this Collection as a Copy of the given Collection.The default implementation iterates over th...
Definition: StlList.h:281
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: StlList.h:268
virtual Iterator< E > * iterator()
an iterator over a set of elements of type T.
Definition: StlList.h:296
Defines an object that can be used to iterate over the elements of a collection.
Definition: Iterator.h:34
Definition: IndexOutOfBoundsException.h:31
Definition: UnsupportedOperationException.h:32
virtual bool contains(const E &value) const
Returns true if this collection contains the specified element.More formally, returns true if and onl...
Definition: StlList.h:515
virtual ListIterator< E > * listIterator(int index)
index of first element to be returned from the list iterator (by a call to the next method)...
Definition: StlList.h:316
AbstractCollection< E > & operator=(const AbstractCollection< E > &collection)
Assignment Operator, copy element from the source collection to this collection after clearing any el...
Definition: AbstractCollection.h:76
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.
Definition: StlList.h:378
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() const
Definition: StlList.h:309
Definition: IllegalStateException.h:32
This class provides a skeletal implementation of the List interface to minimize the effort required t...
Definition: AbstractList.h:65
virtual Iterator< E > * iterator() const
Definition: StlList.h:299
virtual E removeAt(int index)
Removes the element at the specified position in this list.Shifts any subsequent elements to the left...
Definition: StlList.h:473
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:502
StlList(const StlList &source)
Copy constructor - copies the content of the given set into this one.
Definition: StlList.h:252
virtual ListIterator< E > * listIterator(int index) const
Definition: StlList.h:326
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:490
virtual bool add(const E &value)
Returns true if this collection changed as a result of the call.
Definition: StlList.h:410
#define DECAF_UNUSED
Definition: Config.h:160
Definition: NoSuchElementException.h:31
virtual ListIterator< E > * listIterator()
a list iterator over the elements in this list (in proper sequence).
Definition: StlList.h:306
virtual void copy(const Collection< E > &collection)
Renders this Collection as a Copy of the given Collection.
Definition: AbstractCollection.h:184
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:436
virtual std::vector< E > toArray() const =0
Returns an array containing all of the elements in this collection.
virtual bool isEmpty() const
Returns true if this collection contains no elements.This implementation returns size() == 0...
Definition: StlList.h:347
StlList(const Collection< E > &source)
Copy constructor - copies the content of the given set into this one.
Definition: StlList.h:261
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: AprPool.h:25
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