1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_UTIL_SELECT_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_UTIL_SELECT_H
24template <
class X,
typename value_type,
25 typename std::enable_if<std::is_compound<value_type>::value, value_type>::type* =
nullptr>
26std::map<size_t, value_type>
select(
size_t n,
const X& x,
bool max =
false,
bool ignore_sign =
false) {
28 throw std::logic_error(
"select not implemented for complex types");
30template <
class X,
typename value_type,
31 typename std::enable_if<!std::is_compound<value_type>::value, value_type>::type* =
nullptr>
32auto select(
size_t n,
const X& x,
bool max =
false,
bool ignore_sign =
false) {
37 using select_pair = std::pair<value_type, size_t>;
38 auto selection = std::priority_queue<select_pair, std::vector<select_pair>, greater<select_pair>>();
40 for (
size_t i = 0; i < n; ++i, ++ix) {
41 selection.emplace(max ? (ignore_sign ? abs((*ix)) : (*ix)) : (ignore_sign ? -abs((*ix)) : -(*ix)), i);
43 for (
size_t i = n; i < x.size(); ++i, ++ix) {
44 selection.emplace(max ? (ignore_sign ? abs((*ix)) : (*ix)) : (ignore_sign ? -abs((*ix)) : -(*ix)), i);
47 auto selection_map = std::map<size_t, value_type>();
48 auto m = selection.size();
49 for (
size_t i = 0; i < m; ++i) {
50 selection_map.emplace(selection.top().second, selection.top().first);
54 for (
auto& e : selection_map)
112template <
class X,
typename value_type,
113 typename std::enable_if<std::is_compound<value_type>::value, value_type>::type* =
nullptr>
114std::map<size_t, value_type>
select_sparse(
size_t n,
const X& x,
bool max =
false,
bool ignore_sign =
false) {
116 throw std::logic_error(
"select_sparse not implemented for complex types");
119template <
class X,
typename value_type,
120 typename std::enable_if<!std::is_compound<value_type>::value, value_type>::type* =
nullptr>
121auto select_sparse(
size_t n,
const X& x,
bool max =
false,
bool ignore_sign =
false) {
126 using select_pair = std::pair<value_type, size_t>;
127 auto selection = std::priority_queue<select_pair, std::vector<select_pair>, greater<select_pair>>();
129 while (selection.size() < n && ix !=
end(x)) {
130 selection.emplace(max ? (ignore_sign ? abs((ix->second)) : (ix->second))
131 : (ignore_sign ? -abs((ix->second)) : -(ix->second)),
135 while (ix !=
end(x)) {
136 selection.emplace(max ? (ignore_sign ? abs((ix->second)) : (ix->second))
137 : (ignore_sign ? -abs((ix->second)) : -(ix->second)),
142 auto selection_map = std::map<size_t, value_type>();
143 auto m = selection.size();
144 for (
size_t i = 0; i < m; ++i) {
145 selection_map.emplace(selection.top().second, selection.top().first);
149 for (
auto& e : selection_map)
150 e.second = -e.second;
151 return selection_map;
auto begin(Span< T > &x)
Definition Span.h:87
auto end(Span< T > &x)
Definition Span.h:97
Definition ArrayHandler.h:24
std::map< size_t, value_type > 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:114
std::map< size_t, value_type > 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:26