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 <iterator>
6#include <limits>
7#include <type_traits>
8
9#include <adiar/exception.h>
10#include <adiar/type_traits.h>
11#include <adiar/types.h>
12
18
19namespace adiar
20{
25
33
40 template <typename TypeSignature>
41 using function = std::function<TypeSignature>;
42
50 template <typename... Args>
52
63 template <typename Arg>
65
69 template <typename ValueType, typename OutputIt>
72 {
73 return [_iter = std::forward<OutputIt>(iter)](const ValueType& x) mutable { *(_iter++) = x; };
74 }
75
79 template <typename OutputIt,
80 typename = enable_if<
82 >>
85 {
86 using value_type = typename std::iterator_traits<std::remove_reference_t<OutputIt>>::value_type;
87 return make_consumer<value_type, OutputIt>(std::forward<OutputIt>(iter));
88 }
89
93 template <typename OutputIt,
94 typename = enable_if<
96 >>
99 {
100 using value_type = typename std::remove_reference_t<OutputIt>::container_type::value_type;
101 return make_consumer<value_type, OutputIt>(std::forward<OutputIt>(iter));
102 }
103
114 template <typename ForwardIt>
117 {
118 using value_type = typename ForwardIt::value_type;
119
120 return [&begin, &end](const value_type& x) {
121 if (begin == end) {
122 throw out_of_range("Iterator range unable to contain all generated values");
123 }
124 *(begin++) = x;
125 };
126 }
127
135 template <typename ForwardIt>
138 {
139 using value_type =
140 typename std::iterator_traits<std::remove_reference_t<ForwardIt>>::value_type;
141
142 return [_begin = std::forward<ForwardIt>(begin),
143 _end = std::forward<ForwardIt>(end)](const value_type& x) mutable {
144 if (_begin == _end) {
145 throw out_of_range("Iterator range unable to contain all generated values");
146 }
147 *(_begin++) = x;
148 };
149 }
150
157 template <typename VarType>
159
171 template <typename RetType>
173
177 template <typename ForwardIt>
180 {
181 using value_type = typename std::remove_reference_t<ForwardIt>::value_type;
182
183 return [_begin = std::forward<ForwardIt>(begin),
184 _end = std::forward<ForwardIt>(end)]() mutable -> optional<value_type> {
185 if (_begin == _end) { return {}; }
186 return *(_begin++);
187 };
188 }
189
193 template <typename Stream>
196 {
197 using value_type = typename Stream::value_type;
198
199 return [&s]() -> optional<value_type> {
200 if (!s.can_pull()) { return {}; }
201 return s.pull();
202 };
203 }
204
208 template <typename RetType>
209 inline generator<RetType>
211 {
212 return [=, _end = false]() mutable -> optional<RetType> {
213 if (_end) { return {}; }
214
215 _end = true;
216 return r;
217 };
218 }
219
222
225}
226
227#endif // ADIAR_FUNCTIONAL_H
function< optional< RetType >()> generator
Generator function that produces a new value of RetType for each call.
Definition functional.h:172
function< double(VarType)> cost
Cost function that assigns a cost to each variable.
Definition functional.h:158
function< void(Arg)> consumer
Consumer function of value of type Arg.
Definition functional.h:64
generator< typename std::remove_reference_t< ForwardIt >::value_type > make_generator(ForwardIt &&begin, ForwardIt &&end)
Wrap a begin and end iterator pair into a generator function.
Definition functional.h:179
std::function< TypeSignature > function
General-purpose polymorphic function wrapper.
Definition functional.h:41
function< bool(Args...)> predicate
Predicate function given value(s) of type(s) Args.
Definition functional.h:51
consumer< ValueType > make_consumer(OutputIt &&iter)
Wrap an iterator into a consumer function.
Definition functional.h:71
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