1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_UTIL_SELECT_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_UTIL_SELECT_H
22template <
class X,
typename value_type,
23 typename std::enable_if<std::is_compound<value_type>::value, value_type>::type* =
nullptr>
24auto select(
size_t n,
const X& x,
bool max =
false,
bool ignore_sign =
false) {
25 throw std::logic_error(
"select not implemented for complex types");
26 return std::map<size_t, value_type>();
28template <
class X,
typename value_type,
29 typename std::enable_if<!std::is_compound<value_type>::value, value_type>::type* =
nullptr>
30auto select(
size_t n,
const X& x,
bool max =
false,
bool ignore_sign =
false) {
35 using select_pair = std::pair<value_type, size_t>;
36 auto selection = std::priority_queue<select_pair, std::vector<select_pair>, greater<select_pair>>();
38 for (
size_t i = 0; i < n; ++i, ++ix) {
39 selection.emplace(max ? (ignore_sign ? abs((*ix)) : (*ix)) : (ignore_sign ? -abs((*ix)) : -(*ix)), i);
41 for (
size_t i = n; i < x.size(); ++i, ++ix) {
42 selection.emplace(max ? (ignore_sign ? abs((*ix)) : (*ix)) : (ignore_sign ? -abs((*ix)) : -(*ix)), i);
45 auto selection_map = std::map<size_t, value_type>();
46 auto m = selection.size();
47 for (
size_t i = 0; i < m; ++i) {
48 selection_map.emplace(selection.top().second, selection.top().first);
52 for (
auto& e : selection_map)
110template <
class X,
typename value_type>
111auto select_sparse(
size_t n,
const X& x,
bool max =
false,
bool ignore_sign =
false) {
116 using select_pair = std::pair<value_type, size_t>;
117 auto selection = std::priority_queue<select_pair, std::vector<select_pair>, greater<select_pair>>();
119 while (selection.size() < n && ix !=
end(x)) {
120 selection.emplace(max ? (ignore_sign ? abs((ix->second)) : (ix->second))
121 : (ignore_sign ? -abs((ix->second)) : -(ix->second)),
125 while (ix !=
end(x)) {
126 selection.emplace(max ? (ignore_sign ? abs((ix->second)) : (ix->second))
127 : (ignore_sign ? -abs((ix->second)) : -(ix->second)),
132 auto selection_map = std::map<size_t, value_type>();
133 auto m = selection.size();
134 for (
size_t i = 0; i < m; ++i) {
135 selection_map.emplace(selection.top().second, selection.top().first);
139 for (
auto& e : selection_map)
140 e.second = -e.second;
141 return selection_map;
auto begin(Span< T > &x)
Definition: Span.h:84
auto end(Span< T > &x)
Definition: Span.h:94
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:24
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:111