Adiar 2.1.0
An External Memory Decision Diagram Library
Loading...
Searching...
No Matches
functional.h
1#ifndef ADIAR_FUNCTIONAL_H
2#define ADIAR_FUNCTIONAL_H
3
4#include <functional>
5#include <limits>
6
7#include <adiar/exception.h>
8#include <adiar/types.h>
9
15
16namespace adiar
17{
22
30
37 template <typename TypeSignature>
38 using function = std::function<TypeSignature>;
39
47 template <typename... Args>
49
59 template <typename Arg>
61
65 template <typename ValueType, typename OutputIt>
68 {
69 return [&iter](const ValueType& x) { *(iter++) = x; };
70 }
71
75 template <typename ValueType, typename OutputIt>
78 {
79 return [_iter = std::move(iter)](const ValueType& x) mutable { *(_iter++) = x; };
80 }
81
85 template <typename OutputIt>
88 {
89 using value_type = typename OutputIt::container_type::value_type;
90
91 return [&iter](const value_type& x) { *(iter++) = x; };
92 }
93
97 template <typename OutputIt>
100 {
101 using value_type = typename OutputIt::container_type::value_type;
102
103 return [_iter = std::move(iter)](const value_type& x) mutable { *(_iter++) = x; };
104 }
105
113 template <typename ForwardIt>
116 {
117 using value_type = typename ForwardIt::value_type;
118
119 return [&begin, &end](const value_type& x) {
120 if (begin == end) {
121 throw out_of_range("Iterator range unable to contain all generated values");
122 }
123 *(begin++) = x;
124 };
125 }
126
134 template <typename ForwardIt>
137 {
138 using value_type = typename ForwardIt::value_type;
139
140 return [_begin = std::move(begin), _end = std::move(end)](const value_type& x) mutable {
141 if (_begin == _end) {
142 throw out_of_range("Iterator range unable to contain all generated values");
143 }
144 *(_begin++) = x;
145 };
146 }
147
154 template <typename VarType>
156
168 template <typename RetType>
170
174 template <typename ForwardIt>
177 {
178 using value_type = typename ForwardIt::value_type;
179
180 return [&begin, &end]() -> optional<value_type> {
181 if (begin == end) { return {}; }
182 return *(begin++);
183 };
184 }
185
189 template <typename ForwardIt>
192 {
193 using value_type = typename ForwardIt::value_type;
194
195 return [_begin = std::move(begin), _end = std::move(end)]() mutable -> optional<value_type> {
196 if (_begin == _end) { return {}; }
197 return *(_begin++);
198 };
199 }
200
204 template <typename Stream>
207 {
208 using value_type = typename Stream::value_type;
209
210 return [&s]() -> optional<value_type> {
211 if (!s.can_pull()) { return {}; }
212 return s.pull();
213 };
214 }
215
219 template <typename RetType>
220 inline generator<RetType>
222 {
223 return [=, _end = false]() mutable -> optional<RetType> {
224 if (_end) { return {}; }
225
226 _end = true;
227 return r;
228 };
229 }
230
233
236}
237
238#endif // ADIAR_FUNCTIONAL_H
function< optional< RetType >()> generator
Generator function that produces a new value of RetType for each call.
Definition functional.h:169
function< double(VarType)> cost
Cost function that assigns a cost to each variable.
Definition functional.h:155
function< void(Arg)> consumer
Consumer function of value(s) of type(s) Args.
Definition functional.h:60
generator< typename ForwardIt::value_type > make_generator(ForwardIt &begin, ForwardIt &end)
Wrap a begin and end iterator pair into a generator function.
Definition functional.h:176
std::function< TypeSignature > function
General-purpose polymorphic function wrapper.
Definition functional.h:38
function< bool(Args...)> predicate
Predicate function given value(s) of type(s) Args.
Definition functional.h:48
consumer< ValueType > make_consumer(OutputIt &iter)
Wrap an iterator into a consumer function.
Definition functional.h:67
Core types.
Definition adiar.h:40
std::out_of_range out_of_range
Attempt to access elements outside of the given range.
Definition exception.h:43
std::optional< T > optional
An optional value, i.e. a possibly existent value.
Definition types.h:81