From 8a4839354e8f34259531882945bf0f466bcecfeb Mon Sep 17 00:00:00 2001 From: Toon Knapen Date: Fri, 30 Nov 2001 10:38:36 +0000 Subject: [PATCH] permutation_iterator_adaptor and generator [SVN r11823] --- include/boost/permutation_iterator.hpp | 71 ++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 include/boost/permutation_iterator.hpp diff --git a/include/boost/permutation_iterator.hpp b/include/boost/permutation_iterator.hpp new file mode 100644 index 0000000..7c05cab --- /dev/null +++ b/include/boost/permutation_iterator.hpp @@ -0,0 +1,71 @@ +// (C) Copyright Toon Knapen 2001. 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. +// + +#include + +namespace boost { + + template < typename OrderIt > + struct permutation_iterator_policies : public default_iterator_policies + { + permutation_iterator_policies() {} + + permutation_iterator_policies(OrderIt order_it) + : order_it_( order_it ) + {} + + template + typename IteratorAdaptor::reference dereference(const IteratorAdaptor& x) const + { return *(x.base() + *order_it_); } + + template + void increment(IteratorAdaptor& x) + { ++order_it_; } + + template + void decrement(IteratorAdaptor& x) + { --order_it_; } + + template + void advance(IteratorAdaptor& x, DifferenceType n) + { std::advance( order_it_, n ); } + + template + typename IteratorAdaptor1::difference_type + distance(const IteratorAdaptor1& x, const IteratorAdaptor2& y) const + { return std::distance( x.policies().order_it_, y.policies().order_it_ ); } + + template + bool equal(const IteratorAdaptor1& x, const IteratorAdaptor2& y) const + { return x.policies().order_it_ == y.policies().order_it_; } + + OrderIt order_it_; + }; + + /// generate an iterator that will access the elements + /// behind the random-access iterator RAIt in the + /// order specified by the OrderIt iterator. + /// preconditions: + /// The OrderIt::value_type will be used in iterator arithmetic + template < typename RAIt, typename OrderIt > + struct permutation_iterator_generator + { + typedef boost::iterator_adaptor + < RAIt, + permutation_iterator_policies< OrderIt > + > type; + }; + + template < class OrderIt, class RAIt > + inline typename permutation_iterator_generator< RAIt, OrderIt >::type + make_permutation_iterator(RAIt base, OrderIt order) + { + typedef typename permutation_iterator_generator< RAIt, OrderIt >::type result_t; + return result_t( base, order ); + } + +} // namespace boost