1 #ifndef _theplu_yat_utility_sort_index_
2 #define _theplu_yat_utility_sort_index_
25 #include <boost/concept_check.hpp>
26 #include <boost/iterator/iterator_categories.hpp>
27 #include <boost/iterator/iterator_concepts.hpp>
28 #include <boost/iterator/iterator_traits.hpp>
64 template<
typename InputIterator>
65 void sort_index(InputIterator first, InputIterator last,
93 template<
typename InputIterator,
class Compare>
94 void sort_index(InputIterator first, InputIterator last,
95 std::vector<size_t>&
sort_index, Compare comp);
103 template<
typename InputIterator,
class Compare>
104 void sort_index(InputIterator first, InputIterator last,
105 std::vector<size_t>& idx, Compare comp,
106 std::input_iterator_tag);
109 template<
typename RandomAccessIterator,
class Compare>
110 void sort_index(RandomAccessIterator first, RandomAccessIterator last,
111 std::vector<size_t>& idx, Compare comp,
112 std::random_access_iterator_tag);
114 template<
typename RandomAccessIterator,
class Compare>
115 class sort_index_Compare
118 sort_index_Compare(RandomAccessIterator it, Compare comp)
119 : data_(it), compare_(comp)
121 using boost_concepts::ReadableIterator;
122 BOOST_CONCEPT_ASSERT((ReadableIterator<RandomAccessIterator>));
123 using boost_concepts::RandomAccessTraversal;
124 BOOST_CONCEPT_ASSERT((RandomAccessTraversal<RandomAccessIterator>));
126 typename boost::iterator_reference<RandomAccessIterator>::type T;
127 BOOST_CONCEPT_ASSERT((boost::BinaryPredicate<Compare, T, T>));
130 bool operator()(
size_t i,
size_t j)
const
131 {
return compare_(data_[i], data_[j]); }
134 RandomAccessIterator data_;
145 template<
typename InputIterator>
147 std::vector<size_t>& idx)
149 typedef typename boost::iterator_value<InputIterator>::type T;
151 BOOST_CONCEPT_ASSERT((boost::LessThanComparable<T>));
156 template<
typename InputIterator,
class Compare>
158 std::vector<size_t>& idx, Compare comp)
160 BOOST_CONCEPT_ASSERT((boost::InputIterator<InputIterator>));
161 typename std::iterator_traits<InputIterator>::iterator_category category;
162 detail::sort_index(first, last, idx, comp, category);
171 template<
typename InputIterator,
class Compare>
172 void sort_index(InputIterator first, InputIterator last,
173 std::vector<size_t>& idx, Compare comp,
174 std::input_iterator_tag tag)
176 typedef typename boost::iterator_value<InputIterator>::type T;
178 BOOST_CONCEPT_ASSERT((boost::DefaultConstructible<T>));
179 BOOST_CONCEPT_ASSERT((boost::SGIAssignable<T>));
180 std::vector<T> vec(first, last);
182 std::random_access_iterator_tag());
187 template<
typename RandomAccessIterator,
class Compare>
188 void sort_index(RandomAccessIterator first, RandomAccessIterator last,
189 std::vector<size_t>& idx, Compare comp,
190 std::random_access_iterator_tag tag)
192 sort_index_Compare<RandomAccessIterator, Compare> compare(first, comp);
196 size_t n = last-first;
199 for (
size_t i=0; i<n; ++i)
201 std::sort(idx.begin(), idx.end(), compare);
void sort_index(InputIterator first, InputIterator last, std::vector< size_t > &sort_index)
Definition: sort_index.h:146