| 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 |
|
|---|
| 27 | static void printPreds(unsigned int leash) {
|
|---|
| 28 | if (leash==0)
|
|---|
| 29 | return;
|
|---|
| 30 | if (!bobs_hasCurrent()) {
|
|---|
| 31 | printf(" |");
|
|---|
| 32 | return;
|
|---|
| 33 | }
|
|---|
| 34 | int userValue = bobs_getCurrent();
|
|---|
| 35 | bobs_movePrev();
|
|---|
| 36 | printPreds(leash - 1);
|
|---|
| 37 | printf(" %d", userValue);
|
|---|
| 38 | }
|
|---|
| 39 | static void printSuccs(unsigned int leash) {
|
|---|
| 40 | if (leash==0)
|
|---|
| 41 | return;
|
|---|
| 42 | if (!bobs_hasCurrent()) {
|
|---|
| 43 | printf(" |");
|
|---|
| 44 | return;
|
|---|
| 45 | }
|
|---|
| 46 | int userValue = bobs_getCurrent();
|
|---|
| 47 | printf(" %d", userValue);
|
|---|
| 48 | bobs_moveNext();
|
|---|
| 49 | printSuccs(leash - 1);
|
|---|
| 50 | }
|
|---|
| 51 | static void explore(int here) {
|
|---|
| 52 | bobs_seek(here);
|
|---|
| 53 | if (!bobs_hasCurrent()) {
|
|---|
| 54 | printf(" <X>");
|
|---|
| 55 | return;
|
|---|
| 56 | }
|
|---|
| 57 | bobs_movePrev();
|
|---|
| 58 | printPreds(2);
|
|---|
| 59 |
|
|---|
| 60 | bobs_seek(here);
|
|---|
| 61 | int userValue = bobs_getCurrent();
|
|---|
| 62 | printf(" <%d>", userValue);
|
|---|
| 63 |
|
|---|
| 64 | bobs_moveNext();
|
|---|
| 65 | printSuccs(2);
|
|---|
| 66 | }
|
|---|
| 67 | static void exploreRange(int validFrom, int validTo) {
|
|---|
| 68 | int gapsize = validTo - validFrom;
|
|---|
| 69 | int midpoint = (validTo + validFrom) / 2;
|
|---|
| 70 |
|
|---|
| 71 | int listFirstmost, listLastmost;
|
|---|
| 72 | switch(bobs_op_polarity) {
|
|---|
| 73 | case insfirst:
|
|---|
| 74 | listFirstmost = validTo;
|
|---|
| 75 | listLastmost = validFrom;
|
|---|
| 76 | break;
|
|---|
| 77 | case inslast:
|
|---|
| 78 | listFirstmost = validFrom;
|
|---|
| 79 | listLastmost = validTo;
|
|---|
| 80 | break;
|
|---|
| 81 | default:
|
|---|
| 82 | assert(0 && "unsupported bobs_op_movement value");
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | explore(listFirstmost);
|
|---|
| 86 | printf(" ...");
|
|---|
| 87 | if (gapsize > 5) {
|
|---|
| 88 | explore(midpoint);
|
|---|
| 89 | printf(" ...");
|
|---|
| 90 | }
|
|---|
| 91 | explore(listLastmost);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | void bobs_report(void) {
|
|---|
| 95 | if (bobs_prog_rollover_flag) {
|
|---|
| 96 | printf("%8zd + ? (rolling over)\n", bobs_ops_completed);
|
|---|
| 97 | } else {
|
|---|
| 98 | printf("%8zd + %6d/2 + %6d/2", bobs_ops_completed, bobs_prog_inserting, bobs_prog_removing);
|
|---|
| 99 |
|
|---|
| 100 | int validFrom, validTo;
|
|---|
| 101 | switch(bobs_op_movement) {
|
|---|
| 102 | case stack:
|
|---|
| 103 | validFrom = 0;
|
|---|
| 104 | validTo = MIN((signed)bobs_prog_inserting-1, (signed)NumNodes - (signed)bobs_prog_removing - 1);
|
|---|
| 105 | break;
|
|---|
| 106 | case queue:
|
|---|
| 107 | validFrom = (signed)bobs_prog_removing;
|
|---|
| 108 | validTo = (signed)bobs_prog_inserting-1;
|
|---|
| 109 | break;
|
|---|
| 110 | default:
|
|---|
| 111 | assert(0 && "unsupported bobs_op_movement value");
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | printf(" ");
|
|---|
| 115 |
|
|---|
| 116 | if (validTo < validFrom) {
|
|---|
| 117 | printf(" (list is empty)");
|
|---|
| 118 | } else {
|
|---|
| 119 | exploreRange(validFrom, validTo);
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | printf("\n");
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|