trsl logo
sort_iterator.hpp
Go to the documentation of this file.
1 // (C) Copyright Renaud Detry 2007-2011.
2 // Distributed under the Boost Software License, Version 1.0. (See
3 // accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5 
8 #ifndef TRSL_SORT_ITERATOR_HPP
9 #define TRSL_SORT_ITERATOR_HPP
10 
12 #include <trsl/common.hpp>
13 #include <trsl/error_handling.hpp>
14 
15 #include <functional>
16 
17 namespace trsl
18 {
19 
20  namespace detail {
21 
22  template<
23  class RandomIterator,
24  class Comparator
25  > class at_index_comp
26  {
27  public:
28 
29  at_index_comp(const RandomIterator &first, const Comparator &comp) :
30  elements_(first), comp_(comp)
31  {}
32 
33  bool operator() (unsigned i, unsigned j)
34  {
35  return comp_(*(elements_+i), *(elements_+j));
36  }
37 
38  RandomIterator elements_;
39  Comparator comp_;
40  };
41 
42  }
43 
74  template<class ElementIterator, class ElementComparator>
76  sort_iterator(ElementIterator first,
77  ElementIterator last,
78  ElementComparator comp,
79  unsigned permutationSize)
80  {
81  ptrdiff_t size = std::distance(first, last);
82  if (size < 0)
83  throw bad_parameter_value(
84  "sort_iterator: "
85  "bad input range.");
86  if (permutationSize > unsigned(size))
87  throw bad_parameter_value(
88  "sort_iterator: "
89  "parameter permutationSize out of range.");
90 
91  typedef
92  typename reorder_iterator<ElementIterator>::index_container
93  index_container;
94  typedef
95  typename reorder_iterator<ElementIterator>::index_container_ptr
96  index_container_ptr;
97  typedef
98  typename reorder_iterator<ElementIterator>::index_t
99  index_t;
100 
101  index_container_ptr index_collection(new index_container);
102 
103  index_collection->resize(size);
104  for (index_t i = 0; i < index_t(size); ++i)
105  (*index_collection)[i] = i;
106 
107  if (permutationSize == unsigned(size))
108  std::sort(index_collection->begin(),
109  index_collection->end(),
111  <ElementIterator, ElementComparator>(first, comp));
112  else
113  {
114  std::partial_sort(index_collection->begin(),
115  index_collection->begin()+permutationSize,
116  index_collection->end(),
118  <ElementIterator, ElementComparator>(first, comp));
119  index_collection->resize(permutationSize);
120  }
121 
122  return reorder_iterator<ElementIterator>(first, index_collection);
123  }
124 
151  template<class ElementIterator, class ElementComparator>
152  reorder_iterator<ElementIterator>
153  sort_iterator(ElementIterator first,
154  ElementIterator last,
155  ElementComparator comp)
156  {
157  return sort_iterator(first,
158  last,
159  comp,
160  std::distance(first, last));
161  }
162 
178  template<class ElementIterator>
179  reorder_iterator<ElementIterator>
180  sort_iterator(ElementIterator first,
181  ElementIterator last)
182  {
183  return sort_iterator(first,
184  last,
185  std::less
186  <typename std::iterator_traits
187  <ElementIterator>::value_type>());
188  }
189 
190 } // namespace trsl
191 
192 #endif // include guard
trsl::sort_iterator
reorder_iterator< ElementIterator > sort_iterator(ElementIterator first, ElementIterator last, ElementComparator comp, unsigned permutationSize)
Constructs a reorder_iterator that will iterate through the first permutationSize elements of a sorte...
Definition: sort_iterator.hpp:76
common.hpp
trsl::bad_parameter_value
Thrown when a TRSL component receives a parameter that has a forbidden value.
Definition: error_handling.hpp:27
trsl
Public namespace.
Definition: common.hpp:29
error_handling.hpp
reorder_iterator.hpp
trsl::reorder_iterator
Provides an iterator over a permutation of a range.
Definition: reorder_iterator.hpp:31
trsl::detail::at_index_comp
Definition: sort_iterator.hpp:25
© 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.