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<!is_void<
81 typename std::iterator_traits<std::remove_reference_t<OutputIt>>::value_type>>>
84 {
85 using value_type = typename std::iterator_traits<std::remove_reference_t<OutputIt>>::value_type;
86 return make_consumer<value_type, OutputIt>(std::forward<OutputIt>(iter));
87 }
88
92 template <typename OutputIt,
93 typename = enable_if<is_void<
94 typename std::iterator_traits<std::remove_reference_t<OutputIt>>::value_type>>>
97 {
98 using value_type = typename std::remove_reference_t<OutputIt>::container_type::value_type;
99 return make_consumer<value_type, OutputIt>(std::forward<OutputIt>(iter));
100 }
101
112 template <typename ForwardIt>
115 {
116 using value_type = typename ForwardIt::value_type;
117
118 return [&begin, &end](const value_type& x) {
119 if (begin == end) {
120 throw out_of_range("Iterator range unable to contain all generated values");
121 }
122 *(begin++) = x;
123 };
124 }
125
133 template <typename ForwardIt>
136 {
137 using value_type =
138 typename std::iterator_traits<std::remove_reference_t<ForwardIt>>::value_type;
139
140 return [_begin = std::forward<ForwardIt>(begin),
141 _end = std::forward<ForwardIt>(end)](const value_type& x) mutable {
142 if (_begin == _end) {
143 throw out_of_range("Iterator range unable to contain all generated values");
144 }
145 *(_begin++) = x;
146 };
147 }
148
155 template <typename VarType>
157
169 template <typename RetType>
171
175 template <typename ForwardIt>
178 {
179 using value_type = typename std::remove_reference_t<ForwardIt>::value_type;
180
181 return [_begin = std::forward<ForwardIt>(begin),
182 _end = std::forward<ForwardIt>(end)]() mutable -> optional<value_type> {
183 if (_begin == _end) { return {}; }
184 return *(_begin++);
185 };
186 }
187
191 template <typename Stream>
194 {
195 using value_type = typename Stream::value_type;
196
197 return [&s]() -> optional<value_type> {
198 if (!s.can_pull()) { return {}; }
199 return s.pull();
200 };
201 }
202
206 template <typename RetType>
207 inline generator<RetType>
209 {
210 return [=, _end = false]() mutable -> optional<RetType> {
211 if (_end) { return {}; }
212
213 _end = true;
214 return r;
215 };
216 }
217
220
223}
224
225#endif // ADIAR_FUNCTIONAL_H
function< optional< RetType >()> generator
Generator function that produces a new value of RetType for each call.
Definition functional.h:170
function< double(VarType)> cost
Cost function that assigns a cost to each variable.
Definition functional.h:156
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:177
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