1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
// Copyright 2012, Matthias Andreas Benkard.
//
// This program is free software: you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program. If not, see
// <http://www.gnu.org/licenses/>.
#pragma once
#ifndef CELLS_IMPL_HPP
#define CELLS_IMPL_HPP
#include "cells.hpp"
#include "dynvars.hpp"
#include <exception>
#include <stdexcept>
#include <functional>
#include <memory>
#include <unordered_map>
#include <unordered_set>
#include <forward_list>
#include <deque>
#ifndef thread_local
//#define thread_local __thread
#define thread_local
#endif
namespace cells {
using std::cout;
using std::cerr;
using std::endl;
using namespace dynvars;
struct dag_node {
dag_node(observer* item_) : item(item_) { }
observer* item;
std::unordered_set<dag_node*> incoming_edges;
std::unordered_set<dag_node*> outgoing_edges;
};
struct transaction {
std::unordered_map<observer*, dag_node*> dag;
};
static thread_local dynvar<std::forward_list<observer*>> current_dependencies;
static thread_local dynvar<transaction> current_transaction;
inline observer::observer() : self(std::make_shared<observer*>(this)) { };
inline observer::observer(observer const& other) : self(std::make_shared<observer*>(this)) {
reset_dependents(other.dependents);
reset_dependencies(other.dependencies);
}
inline observer& observer::operator =(observer const& other) {
reset_dependents(other.dependents);
reset_dependencies(other.dependencies);
return *this;
}
inline void observer::reset_dependents(std::list<std::weak_ptr<observer*>> const& new_dependents) {
dependents = new_dependents;
}
inline void observer::clear_dependencies() {
for (auto const& dep : dependencies) {
std::shared_ptr<observer*> sdep = dep.lock();
if (sdep) {
(*sdep)->remove_dependent(this);
}
}
dependencies = {};
}
inline void observer::add_dependent(observer* dependent) {
dependents.push_front(dependent->self);
}
inline void observer::remove_dependent(observer* dependent) {
dependents.remove_if([&](std::weak_ptr<observer*> const& other) -> bool {
std::shared_ptr<observer*> other2 = other.lock();
return (!other2 || *other2 == dependent);
});
}
inline void observer::reset_dependencies(std::forward_list<std::weak_ptr<observer*>> const& newdeps) {
clear_dependencies();
dependencies = newdeps;
for (auto const& dep : newdeps) {
shared_ptr<observer*> p = dep.lock();
if (p) {
(*p)->add_dependent(this);
}
}
}
inline void observer::reset_dependencies(std::forward_list<observer*> const& newdeps) {
clear_dependencies();
for (auto const& dep : newdeps) {
dependencies.push_front(dep->self);
dep->add_dependent(this);
}
}
inline void observer::mark() {
if (!current_transaction) {
with_transaction([&]() { this->mark(); });
return;
}
if (current_transaction->dag.find(this) ==
current_transaction->dag.end()) {
std::unordered_set<dag_node*> incoming_edges;
std::unordered_set<dag_node*> outgoing_edges;
dag_node* node = new dag_node(this);
current_transaction->dag[this] = node;
for (auto e = dependents.cbegin(); e != dependents.end(); e++) {
std::weak_ptr<observer*> const& x = *e;
std::shared_ptr<observer*> px = x.lock();
if (px) {
(*px)->mark();
dag_node* xn = current_transaction->dag[*px];
xn->incoming_edges.insert(node);
node->outgoing_edges.insert(xn);
} else {
// Purge dead nodes.
dependents.erase(e);
}
}
}
}
inline observer::~observer() {
clear_dependencies();
}
template <typename T>
cell<T>::cell() {
}
template <typename T>
void cell<T>::update() {
T oldval = current_value;
with<std::forward_list<observer*>, void>
(current_dependencies,
std::forward_list<observer*>(),
[=]{
current_value = recompute(current_value);
reset_dependencies(*current_dependencies);
});
}
template <typename T>
T& cell<T>::get() {
if (current_dependencies) {
current_dependencies->push_front(this);
return current_value;
} else {
return current_value;
}
}
template <typename T>
cell<T>::~cell() {
}
template <typename T>
T formula_cell<T>::recompute(T old) {
return alt_formula(old);
}
template <typename T>
T formula_cell<T>::init() {
return formula();
}
template <typename T>
void formula_cell<T>::reset(T value) {
this->alt_formula = ([=](T) { return value; });
this->formula = ([=]() { return value; });
this->mark();
}
template <typename T>
void formula_cell<T>::reset(std::function<T ()> formula) {
this->formula = formula;
this->alt_formula = ([=](T old) { return formula(); });
this->mark();
}
template <typename T>
void formula_cell<T>::reset(std::function<T ()> formula,
std::function<T (T)> alt_formula) {
this->formula = formula;
this->alt_formula = alt_formula;
this->mark();
}
template <typename T>
formula_cell<T>::~formula_cell() {
}
static void with_transaction(std::function<void ()> thunk) {
using std::cout;
using std::cerr;
using std::endl;
with<transaction, void>(current_transaction, transaction(), [&]() -> void {
//cerr << "; begin transaction." << endl;
thunk();
//cerr << "; number of affected nodes: " << current_transaction->dag.size() << endl;
std::deque<observer*> nodes;
// topological sort
std::forward_list<dag_node*> independent_nodes;
auto left = current_transaction->dag.size();
for (auto const& o_and_n : current_transaction->dag) {
auto node = o_and_n.second;
if (node->incoming_edges.size() == 0) {
independent_nodes.push_front(node);
}
}
while (!independent_nodes.empty()) {
left--;
auto node = independent_nodes.front();
independent_nodes.pop_front();
nodes.push_back(node->item);
// Or we can do away with the nodes list and just do:
// node->item->update()
// which makes transactions non-transactional but improves
// performance.
for (dag_node* other : node->outgoing_edges) {
other->incoming_edges.erase(node);
if (other->incoming_edges.size() == 0) {
independent_nodes.push_front(other);
}
}
delete node;
}
if (left != 0) {
// FIXME: delete remaining dag_nodes.
throw std::logic_error("Cell cycle detected");
}
for (auto const& node : nodes) {
node->update();
}
});
}
}
#endif //CELLS_IMPL_HPP
|