| 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, unsigned int leash) {
|
|---|
| 64 | bobs_seek(here);
|
|---|
| 65 | if (!bobs_hasCurrent()) {
|
|---|
| 66 | printf(" <X>");
|
|---|
| 67 | return;
|
|---|
| 68 | }
|
|---|
| 69 | bobs_movePrev();
|
|---|
| 70 | printPreds(leash);
|
|---|
| 71 |
|
|---|
| 72 | bobs_seek(here);
|
|---|
| 73 | SNAP
|
|---|
| 74 | SHOW("<", ">")
|
|---|
| 75 |
|
|---|
| 76 | bobs_moveNext();
|
|---|
| 77 | printSuccs(leash);
|
|---|
| 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 | printf(" v%d..%d ", listFirstmost, listLastmost);
|
|---|
| 98 |
|
|---|
| 99 | explore(listFirstmost, 6);
|
|---|
| 100 | printf(" ...");
|
|---|
| 101 | // if (gapsize > 5) {
|
|---|
| 102 | // explore(midpoint);
|
|---|
| 103 | // printf(" ...");
|
|---|
| 104 | // }
|
|---|
| 105 | explore(listLastmost, 6);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | void bobs_report(void) {
|
|---|
| 109 | if (bobs_prog_rollover_flag) {
|
|---|
| 110 | printf("%8zd + ? (rolling over)\n", bobs_ops_completed);
|
|---|
| 111 | } else {
|
|---|
| 112 | printf("%8zd + %6d/2 + %6d/2, %6d@e %6d_u", bobs_ops_completed, bobs_prog_inserting, bobs_prog_removing, bobs_prog_removing_end, *bobs_prog_rem_pos);
|
|---|
| 113 |
|
|---|
| 114 | int validFrom, validTo;
|
|---|
| 115 | switch(bobs_op_movement) {
|
|---|
| 116 | case stack:
|
|---|
| 117 | validFrom = 0;
|
|---|
| 118 | validTo = MIN((signed)bobs_prog_inserting-1, (signed)NumNodes - (signed)*bobs_prog_rem_pos - 1);
|
|---|
| 119 | break;
|
|---|
| 120 | case queue:
|
|---|
| 121 | validFrom = (signed)*bobs_prog_rem_pos;
|
|---|
| 122 | validTo = (signed)bobs_prog_inserting-1;
|
|---|
| 123 | break;
|
|---|
| 124 | default:
|
|---|
| 125 | assert(0 && "unsupported bobs_op_movement value");
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | printf(" ");
|
|---|
| 129 |
|
|---|
| 130 | if (validTo < validFrom) {
|
|---|
| 131 | printf(" (list is empty)");
|
|---|
| 132 | } else {
|
|---|
| 133 | exploreRange(validFrom, validTo);
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | printf("\n");
|
|---|
| 137 | }
|
|---|
| 138 | }
|
|---|