-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprego-old.cpp
765 lines (660 loc) · 17.3 KB
/
prego-old.cpp
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
//TODO: are all the todos tested for breaking the app
//TODO: split calc() into a void func and was_changed() func
#include <algorithm>
#include <cassert>
#include <compare>
#include <concepts>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <optional>
#include <set>
#include <utility>
#include <variant>
#include <vector>
using namespace std;
#define RNG(x) std::begin(x), std::end(x)
#define FWD(x) std::forward<decltype(x)>(x)
struct observable;
struct computed;
struct reaction;
using dependent_t = std::variant<computed, reaction>;
using dependency_t = std::variant<observable, computed>;
// TODO: idea - store counter in each atom and store a map in each node: atom -> copy of last seen counter value. If the values don't match ==> node is dirty. Drawback is that it might theoretically overflow such that it compares two identical values, while they are actually different.
// TODO: idea2 - instead of counter, store shared_ptr with counter in atom and weak_ptr in nodes. If weak_count == 0, nothing needs to be done (no one needs to be notified). If increasing counter doesn't overflow, just increase. Otherwise, otherwise create new shared_ptr holding 0. Might not be efficient, but doesn't overflow.
enum class dirty_state {
no,
maybe,
yes,
updated,
};
auto to_string(auto x) { return x; }
auto to_string(dirty_state is_dirty) {
switch (is_dirty) {
default: return "unknown";
case dirty_state::no: return "no";
case dirty_state::maybe: return "maybe";
case dirty_state::yes: return "yes";
case dirty_state::updated: return "updated";
}
}
auto log_(int id, auto ...args) {
switch (id) {
default: break;
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6: return;
break;
}
std::cout << std::boolalpha << id << ": ";
((std::cout << to_string(args)), ...);
std::cout << std::endl;
}
auto log(auto ...args) {
log_(args...);
}
// Conditionally updates dirty_state:
// yes->maybe is ignored
// If value changed returns true, otherwise false
auto set_dirty_state(auto &state, auto is_dirty) {
if (state->is_dirty == dirty_state::yes
&& is_dirty == dirty_state::maybe) {
log(0,
state->name,
":dirty_state ",
state->is_dirty,
" => ",
is_dirty,
" => ",
state->is_dirty
);
return false;
}
if (state->is_dirty == is_dirty) {
log(0,
state->name,
":dirty_state unchanged"
);
return false;
}
log(0,
state->name,
":dirty_state ",
state->is_dirty,
" => ",
is_dirty
);
state->is_dirty = is_dirty;
return true;
}
auto calc(const dependency_t &dep) -> bool;
auto invalidate(
const dependent_t &dep,
dirty_state is_dirty
) -> void;
// calculates all dependencies
// returns true if at least one of them returns true
// true indicates that dependents maybe affected
// and need to be recalculated
auto calc_all(const auto &deps) -> bool {
return std::accumulate(
RNG(deps),
false,
[](auto acc, auto &dep) {
return calc(dep) or acc;
}
);
}
auto dependents_names(const dependency_t &dep) -> string;
auto name(const dependency_t &) -> string;
auto name(const dependent_t &) -> string;
auto names(const auto &deps, auto f) -> string {
return std::accumulate(
RNG(deps),
""s,
[=](auto acc, auto &dep) {
if (acc != "") acc += ",";
return acc + f(dep);
}
);
}
auto print_dependencies(auto dep) {
log(7, "dependencies for ",
dep.state->name, ": ", names(
dep.state->dependencies,
[](auto &dep) {
return name(dep)
+ "->{" + dependents_names(dep) + "}";
})
);
}
auto invalidate_all(
auto n, auto v,
const std::vector<dependent_t> &deps,
auto is_dirty)
-> void;
auto remove_dependencies(auto &dependent) -> void;
// Regular void:
// T -> T
// void -> monostate
auto wrap(auto f, auto ...args) {
if constexpr (is_void_v<decltype(f(args...))>) {
f(args...);
return monostate{};
}
else {
return f(args...);
}
}
auto unwrap(monostate) {}
auto unwrap(auto value) { return value; }
// clears dependencies and unconditionally runs function
auto run(const auto &dependent) {
log(7, "before removal (", dependent.state->name, ")");
print_dependencies(dependent);
remove_dependencies(dependent);
log(7, "after removal (", dependent.state->name, ")");
print_dependencies(dependent);
auto result = wrap(dependent.state->f, dependent);
log(7, "after calc (", dependent.state->name, ")");
print_dependencies(dependent);
return unwrap(result);
}
auto depends(auto &dependent, auto &dependency) -> void;
struct observable {
struct state_t {
std::string name;
int value;
static constexpr auto is_dirty = dirty_state::no;
std::vector<dependent_t> dependents;
};
std::shared_ptr<state_t> state = std::make_shared<state_t>();
public:
explicit observable(std::string name, int value) {
state->name = name;
state->value = value;
}
auto operator==(const observable &rhs) const {
return state == rhs.state;
}
auto calc() const { return false; }
auto operator()(auto dependent) const {
depends(dependent, *this);
log(3, state->name, "() == ", state->value);
return state->value;
}
auto operator()() const {
log(3, state->name, "() == ", state->value);
return state->value;
}
auto set(int value) -> void;
};
struct computed {
struct state_t {
std::string name;
std::function<int (const computed &)> f;
mutable std::optional<int> cache;
mutable dirty_state is_dirty = dirty_state::yes;
std::vector<dependency_t> dependencies;
std::vector<dependent_t> dependents;
};
std::shared_ptr<state_t> state = std::make_shared<state_t>();
public:
auto operator==(const computed &rhs) const {
return state == rhs.state;
}
explicit computed(
std::string name,
std::function<int (const computed &)> f
) {
state->name = name;
state->f = f;
}
auto invalidate(dirty_state is_dirty) const -> void;
auto calc() const {
if (state->is_dirty == dirty_state::no) {
log(1,
state->name,
" is up to date, skip calc..."
);
return false;
}
if (state->is_dirty == dirty_state::updated) {
log(1,
state->name,
" was previously updated, skip calc..."
);
return true;
}
if (state->is_dirty == dirty_state::yes) {
log(1,
state->name,
" must be calculated, checking deps..."
);
}
if (state->is_dirty == dirty_state::maybe) {
log(1,
state->name,
" may have changed, checking deps..."
);
}
// Make sure dependencies are calculated first.
const auto did_any_dep_change = calc_all(state->dependencies);
// Recalculate if any dependency changed
// or if we were forced to recalculate.
auto should_recalculate = did_any_dep_change
or state->is_dirty == dirty_state::yes;
if (!should_recalculate) {
log(1,
state->name,
" deps unchanged, skip calc..."
);
set_dirty_state(this->state, dirty_state::no);
return false;
}
log(2, "calculating ", state->name, "...");
const auto old = std::exchange(
state->cache, run(*this)
);
log(2, state->name
, ":calculated "
, (old ? to_string(*old) : "null")
, " => "
, *state->cache
);
should_recalculate = state->cache != old;
set_dirty_state(
this->state,
should_recalculate ? dirty_state::updated
: dirty_state::no
);
return should_recalculate;
}
auto operator()(auto dependent) const {
// register dependent => dependency
depends(dependent, *this);
calc();
log(3, state->name, "() == ", *state->cache);
return *state->cache;
}
auto operator()() const {
calc();
log(3, state->name, "() == ", *state->cache);
return *state->cache;
}
};
struct reaction {
struct state_t {
std::string name;
std::function<void (const reaction &)> f;
mutable dirty_state is_dirty = dirty_state::yes;
std::vector<dependency_t> dependencies;
};
auto operator==(const reaction &rhs) const {
return state == rhs.state;
}
explicit reaction(
std::string name,
std::function<void (const reaction &)> f
) {
state->name = name;
state->f = f;
run(*this);
}
std::shared_ptr<state_t> state = std::make_shared<state_t>();
auto invalidate(dirty_state is_dirty) const {
set_dirty_state(this->state, is_dirty);
}
};
auto calc(const dependency_t &dep) -> bool {
return std::visit(
[](auto &dep) {
return dep.calc();
}, dep);
}
auto name(const dependency_t &dep) -> string {
return std::visit(
[](auto &dep) {
return dep.state->name;
}, dep);
}
auto name(const dependent_t &dep) -> string {
return std::visit(
[](auto &dep) {
return dep.state->name;
}, dep);
}
auto dependents_names(const computed &dep) -> string {
return names(dep.state->dependents, [](auto &dep) {
return name(dep);
});
}
auto dependents_names(const reaction &dep) -> string {
return "";
}
auto dependents_names(const dependency_t &dep) -> string {
return std::visit(
[](auto &dep) {
return dependents_names(dep);
}, dep);
}
auto computed::invalidate(dirty_state is_dirty) const -> void {
if (set_dirty_state(this->state, is_dirty))
invalidate_all(
state->name,
0,
state->dependents,
dirty_state::maybe
);
}
auto invalidate(
const dependent_t &dep,
dirty_state is_dirty
) -> void {
return std::visit(
[=](auto &dep) {
dep.invalidate(is_dirty);
},
dep
);
}
auto invalidate_all(
auto n, auto v,
const std::vector<dependent_t> &deps,
auto is_dirty
) -> void {
// if (n == "a" and v == 23) return;
// log(8, "foobar");return ;
std::for_each(
RNG(deps),
[=](const auto &dep) { invalidate(dep, is_dirty); }
);
}
auto set_add(auto &container, const auto &e) {
const auto it = std::find(RNG(container), e);
if (it != std::end(container)) {
return false;
}
container.push_back(e);
return true;
}
auto depends(auto &dependent, auto &dependency) -> void {
// dependent -> dependency
if (!set_add(
dependent.state->dependencies,
dependency_t{dependency}
)) {
log(4,
dependent.state->name,
" -> ",
dependency.state->name,
" not added"
);
return;
}
// dependency -> dependent
set_add(
dependency.state->dependents,
dependent_t{dependent}
);
log(4,
dependent.state->name,
" -> ",
dependency.state->name,
" added"
);
}
auto set_remove(
auto &container,
const dependent_t &dependent
) {
// const auto it = std::find(RNG(container), dependent);
// log(7, "erik")
container.erase(
std::remove(RNG(container), dependent),
container.end()
);
}
auto set_remove(dependency_t &dependency, const auto &dependent) {
std::visit(
[=](auto &dependency) {
set_remove(
dependency.state->dependents,
dependent_t{dependent}
);
},
dependency
);
}
auto remove_dependencies(auto &dependent) -> void {
for (auto &dep : dependent.state->dependencies)
set_remove(dep, dependent);
dependent.state->dependencies.clear();
}
auto observable::set(int value) -> void {
log(5, state->name, ":set ",
state->value,
" => ",
value
);
state->value = value;
//const auto reactions =
invalidate_all(state->name, value, state->dependents, dirty_state::yes);
//for_each(RNG(reactions), reaction::run);
}
/*
/////////////////////////////////
// HELL2
/////////////////////////////////
// a---c---reaction2
// \ / \
// b---d---reaction1
// / /
// e---f
*/
/*
What about a dependent that after a state change starts depending on part of the graph that already depends on the changed observable, but now can only be computed after that part of the graph has been fully computed? It might incorrectly conclude that the dependent and sub graph can be calculated concurrently.
*/
/*
// a(21)---c(a>21?22:b)---r2(=)
// \ / \
// b(+)---d(+)---r1(=)
// / /
// e(0)---f(=)
*/
// TODO: c depends on a and b (in that order), a sets c to dirty:yes, whereas b sets c to dirty:maybe (incorrect)
// TODO: c depends on b and a (in that order), b sets c to dirty:maybe, whereas a sets c to dirty:yes (correct)
// TODO: c depends on b, b depends on a; d depends on a and c; updating a should not trigger d before b and c
int main2() {
auto a = observable{"a", 21};
/*
// a(21)
*/
auto e = observable{"e", 0};
/*
// a(21)
//
// e(0)
*/
auto b = computed{"b", [=](auto self) {
return a(self) + e(self);
}};
/*
// a(21)
// \
// b(?)
// /
// e(0)
*/
auto c = computed{"c", [=](auto self) {
return a(self) > 21 ? 22 : b(self);
}};
/*
// a(21)---c(?)
// \ /
// b(?)
// /
// e(0)
*/
auto f = computed{"f", [=](auto self) {
return e(self);
}};
/*
// a(21)---c(?)
// \ /
// b(?)
// /
// e(0)---f(?)
*/
auto d = computed{"d", [=](auto self) {
return c(self) + b(self) + f(self);
}};
/*
// a(21)---c(?)
// \ / \
// b(?)---d(?)
// / /
// e(0)---f(?)
*/
log(6, "eager 1...");
assert_eq(d(), 42);
/*
// a(21)===c(21)
// \\ // \\
// b(21)===d(42)
// // //
// e(0)====f(0)
*/
log(6);
log(6, "eager 2...");
assert_eq(d(), 42);
/*
// a(21)---c(21)
// \ / \
// b(21)---d(42)
// / /
// e(0)---f(0)
*/
log(6);
log(6, "eager 3...");
a.set(20);
assert_eq(d(), 40);
/*
// a(20)---c(20)
// \ / \
// b(20)---d(40)
// / /
// e(0)---f(0)
*/
log(6);
log(6, "eager 4...");
a.set(22);
assert_eq(d(), 44);
/*
// a(22)---c(22)
// \ \
// b(22)---d(44)
// / /
// e(0)---f(0)
*/
log(6);
log(6, "eager 5...");
a.set(23);
assert_eq(d(), 45);
/*
// a(23)---c(22)
// \ \
// b(23)---d(45)
// / /
// e(0)---f(0)
*/
log(6);
log(6, "eager 6...");
e.set(1);
assert_eq(d(), 47);
/*
// a(23)---c(22)
// \ \
// b(24)---d(47)
// / /
// e(1)---f(1)
*/
/* auto q = observable{0};
auto r = computed{[=](auto self) { return q(self); return 0; }};
auto s = computed{[=](auto self) { q(self); r(self); }};
*/
return 0;
// a.set(21);
/*
// a(21)---c(21)
// \ / \
// b(21)---d(42)
// / /
// e(0)---f(0)
*/
auto r1 = reaction{"r1", [=](auto self) {
d(self);
}};
/* depends(b, a);
depends(b, e);
depends(c, a);
depends(c, b);
depends(d, c);
depends(d, b);
depends(f, e);
depends(d, f);
depends(r1, d);*/
/*
// a(21)===c(21)
// \\ // \\
// b(21)===d(42)===r1(!)
// // //
// e(0)====f(0)
*/
/* auto r2 = reaction{"r2", [=](auto self) {
c(self);
}};*/
// depends(r2, c);
/*
// a(21)===c(21)===r2(!)
// \\ // \\
// b(21)===d(42)===r1()
// // //
// e(0)====f(0)
*/
// a.set(22);
// c no longer depends on b
// d can be triggered by b or c
/*
// a(22)===c(22)===r2(!)
// \\ / \\
// b(22)===d(44)===r1(!)
// // //
// e(0)====f(0)
*/
// a.set(23);
// d can be triggered only by b (c didn't change)
/*
// a(23)===c(22)===r2()
// \\ / \\
// b(23)===d(45)===r1(!)
// // //
// e(0)====f(0)
*/
// e.set(1);
// d can be triggered only by b (c didn't change)
/*
// a(23)===c(22)===r2()
// \\ / \\
// b(24)===d(47)===r1(!)
// // //
// e(1)====f(1)
*/
return 0;
}