trsl logo
reorder_iterator.hpp
Go to the documentation of this file.
1 // (C) Copyright Toon Knapen 2001.
2 // (C) Copyright David Abrahams 2003.
3 // (C) Copyright Roland Richter 2003.
4 // (C) Copyright Renaud Detry 2007-2011.
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 
9 // This file is a fork of boost/iterator/permutation_iterator.hpp
10 
13 #ifndef TRSL_REORDER_ITERATOR_HPP
14 #define TRSL_REORDER_ITERATOR_HPP
15 
16 #include <trsl/error_handling.hpp>
17 #include <trsl/common.hpp>
18 
19 #include <iterator>
20 #include <vector>
21 #include <algorithm>
22 #include <boost/iterator.hpp>
23 #include <boost/detail/iterator.hpp>
24 #include <boost/iterator/iterator_categories.hpp>
25 #include <boost/iterator/iterator_adaptor.hpp>
26 #include <boost/shared_ptr.hpp>
27 
28 namespace trsl
29 {
30  template<class ElementIterator>
32 
33  namespace detail
34  {
36  template<class ElementIterator>
38  {
39  typedef ElementIterator element_iterator;
40  typedef size_t index_t;
41  typedef std::vector<index_t> index_container;
42  typedef boost::shared_ptr<index_container> index_container_ptr;
43  typedef typename index_container::const_iterator index_iterator;
44 
45  typedef boost::iterator_adaptor<
47  index_iterator,
48  typename boost::detail::iterator_traits<ElementIterator>::value_type,
49  boost::use_default,
50  typename boost::detail::iterator_traits<ElementIterator>::reference
51  > type;
52  };
53  }
54 
55 
94  template<class ElementIterator>
95  class reorder_iterator
96  : public detail::reorder_iterator_base<ElementIterator>::type
97  {
99  typedef typename base_t::type super_t;
100 
101  friend class boost::iterator_core_access;
102 
103  public:
104 
105  typedef typename base_t::index_t index_t;
106  typedef typename base_t::index_container index_container;
107  typedef typename base_t::index_container_ptr index_container_ptr;
108  typedef typename base_t::index_iterator index_iterator;
109 
110 
111  typedef typename base_t::element_iterator element_iterator;
112 
113  reorder_iterator() :
114  m_elt_iter(),
115  m_index_collection(new index_container)
116  {}
117 
123  explicit reorder_iterator(ElementIterator first,
124  const index_container_ptr& index_collection)
125  : super_t(index_collection->begin()),
126  m_elt_iter(first),
127  m_index_collection(index_collection)
128  {}
129 
140  template<class OtherElementIterator>
143  typename boost::enable_if_convertible<OtherElementIterator, ElementIterator>::type* = 0) :
144  super_t(r.base()), m_elt_iter(r.m_elt_iter),
145  m_index_collection(r.m_index_collection)
146  {}
147 
153  {
154  reorder_iterator<ElementIterator> indexIterator(*this);
155  indexIterator.base_reference() =
156  indexIterator.m_index_collection->begin();
157  return indexIterator;
158  }
159 
165  {
166  reorder_iterator<ElementIterator> indexIterator(*this);
167  indexIterator.base_reference() =
168  indexIterator.m_index_collection->end();
169  return indexIterator;
170  }
171 
176  index_t index() const
177  {
178  return *this->base();
179  }
180 
181  private:
182 
183  typename super_t::reference dereference() const
184  { return *(m_elt_iter + *this->base()); }
185 
186 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
187  template <class> friend class reorder_iterator;
188 #else
189  public:
190 #endif
191  ElementIterator m_elt_iter;
192  protected:
193  index_container_ptr m_index_collection;
194  };
195 
196 } // namespace trsl
197 
198 #endif // include guard
trsl::reorder_iterator::index
index_t index() const
Returns the index of the element that the iterator is currently pointing to.
Definition: reorder_iterator.hpp:176
trsl::reorder_iterator::begin
reorder_iterator< ElementIterator > begin() const
Returns a reorder_iterator pointing to the begining of the permutation.
Definition: reorder_iterator.hpp:152
common.hpp
trsl::reorder_iterator::end
reorder_iterator< ElementIterator > end() const
Returns a reorder_iterator pointing to the end of the permutation.
Definition: reorder_iterator.hpp:164
trsl
Public namespace.
Definition: common.hpp:29
trsl::detail::reorder_iterator_base
Used internally.
Definition: reorder_iterator.hpp:37
trsl::reorder_iterator::reorder_iterator
reorder_iterator(ElementIterator first, const index_container_ptr &index_collection)
Constructs an iterator that will walk through the elements of the range that begins at first,...
Definition: reorder_iterator.hpp:123
error_handling.hpp
trsl::reorder_iterator
Provides an iterator over a permutation of a range.
Definition: reorder_iterator.hpp:31
© Copyright 2007-2011 Renaud Detry.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at www.boost.org/LICENSE_1_0.txt.)
Revised Wed Jan 8 2020 14:43:31.