Anchors
C++ library for Incremental Computing
anchorutil.h
1// anchorutil.h
2#ifndef ANCHORS_ANCHORS_H
3#define ANCHORS_ANCHORS_H
4
5#include "anchor.h"
6
10namespace anchors {
14template <typename T>
15using AnchorPtr = std::shared_ptr<AnchorWrap<T>>;
16
24class Anchors {
25 public:
34 template <typename T>
35 static AnchorPtr<T> create(const T &value);
36
48 template <typename T, typename InputType1 = T>
49 static AnchorPtr<T> map(
50 const AnchorPtr<InputType1> &anchor,
51 const typename Anchor<T, InputType1>::SingleInputUpdater &updater);
52
67 template <typename T, typename InputType1 = T, typename InputType2 = T>
68 static AnchorPtr<T> map2(
69 const AnchorPtr<InputType1> &anchor1,
70 const AnchorPtr<InputType2> &anchor2,
72 &updater);
73
90 template <typename T,
91 typename InputType1 = T,
92 typename InputType2 = T,
93 typename InputType3 = T>
94 static AnchorPtr<T> map3(
95 const AnchorPtr<InputType1> &anchor1,
96 const AnchorPtr<InputType2> &anchor2,
97 const AnchorPtr<InputType3> &anchor3,
98 const std::function<T(InputType1 &, InputType2 &, InputType3 &)>
99 &updater);
100
120 template <typename T,
121 typename InputType1 = T,
122 typename InputType2 = T,
123 typename InputType3 = T,
124 typename InputType4 = T>
125 static AnchorPtr<T> map4(
126 const AnchorPtr<InputType1> &anchor1,
127 const AnchorPtr<InputType2> &anchor2,
128 const AnchorPtr<InputType3> &anchor3,
129 const AnchorPtr<InputType4> &anchor4,
130 const std::function<
131 T(InputType1 &, InputType2 &, InputType3 &, InputType4 &)>
132 &updater);
133};
134
135template <typename T>
137 AnchorPtr<T> newAnchor(std::make_shared<Anchor<T>>(value));
138
139 return newAnchor;
140}
141
142template <typename T, typename InputType1>
144 const AnchorPtr<InputType1> &anchor,
145 const typename Anchor<T, InputType1>::SingleInputUpdater &updater) {
146 AnchorPtr<T> newAnchor(
147 std::make_shared<Anchor<T, InputType1>>(anchor, updater));
148
149 return newAnchor;
150}
151
152template <typename T, typename InputType1, typename InputType2>
154 const AnchorPtr<InputType1> &anchor1,
155 const AnchorPtr<InputType2> &anchor2,
157 &updater) {
158 AnchorPtr<T> newAnchor(std::make_shared<Anchor<T, InputType1, InputType2>>(
159 anchor1, anchor2, updater));
160
161 return newAnchor;
162}
163
164template <typename T,
165 typename InputType1,
166 typename InputType2,
167 typename InputType3>
169 const AnchorPtr<InputType1> &anchor1,
170 const AnchorPtr<InputType2> &anchor2,
171 const AnchorPtr<InputType3> &anchor3,
172 const std::function<T(InputType1 &, InputType2 &, InputType3 &)> &updater) {
173 using PairType = std::pair<InputType1, InputType2>;
174
175 const auto &anchorOfPair(map2<PairType, InputType1, InputType2>(
176 anchor1, anchor2, [](InputType1 t1, InputType2 t2) {
177 return std::make_pair(t1, t2);
178 }));
179
180 const auto &newUpdater = [updater](PairType &pair, InputType3 &anchor3) {
181 return updater(pair.first, pair.second, anchor3);
182 };
183
184 return map2<T, PairType, InputType3>(anchorOfPair, anchor3, newUpdater);
185}
186
187template <typename T,
188 typename InputType1,
189 typename InputType2,
190 typename InputType3,
191 typename InputType4>
193 const AnchorPtr<InputType1> &anchor1,
194 const AnchorPtr<InputType2> &anchor2,
195 const AnchorPtr<InputType3> &anchor3,
196 const AnchorPtr<InputType4> &anchor4,
197 const std::function<
198 T(InputType1 &, InputType2 &, InputType3 &, InputType4 &)> &updater) {
199 using PairType1 = std::pair<InputType1, InputType2>;
200 using PairType2 = std::pair<InputType3, InputType4>;
201
202 const auto &anchorOfPair1(map2<PairType1, InputType1, InputType2>(
203 anchor1, anchor2, [](InputType1 t1, InputType2 t2) {
204 return std::make_pair(t1, t2);
205 }));
206
207 const auto &anchorOfPair2(map2<PairType2, InputType3, InputType4>(
208 anchor3, anchor4, [](InputType1 t1, InputType2 t2) {
209 return std::make_pair(t1, t2);
210 }));
211
212 const auto &newUpdater = [updater](PairType1 &firstPair,
213 PairType2 &secondPair) {
214 return updater(firstPair.first,
215 firstPair.second,
216 secondPair.first,
217 secondPair.second);
218 };
219
220 return map2<T, PairType1, PairType2>(
221 anchorOfPair1, anchorOfPair2, newUpdater);
222}
223
224} // namespace anchors
225#endif // ANCHORS_ANCHORS_H
A single node in the computation graph containing a value.
Definition: anchor.h:59
std::function< T(InputType1 &, InputType2 &)> DualInputUpdater
Alias for function that accepts inputs of type InputType1 and InputType2 and returns a value of type ...
Definition: anchor.h:71
std::function< T(InputType1 &)> SingleInputUpdater
Alias for function that accepts an input of type InputType1 and returns a value of type T.
Definition: anchor.h:65
Anchors is an utility class containing functions to simplify creating a shared pointer to an Anchor,...
Definition: anchorutil.h:24
static AnchorPtr< T > map3(const AnchorPtr< InputType1 > &anchor1, const AnchorPtr< InputType2 > &anchor2, const AnchorPtr< InputType3 > &anchor3, const std::function< T(InputType1 &, InputType2 &, InputType3 &)> &updater)
Creates an Anchor from three input Anchors.
Definition: anchorutil.h:168
static AnchorPtr< T > map(const AnchorPtr< InputType1 > &anchor, const typename Anchor< T, InputType1 >::SingleInputUpdater &updater)
Creates an Anchor from an input Anchor.
Definition: anchorutil.h:143
static AnchorPtr< T > map2(const AnchorPtr< InputType1 > &anchor1, const AnchorPtr< InputType2 > &anchor2, const typename Anchor< T, InputType1, InputType2 >::DualInputUpdater &updater)
Creates an Anchor from two input Anchors.
Definition: anchorutil.h:153
static AnchorPtr< T > create(const T &value)
Creates an Anchor containing the given value.
Definition: anchorutil.h:136
static AnchorPtr< T > map4(const AnchorPtr< InputType1 > &anchor1, const AnchorPtr< InputType2 > &anchor2, const AnchorPtr< InputType3 > &anchor3, const AnchorPtr< InputType4 > &anchor4, const std::function< T(InputType1 &, InputType2 &, InputType3 &, InputType4 &)> &updater)
Creates an Anchor from four input Anchors.
Definition: anchorutil.h:192
Main library namespace.
Definition: anchor.h:32
std::shared_ptr< AnchorWrap< T > > AnchorPtr
Alias representing a shared pointer to an Anchor of output type T.
Definition: anchorutil.h:15