diff --git a/include/boost/iterator_adaptors.hpp b/include/boost/iterator_adaptors.hpp index f11d672..cc6fa43 100644 --- a/include/boost/iterator_adaptors.hpp +++ b/include/boost/iterator_adaptors.hpp @@ -1,9 +1,9 @@ -// (C) Copyright David Abrahams 2000. Permission to copy, use, -// modify, sell and distribute this software is granted provided this -// copyright notice appears in all copies. This software is provided -// "as is" without express or implied warranty, and with no claim as -// to its suitability for any purpose. - +// (C) Copyright David Abrahams 2000. Permission to copy, use, +// modify, sell and distribute this software is granted provided this +// copyright notice appears in all copies. This software is provided +// "as is" without express or implied warranty, and with no claim as +// to its suitability for any purpose. +// // (C) Copyright Jeremy Siek 2000. Permission to copy, use, modify, // sell and distribute this software is granted provided this // copyright notice appears in all copies. This software is provided @@ -59,30 +59,32 @@ struct default_iterator_policies { return x < y; } }; -//=============================================================================// iterator_adaptor - A generalized adaptor around an existing iterator, which is itself an iterator +//============================================================================= +// iterator_adaptor - A generalized adaptor around an existing +// iterator, which is itself an iterator // -// Iterator - the iterator type being wrapped. +// Iterator - the iterator type being wrapped. // -// Policies - a set of policies determining how the resulting iterator +// Policies - a set of policies determining how the resulting iterator // works. // -// NonconstIterator - the corresponding non-const iterator type for +// Traits - a class satisfying the same requirements as a specialization of +// std::iterator_traits for the resulting iterator. +// +// NonconstIterator - the corresponding non-const iterator type for // Iterator, if any. You don't need to supply this if you are not make a // const/non-const iterator pair. // -// Traits - a class satisfying the same requirements as a specialization of -// std::iterator_traits for the resulting iterator. -// template , -#else +#ifdef BOOST_NO_STD_ITERATOR_TRAITS class Traits, +#else + class Traits = std::iterator_traits, #endif class NonconstIterator = Iterator > struct iterator_adaptor - : std::iterator + : boost::iterator { typedef iterator_adaptor Self; public: @@ -237,12 +239,214 @@ template iterator; - typedef iterator_adaptor + typedef iterator_adaptor iterator; + typedef iterator_adaptor const_iterator; }; +//============================================================================= +// Transform Iterator Adaptor + +template +struct transform_iterator_policies : public default_iterator_policies +{ + transform_iterator_policies() { } + transform_iterator_policies(const AdaptableUnaryFunction& f) : m_f(f) { } + + template + Reference dereference(type, const Iterator& x) const + { return m_f(*x); } + + AdaptableUnaryFunction m_f; +}; + +template +struct transform_iterator_traits { + typedef typename AdaptableUnaryFunction::result_type value_type; + typedef value_type reference; + typedef value_type* pointer; + typedef typename IteratorTraits::difference_type difference_type; + typedef typename IteratorTraits::iterator_category iterator_category; +}; + +template +#else + class Traits +#endif + > +struct transform_iterator +{ + typedef transform_iterator_traits + TransTraits; + typedef iterator_adaptor, TransTraits, + Iterator> type; +}; + + +//============================================================================= +// Indirect Iterators Adaptor + +// Tried implementing this with transform_iterator, but that required +// using boost::remove_ref, which is not compiler portable. + +struct indirect_iterator_policies : public default_iterator_policies +{ + template + Reference dereference(type, const Iterator& x) const + { return **x; } +}; + +template , + class Traits = + std::iterator_traits +#endif + > +struct indirect_traits +{ + typedef typename IndirectTraits::difference_type difference_type; + typedef typename Traits::value_type value_type; + typedef typename Traits::pointer pointer; + typedef typename Traits::reference reference; + typedef typename IndirectTraits::iterator_category iterator_category; +}; + +template , + class ConstIndirectTraits = + std::iterator_traits, + class Traits = + std::iterator_traits +#endif + > +struct indirect_iterators +{ + typedef typename IndirectTraits::value_type Iterator; + typedef typename Traits::value_type ValueType; + typedef iterator_adaptors, + indirect_traits, + indirect_iterator_policies + > Adaptors; + typedef typename Adaptors::iterator iterator; + typedef typename Adaptors::const_iterator const_iterator; +}; + + +//============================================================================= +// Reverse Iterators Adaptor + +struct reverse_iterator_policies +{ + template + Reference dereference(type, const Iterator& x) const + { return *boost::prior(x); } + + template + void increment(Iterator& x) const + { --x; } + + template + void decrement(Iterator& x) const + { ++x; } + + template + void advance(Iterator& x, DifferenceType n) const + { x -= n; } + + template + Difference distance(type, Iterator1& x, Iterator2& y) const + { return x - y; } + + template + bool equal(Iterator1& x, Iterator2& y) const + { return x == y; } + + template + bool less(Iterator1& x, Iterator2& y) const + { return y < x; } +}; + +template , + class ConstTraits = std::iterator_traits +#else + class Traits, + class ConstTraits +#endif + > +struct reverse_iterators +{ + typedef iterator_adaptors Adaptor; + typedef typename Adaptor::iterator iterator; + typedef typename Adaptor::const_iterator const_iterator; +}; + +//============================================================================= +// Counting Iterator and Integer Range Class + +struct counting_iterator_policies : public default_iterator_policies +{ + template + IntegerType dereference(type, const IntegerType& i) const + { return i; } +}; +template +struct counting_iterator_traits { + typedef IntegerType value_type; + typedef IntegerType reference; + typedef value_type* pointer; + typedef std::ptrdiff_t difference_type; + typedef std::random_access_iterator_tag iterator_category; +}; + +template +struct integer_range { + typedef iterator_adaptor, IntegerType> iterator; + typedef iterator const_iterator; + typedef IntegerType value_type; + typedef std::ptrdiff_t difference_type; + typedef IntegerType reference; + typedef IntegerType const_reference; + typedef const IntegerType* pointer; + typedef const IntegerType* const_pointer; + typedef IntegerType size_type; + + integer_range(IntegerType start, IntegerType finish) + : m_start(start), m_finish(finish) { } + + iterator begin() const { return iterator(m_start); } + iterator end() const { return iterator(m_finish); } + size_type size() const { return m_finish - m_start; } + bool empty() const { return m_finish == m_start; } + void swap(integer_range& x) { + std::swap(m_start, x.m_start); + std::swap(m_finish, x.m_finish); + } +protected: + IntegerType m_start, m_finish; +}; + + } // namespace boost #endif +