Loading...
Searching...
No Matches
randomAccessIterator.h
Go to the documentation of this file.
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
#ifndef BALL_CONCEPT_RANDOMACCESSITERATOR_H
#define BALL_CONCEPT_RANDOMACCESSITERATOR_H
#ifndef BALL_COMMON_H
# include <BALL/common.h>
#endif
#ifndef BALL_CONCEPT_BIDIRECTIONALITERATOR_H
# include <BALL/CONCEPT/bidirectionalIterator.h>
#endif
namespace BALL
{
template <typename Container, typename DataType, typename Position, typename Traits>
class ConstRandomAccessIterator
: public ConstBidirectionalIterator<Container, DataType, Position, Traits>
{
public:
typedef std::random_access_iterator_tag iterator_category;
// convenience typedef
typedef ConstBidirectionalIterator<Container, DataType, Position, Traits> Base;
BALL_INLINE ConstRandomAccessIterator() {}
BALL_INLINE ConstRandomAccessIterator(const ConstRandomAccessIterator& iterator)
: Base(iterator)
{
}
BALL_INLINE ~ConstRandomAccessIterator() {}
ConstRandomAccessIterator& operator += (Distance distance);
ConstRandomAccessIterator& operator -= (Distance distance);
ConstRandomAccessIterator operator + (Distance distance) const;
ConstRandomAccessIterator operator - (Distance distance) const;
Distance operator - (const ConstRandomAccessIterator& iterator) const;
static ConstRandomAccessIterator begin(const Container& container);
static ConstRandomAccessIterator end(const Container& container);
static ConstRandomAccessIterator rbegin(const Container& container);
static ConstRandomAccessIterator rend(const Container& container);
BALL_INLINE bool operator + () const { return Base::getTraits().isValid(); }
BALL_INLINE bool operator - () const { return !Base::getTraits().isValid(); }
bool operator < (const ConstRandomAccessIterator& iterator) const;
bool operator <= (const ConstRandomAccessIterator& iterator) const;
bool operator >= (const ConstRandomAccessIterator& iterator) const;
bool operator > (const ConstRandomAccessIterator& iterator) const;
const DataType& operator [] (Index index) const;
protected:
ConstRandomAccessIterator(const Container& container)
: Base(container)
{
}
};
template <typename Container, typename DataType, typename Position, typename Traits>
ConstRandomAccessIterator<Container, DataType, Position, Traits> operator +
(Distance distance, const ConstRandomAccessIterator<Container, DataType, Position, Traits>& iterator)
{
ConstRandomAccessIterator<Container, DataType, Position, Traits> tmp_iterator(iterator);
return (tmp_iterator += distance);
}
template <typename Container, typename DataType, typename Position, typename Traits>
Distance ConstRandomAccessIterator<Container, DataType, Position, Traits>::operator -
(const ConstRandomAccessIterator<Container, DataType, Position, Traits>& b) const
{
if (!Base::getTraits().isValid())
{
throw Exception::InvalidIterator(__FILE__, __LINE__);
}
if (!b.getTraits().isValid())
{
throw Exception::InvalidIterator(__FILE__, __LINE__);
}
if (Base::getTraits().getContainer() != b.getTraits().getContainer())
{
throw Exception::IncompatibleIterators(__FILE__, __LINE__);
}
return Base::getTraits().getDistance(b.getTraits());
}
template <typename Container, typename DataType, typename Position, typename Traits>
ConstRandomAccessIterator<Container, DataType, Position, Traits>&
ConstRandomAccessIterator<Container, DataType, Position, Traits>::operator += (Distance distance)
{
if (!Base::getTraits().isValid())
{
throw Exception::InvalidIterator(__FILE__, __LINE__);
}
if (distance < (Distance)0)
{
return (*this -= -distance);
}
Base::getTraits().forward(distance);
return *this;
}
template <typename Container, typename DataType, typename Position, typename Traits>
ConstRandomAccessIterator<Container, DataType, Position, Traits>&
ConstRandomAccessIterator<Container, DataType, Position, Traits>::operator -= (Distance distance)
{
if (Base::getTraits().isSingular())
{
throw Exception::InvalidIterator(__FILE__, __LINE__);
}
if (distance < (Distance)0)
{
return (*this += -distance);
}
if (Base::getTraits().isEnd() == true)
{
Base::getTraits().toRBegin();
Base::getTraits().backward(distance - 1);
}
else
{
Base::getTraits().backward(distance);
}
return *this;
}
template <typename Container, typename DataType, typename Position, typename Traits>
ConstRandomAccessIterator<Container, DataType, Position, Traits>
ConstRandomAccessIterator<Container, DataType, Position, Traits>::operator + (Distance distance) const
{
ConstRandomAccessIterator iterator(*this);
return (iterator += distance);
}
template <typename Container, typename DataType, typename Position, typename Traits>
ConstRandomAccessIterator<Container, DataType, Position, Traits>
ConstRandomAccessIterator<Container, DataType, Position, Traits>::operator - (Distance distance) const
{
ConstRandomAccessIterator iterator(*this);
return (iterator -= distance);
}
template <typename Container, typename DataType, typename Position, typename Traits>
bool ConstRandomAccessIterator<Container, DataType, Position, Traits>::operator <
(const ConstRandomAccessIterator& iterator) const
{
if (!Base::getTraits().isValid())
{
throw Exception::InvalidIterator(__FILE__, __LINE__);
}
if (!iterator.isValid())
{
throw Exception::InvalidIterator(__FILE__, __LINE__);
}
if (Base::getTraits().getContainer() != iterator.getContainer())
{
throw Exception::IncompatibleIterators(__FILE__, __LINE__);
}
return (Base::getTraits().operator < (iterator.getTraits()));
}
template <typename Container, typename DataType, typename Position, typename Traits>
bool ConstRandomAccessIterator<Container, DataType, Position, Traits>::operator <=
(const ConstRandomAccessIterator& iterator) const
{
if (!Base::getTraits().isValid())
{
throw Exception::InvalidIterator(__FILE__, __LINE__);
}
if (!iterator.isValid())
{
throw Exception::InvalidIterator(__FILE__, __LINE__);
}
if (Base::getTraits().getContainer() != iterator.getContainer())
{
throw Exception::IncompatibleIterators(__FILE__, __LINE__);
}
return !(Base::getTraits().operator > (iterator.getTraits()));
}
template <typename Container, typename DataType, typename Position, typename Traits>
bool ConstRandomAccessIterator<Container, DataType, Position, Traits>::operator >=
(const ConstRandomAccessIterator& iterator) const
{
if (!Base::getTraits().isValid())
{
throw Exception::InvalidIterator(__FILE__, __LINE__);
}
if (!iterator.isValid())
{
throw Exception::InvalidIterator(__FILE__, __LINE__);
}
if (Base::getTraits().getContainer() != iterator.getContainer())
{
throw Exception::IncompatibleIterators(__FILE__, __LINE__);
}
return !(Base::getTraits().operator < (iterator.getTraits()));
}
template <typename Container, typename DataType, typename Position, typename Traits>
bool ConstRandomAccessIterator<Container, DataType, Position, Traits>::operator >
(const ConstRandomAccessIterator& iterator) const
{
if (!Base::getTraits().isValid())
{
throw Exception::InvalidIterator(__FILE__, __LINE__);
}
if (!iterator.isValid())
{
throw Exception::InvalidIterator(__FILE__, __LINE__);
}
if (Base::getTraits().getContainer() != iterator.getContainer())
{
throw Exception::IncompatibleIterators(__FILE__, __LINE__);
}
return (Base::getTraits().operator > (iterator.getTraits()));
}
template <typename Container, typename DataType, typename Position, typename Traits>
const DataType& ConstRandomAccessIterator<Container, DataType, Position, Traits>::operator [] (Index index) const
{
if (!Base::getTraits().isValid())
{
throw Exception::InvalidIterator(__FILE__, __LINE__);
}
return Base::getTraits().getData(index);
}
template <typename Container, typename DataType, typename Position, typename Traits>
ConstRandomAccessIterator<Container, DataType, Position, Traits>
ConstRandomAccessIterator<Container, DataType, Position, Traits>::begin(const Container& container)
{
ConstRandomAccessIterator iterator(container);
iterator.toBegin();
return iterator;
}
template <typename Container, typename DataType, typename Position, typename Traits>
ConstRandomAccessIterator<Container, DataType, Position, Traits>
ConstRandomAccessIterator<Container, DataType, Position, Traits>::end(const Container& container)
{
ConstRandomAccessIterator iterator(container);
iterator.toEnd();
return iterator;
}
template <typename Container, typename DataType, typename Position, typename Traits>
ConstRandomAccessIterator<Container, DataType, Position, Traits>
ConstRandomAccessIterator<Container, DataType, Position, Traits>::rbegin(const Container& container)
{
ConstRandomAccessIterator iterator(container);
iterator.toRBegin();
return iterator;
}
template <typename Container, typename DataType, typename Position, typename Traits>
ConstRandomAccessIterator<Container, DataType, Position, Traits>
ConstRandomAccessIterator<Container, DataType, Position, Traits>::rend(const Container& container)
{
ConstRandomAccessIterator iterator(container);
iterator.toREnd();
return iterator;
}
template <typename Container, typename DataType, typename Position, typename Traits>
class RandomAccessIterator
: public ConstRandomAccessIterator<Container, DataType, Position, Traits>
{
public:
typedef DataType& reference;
typedef DataType* pointer;
typedef ConstRandomAccessIterator<Container, DataType, Position, Traits> Base;
BALL_INLINE RandomAccessIterator() {}
BALL_INLINE RandomAccessIterator(const RandomAccessIterator& iterator)
: Base(iterator)
{
}
BALL_INLINE ~RandomAccessIterator() {}
BALL_INLINE reference operator [] (Index index) const { return const_cast<reference>(Base::getTraits().getData(index)); }
BALL_INLINE reference operator * () const { return const_cast<reference>(Base::getTraits().getData()); }
BALL_INLINE pointer operator -> () const { return const_cast<pointer>(&Base::getTraits().getData()); }
static RandomAccessIterator begin(const Container& container);
static RandomAccessIterator end(const Container& container);
static RandomAccessIterator rbegin(const Container& container);
static RandomAccessIterator rend(const Container& container);
protected:
BALL_INLINE RandomAccessIterator(const Container& container)
: Base(container)
{
}
};
template <typename Container, typename DataType, typename Position, typename Traits>
RandomAccessIterator<Container, DataType, Position, Traits>
RandomAccessIterator<Container, DataType, Position, Traits>::begin(const Container& container)
{
RandomAccessIterator iterator(container);
iterator.toBegin();
return iterator;
}
template <typename Container, typename DataType, typename Position, typename Traits>
RandomAccessIterator<Container, DataType, Position, Traits>
RandomAccessIterator<Container, DataType, Position, Traits>::end(const Container& container)
{
RandomAccessIterator iterator(container);
iterator.toEnd();
return iterator;
}
template <typename Container, typename DataType, typename Position, typename Traits>
RandomAccessIterator<Container, DataType, Position, Traits>
RandomAccessIterator<Container, DataType, Position, Traits>::rbegin(const Container& container)
{
RandomAccessIterator iterator(container);
iterator.toRBegin();
return iterator;
}
template <typename Container, typename DataType, typename Position, typename Traits>
RandomAccessIterator<Container, DataType, Position, Traits>
RandomAccessIterator<Container, DataType, Position, Traits>::rend(const Container& container)
{
RandomAccessIterator iterator(container);
iterator.toREnd();
return iterator;
}
} // namespace BALL
#endif // BALL_CONCEPT_RANDOMACCESSITERATOR_H
bidirectionalIterator.h
BALL::BaseIterator::getTraits
BALL_INLINE const Traits & getTraits() const
Get a constant reference to the traits of this iterator.
Definition baseIterator.h:130
BALL::BaseIterator::getContainer
BALL_INLINE const Container * getContainer() const
Get a constant pointer to the container of this iterator.
Definition baseIterator.h:136
BALL::BaseIterator::isValid
BALL_INLINE bool isValid() const
Definition baseIterator.h:175
BALL::BaseIterator::isSingular
BALL_INLINE bool isSingular() const
Definition baseIterator.h:170
BALL::ConstBidirectionalIterator::isEnd
BALL_INLINE bool isEnd() const
Check whether the iterator points to the position after the last item of the container.
Definition bidirectionalIterator.h:73
BALL::ConstBidirectionalIterator::toBegin
BALL_INLINE void toBegin()
Definition bidirectionalIterator.h:144
BALL::ConstBidirectionalIterator::toEnd
void toEnd()
Definition bidirectionalIterator.h:152
BALL::ConstBidirectionalIterator::ConstBidirectionalIterator
BALL_INLINE ConstBidirectionalIterator()
Definition bidirectionalIterator.h:43
BALL::ConstBidirectionalIterator::toRBegin
void toRBegin()
Definition bidirectionalIterator.h:160
BALL::ConstBidirectionalIterator::toREnd
void toREnd()
Definition bidirectionalIterator.h:168
BALL::ConstRandomAccessIterator
Definition randomAccessIterator.h:26
BALL::ConstRandomAccessIterator::operator[]
const DataType & operator[](Index index) const
Definition randomAccessIterator.h:359
BALL::ConstRandomAccessIterator::rbegin
static ConstRandomAccessIterator rbegin(const Container &container)
Definition randomAccessIterator.h:389
BALL::ConstRandomAccessIterator::ConstRandomAccessIterator
BALL_INLINE ConstRandomAccessIterator()
Definition randomAccessIterator.h:44
BALL::ConstRandomAccessIterator::operator-
BALL_INLINE bool operator-() const
Definition randomAccessIterator.h:127
BALL::ConstRandomAccessIterator::iterator_category
std::random_access_iterator_tag iterator_category
Definition randomAccessIterator.h:34
BALL::ConstRandomAccessIterator::end
static ConstRandomAccessIterator end(const Container &container)
Definition randomAccessIterator.h:380
BALL::ConstRandomAccessIterator::operator+=
ConstRandomAccessIterator & operator+=(Distance distance)
Definition randomAccessIterator.h:224
BALL::ConstRandomAccessIterator::operator>
bool operator>(const ConstRandomAccessIterator &iterator) const
Definition randomAccessIterator.h:339
BALL::ConstRandomAccessIterator::operator<
bool operator<(const ConstRandomAccessIterator &iterator) const
Definition randomAccessIterator.h:280
BALL::ConstRandomAccessIterator::operator-=
ConstRandomAccessIterator & operator-=(Distance distance)
Definition randomAccessIterator.h:240
BALL::ConstRandomAccessIterator::rend
static ConstRandomAccessIterator rend(const Container &container)
Definition randomAccessIterator.h:398
BALL::ConstRandomAccessIterator::ConstRandomAccessIterator
ConstRandomAccessIterator(const Container &container)
Definition randomAccessIterator.h:178
BALL::ConstRandomAccessIterator::~ConstRandomAccessIterator
BALL_INLINE ~ConstRandomAccessIterator()
Definition randomAccessIterator.h:53
BALL::ConstRandomAccessIterator::operator>=
bool operator>=(const ConstRandomAccessIterator &iterator) const
Definition randomAccessIterator.h:319
BALL::ConstRandomAccessIterator::operator<=
bool operator<=(const ConstRandomAccessIterator &iterator) const
Definition randomAccessIterator.h:300
BALL::ConstRandomAccessIterator::operator+
BALL_INLINE bool operator+() const
Definition randomAccessIterator.h:124
BALL::ConstRandomAccessIterator::ConstRandomAccessIterator
BALL_INLINE ConstRandomAccessIterator(const ConstRandomAccessIterator &iterator)
Definition randomAccessIterator.h:47
BALL::ConstRandomAccessIterator::Base
ConstBidirectionalIterator< Container, DataType, Position, Traits > Base
Definition randomAccessIterator.h:36
BALL::ConstRandomAccessIterator::begin
static ConstRandomAccessIterator begin(const Container &container)
Definition randomAccessIterator.h:371
BALL::Exception::IncompatibleIterators
Definition COMMON/exception.h:281
BALL::Exception::InvalidIterator
Definition COMMON/exception.h:270
BALL::RandomAccessIterator
Definition randomAccessIterator.h:415
BALL::RandomAccessIterator::operator->
BALL_INLINE pointer operator->() const
Definition randomAccessIterator.h:456
BALL::RandomAccessIterator::operator*
BALL_INLINE reference operator*() const
Definition randomAccessIterator.h:454
BALL::RandomAccessIterator::~RandomAccessIterator
BALL_INLINE ~RandomAccessIterator()
Definition randomAccessIterator.h:443
BALL::RandomAccessIterator::reference
DataType & reference
Definition randomAccessIterator.h:422
BALL::RandomAccessIterator::RandomAccessIterator
BALL_INLINE RandomAccessIterator(const Container &container)
Definition randomAccessIterator.h:489
BALL::RandomAccessIterator::RandomAccessIterator
BALL_INLINE RandomAccessIterator()
Definition randomAccessIterator.h:434
BALL::RandomAccessIterator::rbegin
static RandomAccessIterator rbegin(const Container &container)
Definition randomAccessIterator.h:517
BALL::RandomAccessIterator::begin
static RandomAccessIterator begin(const Container &container)
Definition randomAccessIterator.h:499
BALL::RandomAccessIterator::rend
static RandomAccessIterator rend(const Container &container)
Definition randomAccessIterator.h:526
BALL::RandomAccessIterator::pointer
DataType * pointer
Definition randomAccessIterator.h:424
BALL::RandomAccessIterator::Base
ConstRandomAccessIterator< Container, DataType, Position, Traits > Base
Definition randomAccessIterator.h:426
BALL::RandomAccessIterator::end
static RandomAccessIterator end(const Container &container)
Definition randomAccessIterator.h:508
BALL::RandomAccessIterator::operator[]
BALL_INLINE reference operator[](Index index) const
Definition randomAccessIterator.h:452
BALL::RandomAccessIterator::RandomAccessIterator
BALL_INLINE RandomAccessIterator(const RandomAccessIterator &iterator)
Definition randomAccessIterator.h:437
common.h
BALL_INLINE
#define BALL_INLINE
Definition debug.h:15
BALL
Definition constants.h:13
BALL::Distance
BALL_INDEX_TYPE Distance
Definition COMMON/global.h:86
BALL::Index
BALL_INDEX_TYPE Index
Definition COMMON/global.h:105