Latin Hypercube Samples (lhs)  1.0
R, C++, and Rcpp code to generate Latin hypercube samples
All Classes Namespaces Files Functions Variables Typedefs Macros Pages
order.h
Go to the documentation of this file.
1 
21 #ifndef ORDER_H
22 #define ORDER_H
23 
24 #include <vector>
25 #include <algorithm>
26 
27 namespace bclib
28 {
35  template <class T>
36  bool findranksCompare(const std::pair<T, int> first, const std::pair<T, int> second)
37  {
38  return (first.first < second.first);
39  }
40 
47  template <class T>
48  void findorder_zero(const std::vector<T> & v, std::vector<int> & order)
49  {
50  // create a vector of pairs to hold the value and the integer rank
51  std::vector<std::pair<T, int> > p(v.size());
52 
53  typename std::vector<T>::const_iterator vi;
54  typename std::vector<std::pair<T, int> >::iterator pi;
55  int position = 0;
56  for (vi = v.begin(), pi = p.begin();
57  vi != v.end() && pi != p.end(); ++vi, ++pi)
58  {
59  *pi = std::pair<T, int>(*vi, position);
60  position++;
61  }
62 
63  // if the rank vector is not the right size, resize it (the original values may be lost)
64  if (order.size() != v.size())
65  {
66  order.resize(v.size());
67  }
68 
69  // sort the pairs of values
70  std::sort(p.begin(), p.end(), findranksCompare<double>);
71 
72  // take the ranks from the pairs and put them in the rank vector
73  std::vector<int>::iterator oi;
74  for (oi = order.begin(), pi = p.begin();
75  oi != order.end() && pi != p.end(); ++oi, ++pi)
76  {
77  *oi = pi->second;
78  //order[i] = p[i].second;
79  }
80  }
81 
88  template <class T>
89  void findorder(const std::vector<T> & v, std::vector<int> & order)
90  {
91  findorder_zero<T>(v, order);
92  for (std::vector<int>::size_type i = 0; i < order.size(); i++)
93  {
94  order[i] += 1;
95  }
96  }
97 } // end namespace
98 
99 #endif /* ORDER_H */
100 
bclib::findorder
void findorder(const std::vector< T > &v, std::vector< int > &order)
Definition: order.h:89
bclib::findranksCompare
bool findranksCompare(const std::pair< T, int > first, const std::pair< T, int > second)
Definition: order.h:36