From f0cc713521a9f2f10c4374264e1f3b9396f03b8b Mon Sep 17 00:00:00 2001 From: Jeremy Siek Date: Tue, 22 Oct 2002 20:32:13 +0000 Subject: [PATCH] work on new version of iterator adaptors [SVN r597] --- include/boost/iterator/iterator_adaptors.hpp | 156 ++++++++++++++++++ .../boost/iterator/iterator_categories.hpp | 4 +- include/boost/iterator/iterator_concepts.hpp | 4 +- 3 files changed, 160 insertions(+), 4 deletions(-) create mode 100644 include/boost/iterator/iterator_adaptors.hpp diff --git a/include/boost/iterator/iterator_adaptors.hpp b/include/boost/iterator/iterator_adaptors.hpp new file mode 100644 index 0000000..609e985 --- /dev/null +++ b/include/boost/iterator/iterator_adaptors.hpp @@ -0,0 +1,156 @@ +#ifndef BOOST_ITERATOR_ADAPTORS_HPP +#define BOOST_ITERATOR_ADAPTORS_HPP + +#include "boost/utility.hpp" // for prior + +namespace boost { + +template +struct iterator_comparisons { }; + +template +inline bool operator==(const iterator_comparisons& xb, + const iterator_comparisons& yb) +{ + const D1& x = static_cast(xb); + const D2& y = static_cast(yb); + return x.equal(y); +} + +template +inline bool operator!=(const iterator_comparisons& xb, + const iterator_comparisons& yb) +{ + const D1& x = static_cast(xb); + const D2& y = static_cast(yb); + return !x.equal(y); +} + +template +inline bool operator<(const iterator_comparisons& xb, + const iterator_comparisons& yb) +{ + const D1& x = static_cast(xb); + const D2& y = static_cast(yb); + return y.distance(x) < 0; +} + +template +inline bool operator>(const iterator_comparisons& xb, + const iterator_comparisons& yb) +{ + const D1& x = static_cast(xb); + const D2& y = static_cast(yb); + return y.distance(x) > 0; +} + +template +inline bool operator>=(const iterator_comparisons& xb, + const iterator_comparisons& yb) +{ + const D1& x = static_cast(xb); + const D2& y = static_cast(yb); + return y.distance(x) >= 0; +} + +template +inline bool operator<=(const iterator_comparisons& xb, + const iterator_comparisons& yb) +{ + const D1& x = static_cast(xb); + const D2& y = static_cast(yb); + return y.distance(x) <= 0; +} + + +template +struct iterator_arith : public iterator_comparisons { }; + +template +D operator+(iterator_arith i, D n) { return i += n; } + +template +D operator+(D n, iterator_arith i) { return i += n; } + +template +D operator-(const iterator_arith& i, + const iterator_arith& j) + { return static_cast(j).distance(static_cast(i)); } + + +template +class iterator_adaptor : public iterator_arith +{ +public: + typedef V value_type; + typedef R reference; + typedef P pointer; + typedef C iterator_category; + typedef D difference_type; + + reference operator*() const + { return derived().dereference(); } + //operator->() const { return detail::operator_arrow(*this, iterator_category()); } + reference operator[](difference_type n) const + { return *(*this + n); } + Derived& operator++() + { derived().increment(); return derived(); } + Derived operator++(int) + { Derived tmp(derived()); ++*this; return tmp; } + Derived& operator--() + { derived().decrement(); return derived(); } + Derived operator--(int) + { Derived tmp(derived()); --*this; return tmp; } + Derived& operator+=(difference_type n) + { derived().advance(n); return derived(); } + Derived& operator-=(difference_type n) + { derived().advance(-n); return derived(); } + Derived operator-(difference_type x) const + { Derived result(derived()); return result -= x; } +private: + Derived& derived() { return static_cast(*this); } + const Derived& derived() const { return static_cast(*this); } +}; + + + +template +class reverse_iterator : public iterator_adaptor, V, R, P, C, D> +{ + typedef iterator_adaptor, V, R, P, C, D> super; + friend class super; + // hmm, I don't like the next two lines + template friend struct iterator_comparisons; + template friend struct iterator_arith; +public: + reverse_iterator(const Base& x) : m_base(x) { } + + reverse_iterator() { } + + template + reverse_iterator(const reverse_iterator& y) + : m_base(y.m_base) { } + //private: hmm, the above friend decls aren't doing the job. + typename super::reference dereference() const { *boost::prior(m_base); } + void increment() { --m_base; } + void decrement() { ++m_base; } + void advance(typename super::difference_type n) + { m_base -= n; } + + template + typename super::difference_type + distance(const reverse_iterator& y) const + { return m_base - y.m_base; } + + Base m_base; +}; + + + + +} // namespace boost + + +#endif // BOOST_ITERATOR_ADAPTORS_HPP diff --git a/include/boost/iterator/iterator_categories.hpp b/include/boost/iterator/iterator_categories.hpp index 7241882..c99d28f 100644 --- a/include/boost/iterator/iterator_categories.hpp +++ b/include/boost/iterator/iterator_categories.hpp @@ -123,7 +123,7 @@ namespace boost { typedef typename OldTraits::iterator_category Cat; typedef typename OldTraits::value_type value_type; public: - typedef iter_category_to_return::type type; + typedef typename iter_category_to_return::type type; }; }; @@ -132,7 +132,7 @@ namespace boost { typedef boost::detail::iterator_traits OldTraits; typedef typename OldTraits::iterator_category Cat; public: - typedef iter_category_to_traversal::type type; + typedef typename iter_category_to_traversal::type type; }; }; diff --git a/include/boost/iterator/iterator_concepts.hpp b/include/boost/iterator/iterator_concepts.hpp index 2e51a9f..22ed509 100644 --- a/include/boost/iterator/iterator_concepts.hpp +++ b/include/boost/iterator/iterator_concepts.hpp @@ -139,7 +139,7 @@ namespace boost_concepts { traversal_category; void constraints() { - boost::function_requires< ForwardIteratorConcept >(); + boost::function_requires< ForwardTraversalConcept >(); BOOST_STATIC_ASSERT((boost::is_convertible::value)); @@ -159,7 +159,7 @@ namespace boost_concepts { difference_type; void constraints() { - boost::function_requires< BidirectionalIteratorConcept >(); + boost::function_requires< BidirectionalTraversalConcept >(); BOOST_STATIC_ASSERT((boost::is_convertible::value));