1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_UTIL_SELECT_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_UTIL_SELECT_H
23template <
class X,
typename value_type,
24 typename std::enable_if<std::is_compound<value_type>::value, value_type>::type* =
nullptr>
25auto select(
size_t n,
const X& x,
bool max =
false,
bool ignore_sign =
false) {
26 throw std::logic_error(
"select not implemented for complex types");
27 return std::map<size_t, value_type>();
29template <
class X,
typename value_type,
30 typename std::enable_if<!std::is_compound<value_type>::value, value_type>::type* =
nullptr>
31auto select(
size_t n,
const X& x,
bool max =
false,
bool ignore_sign =
false) {
36 using select_pair = std::pair<value_type, size_t>;
37 auto selection = std::priority_queue<select_pair, std::vector<select_pair>, greater<select_pair>>();
39 for (
size_t i = 0; i < n; ++i, ++ix) {
40 selection.emplace(max ? (ignore_sign ? abs((*ix)) : (*ix)) : (ignore_sign ? -abs((*ix)) : -(*ix)), i);
42 for (
size_t i = n; i < x.size(); ++i, ++ix) {
43 selection.emplace(max ? (ignore_sign ? abs((*ix)) : (*ix)) : (ignore_sign ? -abs((*ix)) : -(*ix)), i);
46 auto selection_map = std::map<size_t, value_type>();
47 auto m = selection.size();
48 for (
size_t i = 0; i < m; ++i) {
49 selection_map.emplace(selection.top().second, selection.top().first);
53 for (
auto& e : selection_map)
111template <
class X,
typename value_type>
112auto select_sparse(
size_t n,
const X& x,
bool max =
false,
bool ignore_sign =
false) {
117 using select_pair = std::pair<value_type, size_t>;
118 auto selection = std::priority_queue<select_pair, std::vector<select_pair>, greater<select_pair>>();
120 while (selection.size() < n && ix !=
end(x)) {
121 selection.emplace(max ? (ignore_sign ? abs((ix->second)) : (ix->second))
122 : (ignore_sign ? -abs((ix->second)) : -(ix->second)),
126 while (ix !=
end(x)) {
127 selection.emplace(max ? (ignore_sign ? abs((ix->second)) : (ix->second))
128 : (ignore_sign ? -abs((ix->second)) : -(ix->second)),
133 auto selection_map = std::map<size_t, value_type>();
134 auto m = selection.size();
135 for (
size_t i = 0; i < m; ++i) {
136 selection_map.emplace(selection.top().second, selection.top().first);
140 for (
auto& e : selection_map)
141 e.second = -e.second;
142 return selection_map;
auto begin(Span< T > &x)
Definition: Span.h:86
auto end(Span< T > &x)
Definition: Span.h:96
Definition: ArrayHandler.h:23
auto select(size_t n, const X &x, bool max=false, bool ignore_sign=false)
Select n indices with largest (or smallest) actual (or absolute) value.
Definition: select.h:25
auto select_sparse(size_t n, const X &x, bool max=false, bool ignore_sign=false)
Select n indices with largest (or smallest) actual (or absolute) value.
Definition: select.h:112