activemq-cpp-3.9.5
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  if (this->current == list->end()) {
106  this->list->insert(this->current, e);
107  } else {
108  *(this->current) = e;
109  }
110  }
111 
112  virtual bool hasPrevious() const {
113  return (this->current != this->list->begin());
114  }
115 
116  virtual E previous() {
117  if (this->current == this->list->begin()) {
119  __FILE__, __LINE__,
120  "List::ListIterator::previous - No Previous element." );
121  }
122 
123  typename std::list<E>::const_iterator iter = this->current;
124  return *(iter--);
125  }
126 
127  virtual int nextIndex() const {
128  if (this->current == this->list->end()) {
129  return (int) this->list->size();
130  }
131 
132  return (int) std::distance(this->list->begin(), this->current);
133  }
134 
135  virtual int previousIndex() const {
136  if (this->current == this->list->begin()) {
137  return -1;
138  }
139 
140  return (int) std::distance(this->list->begin(), this->current) - 1;
141  }
142  };
143 
144  class ConstStlListIterator : public decaf::util::ListIterator<E> {
145  private:
146 
147  typename std::list<E>::const_iterator current;
148  typename std::list<E>::const_iterator prev;
149  const typename std::list<E>* list;
150 
151  private:
152 
153  ConstStlListIterator(const ConstStlListIterator&);
154  ConstStlListIterator operator= (const ConstStlListIterator&);
155 
156  public:
157 
158  ConstStlListIterator(const typename std::list<E>* list, int index) :
159  ListIterator<E>(), current(list->begin()), prev(list->end()), list( list) {
160 
161  if (index < (int) list->size()) {
162  std::advance(this->current, index);
163  } else {
164  this->current = list->end();
165  }
166  }
167 
168  virtual ~ConstStlListIterator() {}
169 
170  virtual E next() {
171  if (this->current == list->end()) {
173  __FILE__, __LINE__,
174  "List::Iterator::next - No more elements to return");
175  }
176 
177  this->prev = this->current;
178  return *(this->current++);
179  }
180 
181  virtual bool hasNext() const {
182  return (this->current != list->end());
183  }
184 
185  virtual void remove() {
186 
188  __FILE__, __LINE__,
189  "List::ListIterator::remove - Const Iterator.");
190  }
191 
192  virtual void add(const E& e DECAF_UNUSED) {
193 
195  __FILE__, __LINE__,
196  "List::ListIterator::add - Const Iterator.");
197  }
198 
199  virtual void set(const E& e DECAF_UNUSED) {
200 
202  __FILE__, __LINE__,
203  "List::ListIterator::set - Const Iterator.");
204  }
205 
206  virtual bool hasPrevious() const {
207  return (this->current != this->list->begin());
208  }
209 
210  virtual E previous() {
211  if (this->current == this->list->begin()) {
213  __FILE__, __LINE__,
214  "List::ListIterator::previous - No Previous element.");
215  }
216 
217  typename std::list<E>::const_iterator iter = this->current;
218  return *(iter--);
219  }
220 
221  virtual int nextIndex() const {
222  if (this->current == this->list->end()) {
223  return (int) this->list->size();
224  }
225 
226  return (int) std::distance(this->list->begin(), this->current);
227  }
228 
229  virtual int previousIndex() const {
230  if (this->current == this->list->begin()) {
231  return -1;
232  }
233 
234  return (int) std::distance(this->list->begin(), this->current) - 1;
235  }
236  };
237 
238  public:
239 
243  StlList() : AbstractList<E>(), values() {}
244 
250  StlList(const StlList& source) : AbstractList<E>(), values() {
251  copy(source);
252  }
253 
259  StlList(const Collection<E>& source) : AbstractList<E>(), values() {
260  AbstractList<E>::copy(source);
261  }
262 
263  virtual ~StlList() {}
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 
433  virtual bool addAll(int index, const Collection<E>& collection) {
434 
435  if (index < 0 || index > this->size()) {
437  __FILE__, __LINE__,
438  "List::addAll - Index greater than size()");
439  }
440 
441  if (collection.isEmpty()) {
442  return false;
443  }
444 
445  std::vector<E> array = collection.toArray();
446  typename std::vector<E>::const_iterator vecIter = array.begin();
447 
448  std::auto_ptr<ListIterator<E> > iter(this->listIterator(index));
449 
450  while (vecIter != array.end()) {
451  iter->add(*(vecIter++));
452  }
453 
454  return true;
455  }
456 
457  virtual bool remove(const E& value) {
458  int origSize = this->size();
459  this->values.remove(value);
460  return origSize != this->size();
461  }
462 
463  virtual E removeAt(int index) {
464 
465  if (index < 0 || index >= this->size()) {
467  __FILE__, __LINE__,
468  "List::removeAt - Index greater than size() or negative");
469  }
470 
471  // Advance from begin and insert the value at that location
472  typename std::list<E>::iterator iter = this->values.begin();
473  std::advance(iter, index);
474  E oldValue = *iter;
475  this->values.erase(iter);
476 
477  return oldValue;
478  }
479 
480  virtual int indexOf(const E& value) const {
481 
482  typename std::list<E>::const_iterator iter;
483  iter = std::find(values.begin(), values.end(), value);
484 
485  if (iter == values.end()) {
486  return -1;
487  }
488 
489  return (int) std::distance(values.begin(), iter);
490  }
491 
492  virtual int lastIndexOf(const E& value) const {
493 
494  typename std::list<E>::const_reverse_iterator iter;
495  iter = std::find(values.rbegin(), values.rend(), value);
496 
497  if (iter == values.rend()) {
498  return -1;
499  }
500 
501  // Now reverse the result to get the last index
502  return (int) (this->size() - std::distance(values.rbegin(), iter) - 1);
503  }
504 
505  virtual bool contains(const E& value) const {
506  typename std::list<E>::const_iterator iter;
507  iter = std::find(values.begin(), values.end(), value);
508  return iter != values.end();
509  }
510 
511  };
512 
513 }}
514 
515 #endif /*_DECAF_UTIL_STLLIST_H_*/
StlList()
Default constructor - does nothing.
Definition: StlList.h:243
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
virtual ~StlList()
Definition: StlList.h:263
#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:505
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:92
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.
Definition: StlList.h:463
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
StlList(const StlList &source)
Copy constructor - copies the content of the given set into this one.
Definition: StlList.h:250
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:480
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:198
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 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:259
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