iterative-solver 0.0
wrap.h
1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_WRAP_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_WRAP_H
3#include <functional>
4#include <iterator>
5#include <type_traits>
6#include <vector>
7
9
10template <class A>
11using VecRef = std::vector<std::reference_wrapper<A>>;
12
13template <class A>
14using CVecRef = std::vector<std::reference_wrapper<const A>>;
15
17template <class T>
18struct decay {
19 using type = std::decay_t<T>;
20};
21
22template <class T>
23struct decay<std::reference_wrapper<T>> {
24 using type = std::decay_t<T>;
25};
26
27template <class T>
28using decay_t = typename decay<T>::type;
29
31template <class ForwardIt>
32auto wrap(ForwardIt begin, ForwardIt end) {
33 using T = decay_t<decay_t<decltype(*begin)>>;
34 auto w = VecRef<T>{};
35 for (auto it = begin; it != end; ++it)
36 w.push_back(std::ref(*it));
37 return w;
38}
39
41template <class ForwardIt>
42auto const_cast_wrap(ForwardIt begin, ForwardIt end) {
43 using T = decay_t<decay_t<decltype(*begin)>>;
44 auto w = VecRef<T>{};
45 for (auto it = begin; it != end; ++it)
46 w.push_back(std::ref(const_cast<decay_t<T>&>(it->get())));
47 return w;
48}
49
51template <class ForwardIt>
52auto cwrap(ForwardIt begin, ForwardIt end) {
53 using T = decay_t<decay_t<decltype(*begin)>>;
54 auto w = CVecRef<T>{};
55 for (auto it = begin; it != end; ++it)
56 w.push_back(std::cref(*it));
57 return w;
58}
59
67template <class IterableContainer>
68auto wrap(const IterableContainer& parameters) {
69 using T = typename IterableContainer::value_type;
70 using std::begin;
71 using std::end;
72 auto w = CVecRef<decay_t<T>>{};
73 for (auto it = begin(parameters); it != end(parameters); ++it)
74 w.emplace_back(std::cref(*it));
75 return w;
76}
77
85template <class IterableContainer>
86auto wrap(IterableContainer& parameters) {
87 using T = typename IterableContainer::value_type;
88 using std::begin;
89 using std::end;
90 auto w = VecRef<decay_t<T>>{};
91 for (auto it = begin(parameters); it != end(parameters); ++it)
92 w.emplace_back(std::ref(*it));
93 return w;
94}
102template <class IterableContainer>
103auto const_cast_wrap(IterableContainer& parameters) {
104 using T = typename IterableContainer::value_type;
105 using std::begin;
106 using std::end;
107 auto w = VecRef<decay_t<T>>{};
108 for (auto it = begin(parameters); it != end(parameters); ++it)
109 w.emplace_back(std::ref(const_cast<decay_t<T>&>(it->get())));
110 return w;
111}
112
120template <class IterableContainer>
121auto cwrap(IterableContainer& parameters) {
122 using T = typename IterableContainer::value_type;
123 using std::begin;
124 using std::end;
125 auto w = CVecRef<decay_t<T>>{};
126 for (auto it = begin(parameters); it != end(parameters); ++it)
127 w.emplace_back(std::cref(*it));
128 return w;
129}
130
134template <typename T, typename... S>
135auto wrap_arg(T&& arg, S&&... args)
136 -> std::enable_if_t<std::conjunction_v<std::is_same<decay_t<T>, decay_t<S>>...>, VecRef<decay_t<T>>> {
137 using std::begin;
138 using std::end;
139 auto w = VecRef<decay_t<T>>{};
140 w.emplace_back(std::ref(arg));
141 (w.emplace_back(std::ref(args)), ...);
142 return w;
143}
144
148template <typename T, typename... S>
149auto cwrap_arg(T&& arg, S&&... args)
150 -> std::enable_if_t<std::conjunction_v<std::is_same<decay_t<T>, decay_t<S>>...>, CVecRef<decay_t<T>>> {
151 using std::begin;
152 using std::end;
153 auto w = CVecRef<decay_t<T>>{};
154 w.emplace_back(std::cref(arg));
155 (w.emplace_back(std::cref(args)), ...);
156 return w;
157}
158
159} // namespace molpro::linalg::itsolv
160#endif // LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_WRAP_H
4-parameter interpolation of a 1-dimensional function given two points for which function values and ...
Definition: helper.h:10
auto cwrap(ForwardIt begin, ForwardIt end)
Takes a begin and end iterators and returns a vector of references to each element.
Definition: wrap.h:52
auto wrap_arg(T &&arg, S &&... args) -> std::enable_if_t< std::conjunction_v< std::is_same< decay_t< T >, decay_t< S > >... >, VecRef< decay_t< T > > >
Constructs a vector of reference wrappers with provided arguments.
Definition: wrap.h:135
typename decay< T >::type decay_t
Definition: wrap.h:28
auto cwrap_arg(T &&arg, S &&... args) -> std::enable_if_t< std::conjunction_v< std::is_same< decay_t< T >, decay_t< S > >... >, CVecRef< decay_t< T > > >
Constructs a vector of const reference wrappers with provided arguments.
Definition: wrap.h:149
auto wrap(ForwardIt begin, ForwardIt end)
Takes a begin and end iterators and returns a vector of references to each element.
Definition: wrap.h:32
std::vector< std::reference_wrapper< const A > > CVecRef
Definition: wrap.h:14
auto const_cast_wrap(ForwardIt begin, ForwardIt end)
Takes a begin and end iterators and returns a vector of const-casted references to each element.
Definition: wrap.h:42
std::vector< std::reference_wrapper< A > > VecRef
Definition: wrap.h:11
Decays CV and reference qualifiers same as std::decay, but also decays std::reference_wrapper to its ...
Definition: wrap.h:18
std::decay_t< T > type
Definition: wrap.h:19