1 | #ifndef _theplu_yat_normalizer_spearman_ |
---|
2 | #define _theplu_yat_normalizer_spearman_ |
---|
3 | |
---|
4 | /* |
---|
5 | Copyright (C) 2008 Peter Johansson |
---|
6 | |
---|
7 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
8 | |
---|
9 | The yat library is free software; you can redistribute it and/or |
---|
10 | modify it under the terms of the GNU General Public License as |
---|
11 | published by the Free Software Foundation; either version 3 of the |
---|
12 | License, or (at your option) any later version. |
---|
13 | |
---|
14 | The yat library is distributed in the hope that it will be useful, |
---|
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU General Public License |
---|
20 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
21 | */ |
---|
22 | |
---|
23 | #include "yat/utility/iterator_traits.h" |
---|
24 | #include "yat/utility/sort_index.h" |
---|
25 | |
---|
26 | #include <vector> |
---|
27 | |
---|
28 | namespace theplu { |
---|
29 | namespace yat { |
---|
30 | namespace normalizer { |
---|
31 | |
---|
32 | /** |
---|
33 | \brief Replace elements with normalized rank |
---|
34 | |
---|
35 | \since New in yat 0.5 |
---|
36 | */ |
---|
37 | class Spearman |
---|
38 | { |
---|
39 | public: |
---|
40 | /** |
---|
41 | \brief default constructor |
---|
42 | */ |
---|
43 | Spearman(void){} |
---|
44 | |
---|
45 | /** |
---|
46 | It is possible to centralize a range "in place"; it is |
---|
47 | permissible for the iterators \a first and \a result to be the |
---|
48 | same. |
---|
49 | |
---|
50 | \note Currently weighted version is not implemented |
---|
51 | |
---|
52 | \return result + (last-first) |
---|
53 | */ |
---|
54 | template<typename InputIterator, typename RandomAccessIterator> |
---|
55 | RandomAccessIterator operator()(InputIterator first, InputIterator last, |
---|
56 | RandomAccessIterator result) const |
---|
57 | { |
---|
58 | typename utility::weighted_iterator_traits<InputIterator>::type tag; |
---|
59 | return normalize(first, last, result, tag); |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | private: |
---|
64 | // unweighted version |
---|
65 | template<typename InputIterator, typename RandomAccessIterator> |
---|
66 | RandomAccessIterator normalize(InputIterator first, InputIterator last, |
---|
67 | RandomAccessIterator result, |
---|
68 | utility::unweighted_iterator_tag) const |
---|
69 | { |
---|
70 | std::vector<size_t> perm; |
---|
71 | utility::sort_index(first, last, perm); |
---|
72 | double n = perm.size(); |
---|
73 | for ( size_t i=0; i<perm.size(); ++i) |
---|
74 | result[perm[i]] = static_cast<double>(i)/n; |
---|
75 | return result + std::distance(first, last); |
---|
76 | } |
---|
77 | |
---|
78 | }; |
---|
79 | |
---|
80 | }}} // end of namespace normalizer, yat and thep |
---|
81 | #endif |
---|