1 | #include <assert.h>
|
---|
2 |
|
---|
3 | #ifdef __needExternC
|
---|
4 | #error Only compile observation.c (once) in plain C
|
---|
5 | #endif
|
---|
6 |
|
---|
7 | #include <stdio.h>
|
---|
8 | #include <sys/param.h>
|
---|
9 |
|
---|
10 | #include "observation.h"
|
---|
11 |
|
---|
12 | static int NumNodes;
|
---|
13 |
|
---|
14 | void bobs_init(int NumNodes_) {
|
---|
15 | // printf("at bobs_init; bobs_ops_completed = %zd\n", bobs_ops_completed);
|
---|
16 |
|
---|
17 | // todo: register a signal handler that prints progress info
|
---|
18 | // note function bobs_XXX in driver.c are untested until that's working
|
---|
19 | // to be driven as:
|
---|
20 | // ./a.out & # then, very quickly
|
---|
21 | // kill -s USR2 $!
|
---|
22 |
|
---|
23 | NumNodes = NumNodes_;
|
---|
24 | }
|
---|
25 |
|
---|
26 | #ifdef BOBS_SHOW_ADDRESSES
|
---|
27 | #define SNAP \
|
---|
28 | void * userAddr = bobs_getCurrentLoc(); \
|
---|
29 | int userValue = bobs_getCurrentVal();
|
---|
30 | #define SHOW(pfx, sfx) \
|
---|
31 | printf(" " pfx "%d@%p" sfx, userValue, userAddr);
|
---|
32 | #else
|
---|
33 | #define SNAP \
|
---|
34 | int userValue = bobs_getCurrentVal();
|
---|
35 | #define SHOW(pfx, sfx) \
|
---|
36 | printf(" " pfx "%d" sfx, userValue);
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | static void printPreds(unsigned int leash) {
|
---|
40 | if (leash==0)
|
---|
41 | return;
|
---|
42 | if (!bobs_hasCurrent()) {
|
---|
43 | printf(" |");
|
---|
44 | return;
|
---|
45 | }
|
---|
46 | SNAP
|
---|
47 | bobs_movePrev();
|
---|
48 | printPreds(leash - 1);
|
---|
49 | SHOW("", "")
|
---|
50 | }
|
---|
51 | static void printSuccs(unsigned int leash) {
|
---|
52 | if (leash==0)
|
---|
53 | return;
|
---|
54 | if (!bobs_hasCurrent()) {
|
---|
55 | printf(" |");
|
---|
56 | return;
|
---|
57 | }
|
---|
58 | SNAP
|
---|
59 | SHOW("", "")
|
---|
60 | bobs_moveNext();
|
---|
61 | printSuccs(leash - 1);
|
---|
62 | }
|
---|
63 | static void explore(int here) {
|
---|
64 | bobs_seek(here);
|
---|
65 | if (!bobs_hasCurrent()) {
|
---|
66 | printf(" <X>");
|
---|
67 | return;
|
---|
68 | }
|
---|
69 | bobs_movePrev();
|
---|
70 | printPreds(2);
|
---|
71 |
|
---|
72 | bobs_seek(here);
|
---|
73 | SNAP
|
---|
74 | SHOW("<", ">")
|
---|
75 |
|
---|
76 | bobs_moveNext();
|
---|
77 | printSuccs(2);
|
---|
78 | }
|
---|
79 | static void exploreRange(int validFrom, int validTo) {
|
---|
80 | int gapsize = validTo - validFrom;
|
---|
81 | int midpoint = (validTo + validFrom) / 2;
|
---|
82 |
|
---|
83 | int listFirstmost, listLastmost;
|
---|
84 | switch(bobs_op_polarity) {
|
---|
85 | case insfirst:
|
---|
86 | listFirstmost = validTo;
|
---|
87 | listLastmost = validFrom;
|
---|
88 | break;
|
---|
89 | case inslast:
|
---|
90 | listFirstmost = validFrom;
|
---|
91 | listLastmost = validTo;
|
---|
92 | break;
|
---|
93 | default:
|
---|
94 | assert(0 && "unsupported bobs_op_movement value");
|
---|
95 | }
|
---|
96 |
|
---|
97 | explore(listFirstmost);
|
---|
98 | printf(" ...");
|
---|
99 | if (gapsize > 5) {
|
---|
100 | explore(midpoint);
|
---|
101 | printf(" ...");
|
---|
102 | }
|
---|
103 | explore(listLastmost);
|
---|
104 | }
|
---|
105 |
|
---|
106 | void bobs_report(void) {
|
---|
107 | if (bobs_prog_rollover_flag) {
|
---|
108 | printf("%8zd + ? (rolling over)\n", bobs_ops_completed);
|
---|
109 | } else {
|
---|
110 | printf("%8zd + %6d/2 + %6d/2", bobs_ops_completed, bobs_prog_inserting, bobs_prog_removing);
|
---|
111 |
|
---|
112 | int validFrom, validTo;
|
---|
113 | switch(bobs_op_movement) {
|
---|
114 | case stack:
|
---|
115 | validFrom = 0;
|
---|
116 | validTo = MIN((signed)bobs_prog_inserting-1, (signed)NumNodes - (signed)bobs_prog_removing - 1);
|
---|
117 | break;
|
---|
118 | case queue:
|
---|
119 | validFrom = (signed)bobs_prog_removing;
|
---|
120 | validTo = (signed)bobs_prog_inserting-1;
|
---|
121 | break;
|
---|
122 | default:
|
---|
123 | assert(0 && "unsupported bobs_op_movement value");
|
---|
124 | }
|
---|
125 |
|
---|
126 | printf(" ");
|
---|
127 |
|
---|
128 | if (validTo < validFrom) {
|
---|
129 | printf(" (list is empty)");
|
---|
130 | } else {
|
---|
131 | exploreRange(validFrom, validTo);
|
---|
132 | }
|
---|
133 |
|
---|
134 | printf("\n");
|
---|
135 | }
|
---|
136 | }
|
---|