forked from stlab/adobe_source_libraries
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadam.cpp
More file actions
1556 lines (1148 loc) · 56.2 KB
/
adam.cpp
File metadata and controls
1556 lines (1148 loc) · 56.2 KB
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
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2013 Adobe
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
/**************************************************************************************************/
#include <adobe/adam.hpp>
#include <bitset>
#include <deque>
#include <functional>
#include <string>
#include <utility>
#include <vector>
#include <adobe/algorithm/append.hpp>
#include <adobe/algorithm/find.hpp>
#include <adobe/algorithm/for_each.hpp>
#include <adobe/algorithm/sort.hpp>
#include <adobe/algorithm/transform.hpp>
#include <adobe/algorithm/unique.hpp>
#include <adobe/any_regular.hpp>
#include <adobe/array.hpp>
#include <adobe/dictionary.hpp>
#include <adobe/name.hpp>
#include <adobe/functional.hpp>
#include <adobe/istream.hpp>
#include <adobe/table_index.hpp>
#include <adobe/virtual_machine.hpp>
#ifndef NDEBUG
#include <iostream>
#endif // NDEBUG
/**************************************************************************************************/
using namespace std;
using namespace std::placeholders;
/**************************************************************************************************/
namespace anonymous_adam_cpp { // can't instantiate templates on types from real anonymous
/**************************************************************************************************/
#ifndef NDEBUG
struct check_reentrancy {
check_reentrancy(bool& x) : check_m(x) {
assert(!x && "FATAL (sparent) : Function Not Reentrant.");
check_m = true;
}
~check_reentrancy() { check_m = false; }
bool& check_m;
};
#endif // NDEBUG
/**************************************************************************************************/
/*
REVISIT (sparent) : Move to utility? This is generally useful to provide a copy for
non-copyable types such as boost::signals2::signal<>.
*/
template <typename T> // T models default constructable
struct empty_copy : T {
empty_copy() : T() {}
empty_copy(const empty_copy&) : T() {}
empty_copy& operator=(const empty_copy&) { return *this; }
};
/**************************************************************************************************/
typedef adobe::sheet_t sheet_t;
// This currently establishes an upperbound on the number of cells in the sheet at 1K.
typedef std::bitset<1024> cell_bits_t;
typedef int priority_t;
enum access_specifier_t {
access_input,
access_interface_input,
access_interface_output,
access_output,
access_logic,
access_constant,
access_invariant
};
/**************************************************************************************************/
/*
REVISIT (sparent) : A thought - this could be packaged as a general template function for
converting exceptions on function object calls.
*/
/*
REVISIT (sparent) : Some version of MSVC didn't like function level try blocks. Need to test.
*/
void evaluate(adobe::virtual_machine_t& machine, const adobe::line_position_t& position,
const adobe::array_t& expression)
#ifdef BOOST_MSVC
{
#endif
try {
machine.evaluate(expression);
} catch (const std::exception& error) {
throw adobe::stream_error_t(error, position);
}
#ifdef BOOST_MSVC
}
#endif
/**************************************************************************************************/
struct scope_count : boost::noncopyable {
scope_count(std::size_t& x) : value_m(x) { ++value_m; }
~scope_count() { --value_m; }
private:
std::size_t& value_m;
};
template <typename T>
struct scope_value_t : boost::noncopyable {
scope_value_t(T& x, const T& v) : value_m(x), store_m(x) { x = v; }
~scope_value_t() { value_m = store_m; }
private:
T& value_m;
T store_m;
};
/**************************************************************************************************/
} // namespace anonymous_adam_cpp
using namespace anonymous_adam_cpp;
/**************************************************************************************************/
namespace adobe {
/**************************************************************************************************/
class sheet_t::implementation_t : boost::noncopyable {
public:
typedef sheet_t::connection_t connection_t;
explicit implementation_t(virtual_machine_t& machine);
any_regular_t inspect(const array_t& expression);
void set(name_t, const any_regular_t&); // input cell.
void touch(const name_t*, const name_t*); // range of input cells.
any_regular_t get(name_t);
const any_regular_t& operator[](name_t) const;
void add_input(name_t, const line_position_t&, const array_t& initializer);
void add_output(name_t, const line_position_t&, const array_t& expression);
void add_constant(name_t, const line_position_t&, const array_t& initializer);
void add_constant(name_t, any_regular_t value);
void add_logic(name_t, const line_position_t&, const array_t& expression);
void add_invariant(name_t, const line_position_t&, const array_t& expression);
void add_interface(name_t, bool linked, const line_position_t&, const array_t& initializer,
const line_position_t&, const array_t& expression);
void add_interface(name_t, any_regular_t initial);
void add_relation(const line_position_t&, const array_t& conditional, const relation_t* first,
const relation_t* last);
connection_t monitor_value(name_t, const monitor_value_t&); // output only
// input only
connection_t monitor_enabled(name_t, const name_t* first, const name_t* last,
const monitor_enabled_t&);
connection_t monitor_contributing(name_t, const dictionary_t&, const monitor_contributing_t&);
#if 0
connection_t monitor_invariant_contributing(name_t invariant, const monitor_invariant_t&);
// REVISIT (sparent) : UNIMPLEMENTED
#endif
connection_t monitor_invariant_dependent(name_t invariant, const monitor_invariant_t&);
bool has_input(name_t) const;
bool has_output(name_t) const;
void update();
void reinitialize();
void set(const dictionary_t& dictionary);
// set input cells to corresponding values in dictionary.
dictionary_t contributing(const dictionary_t&) const;
// all contributing values that have changed since mark
dictionary_t contributing_to_cell(name_t) const;
private:
struct relation_cell_t;
struct cell_t;
typedef vector<relation_cell_t*> relation_index_t;
typedef vector<relation_t> relation_set_t;
struct relation_cell_t {
relation_cell_t(const line_position_t& position, const array_t& conditional,
const relation_t* first, const relation_t* last)
: resolved_m(false), position_m(position), conditional_m(conditional),
terms_m(first, last) {}
bool resolved_m;
line_position_t position_m;
array_t conditional_m;
relation_set_t terms_m;
vector<cell_t*> edges_m;
// REVISIT (sparent) : There should be a function object to set members
void clear_resolved() { resolved_m = false; }
};
struct cell_t {
using calculator_t = std::function<any_regular_t()>;
typedef empty_copy<boost::signals2::signal<void(bool)>> monitor_invariant_list_t;
typedef empty_copy<boost::signals2::signal<void(const any_regular_t&)>>
monitor_value_list_t;
typedef empty_copy<boost::signals2::signal<void(const cell_bits_t&)>>
monitor_contributing_list_t;
cell_t(access_specifier_t specifier, name_t, const calculator_t& calculator,
std::size_t cell_set_pos, cell_t*); // output
cell_t(access_specifier_t specifier, name_t, any_regular_t,
std::size_t cell_set_pos); // constant
cell_t(name_t, any_regular_t, std::size_t cell_set_pos); // input cell
cell_t(name_t, bool linked, const calculator_t& init_expression,
std::size_t cell_set_pos); // interface cell (input)
#if 0
// compiler generated.
cell_t(const cell_t& x);
cell_t& operator=(const cell_t& x);
#endif
access_specifier_t specifier_m;
name_t name_m;
calculator_t calculator_m;
bool linked_m;
bool invariant_m;
priority_t priority_m; // For linked input cells only - zero otherwise
bool resolved_m; // For interface cells only - false if cell hasn't been flowed
bool evaluated_m; // true if cell has been calculated (or has no calculator).
std::size_t relation_count_m;
std::size_t initial_relation_count_m;
bool dirty_m; // denotes change state_m value
any_regular_t state_m;
cell_bits_t contributing_m;
cell_bits_t init_contributing_m;
std::size_t cell_set_pos_m; // self index in sheet_t::cell_set_m
calculator_t term_m;
// For output half of interface cells this points to corresponding input half. NULL
// otherwise.
cell_t* interface_input_m;
priority_t priority() const {
assert(
(specifier_m == access_interface_input || specifier_m == access_interface_output) &&
"should not read priority of this cell type");
return interface_input_m ? interface_input_m->priority_m : priority_m;
}
// For output half of interface cells this points to any possible connected relations.
relation_index_t relation_index_m;
monitor_value_list_t monitor_value_m;
monitor_contributing_list_t monitor_contributing_m;
monitor_invariant_list_t monitor_invariant_m;
void calculate();
void clear_dirty() {
dirty_m = false;
relation_count_m = initial_relation_count_m;
term_m = nullptr;
evaluated_m = specifier_m == access_input ||
specifier_m == access_constant /* || calculator_m.empty() */;
/*
REVISIT (sparent) : What exactly is the distinction between evaluated and resolved.
*/
resolved_m = evaluated_m;
}
/*
REVISIT (sparent) : A member wise implementation of swap would be better - but I'm
going to swap this way for expendiancy since cell_t will likely change a lot when
I get around to rewriting Adam.
*/
// friend void swap(cell_t& x, cell_t&y) { std::swap(x, y); }
};
friend struct cell_t;
any_regular_t calculate_expression(const line_position_t& position, const array_t& expression);
any_regular_t calculate_indexed(const line_position_t& position, const array_t& expression,
std::size_t index) {
return calculate_expression(position, expression).cast<array_t>()[index];
}
dictionary_t contributing_set(const dictionary_t&, const cell_bits_t&) const;
void initialize_one(cell_t& cell);
void enabled_filter(const cell_bits_t& touch_set, std::size_t contributing_index_pos,
monitor_enabled_t monitor, const cell_bits_t& new_priority_accessed_bits,
const cell_bits_t& new_active_bits);
// std::size_t cell_set_to_contributing(std::size_t cell_set_pos) const;
priority_t name_to_priority(name_t name) const;
void flow(cell_bits_t& priority_accessed);
/// Returns the output cell with the given name.
///
/// \pre A cell with that name exists.
cell_t& output_cell(const name_t& name) {
auto p = output_index_m.find(name);
assert(p != output_index_m.end() && "output cell not found");
return *p;
}
const cell_t& output_cell(const name_t& name) const {
auto p = output_index_m.find(name);
assert(p != output_index_m.end() && "output cell not found");
return *p;
}
/// Returns whether any output cells in the relation are resolved.
bool resolved(const relation_t& relation) const {
return find_if(relation.name_set_m, [&](const auto& name) {
return output_cell(name).resolved_m;
}) != relation.name_set_m.end();
}
/*
NOTE (sparent) : cell_t contains boost::signals2::signal<> which is not copyable. The cells
support
limited copying until they have monitors attached - this allows them to be placed into a
container prior to any connections being made. A deque is used rather than a vector because
it
does not reallocate when it grows.
*/
typedef std::deque<cell_t> cell_set_t;
typedef std::deque<relation_cell_t> relation_cell_set_t;
typedef std::vector<pair<name_t, bool>> get_stack_t;
typedef std::vector<cell_t*> index_vector_t;
typedef hash_index<cell_t, std::hash<name_t>, equal_to, mem_data_t<cell_t, const name_t>>
index_t;
index_t name_index_m;
index_t setable_index_m; // input of interface or input;
index_t input_index_m;
index_t output_index_m;
index_vector_t invariant_index_m;
priority_t priority_high_m;
priority_t priority_low_m;
cell_bits_t conditional_indirect_contributing_m;
virtual_machine_t& machine_m;
get_stack_t get_stack_m;
std::size_t get_count_m;
cell_bits_t init_dirty_m;
cell_bits_t priority_accessed_m;
cell_bits_t value_accessed_m;
cell_bits_t active_m;
typedef boost::signals2::signal<void(const cell_bits_t&, const cell_bits_t&)>
monitor_enabled_list_t;
monitor_enabled_list_t monitor_enabled_m;
cell_bits_t accumulate_contributing_m;
bool has_output_m; // true if there are any output cells.
bool initialize_mode_m; // true during reinitialize call.
// Actual cell storage - every thing else is index or state.
cell_set_t cell_set_m;
relation_cell_set_t relation_cell_set_m;
#ifndef NDEBUG
bool updated_m;
bool check_update_reentrancy_m;
#endif
};
/**************************************************************************************************/
void sheet_t::implementation_t::enabled_filter(const cell_bits_t& touch_set,
std::size_t contributing_index_pos,
monitor_enabled_t monitor,
const cell_bits_t& new_priority_accessed_bits,
const cell_bits_t& new_active_bits) {
cell_bits_t new_priority_accessed_touch = new_priority_accessed_bits & touch_set;
cell_bits_t old_priority_accessed_touch = priority_accessed_m & touch_set;
bool unchanged_priority_accessed_touch =
(new_priority_accessed_touch ^ old_priority_accessed_touch).none();
cell_t& cell = cell_set_m[contributing_index_pos];
bool active(active_m.test(contributing_index_pos));
bool new_active(new_active_bits.test(contributing_index_pos));
/*
REVISIT <sean_parent@mac.com> : This is check seems to missing a check on value_accessed_m.
A change there might go unnoticed and cause the control active state to go out of sync.
*/
if (unchanged_priority_accessed_touch && (active == new_active))
return;
monitor(new_active ||
(value_accessed_m.test(cell.cell_set_pos_m) && new_priority_accessed_touch.any()));
}
/**************************************************************************************************/
/*
REVISIT (sparent) : Need to figure out what happens if this is called on an input cell during
initialization (before it is resolved).
*/
void sheet_t::implementation_t::cell_t::calculate() {
if (evaluated_m)
return;
// REVISIT (sparent) : review resolved_m resolved issue.
// assert(resolved_m && "Cell in an invalid state?");
// This is to handle conditionals which refer to cells involved in relate clauses
if (relation_count_m)
throw std::logic_error(
make_string("cell ", name_m.c_str(), " is attached to an unresolved relate clause."));
any_regular_t result = calculator_m();
dirty_m = (result != state_m);
state_m = std::move(result);
evaluated_m = true;
}
/**************************************************************************************************/
sheet_t::implementation_t::cell_t::cell_t(name_t name, any_regular_t x, std::size_t cell_set_pos)
: specifier_m(access_input), name_m(name), invariant_m(false), priority_m(0), resolved_m(true),
evaluated_m(true), relation_count_m(0), initial_relation_count_m(0), dirty_m(false),
state_m(std::move(x)), cell_set_pos_m(cell_set_pos), interface_input_m(0) {
init_contributing_m.set(cell_set_pos);
}
/**************************************************************************************************/
sheet_t::implementation_t::cell_t::cell_t(name_t name, bool linked, const calculator_t& initializer,
std::size_t cell_set_pos)
: specifier_m(access_interface_input), name_m(name), calculator_m(initializer),
linked_m(linked), invariant_m(false), priority_m(0), resolved_m(true), evaluated_m(true),
relation_count_m(0), initial_relation_count_m(0), cell_set_pos_m(cell_set_pos),
interface_input_m(0) {
contributing_m.set(cell_set_pos);
}
/**************************************************************************************************/
sheet_t::implementation_t::cell_t::cell_t(access_specifier_t specifier, name_t name,
const calculator_t& calculator, std::size_t cell_set_pos,
cell_t* input)
: specifier_m(specifier), name_m(name), calculator_m(calculator), linked_m(false),
invariant_m(false), priority_m(0), resolved_m(false), evaluated_m(!calculator_m),
relation_count_m(0), initial_relation_count_m(0), cell_set_pos_m(cell_set_pos),
interface_input_m(input) {}
/**************************************************************************************************/
sheet_t::implementation_t::cell_t::cell_t(access_specifier_t specifier, name_t name,
any_regular_t x, std::size_t cell_set_pos)
: specifier_m(specifier), name_m(name), linked_m(false), invariant_m(false), priority_m(0),
resolved_m(true), evaluated_m(true), relation_count_m(0), initial_relation_count_m(0),
state_m(std::move(x)), cell_set_pos_m(cell_set_pos), interface_input_m(0) {}
/**************************************************************************************************/
sheet_t::sheet_t() : object_m(new implementation_t(machine_m)) {}
sheet_t::~sheet_t() { delete object_m; }
any_regular_t sheet_t::inspect(const array_t& expression) { return object_m->inspect(expression); }
void sheet_t::set(name_t input, const any_regular_t& value) { object_m->set(input, value); }
void sheet_t::touch(const name_t* first, const name_t* last) { object_m->touch(first, last); }
void sheet_t::add_input(name_t input, const line_position_t& position, const array_t& initializer) {
object_m->add_input(input, position, initializer);
}
void sheet_t::add_output(name_t output, const line_position_t& position,
const array_t& expression) {
object_m->add_output(output, position, expression);
}
void sheet_t::add_constant(name_t constant, const line_position_t& position,
const array_t& initializer) {
object_m->add_constant(constant, position, initializer);
}
void sheet_t::add_constant(name_t name, any_regular_t value) {
object_m->add_constant(name, std::move(value));
}
void sheet_t::add_logic(name_t logic, const line_position_t& position, const array_t& expression) {
object_m->add_logic(logic, position, expression);
}
void sheet_t::add_invariant(name_t invariant, const line_position_t& position,
const array_t& expression) {
object_m->add_invariant(invariant, position, expression);
}
void sheet_t::add_interface(name_t name, bool linked, const line_position_t& position1,
const array_t& initializer, const line_position_t& position2,
const array_t& expression) {
object_m->add_interface(name, linked, position1, initializer, position2, expression);
}
void sheet_t::add_interface(name_t name, any_regular_t initial) {
object_m->add_interface(name, std::move(initial));
}
void sheet_t::add_relation(const line_position_t& position, const array_t& conditional,
const relation_t* first, const relation_t* last) {
object_m->add_relation(position, conditional, first, last);
}
sheet_t::connection_t sheet_t::monitor_value(name_t output, const monitor_value_t& monitor) {
return object_m->monitor_value(output, monitor);
}
sheet_t::connection_t sheet_t::monitor_contributing(name_t output, const dictionary_t& mark,
const monitor_contributing_t& monitor) {
return object_m->monitor_contributing(output, mark, monitor);
}
sheet_t::connection_t sheet_t::monitor_enabled(name_t input, const name_t* first,
const name_t* last,
const monitor_enabled_t& monitor) {
return object_m->monitor_enabled(input, first, last, monitor);
}
#if 0
sheet_t::connection_t sheet_t::monitor_invariant_contributing(name_t input,
const monitor_invariant_t& monitor)
{ return object_m->monitor_invariant_contributing(input, monitor); }
#endif
sheet_t::connection_t sheet_t::monitor_invariant_dependent(name_t output,
const monitor_invariant_t& monitor) {
return object_m->monitor_invariant_dependent(output, monitor);
}
bool sheet_t::has_input(name_t name) const { return object_m->has_input(name); }
bool sheet_t::has_output(name_t name) const { return object_m->has_output(name); }
void sheet_t::update() { object_m->update(); }
void sheet_t::reinitialize() { object_m->reinitialize(); }
void sheet_t::set(const dictionary_t& dictionary) { object_m->set(dictionary); }
any_regular_t sheet_t::get(name_t cell) { return object_m->get(cell); }
const any_regular_t& sheet_t::operator[](name_t x) const { return (*object_m)[x]; }
dictionary_t sheet_t::contributing(const dictionary_t& mark) const {
return object_m->contributing(mark);
}
dictionary_t sheet_t::contributing() const { return object_m->contributing(dictionary_t()); }
dictionary_t sheet_t::contributing_to_cell(name_t x) const {
return object_m->contributing_to_cell(x);
}
/**************************************************************************************************/
sheet_t::implementation_t::implementation_t(virtual_machine_t& machine)
: name_index_m(std::hash<name_t>(), equal_to(), &cell_t::name_m),
input_index_m(std::hash<name_t>(), equal_to(), &cell_t::name_m),
output_index_m(std::hash<name_t>(), equal_to(), &cell_t::name_m), priority_high_m(0),
priority_low_m(0), machine_m(machine), get_count_m(0), has_output_m(false),
initialize_mode_m(false)
#ifndef NDEBUG
,
updated_m(false), check_update_reentrancy_m(false)
#endif
{
}
/**************************************************************************************************/
any_regular_t sheet_t::implementation_t::inspect(const array_t& expression) {
machine_m.evaluate(expression);
any_regular_t result = std::move(machine_m.back());
machine_m.pop_back();
return result;
}
/**************************************************************************************************/
void sheet_t::implementation_t::set(name_t n, const any_regular_t& v) {
#ifndef NDEBUG
assert(!check_update_reentrancy_m &&
"sheet_t::set() cannot be called during call to sheet_t::update().");
updated_m = false;
#endif
index_t::iterator iter(input_index_m.find(n));
if (iter == input_index_m.end()) {
throw std::logic_error(make_string("input cell ", n.c_str(), " does not exist."));
}
++priority_high_m;
iter->state_m = v;
iter->priority_m = priority_high_m;
// Leave contributing untouched.
if (iter->specifier_m == access_input)
init_dirty_m.set(iter->cell_set_pos_m);
}
/**************************************************************************************************/
void sheet_t::implementation_t::touch(const name_t* first, const name_t* last) {
// REVISIT (sparent) : This should be constrained to interface cells only.
// REVISIT (sparent) : This logic is similar to the logic in flow and should be the same.
// build an index of the cells to touch sorted by current priority.
typedef table_index<priority_t, cell_t> priority_index_t;
priority_index_t index(&cell_t::priority_m);
// REVISIT (sparent) : This loop is transform but the soft condition in the middle breaks that.
// If we resolve the REVISIT() inside the loop so failure on find is a throw then this loop
// is transform.
while (first != last) {
index_t::iterator iter(input_index_m.find(*first));
/*
REVISIT (sparent) : Cells that aren't present are ignored because sheets get "scoped"
so if a cell isn't touched in a local sheet it might be in a more global scope.
Perhaps the client should be keeping two lists and this would go back to an error.
*/
if (iter != input_index_m.end())
index.push_back(*iter);
++first;
}
index.sort();
// Touch the cells - keeping their relative priority
for (priority_index_t::iterator f(index.begin()), l(index.end()); f != l; ++f) {
++priority_high_m;
f->priority_m = priority_high_m;
}
}
/**************************************************************************************************/
void sheet_t::implementation_t::add_input(name_t name, const line_position_t& position,
const array_t& initializer) {
scope_value_t<bool> scope(initialize_mode_m, true);
any_regular_t initial_value;
if (initializer.size())
initial_value = calculate_expression(position, initializer);
cell_set_m.push_back(cell_t(name, std::move(initial_value), cell_set_m.size()));
// REVISIT (sparent) : Non-transactional on failure.
input_index_m.insert(cell_set_m.back());
if (!name_index_m.insert(cell_set_m.back()).second) {
throw stream_error_t(make_string("cell named '", name.c_str(), "'already exists."),
position);
}
}
/**************************************************************************************************/
void sheet_t::implementation_t::add_output(name_t name, const line_position_t& position,
const array_t& expression) {
// REVISIT (sparent) : Non-transactional on failure.
cell_set_m.push_back(cell_t(
access_output, name,
[position, expression, this]() { return calculate_expression(position, expression); },
cell_set_m.size(), nullptr));
output_index_m.insert(cell_set_m.back());
if (!name_index_m.insert(cell_set_m.back()).second) {
throw stream_error_t(make_string("cell named '", name.c_str(), "'already exists."),
position);
}
has_output_m = true;
}
/**************************************************************************************************/
// REVISIT (sparent) : Hacked glom of input/output pair.
void sheet_t::implementation_t::add_interface(name_t name, bool linked,
const line_position_t& position1,
const array_t& initializer_expression,
const line_position_t& position2,
const array_t& expression) {
scope_value_t<bool> scope(initialize_mode_m, true);
if (initializer_expression.size()) {
cell_set_m.push_back(cell_t(name, linked,
[position1, initializer_expression, this]() {
return calculate_expression(position1, initializer_expression);
},
cell_set_m.size()));
} else {
cell_set_m.push_back(cell_t(name, linked, cell_t::calculator_t(), cell_set_m.size()));
}
// REVISIT (sparent) : Non-transactional on failure.
input_index_m.insert(cell_set_m.back());
if (initializer_expression.size())
initialize_one(cell_set_m.back());
if (expression.size()) {
// REVISIT (sparent) : Non-transactional on failure.
cell_set_m.push_back(cell_t(access_interface_output, name,
[position2, expression, this]() {
return calculate_expression(position2, expression);
},
cell_set_m.size(), &cell_set_m.back()));
} else {
cell_set_m.push_back(cell_t(access_interface_output, name,
[name, this]() { return get(name); },
cell_set_m.size(), &cell_set_m.back()));
}
output_index_m.insert(cell_set_m.back());
if (!name_index_m.insert(cell_set_m.back()).second) {
throw stream_error_t(make_string("cell named '", name.c_str(), "'already exists."),
position2);
}
}
/**************************************************************************************************/
void sheet_t::implementation_t::add_interface(name_t name, any_regular_t initial) {
cell_set_m.push_back(cell_t(name, true, cell_t::calculator_t(), cell_set_m.size()));
cell_t& cell = cell_set_m.back();
input_index_m.insert(cell);
cell.state_m = std::move(initial);
cell.priority_m = ++priority_high_m;
cell_set_m.push_back(cell_t(access_interface_output, name,
[name, this]() { return get(name); },
cell_set_m.size(), &cell));
output_index_m.insert(cell_set_m.back());
if (!name_index_m.insert(cell_set_m.back()).second) {
throw std::logic_error(make_string("cell named '", name.c_str(), "'already exists."));
}
}
/**************************************************************************************************/
void sheet_t::implementation_t::add_constant(name_t name, const line_position_t& position,
const array_t& initializer) {
scope_value_t<bool> scope(initialize_mode_m, true);
cell_set_m.push_back(cell_t(access_constant, name, calculate_expression(position, initializer),
cell_set_m.size()));
// REVISIT (sparent) : Non-transactional on failure.
if (!name_index_m.insert(cell_set_m.back()).second) {
throw stream_error_t(make_string("cell named '", name.c_str(), "'already exists."),
position);
}
}
/**************************************************************************************************/
void sheet_t::implementation_t::add_constant(name_t name, any_regular_t value) {
cell_set_m.push_back(cell_t(access_constant, name, std::move(value), cell_set_m.size()));
if (!name_index_m.insert(cell_set_m.back()).second) {
throw std::logic_error(make_string("cell named '", name.c_str(), "'already exists."));
}
}
/**************************************************************************************************/
void sheet_t::implementation_t::add_logic(name_t logic, const line_position_t& position,
const array_t& expression) {
cell_set_m.push_back(cell_t(
access_logic, logic,
[position, expression, this]() { return calculate_expression(position, expression); },
cell_set_m.size(), nullptr));
if (!name_index_m.insert(cell_set_m.back()).second) {
throw stream_error_t(make_string("cell named '", logic.c_str(), "'already exists."),
position);
}
}
/**************************************************************************************************/
void sheet_t::implementation_t::add_invariant(name_t name, const line_position_t& position,
const array_t& expression) {
// REVISIT (sparent) : Non-transactional on failure.
cell_set_m.push_back(cell_t(
access_invariant, name,
[position, expression, this]() { return calculate_expression(position, expression); },
cell_set_m.size(), nullptr));
output_index_m.insert(cell_set_m.back());
if (!name_index_m.insert(cell_set_m.back()).second) {
throw stream_error_t(make_string("cell named '", name.c_str(), "'already exists."),
position);
}
invariant_index_m.push_back(&cell_set_m.back());
}
/**************************************************************************************************/
void sheet_t::implementation_t::add_relation(const line_position_t& position,
const array_t& conditional, const relation_t* first,
const relation_t* last) {
relation_cell_set_m.push_back(relation_cell_t(position, conditional, first, last));
relation_cell_t& relation = relation_cell_set_m.back();
// build a unique list of lhs cells
vector<name_t> cell_set;
for (; first != last; ++first) {
cell_set.insert(cell_set.end(), first->name_set_m.begin(), first->name_set_m.end());
}
sort_unique(cell_set);
for (vector<name_t>::iterator f = cell_set.begin(), l = cell_set.end(); f != l; ++f) {
index_t::iterator p = output_index_m.find(*f);
if (p == output_index_m.end() || !p->interface_input_m)
throw stream_error_t(make_string("interface cell ", f->c_str(), " does not exist."),
position);
relation.edges_m.push_back(&(*p));
p->relation_index_m.push_back(&relation);
++p->initial_relation_count_m;
}
}
/**************************************************************************************************/
any_regular_t sheet_t::implementation_t::calculate_expression(const line_position_t& position,
const array_t& expression) {
evaluate(machine_m, position, expression);
any_regular_t result = std::move(machine_m.back());
machine_m.pop_back();
return result;
}
/**************************************************************************************************/
sheet_t::connection_t sheet_t::implementation_t::monitor_enabled(name_t n, const name_t* first,
const name_t* last,
const monitor_enabled_t& monitor) {
assert(updated_m && "Must call sheet_t::update() prior to monitor_enabled.");
index_t::iterator iter(input_index_m.find(n));
if (iter == input_index_m.end())
throw std::logic_error(make_string("Attempt to monitor nonexistent cell: ", n.c_str()));
cell_bits_t touch_set;
while (first != last) {
index_t::iterator i(input_index_m.find(*first));
if (i == input_index_m.end())
throw std::logic_error(
make_string("Attempt to monitor nonexistent cell: ", first->c_str()));
touch_set.set(i->cell_set_pos_m);
++first;
}
/*
REVISIT <sean_parent@mac.com> : This is a complex test that is duplicated in enabled_filter
and should be distilled down to a simply the test of active_m.
*/
monitor(active_m.test(iter->cell_set_pos_m) || (value_accessed_m.test(iter->cell_set_pos_m) &&
(touch_set & priority_accessed_m).any()));
return monitor_enabled_m.connect(
[touch_set, iter_pos = iter->cell_set_pos_m, monitor, this](const cell_bits_t& a, const cell_bits_t& b) {
enabled_filter(touch_set, iter_pos, monitor, a, b);
});
}
/**************************************************************************************************/
sheet_t::connection_t
sheet_t::implementation_t::monitor_invariant_dependent(name_t n,
const monitor_invariant_t& monitor) {
assert(updated_m && "Must call sheet_t::update() prior to monitor_invariant_dependent.");
index_t::iterator iter(output_index_m.find(n));
if (iter == output_index_m.end())
throw std::logic_error(make_string("Attempt to monitor nonexistent cell: ", n.c_str()));
monitor(iter->invariant_m);
return iter->monitor_invariant_m.connect(monitor);
}
/**************************************************************************************************/
sheet_t::connection_t sheet_t::implementation_t::monitor_value(name_t name,
const monitor_value_t& monitor) {
assert(updated_m && "Must call sheet_t::update() prior to monitor_value.");
index_t::iterator iter = output_index_m.find(name);
if (iter == output_index_m.end()) {
throw std::logic_error(make_string("Attempt to monitor nonexistent cell: ", name.c_str()));
}
monitor(iter->state_m);
return iter->monitor_value_m.connect(monitor);
}
/**************************************************************************************************/
sheet_t::connection_t
sheet_t::implementation_t::monitor_contributing(name_t n, const dictionary_t& mark,