1 | #ifndef _theplu_yat_utility_sort_index_ |
---|
2 | #define _theplu_yat_utility_sort_index_ |
---|
3 | |
---|
4 | // $Id: sort_index.h 1495 2008-09-12 14:58:35Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2008 Peter Johansson |
---|
8 | |
---|
9 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
10 | |
---|
11 | The yat library is free software; you can redistribute it and/or |
---|
12 | modify it under the terms of the GNU General Public License as |
---|
13 | published by the Free Software Foundation; either version 3 of the |
---|
14 | License, or (at your option) any later version. |
---|
15 | |
---|
16 | The yat library is distributed in the hope that it will be useful, |
---|
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
19 | General Public License for more details. |
---|
20 | |
---|
21 | You should have received a copy of the GNU General Public License |
---|
22 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | */ |
---|
24 | |
---|
25 | #include "StrideIterator.h" |
---|
26 | |
---|
27 | #include <gsl/gsl_sort.h> |
---|
28 | |
---|
29 | #include <algorithm> |
---|
30 | #include <iterator> |
---|
31 | #include <vector> |
---|
32 | |
---|
33 | namespace theplu { |
---|
34 | namespace yat { |
---|
35 | namespace utility { |
---|
36 | |
---|
37 | /** |
---|
38 | Create a vector \a sort_index containing the indeces of elements |
---|
39 | in a range [first, last). The elements of \a sort_index give the |
---|
40 | index of the range element which would have been stored in that |
---|
41 | position if the range had been sorted in place. The first element |
---|
42 | of \a sort_index gives the index of the least element in the |
---|
43 | range, and the last element of \a sort_index gives the index of |
---|
44 | the greatest element in the range. The function will not affect |
---|
45 | the range, i.e., ForwardIterator can be read-only. |
---|
46 | |
---|
47 | \since New in yat 0.5 |
---|
48 | */ |
---|
49 | template<typename ForwardIterator> |
---|
50 | void sort_index(ForwardIterator first, ForwardIterator last, |
---|
51 | std::vector<size_t>& sort_index); |
---|
52 | |
---|
53 | |
---|
54 | /** |
---|
55 | Specialization for StrideIterator<double*> |
---|
56 | |
---|
57 | \since New in yat 0.5 |
---|
58 | */ |
---|
59 | void sort_index(StrideIterator<double*> first, |
---|
60 | StrideIterator<double*> last, |
---|
61 | std::vector<size_t>& sort_index); |
---|
62 | |
---|
63 | /** |
---|
64 | Specialization for StrideIterator<const double*> |
---|
65 | |
---|
66 | \since New in yat 0.5 |
---|
67 | */ |
---|
68 | void sort_index(StrideIterator<const double*> first, |
---|
69 | StrideIterator<const double*> last, |
---|
70 | std::vector<size_t>& sort_index); |
---|
71 | |
---|
72 | /** |
---|
73 | Specialization for std::vector<double>::iterator |
---|
74 | |
---|
75 | \since New in yat 0.5 |
---|
76 | */ |
---|
77 | void sort_index(std::vector<double>::iterator first, |
---|
78 | std::vector<double>::iterator last, |
---|
79 | std::vector<size_t>& sort_index); |
---|
80 | |
---|
81 | /** |
---|
82 | Specialization for std::vector<double>::const_iterator |
---|
83 | |
---|
84 | \since New in yat 0.5 |
---|
85 | */ |
---|
86 | void sort_index(std::vector<double>::const_iterator first, |
---|
87 | std::vector<double>::const_iterator last, |
---|
88 | std::vector<size_t>& sort_index); |
---|
89 | |
---|
90 | |
---|
91 | // template implementation |
---|
92 | |
---|
93 | template<typename ForwardIterator> |
---|
94 | void sort_index(ForwardIterator first, ForwardIterator last, |
---|
95 | std::vector<size_t>& result) |
---|
96 | { |
---|
97 | std::vector<double> vec; |
---|
98 | vec.reserve(std::distance(first, last)); |
---|
99 | std::copy(first, last, |
---|
100 | std::back_insert_iterator<std::vector<double> >(vec)); |
---|
101 | sort_index(vec.begin(), vec.end(), result); |
---|
102 | } |
---|
103 | |
---|
104 | }}} // of namespace utility, yat, and theplu |
---|
105 | |
---|
106 | #endif |
---|