Automatic Differentiation
 
Loading...
Searching...
No Matches
sort_indices.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_SORT_INDICES_HPP
2#define STAN_MATH_PRIM_FUN_SORT_INDICES_HPP
3
7#include <algorithm>
8#include <vector>
9
10namespace stan {
11namespace math {
12
20namespace internal {
21template <bool ascending, typename C>
23 const C& xs_;
24
25 public:
32 explicit index_comparator(const C& xs) : xs_(xs) {}
33
42 bool operator()(int i, int j) const {
43 if (ascending) {
44 return xs_[i - 1] < xs_[j - 1];
45 } else {
46 return xs_[i - 1] > xs_[j - 1];
47 }
48 }
49};
50
51} // namespace internal
52
63template <bool ascending, typename C>
64std::vector<int> sort_indices(const C& xs) {
65 using idx_t = index_type_t<C>;
66 idx_t xs_size = xs.size();
67 std::vector<int> idxs;
68 idxs.resize(xs_size);
69 for (idx_t i = 0; i < xs_size; ++i) {
70 idxs[i] = i + 1;
71 }
73 std::sort(idxs.begin(), idxs.end(), comparator);
74 return idxs;
75}
76
77} // namespace math
78} // namespace stan
79
80#endif
index_comparator(const C &xs)
Construct an index comparator holding a reference to the specified container.
bool operator()(int i, int j) const
Return true if the value at the first index is sorted in front of the value at the second index; this...
typename index_type< T >::type index_type_t
std::vector< int > sort_indices(const C &xs)
Return an integer array of indices of the specified container sorting the values in ascending or desc...
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9