Loading...
Searching...
No Matches
iterator.h
Go to the documentation of this file.
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
#ifndef BALL_KERNEL_ITERATOR_H
#define BALL_KERNEL_ITERATOR_H
#ifndef BALL_CONCEPT_BIDIRECTIONALITERATOR_H
# include <BALL/CONCEPT/bidirectionalIterator.h>
#endif
#ifndef BALL_CONCEPT_COMPOSITE_H
# include <BALL/CONCEPT/composite.h>
#endif
#define BALL_KERNEL_DEFINE_ITERATOR_CREATORS(Type) \
Type##Iterator \
begin##Type () \
{ \
return Type##Iterator::begin(*this); \
} \
\
Type##Iterator \
end##Type () \
{ \
return Type##Iterator::end(*this); \
} \
\
Type##ReverseIterator \
rbegin##Type () \
{ \
return Type##ReverseIterator(end##Type ()); \
} \
\
Type##ReverseIterator \
rend##Type () \
{ \
return Type##ReverseIterator(begin##Type ()); \
} \
\
Type##ConstIterator \
begin##Type () const \
{ \
return Type##ConstIterator::begin(*this); \
} \
\
Type##ConstIterator \
end##Type () const \
{ \
return Type##ConstIterator::end(*this); \
} \
\
Type##ConstReverseIterator \
rbegin##Type () const \
{ \
return Type##ConstReverseIterator(end##Type ()); \
} \
\
Type##ConstReverseIterator \
rend##Type () const \
{ \
return Type##ConstReverseIterator(begin##Type ()); \
}
namespace BALL
{
class BALL_EXPORT CompositeIteratorTraits
{
public:
inline CompositeIteratorTraits();
inline CompositeIteratorTraits(const Composite& composite);
inline CompositeIteratorTraits(const CompositeIteratorTraits& traits);
inline ~CompositeIteratorTraits() {}
inline CompositeIteratorTraits& operator = (const CompositeIteratorTraits& traits);
Composite* getContainer() { return bound_; }
inline const Composite* getContainer() const { return bound_; }
inline bool operator == (const CompositeIteratorTraits& traits) const;
inline bool operator != (const CompositeIteratorTraits& traits) const;
inline bool isValid() const { return ((bound_ != 0) && composite_iterator_.isValid()); }
inline bool isSingular() const { return (bound_ == 0); }
inline bool isBegin() const;
inline bool isEnd() const
{
return composite_iterator_.isEnd();
}
inline bool isRBegin() const;
inline bool isREnd() const;
inline Composite::CompositeIterator& getPosition() { return composite_iterator_; }
inline const Composite::CompositeIterator& getPosition() const { return composite_iterator_; }
inline void invalidate();
inline void toBegin();
inline void toEnd();
inline Composite& getData();
inline const Composite& getData() const;
inline void forward();
inline void backward();
inline void toRBegin();
inline void toREnd();
inline void setPredicate(const UnaryPredicate<Composite>& predicate) { predicate_ = &predicate; }
inline const UnaryPredicate<Composite>* getPredicate() const { return predicate_; }
protected:
Composite* bound_;
Composite::CompositeIterator composite_iterator_;
const UnaryPredicate<Composite>* predicate_;
};
inline CompositeIteratorTraits::CompositeIteratorTraits()
: bound_(0),
composite_iterator_(),
predicate_(0)
{
}
inline CompositeIteratorTraits::CompositeIteratorTraits(const Composite& composite)
: bound_(const_cast<Composite*>(&composite)),
composite_iterator_(const_cast<Composite&>(composite).beginComposite()),
predicate_(0)
{
}
inline CompositeIteratorTraits::CompositeIteratorTraits(const CompositeIteratorTraits& traits)
: bound_(traits.bound_),
composite_iterator_(traits.composite_iterator_),
predicate_(traits.predicate_)
{
}
inline CompositeIteratorTraits& CompositeIteratorTraits::operator = (const CompositeIteratorTraits& traits)
{
bound_ = traits.bound_;
composite_iterator_ = traits.composite_iterator_;
predicate_ = traits.predicate_;
return *this;
}
inline bool CompositeIteratorTraits::operator == (const CompositeIteratorTraits& traits) const
{
return ((composite_iterator_ == traits.composite_iterator_) && (bound_ == traits.bound_));
}
inline bool CompositeIteratorTraits::operator != (const CompositeIteratorTraits& traits) const
{
return !this->operator == (traits);
}
inline void CompositeIteratorTraits::invalidate()
{
bound_ = 0;
composite_iterator_.invalidate();
}
inline void CompositeIteratorTraits::toBegin()
{
BALL_PRECONDITION_EXCEPTION((bound_ != 0), "cannot move unbound iterator to begin")
composite_iterator_ = bound_->beginComposite();
while (+composite_iterator_ && (predicate_->operator () (*composite_iterator_) == false))
{
++composite_iterator_;
}
}
inline bool CompositeIteratorTraits::isBegin() const
{
if (isSingular())
{
return false;
}
try
{
Composite::CompositeIterator sub_iterator(bound_->beginComposite());
while (+sub_iterator && (predicate_->operator () (*sub_iterator) == false))
{
++sub_iterator;
}
return (composite_iterator_ == sub_iterator);
}
catch (Exception::Precondition&)
{
}
return false;
}
inline void CompositeIteratorTraits::toEnd()
{
composite_iterator_.toEnd();
}
inline Composite& CompositeIteratorTraits::getData()
{
return const_cast<Composite&>(*composite_iterator_);
}
inline const Composite& CompositeIteratorTraits::getData() const
{
return *composite_iterator_;
}
inline void CompositeIteratorTraits::forward()
{
++composite_iterator_;
while (+composite_iterator_ && (predicate_->operator () (*composite_iterator_) == false))
{
++composite_iterator_;
}
}
inline void CompositeIteratorTraits::toRBegin()
{
BALL_PRECONDITION_EXCEPTION(!isSingular(), "cannot move singular iterator to reverse begin")
composite_iterator_ = --bound_->endComposite();
while (+composite_iterator_ && (predicate_->operator () (*composite_iterator_) == false))
{
--composite_iterator_;
}
}
inline bool CompositeIteratorTraits::isRBegin() const
{
if (isSingular())
{
return false;
}
Composite::CompositeIterator sub_iterator = --bound_->endComposite();
while (+sub_iterator && (predicate_->operator () (*sub_iterator) == false))
{
--sub_iterator;
}
return (composite_iterator_ == sub_iterator);
}
inline void CompositeIteratorTraits::toREnd()
{
composite_iterator_.toREnd();
}
inline bool CompositeIteratorTraits::isREnd() const
{
if (isSingular())
{
return false;
}
return composite_iterator_.isREnd();
}
inline void CompositeIteratorTraits::backward()
{
--composite_iterator_;
while (+composite_iterator_ && (predicate_->operator () (*composite_iterator_) == false))
{
--composite_iterator_;
}
}
} // namespace BALL
#endif // BALL_KERNEL_ITERATOR_H
BALL_EXPORT
#define BALL_EXPORT
Definition COMMON/global.h:50
bidirectionalIterator.h
BALL::CompositeIteratorTraits
Definition iterator.h:84
BALL::CompositeIteratorTraits::isSingular
bool isSingular() const
Check whether the iterator is bound to a container.
Definition iterator.h:144
BALL::CompositeIteratorTraits::predicate_
const UnaryPredicate< Composite > * predicate_
The predicate.
Definition iterator.h:235
BALL::CompositeIteratorTraits::toBegin
void toBegin()
Definition iterator.h:283
BALL::CompositeIteratorTraits::invalidate
void invalidate()
Definition iterator.h:277
BALL::CompositeIteratorTraits::CompositeIteratorTraits
CompositeIteratorTraits()
Default constructor.
Definition iterator.h:238
BALL::CompositeIteratorTraits::getData
Composite & getData()
Return a reference to the current element.
Definition iterator.h:321
BALL::CompositeIteratorTraits::isValid
bool isValid() const
Definition iterator.h:141
BALL::CompositeIteratorTraits::forward
void forward()
Increment the iterator by one element.
Definition iterator.h:331
BALL::CompositeIteratorTraits::setPredicate
void setPredicate(const UnaryPredicate< Composite > &predicate)
Assign the current predicate associated with the iterator.
Definition iterator.h:220
BALL::CompositeIteratorTraits::getPosition
Composite::CompositeIterator & getPosition()
Return the current iterator position.
Definition iterator.h:174
BALL::CompositeIteratorTraits::bound_
Composite * bound_
The pointer to the container.
Definition iterator.h:229
BALL::CompositeIteratorTraits::isRBegin
bool isRBegin() const
Definition iterator.h:350
BALL::CompositeIteratorTraits::getContainer
Composite * getContainer()
Return a pointer to the container the iterator is bound to.
Definition iterator.h:117
BALL::CompositeIteratorTraits::operator=
CompositeIteratorTraits & operator=(const CompositeIteratorTraits &traits)
Assignment operator.
Definition iterator.h:259
BALL::CompositeIteratorTraits::composite_iterator_
Composite::CompositeIterator composite_iterator_
The internal iterator to iterate over the current node's children.
Definition iterator.h:232
BALL::CompositeIteratorTraits::backward
void backward()
Decrement the iterator one element.
Definition iterator.h:379
BALL::CompositeIteratorTraits::toRBegin
void toRBegin()
Definition iterator.h:340
BALL::CompositeIteratorTraits::operator!=
bool operator!=(const CompositeIteratorTraits &traits) const
Inequality operator.
Definition iterator.h:272
BALL::CompositeIteratorTraits::toREnd
void toREnd()
Definition iterator.h:365
BALL::CompositeIteratorTraits::~CompositeIteratorTraits
~CompositeIteratorTraits()
Destructor.
Definition iterator.h:101
BALL::CompositeIteratorTraits::isREnd
bool isREnd() const
Definition iterator.h:370
BALL::CompositeIteratorTraits::getPredicate
const UnaryPredicate< Composite > * getPredicate() const
Return the current predicate associated with the iterator.
Definition iterator.h:223
BALL::CompositeIteratorTraits::operator==
bool operator==(const CompositeIteratorTraits &traits) const
Equality operator.
Definition iterator.h:267
BALL::CompositeIteratorTraits::isEnd
bool isEnd() const
Definition iterator.h:155
BALL::CompositeIteratorTraits::getPosition
const Composite::CompositeIterator & getPosition() const
Return the current iterator posittion (const method).
Definition iterator.h:177
BALL::CompositeIteratorTraits::getContainer
const Composite * getContainer() const
Return a const pointer to the container the iterator is bound to.
Definition iterator.h:120
BALL::CompositeIteratorTraits::isBegin
bool isBegin() const
Definition iterator.h:293
BALL::CompositeIteratorTraits::toEnd
void toEnd()
Definition iterator.h:316
BALL::Composite
Definition composite.h:74
BALL::Composite::CompositeIterator
BidirectionalIterator< Composite, Composite, Composite *, CompositeIteratorTraits > CompositeIterator
Definition composite.h:1468
BALL::Exception::Precondition
Definition COMMON/exception.h:400
BALL::UnaryPredicate
Definition CONCEPT/predicate.h:30
composite.h
BALL_PRECONDITION_EXCEPTION
#define BALL_PRECONDITION_EXCEPTION(condition, message)
Definition debug.h:89
BALL
Definition constants.h:13