| Stan Math Library 2.6.3 probability, sampling & optimization

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
sort_indices.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_SORT_INDICES_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_SORT_INDICES_HPP
3
4 #include <stan/math/prim/mat/fun/Eigen.hpp>
5 #include <stan/math/prim/mat/meta/index_type.hpp>
6 #include <stan/math/prim/arr/meta/index_type.hpp>
7 #include <algorithm> // std::sort
8 #include <iostream>
9 #include <vector>
10
11 namespace stan {
12
13 namespace math {
14
22 namespace {
23 template <bool ascending, typename C>
24 class index_comparator {
25 const C& xs_;
26
27 public:
34 explicit index_comparator(const C& xs) : xs_(xs) { }
35
44 bool operator()(int i, int j) const {
45 if (ascending)
46 return xs_[i-1] < xs_[j-1];
47 else
48 return xs_[i-1] > xs_[j-1];
49 }
50 };
51
52
63 template <bool ascending, typename C>
64 std::vector<int> sort_indices(const C& xs) {
65 typedef typename index_type<C>::type idx_t;
66 idx_t size = xs.size();
67 std::vector<int> idxs;
68 idxs.resize(size);
69 for (idx_t i = 0; i < size; ++i)
70 idxs[i] = i + 1;
71 index_comparator<ascending, C> comparator(xs);
72 std::sort(idxs.begin(), idxs.end(), comparator);
73 return idxs;
74 }
75
76 }
77
78 }
79 }
80 #endif
index_type.hpp
index_type.hpp
Eigen.hpp
stan::math::size
int size(const std::vector< T > &x)
Definition: size.hpp:11
xs_
const C & xs_
Definition: sort_indices.hpp:25

[ Stan Home Page ] © 2011–2015, Stan Development Team.