BALL  1.4.79
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
iterator.h
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 2; -*-
2 // vi: set ts=2:
3 //
4 
5 #ifndef BALL_KERNEL_ITERATOR_H
6 #define BALL_KERNEL_ITERATOR_H
7 
8 #ifndef BALL_CONCEPT_BIDIRECTIONALITERATOR_H
10 #endif
11 
12 #ifndef BALL_CONCEPT_COMPOSITE_H
13 # include <BALL/CONCEPT/composite.h>
14 #endif
15 
16 
25 #define BALL_KERNEL_DEFINE_ITERATOR_CREATORS(Type) \
26 Type##Iterator \
27 begin##Type () \
28 { \
29  return Type##Iterator::begin(*this); \
30 } \
31  \
32 Type##Iterator \
33 end##Type () \
34 { \
35  return Type##Iterator::end(*this); \
36 } \
37  \
38 Type##ReverseIterator \
39 rbegin##Type () \
40 { \
41  return Type##ReverseIterator(end##Type ()); \
42 } \
43  \
44 Type##ReverseIterator \
45 rend##Type () \
46 { \
47  return Type##ReverseIterator(begin##Type ()); \
48 } \
49  \
50 Type##ConstIterator \
51 begin##Type () const \
52 { \
53  return Type##ConstIterator::begin(*this); \
54 } \
55  \
56 Type##ConstIterator \
57 end##Type () const \
58 { \
59  return Type##ConstIterator::end(*this); \
60 } \
61  \
62 Type##ConstReverseIterator \
63 rbegin##Type () const \
64 { \
65  return Type##ConstReverseIterator(end##Type ()); \
66 } \
67  \
68 Type##ConstReverseIterator \
69 rend##Type () const \
70 { \
71  return Type##ConstReverseIterator(begin##Type ()); \
72 }
73 
74 namespace BALL
75 {
84  {
85  public:
86 
90 
92  inline CompositeIteratorTraits();
93 
95  inline CompositeIteratorTraits(const Composite& composite);
96 
99 
102 
104 
107 
109  inline CompositeIteratorTraits& operator = (const CompositeIteratorTraits& traits);
111 
115 
117  Composite* getContainer() { return bound_; }
118 
120  inline const Composite* getContainer() const { return bound_; }
122 
126 
128  inline bool operator == (const CompositeIteratorTraits& traits) const;
129 
131  inline bool operator != (const CompositeIteratorTraits& traits) const;
133 
141  inline bool isValid() const { return ((bound_ != 0) && composite_iterator_.isValid()); }
142 
144  inline bool isSingular() const { return (bound_ == 0); }
145 
149  inline bool isBegin() const;
150 
155  inline bool isEnd() const
156  {
157  return composite_iterator_.isEnd();
158  }
159 
164  inline bool isRBegin() const;
165 
170  inline bool isREnd() const;
172 
174  inline Composite::CompositeIterator& getPosition() { return composite_iterator_; }
175 
177  inline const Composite::CompositeIterator& getPosition() const { return composite_iterator_; }
178 
183  inline void invalidate();
184 
189  inline void toBegin();
190 
195  inline void toEnd();
196 
198  inline Composite& getData();
199 
201  inline const Composite& getData() const;
202 
204  inline void forward();
205 
207  inline void backward();
208 
212  inline void toRBegin();
213 
217  inline void toREnd();
218 
220  inline void setPredicate(const UnaryPredicate<Composite>& predicate) { predicate_ = &predicate; }
221 
223  inline const UnaryPredicate<Composite>* getPredicate() const { return predicate_; }
224 
225 
226  protected:
227 
230 
233 
236  };
237 
239  : bound_(0),
240  composite_iterator_(),
241  predicate_(0)
242  {
243  }
244 
246  : bound_(const_cast<Composite*>(&composite)),
247  composite_iterator_(const_cast<Composite&>(composite).beginComposite()),
248  predicate_(0)
249  {
250  }
251 
253  : bound_(traits.bound_),
254  composite_iterator_(traits.composite_iterator_),
255  predicate_(traits.predicate_)
256  {
257  }
258 
260  {
261  bound_ = traits.bound_;
263  predicate_ = traits.predicate_;
264  return *this;
265  }
266 
268  {
269  return ((composite_iterator_ == traits.composite_iterator_) && (bound_ == traits.bound_));
270  }
271 
273  {
274  return !this->operator == (traits);
275  }
276 
278  {
279  bound_ = 0;
281  }
282 
284  {
285  BALL_PRECONDITION_EXCEPTION((bound_ != 0), "cannot move unbound iterator to begin")
287  while (+composite_iterator_ && (predicate_->operator () (*composite_iterator_) == false))
288  {
290  }
291  }
292 
294  {
295  if (isSingular())
296  {
297  return false;
298  }
299 
300  try
301  {
303  while (+sub_iterator && (predicate_->operator () (*sub_iterator) == false))
304  {
305  ++sub_iterator;
306  }
307  return (composite_iterator_ == sub_iterator);
308  }
309  catch (Exception::Precondition&)
310  {
311  }
312 
313  return false;
314  }
315 
317  {
319  }
320 
322  {
323  return const_cast<Composite&>(*composite_iterator_);
324  }
325 
327  {
328  return *composite_iterator_;
329  }
330 
332  {
334  while (+composite_iterator_ && (predicate_->operator () (*composite_iterator_) == false))
335  {
337  }
338  }
339 
341  {
342  BALL_PRECONDITION_EXCEPTION(!isSingular(), "cannot move singular iterator to reverse begin")
344  while (+composite_iterator_ && (predicate_->operator () (*composite_iterator_) == false))
345  {
347  }
348  }
349 
351  {
352  if (isSingular())
353  {
354  return false;
355  }
357 
358  while (+sub_iterator && (predicate_->operator () (*sub_iterator) == false))
359  {
360  --sub_iterator;
361  }
362  return (composite_iterator_ == sub_iterator);
363  }
364 
366  {
368  }
369 
371  {
372  if (isSingular())
373  {
374  return false;
375  }
376  return composite_iterator_.isREnd();
377  }
378 
380  {
382  while (+composite_iterator_ && (predicate_->operator () (*composite_iterator_) == false))
383  {
385  }
386  }
387 } // namespace BALL
388 
389 #endif // BALL_KERNEL_ITERATOR_H
BALL_INLINE void invalidate()
Invalidate the iterator.
Definition: baseIterator.h:124
CompositeIteratorTraits()
Default constructor.
Definition: iterator.h:238
const Composite::CompositeIterator & getPosition() const
Return the current iterator posittion (const method)
Definition: iterator.h:177
bool operator!=(const CompositeIteratorTraits &traits) const
Inequality operator.
Definition: iterator.h:272
#define BALL_PRECONDITION_EXCEPTION(condition, message)
Definition: debug.h:89
void forward()
Increment the iterator by one element.
Definition: iterator.h:331
Composite::CompositeIterator composite_iterator_
The internal iterator to iterate over the current node's children.
Definition: iterator.h:232
const Composite * getContainer() const
Return a const pointer to the container the iterator is bound to.
Definition: iterator.h:120
const UnaryPredicate< Composite > * getPredicate() const
Return the current predicate associated with the iterator.
Definition: iterator.h:223
CompositeIterator beginComposite()
Definition: composite.h:1470
BALL_EXPORT bool operator!=(const String &s1, const String &s2)
Composite * bound_
The pointer to the container.
Definition: iterator.h:229
Composite * getContainer()
Return a pointer to the container the iterator is bound to.
Definition: iterator.h:117
const UnaryPredicate< Composite > * predicate_
The predicate.
Definition: iterator.h:235
CompositeIteratorTraits & operator=(const CompositeIteratorTraits &traits)
Assignment operator.
Definition: iterator.h:259
BALL_EXPORT bool operator==(const String &s1, const String &s2)
void setPredicate(const UnaryPredicate< Composite > &predicate)
Assign the current predicate associated with the iterator.
Definition: iterator.h:220
Composite::CompositeIterator & getPosition()
Return the current iterator position.
Definition: iterator.h:174
bool isSingular() const
Check whether the iterator is bound to a container.
Definition: iterator.h:144
~CompositeIteratorTraits()
Destructor.
Definition: iterator.h:101
CompositeIterator endComposite()
Definition: composite.h:1472
bool operator==(const CompositeIteratorTraits &traits) const
Equality operator.
Definition: iterator.h:267
void backward()
Decrement the iterator one element.
Definition: iterator.h:379
BALL_INLINE bool isREnd() const
Test wheter the iterator points to the position before the first element.
Composite & getData()
Return a reference to the current element.
Definition: iterator.h:321
#define BALL_EXPORT
Definition: COMMON/global.h:50