2#ifndef ANCHORS_ENGINE_H
3#define ANCHORS_ENGINE_H
10#include <unordered_set>
86 using min_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
92 void observeNode(std::shared_ptr<AnchorBase>& current,
93 std::unordered_set<std::shared_ptr<AnchorBase>>&);
97 void unobserveNode(std::shared_ptr<AnchorBase>& current);
103 int d_stabilizationNumber;
107 std::unordered_set<std::shared_ptr<AnchorBase>> d_observedNodes;
110 min_heap<std::shared_ptr<AnchorBase>> d_recomputeHeap;
114 std::unordered_set<std::shared_ptr<AnchorBase>> d_recomputeSet;
124 if (d_observedNodes.contains(anchor)) {
128 return anchor->get();
133 T oldVal = anchor->get();
135 if (oldVal == val)
return;
136 d_stabilizationNumber++;
138 anchor->setChangeId(d_stabilizationNumber);
141 if (anchor->isNecessary()) {
142 for (
const auto& dependant : anchor->getDependants()) {
143 if (dependant->isNecessary() &&
144 !d_recomputeSet.contains(dependant)) {
145 d_recomputeHeap.push(dependant);
146 d_recomputeSet.insert(dependant);
154 if (d_observedNodes.contains(anchor)) {
158 d_observedNodes.insert(anchor);
160 std::unordered_set<std::shared_ptr<AnchorBase>> visited;
161 std::shared_ptr<AnchorBase> anchorBase = anchor;
162 observeNode(anchorBase, visited);
174 if (!d_observedNodes.contains(anchor)) {
178 d_observedNodes.erase(anchor);
180 std::shared_ptr<AnchorBase> anchorBase = anchor;
181 unobserveNode(anchorBase);
Engine is the brain of Anchors, containing the necessary functions and data to retrieve the value of ...
Definition: engine.h:19
Engine()
Creates an instance of the Engine class.
void set(AnchorPtr< T > &anchor, T val)
Sets the value of the given Anchor.
Definition: engine.h:132
void observe(AnchorPtr< T > &anchor)
Marks an Anchor as observed.
Definition: engine.h:153
T get(const AnchorPtr< T > &anchor)
Returns the value of the given Anchor.
Definition: engine.h:123
void unobserve(AnchorPtr< T > &anchor)
Marks an Anchor as unobserved.
Definition: engine.h:173
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