-
Notifications
You must be signed in to change notification settings - Fork 0
/
sat_ca.c
1782 lines (1498 loc) · 37 KB
/
sat_ca.c
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
#include "sat.h"
#include "sat_ca.h"
#include "util.h"
#include <stdlib.h>
#define ID_UNDEFINED 0
#define CL_UNDEFINED 0
#define LP_UNDEFINED -1
#define PRINT_SCHEME_START(table) \
static sat_print_t table[] = {
#define PRINT_SCHEME_ENTRY(val, sym, colour, is_bright) \
[val] = {sym, colour, is_bright},
#define PRINT_SCHEME_END \
};
#define ROTATE(dir, ca, exp) do { \
ca_direction_t X; \
for (X = DR_UP; X != DR_UNDEFINED && !(exp); X++); \
dir = X; \
} while (0);
#define IDX_QUIESCENT 0
#define IDX_QUIESCENT_BOUND 1
#define IDX_ERASE_LOOP 2
#define IDX_COLLISION 3
#define IDX_BRANCH_SEQ 4
#define IDX_GEN_ZERO 5
#define IDX_GEN_ONE 6
#define IDX_MONITOR_ACTIVE 7
#define IDX_MONITOR_ALLERT 8
typedef enum {
CD_QUIESCENT = 0,
CD_FLOW = 1,
CD_GROW = 2,
CD_TURN_LEFT = 3,
CD_ARM_EXT_START = 4,
CD_ARM_EXT_END = 5,
CD_DETACH = 6,
CD_UNEXPLORED_0 = 7,
CD_UNEXPLORED_1 = 8,
CD_ZERO = 9,
CD_ONE = 10,
CD_TAUTOLOGY = 11,
CD_PARADOX = 12,
} ca_code_t;
typedef enum {
DR_QUIESCENT = 0,
DR_UP = 1,
DR_DOWN = 2,
DR_LEFT = 3,
DR_RIGHT = 4,
DR_UNDEFINED = 5
} ca_direction_t;
typedef enum {
CL_QUIESCENT = 0,
CL_WHITE = 1,
CL_YELLOW = 2,
CL_BLUE = 3,
CL_RED = 4
} ca_colour_t;
typedef enum {
FL_QUIESCENT = SHIFT_LEFT(IDX_QUIESCENT),
FL_ERASE_LOOP = SHIFT_LEFT(IDX_ERASE_LOOP),
FL_COLLISION = SHIFT_LEFT(IDX_COLLISION),
FL_BRANCH_SEQ = SHIFT_LEFT(IDX_BRANCH_SEQ),
FL_GEN_ZERO = SHIFT_LEFT(IDX_GEN_ZERO),
FL_GEN_ONE = SHIFT_LEFT(IDX_GEN_ONE),
FL_MONITOR_ACTIVE = SHIFT_LEFT(IDX_MONITOR_ACTIVE),
FL_MONITOR_ALLERT = SHIFT_LEFT(IDX_MONITOR_ALLERT),
} ca_flag_t;
typedef enum {
MN_NOOP = 0,
MN_SUCCESS = 1,
MN_ACTIVE = 2,
MN_ALLERT = 3,
MN_FAIL = 4
} ca_monitor_state_t;
typedef struct monitor_t {
int *pos_tbl;
int pos_tbl_sz;
int offset;
int is_active;
} monitor_t;
typedef struct ca_t {
struct ca_t *neighbours[8];
ca_code_t code;
ca_direction_t dir;
ca_colour_t col;
ca_flag_t flag;
int id;
monitor_t mon;
void *loop;
} ca_t;
typedef struct ca_space_t {
ca_space_cb_t *cb;
ca_t **sp;
int sp_dim;
int loop_dim;
sat_print_field_t print_field;
int x0;
int y0;
} ca_space_t;
typedef struct sat_loop_t {
struct sat_loop_t *next;
ca_t **ca_list;
int is_success;
} sat_loop_t;
PRINT_SCHEME_START(sat_print_code)
PRINT_SCHEME_ENTRY(CD_QUIESCENT, ' ', COL_BLACK, ATTR_DULL)
PRINT_SCHEME_ENTRY(CD_FLOW, 'o', COL_GREY, ATTR_DULL)
PRINT_SCHEME_ENTRY(CD_GROW, 'G', COL_RED, ATTR_BRIGHT)
PRINT_SCHEME_ENTRY(CD_TURN_LEFT, 'L', COL_YELLOW, ATTR_DULL)
PRINT_SCHEME_ENTRY(CD_ARM_EXT_START, 'E', COL_BLUE, ATTR_BRIGHT)
PRINT_SCHEME_ENTRY(CD_ARM_EXT_END, 'F', COL_MAGENTA, ATTR_DULL)
PRINT_SCHEME_ENTRY(CD_DETACH, 'D', COL_WHITE, ATTR_BRIGHT)
PRINT_SCHEME_ENTRY(CD_UNEXPLORED_0, 'A', COL_GREEN, ATTR_BRIGHT)
PRINT_SCHEME_ENTRY(CD_ZERO, '0', COL_YELLOW, ATTR_BRIGHT)
PRINT_SCHEME_ENTRY(CD_ONE, '1', COL_CYAN, ATTR_BRIGHT)
PRINT_SCHEME_END;
PRINT_SCHEME_START(sat_print_dir)
PRINT_SCHEME_ENTRY(DR_QUIESCENT, ' ', COL_BLACK, ATTR_DULL)
PRINT_SCHEME_ENTRY(DR_UP, '^', COL_WHITE, ATTR_DULL)
PRINT_SCHEME_ENTRY(DR_RIGHT, '>', COL_RED, ATTR_DULL)
PRINT_SCHEME_ENTRY(DR_DOWN, 'v', COL_GREEN, ATTR_DULL)
PRINT_SCHEME_ENTRY(DR_LEFT, '<', COL_CYAN, ATTR_DULL)
PRINT_SCHEME_END;
PRINT_SCHEME_START(sat_print_flag)
PRINT_SCHEME_ENTRY(IDX_QUIESCENT, ' ', COL_BLACK, ATTR_DULL)
PRINT_SCHEME_ENTRY(IDX_QUIESCENT_BOUND, ASCII_DOT, COL_GREY, ATTR_DULL)
PRINT_SCHEME_ENTRY(IDX_ERASE_LOOP, '#', COL_RED, ATTR_BRIGHT)
PRINT_SCHEME_ENTRY(IDX_COLLISION, '!', COL_BLUE, ATTR_BRIGHT)
PRINT_SCHEME_ENTRY(IDX_BRANCH_SEQ, '*', COL_MAGENTA, ATTR_DULL)
PRINT_SCHEME_ENTRY(IDX_GEN_ZERO, '+', COL_YELLOW, ATTR_BRIGHT)
PRINT_SCHEME_ENTRY(IDX_GEN_ONE, '-', COL_CYAN, ATTR_BRIGHT)
PRINT_SCHEME_ENTRY(IDX_MONITOR_ACTIVE, '?', COL_GREEN, ATTR_BRIGHT)
PRINT_SCHEME_ENTRY(IDX_MONITOR_ALLERT, '%', COL_WHITE, ATTR_BRIGHT)
PRINT_SCHEME_END;
PRINT_SCHEME_START(sat_print_colour)
PRINT_SCHEME_ENTRY(CL_QUIESCENT, ' ', COL_BLACK, ATTR_DULL)
PRINT_SCHEME_ENTRY(CL_WHITE, 'w', COL_WHITE, ATTR_DULL)
PRINT_SCHEME_ENTRY(CL_RED, 'r', COL_RED, ATTR_DULL)
PRINT_SCHEME_ENTRY(CL_YELLOW, 'g', COL_GREEN, ATTR_DULL)
PRINT_SCHEME_ENTRY(CL_BLUE, 'b', COL_CYAN, ATTR_DULL)
PRINT_SCHEME_END;
void sat_print_init(void *o, int dim, sat_print_speed_t print_speed);
void sat_print_uninit(void);
static sat_loop_t *loop_queue_active;
static sat_loop_t *loop_queue_success;
static ca_space_t ca_space;
static table_t *table;
static int loop_len, clause_num;
static void loop_fail_reset(void *o);
static void mon_spread_scan(void *o);
static void ca_scan(void *o);
static void mon_spread_init_phase2(void *o);
static code2code_t truth_values[] = {
{CD_ZERO, SAT_TV_FALSE},
{CD_ONE, SAT_TV_TRUE},
{-1}
};
static code2code_t flag2idx[] = {
{FL_QUIESCENT, IDX_QUIESCENT},
{FL_ERASE_LOOP, IDX_ERASE_LOOP},
{FL_COLLISION, IDX_COLLISION},
{FL_BRANCH_SEQ, IDX_BRANCH_SEQ},
{FL_GEN_ZERO, IDX_GEN_ZERO},
{FL_GEN_ONE, IDX_GEN_ONE},
{FL_MONITOR_ACTIVE, IDX_MONITOR_ACTIVE},
{FL_MONITOR_ALLERT, IDX_MONITOR_ALLERT},
{-1}
};
static wind_dir_t cadir2dir(ca_direction_t dir)
{
code2code_t cd2d[] = {
{DR_UP, DIR_NO},
{DR_LEFT, DIR_WE},
{DR_DOWN, DIR_SO},
{DR_RIGHT, DIR_EA},
{-1}
};
return code2code(cd2d, dir);
}
static ca_direction_t dir2cadir(wind_dir_t dir)
{
code2code_t d2cd[] = {
{DIR_NO, DR_UP},
{DIR_WE, DR_LEFT},
{DIR_SO, DR_DOWN},
{DIR_EA, DR_RIGHT},
{-1}
};
return code2code(d2cd, dir);
}
static ca_direction_t dir_opposite(ca_direction_t dir)
{
ca_direction_t opp;
switch (dir)
{
case DR_UP:
opp = DR_DOWN;
break;
case DR_LEFT:
opp = DR_RIGHT;
break;
case DR_RIGHT:
opp = DR_LEFT;
break;
case DR_DOWN:
opp = DR_UP;
break;
default:
opp = DR_UNDEFINED;
break;
}
return opp;
}
static void error_set(void)
{
signal_set(SIG_ERROR);
}
static void signal_loop(void *o)
{
signal_set(SIG_LOOP);
}
static ca_t *pointing_neighbour(void *o)
{
ca_t *ca = (ca_t *)o;
ca_direction_t opp = dir_opposite(ca->dir);
return opp == DR_UNDEFINED ? NULL : ca->neighbours[cadir2dir(opp)];
}
static sat_loop_t *loop_remove_active(sat_loop_t *loop)
{
sat_loop_t **q, *ret;
for (q = &loop_queue_active; (ret = *q) && *q != loop; q = &(*q)->next);
*q = (*q)->next;
event_add_once(signal_loop, NULL);
return ret;
}
static void loop_del(sat_loop_t *loop)
{
int i;
for (i = 0; i < loop_len; i++)
((ca_t *)loop->ca_list[i])->loop = NULL;
free(loop->ca_list);
free(loop);
}
static void loop_new(void *o)
{
ca_t *tmp = (ca_t *)o, **ca_list;
sat_loop_t *loop;
int i;
if (!(loop = calloc(1, sizeof(sat_loop_t))))
goto Error;
if (!(ca_list = calloc(loop_len, sizeof(ca_t *)))) {
free(loop);
goto Error;
}
loop->ca_list = ca_list;
for (i = 0; i < loop_len; i++) {
tmp->loop = loop;
loop->ca_list[i] = tmp;
tmp = pointing_neighbour(tmp);
}
loop->next = loop_queue_active;
loop_queue_active = loop;
return;
Error:
error_set();
}
static void loop_new_set(ca_t *ca)
{
event_add(loop_new, ca);
}
static void loop_fail(void *o)
{
loop_del(loop_remove_active((sat_loop_t *)o));
}
static void loop_fail_set(void *o)
{
sat_loop_t *loop = (sat_loop_t *)o;
int i;
for (i = 0; i < loop_len && loop->ca_list[i]->flag != FL_ERASE_LOOP;
i++);
event_add_once(i < loop_len ? loop_fail_reset : loop_fail, o);
}
static void loop_fail_reset(void *o)
{
event_add_once(loop_fail_set, o);
}
static void loop_success(void *o)
{
sat_loop_t *loop = (sat_loop_t *)o;
loop->is_success = 1;
loop_remove_active(loop);
loop->next = loop_queue_success;
loop_queue_success = loop;
}
static void loop_success_set(sat_loop_t *loop)
{
int i;
if (loop->is_success)
return;
for (i = 0; i < loop_len; i++) {
if (loop->ca_list[i]->mon.is_active ||
loop->ca_list[i]->flag == FL_ERASE_LOOP) {
return;
}
}
event_add_once(loop_success, loop);
}
static int mon_init(monitor_t *mon, int offset, int pos_tbl_sz)
{
if (!(mon->pos_tbl = calloc(pos_tbl_sz, sizeof(int))))
return -1;
mon->pos_tbl_sz = pos_tbl_sz;
mon->offset = offset;
mon->is_active = 1;
return 0;
}
static void mon_uninit(void *o)
{
monitor_t *mon = (monitor_t *)o;
if (mon->pos_tbl)
free(mon->pos_tbl);
mon->pos_tbl = NULL;
mon->pos_tbl_sz = 0;
mon->offset = 0;
mon->is_active = 0;
}
static void mon_uninit_set(ca_t *ca)
{
event_add(mon_uninit, &ca->mon);
}
static void mon_deactivate(void *o)
{
((monitor_t *)o)->is_active = 0;
}
static void mon_deactivate_set(monitor_t *mon)
{
event_add(mon_deactivate, mon);
}
static ca_t *ca_alloc(int num)
{
ca_t *ca;
int i;
if (!(ca = calloc(num, sizeof(ca_t))))
return NULL;
for (i = 0; i < num; i++) {
ca[i].code = CD_QUIESCENT;
ca[i].dir = DR_QUIESCENT;
ca[i].col = CL_QUIESCENT;
ca[i].flag = FL_QUIESCENT;
ca[i].id = ID_UNDEFINED;
ca[i].loop = NULL;
}
return ca;
}
static void ca_space_free(ca_t **sp, int hight, int legnth)
{
int i, j;
if (!sp)
return;
for (i = 0; i < hight; i++) {
for (j = 0; j < legnth; j++)
mon_uninit(&sp[i][j].mon);
free(sp[i]);
}
free(sp);
}
static ca_t **ca_space_alloc(int hight, int length)
{
ca_t **sp;
int i;
if (!(sp = calloc(hight, sizeof(ca_t *))))
return NULL;
for (i = 0; i < hight && (sp[i] = ca_alloc(length)); i++);
if (i < hight)
goto Error;
for (i = 0; i < hight; i++) {
int j;
for (j = 0; j < length; j++) {
wind_dir_t dir;
for (dir = DIR_NO; dir <= DIR_NW; dir++) {
coordinate_euclid_t cell, neighbour;
cell.n = i;
cell.m = j;
coordinate_moore_neighbour(hight, length, &cell,
dir, &neighbour);
sp[i][j].neighbours[dir] =
&sp[neighbour.n][neighbour.m];
}
}
}
return sp;
Error:
ca_space_free(sp, i, length);
return NULL;
}
static int sp_dim_get(int var_num)
{
return ((2 * var_num) + 1) * ((var_num + 2) / 3) + (6 * var_num) + 2;
}
static void sp_uninit(ca_space_t *s)
{
ca_space_free(s->sp, s->sp_dim, s->sp_dim);
}
static int is_bound(ca_t *ca)
{
return ca->dir != DR_QUIESCENT;
}
static void sat_event_loop_clear(void *o)
{
ca_space_t *s = (ca_space_t *)o;
int i;
/* delete all calls to CAs in the event loop */
for (i = 0; i < s->sp_dim; i++) {
int j;
for (j = 0; j < s->sp_dim; j++) {
ca_t *ca = &s->sp[i][j];
if (is_bound(ca))
event_del_all(ca);
}
}
/* delete the call to the printing function */
event_del_once(s);
}
static void sat_error_handler(void *o)
{
ca_space_t *s = (ca_space_t *)o;
sp_uninit(s);
event_add(s->cb->fail_cb, s->cb->fail_data);
}
static void sat_success_handler(void *o)
{
ca_space_t *s = (ca_space_t *)o;
sp_uninit(s);
event_add(s->cb->success_cb, s->cb->success_data);
}
static int monitor_offset_get(ca_t *ca)
{
ca_t *nei = pointing_neighbour(ca);
return nei->mon.offset ? nei->mon.offset + 1 : 1;
}
static int monitor_pos_tbl_sz_get(int offset)
{
int p = clause_num / loop_len;
int q = clause_num - p * loop_len;
return p + (offset <= q ? 1 : 0);
}
static void mon_spread_do(void *o)
{
ca_t *ca = (ca_t *)o;
int offset = monitor_offset_get(ca);
int pos_tbl_sz = monitor_pos_tbl_sz_get(offset);
ca_t *nei_ahead = ca->neighbours[cadir2dir(ca->dir)];
ca_t *nei_left =
ca->neighbours[wind_dir_offset(cadir2dir(ca->dir), -2)];
ca_t *ca_propogate = is_bound(nei_left) &&
pointing_neighbour(nei_left) == ca ? nei_left : nei_ahead;
if (mon_init(&(ca->mon), offset, pos_tbl_sz))
error_set();
else
event_add_once(mon_spread_scan, ca_propogate);
}
static void mon_spread_scan(void *o)
{
ca_t *ca = (ca_t *)o;
ca_direction_t sw_dir, sw_dir_bad;
if (ca->flag == FL_ERASE_LOOP)
return;
if (!is_bound(ca) ||
(!is_bound(ca->neighbours[wind_dir_offset(cadir2dir(ca->dir),
-2)]) && !is_bound(ca->neighbours[cadir2dir(ca->dir)]))) {
/* if the cell is not bound or neither the cell ahead of it
* and the cell to its left are bound initiate rescanning */
event_add(mon_spread_init_phase2, o);
return;
}
sw_dir = ca->neighbours[wind_dir_offset(cadir2dir(ca->dir), -3)]->dir;
sw_dir_bad = dir2cadir(wind_dir_offset(cadir2dir(ca->dir), -2));
if (ca->mon.pos_tbl || sw_dir == sw_dir_bad ||
!monitor_pos_tbl_sz_get(monitor_offset_get(ca))) {
/* do not spread a monitor to the cell if:
* - it has already been spread
* - the cell is connecting a loop to its replicate
* - monitoring of all clauses has already been accounted for */
return;
}
event_add(mon_spread_do, ca);
}
static void mon_spread_init_phase2(void *o)
{
event_add(mon_spread_scan, o);
}
static void mon_spread_init_phase2_set(void *o)
{
ca_t *ca = (ca_t *)o;
event_add(mon_spread_init_phase2, ca->neighbours[cadir2dir(ca->dir)]);
}
static void mon_spread_init_phase1(void *o)
{
event_add(mon_spread_init_phase2_set, o);
}
static void mon_spread_init_phase1_set(ca_t *ca)
{
event_add(mon_spread_init_phase1, ca);
}
static int is_mon_danger(monitor_t *mon, int pos, int distance)
{
return table ? mon->pos_tbl[pos] == (table->record_num - distance) : 0;
}
static int is_mon_fail(monitor_t *mon, int pos)
{
return is_mon_danger(mon, pos, 1);
}
static int is_mon_allert(monitor_t *mon, int pos)
{
return is_mon_danger(mon, pos, 2);
}
static ca_monitor_state_t mon_scan(monitor_t *mon, int id, ca_code_t code)
{
int i;
ca_monitor_state_t status = MN_NOOP;
if (!mon->is_active)
goto Exit;
for (i = 0; i < mon->pos_tbl_sz; i++) {
int clause = mon->offset + i*loop_len;
if (mon->pos_tbl[i] == -1)
continue;
if (!table->t[mon->pos_tbl[i]][clause].id &&
table->t[mon->pos_tbl[i]][clause].tv ==
SAT_TV_TAUTOLOGY) {
return MN_FAIL;
}
if (table->t[mon->pos_tbl[i]][clause].id != id)
continue;
if (table->t[mon->pos_tbl[i]][clause].tv == SAT_TV_PARADOX ||
table->t[mon->pos_tbl[i]][clause].tv !=
code2code(truth_values, code)) {
if (code == CD_ZERO || code == CD_ONE)
mon->pos_tbl[i] = -1;
continue;
}
if (is_mon_fail(mon, i))
return MN_FAIL;
status = is_mon_allert(mon, i) ? MN_ALLERT : MN_ACTIVE;
mon->pos_tbl[i]++;
}
for (i = 0; i < mon->pos_tbl_sz; i++) {
if (mon->pos_tbl[i] != -1)
goto Exit;
}
return MN_SUCCESS;
Exit:
return status;
}
static int is_flag_degenerate(ca_flag_t flag)
{
return flag & (FL_ERASE_LOOP | FL_COLLISION);
}
static int is_neighbour_degenerate(ca_t *ca)
{
ca_direction_t dir;
ROTATE(dir, ca, ca->neighbours[cadir2dir(X)]->flag == FL_ERASE_LOOP);
return dir == DR_UNDEFINED ? 0 : 1;
}
static ca_flag_t is_replicate_arm_retract(ca_t *ca)
{
ca_direction_t dir;
/* X is the rotating ca_direction_t */
ROTATE(dir, ca, ca->neighbours[cadir2dir(X)]->dir == X &&
ca->neighbours[cadir2dir(X)]->flag == FL_COLLISION);
return dir;
}
static int is_replication_complete(ca_t *ca)
{
ca_direction_t dir;
ROTATE(dir, ca, ca->neighbours[cadir2dir(X)]->code == CD_DETACH ||
ca->neighbours[cadir2dir(dir_opposite(X))]->code == CD_DETACH);
return dir == DR_UNDEFINED ? 0 : 1;
}
static int is_arm_extrusion_failure(ca_t *ca)
{
ca_direction_t dir;
ROTATE(dir, ca, ca->dir == X && !ca->neighbours[cadir2dir(X)]->dir &&
ca->neighbours[wind_dir_offset(cadir2dir(X), -2)]->code ==
CD_FLOW);
return dir == DR_UNDEFINED ? 0 : 1;
}
/* flag setting functions */
static void flag_set_queiscent(void *o)
{
((ca_t *)o)->flag = FL_QUIESCENT;
}
static void flag_set_erase_loop(void *o)
{
((ca_t *)o)->flag = FL_ERASE_LOOP;
}
static void flag_set_collision(void *o)
{
((ca_t *)o)->flag = FL_COLLISION;
}
static void flag_set_branch_seq(void *o)
{
((ca_t *)o)->flag = FL_BRANCH_SEQ;
}
static void flag_set_gen_zero(void *o)
{
((ca_t *)o)->flag = FL_GEN_ZERO;
}
static void flag_set_gen_one(void *o)
{
((ca_t *)o)->flag = FL_GEN_ONE;
}
static void flag_set_monitor_active(void *o)
{
((ca_t *)o)->flag = FL_MONITOR_ACTIVE;
}
static void flag_set_monitor_allert(void *o)
{
((ca_t *)o)->flag = FL_MONITOR_ALLERT;
}
static void flag_set(ca_t *ca, ca_flag_t flag)
{
event_func_t func;
switch (flag)
{
case FL_QUIESCENT:
func = flag_set_queiscent;
break;
case FL_ERASE_LOOP:
func = flag_set_erase_loop;
break;
case FL_COLLISION:
func = flag_set_collision;
break;
case FL_BRANCH_SEQ:
func = flag_set_branch_seq;
break;
case FL_GEN_ZERO:
func = flag_set_gen_zero;
break;
case FL_GEN_ONE:
func = flag_set_gen_one;
break;
case FL_MONITOR_ACTIVE:
func = flag_set_monitor_active;
break;
case FL_MONITOR_ALLERT:
func = flag_set_monitor_allert;
break;
default:
return;
}
event_add(func, ca);
}
/* flags scanning function */
static void flags_scan(ca_t *ca)
{
ca_direction_t dir;
/* if any of the destruction flags are set reset the flag */
if (is_flag_degenerate(ca->flag)) {
flag_set(ca, FL_QUIESCENT);
if (ca->loop)
loop_fail_set(ca->loop);
return;
}
/* if ca is bound and there is a destruction flag nearby, set the
* destruction flag */
if (ca->dir && is_neighbour_degenerate(ca)) {
flag_set(ca, FL_ERASE_LOOP);
return;
}
/* if a collision flag nearby copy the retracting flag until reaching a
* corner, then set the branch sequence flag */
dir = is_replicate_arm_retract(ca);
if (dir != DR_UNDEFINED) {
wind_dir_t side = wind_dir_offset(cadir2dir(dir), -2);
if (ca->neighbours[side]->dir == dir)
flag_set(ca, FL_BRANCH_SEQ);
else
flag_set(ca, FL_COLLISION);
return;
}
/* if the replication arm is closing on itself set the flag to mutate a
* new one bit */
if (ca->code && ca->flag & (FL_QUIESCENT | FL_MONITOR_ACTIVE |
FL_MONITOR_ALLERT) && is_replication_complete(ca)) {
flag_set(ca, FL_GEN_ZERO);
return;
}
/* arm extrusion failure checking. a failed attempt at new arm
* extrusion will result in the FL_BRANCH_SEQ flag being set in the
* corner. this allows further attempts at the other directions later */
if (ca->code == CD_ARM_EXT_END && is_arm_extrusion_failure(ca)) {
flag_set(ca, FL_BRANCH_SEQ);
return;
}
/* change FL_GEN_ZERO to FL_GEN_ONE after seeing a CD_TURN_LEFT in
* order to mutate a new one bit */
if (ca->flag == FL_GEN_ZERO && ca->code == CD_TURN_LEFT) {
flag_set(ca, FL_GEN_ONE);
return;
}
/* CD_ARM_EXT_START always clears the flag */
if (ca->code == CD_ARM_EXT_START) {
flag_set(ca, FL_QUIESCENT);
return;
}
/* reset FL_GEN_ONE/FL_GEN_ZERO to either FL_QUIESCENT or
* FL_BRANCH_SEQ */
if (ca->flag & (FL_GEN_ZERO | FL_GEN_ONE)) {
if (pointing_neighbour(ca)->code == CD_FLOW) {
flag_set(ca, FL_QUIESCENT);
return;
}
if (ca->code == CD_UNEXPLORED_0 ||
ca->code == CD_UNEXPLORED_1) {
flag_set(ca, FL_BRANCH_SEQ);
return;
}
}
if (ca->id) {
switch (mon_scan(&ca->mon, ca->id, ca->code))
{
case MN_SUCCESS:
mon_deactivate_set(&ca->mon);
break;
case MN_ACTIVE:
flag_set(ca, FL_MONITOR_ACTIVE);
break;
case MN_ALLERT:
flag_set(ca, FL_MONITOR_ALLERT);
break;
case MN_FAIL:
flag_set(ca, FL_ERASE_LOOP);
break;
case MN_NOOP:
/* do nothing */
default:
break;
}
}
}
/* code set functions */
static void code_set_quiescent(void *o)
{
((ca_t *)o)->code = CD_QUIESCENT;
event_add_once(ca_scan, o);
}
static void code_set_flow(void *o)
{
((ca_t *)o)->code = CD_FLOW;
event_add_once(ca_scan, o);
}
static void code_set_grow(void *o)
{
ca_t *ca = (ca_t *)o;
ca_t *nei_ahead = ca->neighbours[cadir2dir(ca->dir)];
ca->code = CD_GROW;
event_add_once(ca_scan, ca);
event_add_once(ca_scan, nei_ahead);
}
static void code_set_turn_left(void *o)
{
ca_t *ca = (ca_t *)o;
ca_t *nei_left = ca->neighbours[wind_dir_offset(cadir2dir(ca->dir),
-2)];
ca->code = CD_TURN_LEFT;
event_add_once(ca_scan, ca);
event_add_once(ca_scan, nei_left);
}
static void code_set_arm_ext_start(void *o)
{
ca_t *ca = (ca_t *)o;
ca_t *nei_ahead = ca->neighbours[cadir2dir(ca->dir)];
ca->code = CD_ARM_EXT_START;
event_add_once(ca_scan, ca);
event_add_once(ca_scan, nei_ahead);
}
static void code_set_arm_ext_end(void *o)
{
((ca_t *)o)->code = CD_ARM_EXT_END;
event_add_once(ca_scan, o);
}
static void code_set_detach(void *o)
{
((ca_t *)o)->code = CD_DETACH;
event_add_once(ca_scan, o);
}
static void code_set_unexplored_0(void *o)
{
((ca_t *)o)->code = CD_UNEXPLORED_0;
event_add_once(ca_scan, o);
}
static void code_set_unexplored_1(void *o)
{
((ca_t *)o)->code = CD_UNEXPLORED_1;
event_add_once(ca_scan, o);
}
static void code_set_zero(void *o)
{
((ca_t *)o)->code = CD_ZERO;
event_add_once(ca_scan, o);
}
static void code_set_one(void *o)
{
((ca_t *)o)->code = CD_ONE;
event_add_once(ca_scan, o);
}
static void code_set_tautology(void *o)
{
((ca_t *)o)->code = CD_TAUTOLOGY;
event_add_once(ca_scan, o);
}
static void code_set_paradox(void *o)
{
((ca_t *)o)->code = CD_PARADOX;
event_add_once(ca_scan, o);
}
static void code_set(ca_t *ca, ca_code_t code)
{
event_func_t func;
switch (code)
{
case CD_QUIESCENT:
func = code_set_quiescent;
break;
case CD_FLOW:
func = code_set_flow;
break;
case CD_GROW:
func = code_set_grow;
break;
case CD_TURN_LEFT: