-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathorder.h
100 lines (89 loc) · 3.28 KB
/
order.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* @file order.h
* @author Robert Carnell
* @copyright Copyright (c) 2013, Robert Carnell
*
* @license <a href="http://www.gnu.org/licenses/gpl.html">GNU General Public License (GPL v3)</a>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ORDER_H
#define ORDER_H
#include <vector>
#include <algorithm>
namespace bclib
{
/**
* Comparison operator to use in the findranks method
* @param first the first pair of arguments (value, rank)
* @param second the second pair of arguments (value, rank)
* @return true if the value in the first argument is less than the value in the second argument
*/
template <class T>
bool findranksCompare(const std::pair<T, int> first, const std::pair<T, int> second)
{
return (first.first < second.first);
}
/**
* Find the order of each vector element (zero based)
* @tparam T numeric argument that can be ordered
* @param v the vector to be ordered
* @param order the order of the elements
*/
template <class T>
void findorder_zero(const std::vector<T> & v, std::vector<int> & order)
{
// create a vector of pairs to hold the value and the integer rank
std::vector<std::pair<T, int> > p(v.size());
typename std::vector<T>::const_iterator vi;
typename std::vector<std::pair<T, int> >::iterator pi;
int position = 0;
for (vi = v.begin(), pi = p.begin();
vi != v.end() && pi != p.end(); ++vi, ++pi)
{
*pi = std::pair<T, int>(*vi, position);
position++;
}
// if the rank vector is not the right size, resize it (the original values may be lost)
if (order.size() != v.size())
{
order.resize(v.size());
}
// sort the pairs of values
std::sort(p.begin(), p.end(), findranksCompare<double>);
// take the ranks from the pairs and put them in the rank vector
std::vector<int>::iterator oi;
for (oi = order.begin(), pi = p.begin();
oi != order.end() && pi != p.end(); ++oi, ++pi)
{
*oi = pi->second;
//order[i] = p[i].second;
}
}
/**
* Find the order of each vector element (one based)
* @tparam T numeric argument that can be ordered
* @param v the vector to be ranked
* @param order the order of the elements
*/
template <class T>
void findorder(const std::vector<T> & v, std::vector<int> & order)
{
findorder_zero<T>(v, order);
for (std::vector<int>::size_type i = 0; i < order.size(); i++)
{
order[i] += 1;
}
}
} // end namespace
#endif /* ORDER_H */