ranker.h
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
27 #ifndef RANKER_H
28 #define RANKER_H
29 
30 #include <vector>
31 #include <string>
32 #include <algorithm>
33 
34 using std::vector;
35 using std::string;
36 
37 #ifndef uint
38 typedef unsigned int uint;
39 #endif
40 
41 template <class T>
42 class lt { public: static int compare(T a, T b) { return(a < b); } };
43 template <class T>
44 class gt { public: static int compare(T a, T b) { return(a > b); } };
45 
46 template <class T, class C>
47 class ranker
48 {
49  private:
50  const T* p;
52 
53  public:
54  ranker(const vector<T>& v) : p(&v[0]), sz(v.size()) { }
55  ranker(const T* tp, uint s) : p(tp), sz(s) { }
56 
57  int operator()(uint i1, uint i2) const { return(C::compare(p[i1],p[i2])); }
58 
59  template <class S>
60  void get_orders(vector<S>& w) const {
61  w.resize(sz);
62  w.front() = 0;
63  for (typename vector<S>::iterator i = w.begin(); i != w.end() - 1; ++i)
64  *(i + 1) = *i + 1;
65  std::sort(w.begin(), w.end(), *this);
66  }
67 
68  template <class S>
69  void get_partial_orders(vector<S>& w, uint num) const {
70  if (num > sz) num = sz;
71  w.resize(sz);
72  w.front() = 0;
73  for (typename vector<S>::iterator i = w.begin(); i != w.end() - 1; ++i)
74  *(i + 1) = *i + 1;
75  std::partial_sort(w.begin(), w.begin() + num, w.end(), *this);
76  w.resize(num);
77  }
78 
79  template <class S>
80  void get_ranks(vector<S>& w, const string& method) const {
81  w.resize(sz);
82  vector<uint> tmp(w.size());
83  get_orders(tmp);
84  if (method == "average") {
85  for (uint c = 0, reps; c < w.size(); c += reps) { reps = 1;
86  while (c + reps < w.size() && p[tmp[c]] == p[tmp[c + reps]]) ++reps;
87  for (uint k = 0; k < reps; ++k)
88  w[tmp[c + k]] = S(2 * c + reps - 1) / 2 + 1;
89  }
90  } else if (method == "min") {
91  for (uint c = 0, reps; c < w.size(); c += reps) { reps = 1;
92  while (c + reps < w.size() && p[tmp[c]] == p[tmp[c + reps]]) ++reps;
93  for (uint k = 0; k < reps; ++k) w[tmp[c + k]] = c + 1;
94  }
95  } else if (method == "max") {
96  for (uint c = 0, reps; c < w.size(); c += reps) { reps = 1;
97  while (c + reps < w.size() && p[tmp[c]] == p[tmp[c + reps]]) ++reps;
98  for (uint k = 0; k < reps; ++k) w[tmp[c + k]] = c + reps;
99  }
100  } else // default
101  for (uint c = 0; c < w.size(); ++c) w[tmp[c]] = c + 1;
102  }
103 
104  template <class S>
105  void get_partial_ranks(vector<S>& w, const string& method, size_t num) const {
106  if (num > sz) num = sz;
107  vector<uint> tmp(sz);
108  get_partial_orders(tmp, num);
109  w.resize(sz);
110  fill(w.begin(), w.end(), 0);
111  if (method == "average") {
112  for (uint c = 0, reps; c < num; c += reps) { reps = 1;
113  while (c + reps < num && p[tmp[c]] == p[tmp[c + reps]]) ++reps;
114  for (uint k = 0; k < reps; ++k)
115  w[tmp[c + k]] = S(2 * c + reps - 1) / 2 + 1;
116  }
117  } else if (method == "min") {
118  for (uint c = 0, reps; c < num; c += reps) { reps = 1;
119  while (c + reps < num && p[tmp[c]] == p[tmp[c + reps]]) ++reps;
120  for (uint k = 0; k < reps; ++k) w[tmp[c + k]] = c + 1;
121  }
122  } else if (method == "max") {
123  for (uint c = 0, reps; c < num; c += reps) { reps = 1;
124  while (c + reps < num && p[tmp[c]] == p[tmp[c + reps]]) ++reps;
125  for (uint k = 0; k < reps; ++k) w[tmp[c + k]] = c + reps;
126  }
127  } else // default
128  for (uint c = 0; c < num; ++c) w[tmp[c]] = c + 1;
129  }
130 
131 };
132 
133 template <class T, class S>
134 inline void rank(const vector<T>& v, vector<S>& w,
135  const string& method = "average")
136  { ranker<T, lt<T> > r(v); r.get_ranks(w, method); }
137 
138 template <class T, class S>
139 inline void rank(const T* d, uint size, vector<S>& w,
140  const string& method = "average")
141  { ranker<T, lt<T> > r(d, size); r.get_ranks(w, method); }
142 
143 template <class T, class S>
144 inline void partial_rank(const vector<T>& v, vector<S>& w, uint num,
145  const string& method = "average")
146  { ranker<T, lt<T> > r(v); r.get_partial_ranks(w, method, num); }
147 
148 template <class T, class S>
149 inline void partial_rank(const T* d, uint size, vector<S>& w, uint num,
150  const string& method = "average")
151  { ranker<T, lt<T> > r(d, size); r.get_partial_ranks(w, method, num); }
152 
153 template <class T, class S>
154 inline void order(const vector<T>& v, vector<S>& w)
155  { ranker<T, lt<T> > r(v); r.get_orders(w); }
156 
157 template <class T, class S>
158 inline void order(const T* d, uint size, vector<S>& w)
159  { ranker<T, lt<T> > r(d, size); r.get_orders(w); }
160 
161 template <class T, class S>
162 inline void partial_order(const vector<T>& v, vector<S>& w, uint num)
163  { ranker<T, lt<T> > r(v); r.get_partial_orders(w, num); }
164 
165 template <class T, class S>
166 inline void partial_order(const T* d, uint size, vector<S>& w, uint num)
167  { ranker<T, lt<T> > r(d, size); r.get_partial_orders(w, num); }
168 
169 template <class T, class S>
170 inline void rankhigh(const vector<T>& v, vector<S>& w,
171  const string& method = "average")
172  { ranker<T, gt<T> > r(v); r.get_ranks(w, method); }
173 
174 template <class T, class S>
175 inline void rankhigh(const T* d, uint size, vector<S>& w,
176  const string& method = "average")
177  { ranker<T, gt<T> > r(d, size); r.get_ranks(w, method); }
178 
179 template <class T, class S>
180 inline void partial_rankhigh(const vector<T>& v, vector<S>& w, uint num,
181  const string& method = "average")
182  { ranker<T, gt<T> > r(v); r.get_partial_ranks(w, method, num); }
183 
184 template <class T, class S>
185 inline void partial_rankhigh(const T* d, uint size, vector<S>& w, uint num,
186  const string& method = "average")
187  { ranker<T, gt<T> > r(d, size); r.get_partial_ranks(w, method, num); }
188 
189 template <class T, class S>
190 inline void orderhigh(const vector<T>& v, vector<S>& w)
191  { ranker<T, gt<T> > r(v); r.get_orders(w); }
192 
193 template <class T, class S>
194 inline void orderhigh(const T* d, uint size, vector<S>& w)
195  { ranker<T, gt<T> > r(d, size); r.get_orders(w); }
196 
197 template <class T, class S>
198 inline void partial_orderhigh(const vector<T>& v, vector<S>& w, uint num)
199  { ranker<T, gt<T> > r(v); r.get_partial_orders(w, num); }
200 
201 template <class T, class S>
202 inline void partial_orderhigh(const T* d, uint size, vector<S>& w, uint num)
203  { ranker<T, gt<T> > r(d, size); r.get_partial_orders(w, num); }
204 
205 template <class T>
206 inline T quantile(const T* d, const uint size, const double q)
207 {
208  if (size == 0) return T(0);
209  if (size == 1) return d[0];
210  if (q <= 0) return *std::min_element(d, d + size);
211  if (q >= 1) return *std::max_element(d, d + size);
212 
213  double pos = (size - 1) * q;
214  uint ind = uint(pos);
215  double delta = pos - ind;
216  vector<T> w(size); std::copy(d, d + size, w.begin());
217  std::nth_element(w.begin(), w.begin() + ind, w.end());
218  T i1 = *(w.begin() + ind);
219  T i2 = *std::min_element(w.begin() + ind + 1, w.end());
220  return (T) (i1 * (1.0 - delta) + i2 * delta);
221 }
222 
223 template <class T>
224 inline T quantile(const vector<T>& v, const double q)
225  { return quantile(&v[0], v.size(), q); }
226 
227 #endif
228 
T quantile(const T *d, const uint size, const double q)
Definition: ranker.h:206
int operator()(uint i1, uint i2) const
Definition: ranker.h:57
void get_partial_orders(vector< S > &w, uint num) const
Definition: ranker.h:69
Definition: ranker.h:47
ranker(const T *tp, uint s)
Definition: ranker.h:55
void partial_rankhigh(const vector< T > &v, vector< S > &w, uint num, const string &method="average")
Definition: ranker.h:180
ranker(const vector< T > &v)
Definition: ranker.h:54
void get_partial_ranks(vector< S > &w, const string &method, size_t num) const
Definition: ranker.h:105
Definition: ranker.h:44
void rankhigh(const vector< T > &v, vector< S > &w, const string &method="average")
Definition: ranker.h:170
void partial_order(const vector< T > &v, vector< S > &w, uint num)
Definition: ranker.h:162
const T * p
Definition: ranker.h:50
void rank(const vector< T > &v, vector< S > &w, const string &method="average")
Definition: ranker.h:134
void get_orders(vector< S > &w) const
Definition: ranker.h:60
uint sz
Definition: ranker.h:51
unsigned int uint
Definition: ranker.h:38
void order(const vector< T > &v, vector< S > &w)
Definition: ranker.h:154
void partial_orderhigh(const vector< T > &v, vector< S > &w, uint num)
Definition: ranker.h:198
static int compare(T a, T b)
Definition: ranker.h:44
void orderhigh(const vector< T > &v, vector< S > &w)
Definition: ranker.h:190
void get_ranks(vector< S > &w, const string &method) const
Definition: ranker.h:80
Definition: ranker.h:42
static int compare(T a, T b)
Definition: ranker.h:42
void partial_rank(const vector< T > &v, vector< S > &w, uint num, const string &method="average")
Definition: ranker.h:144


caltech_lanes
Author(s): Ricardo Morais
autogenerated on Mon Mar 2 2015 01:31:31