8 #ifndef TRSL_SORT_ITERATOR_HPP
9 #define TRSL_SORT_ITERATOR_HPP
29 at_index_comp(
const RandomIterator &first,
const Comparator &comp) :
30 elements_(first), comp_(comp)
33 bool operator() (
unsigned i,
unsigned j)
35 return comp_(*(elements_+i), *(elements_+j));
38 RandomIterator elements_;
74 template<
class ElementIterator,
class ElementComparator>
78 ElementComparator comp,
79 unsigned permutationSize)
81 ptrdiff_t size = std::distance(first, last);
86 if (permutationSize >
unsigned(size))
89 "parameter permutationSize out of range.");
92 typename reorder_iterator<ElementIterator>::index_container
95 typename reorder_iterator<ElementIterator>::index_container_ptr
98 typename reorder_iterator<ElementIterator>::index_t
101 index_container_ptr index_collection(
new index_container);
103 index_collection->resize(size);
104 for (index_t i = 0; i < index_t(size); ++i)
105 (*index_collection)[i] = i;
107 if (permutationSize ==
unsigned(size))
108 std::sort(index_collection->begin(),
109 index_collection->end(),
111 <ElementIterator, ElementComparator>(first, comp));
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);
151 template<
class ElementIterator,
class ElementComparator>
152 reorder_iterator<ElementIterator>
154 ElementIterator last,
155 ElementComparator comp)
160 std::distance(first, last));
178 template<
class ElementIterator>
179 reorder_iterator<ElementIterator>
181 ElementIterator last)
186 <
typename std::iterator_traits
187 <ElementIterator>::value_type>());
192 #endif // include guard