--- ray/src/cal/cnt.c 2022/04/20 19:13:12 1.4 +++ ray/src/cal/cnt.c 2022/04/22 15:52:50 1.8 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: cnt.c,v 1.4 2022/04/20 19:13:12 greg Exp $"; +static const char RCSid[] = "$Id: cnt.c,v 1.8 2022/04/22 15:52:50 greg Exp $"; #endif /* * cnt.c - simple counting program. @@ -128,11 +128,11 @@ tree_alloc(long alen) exit(1); } *troot = ht; /* first byte is tree height */ - for (i = 256; i--; ) { /* assign bit count table */ + for (i = 256; i--; ) { /* assign 0-bit count table */ int b; - buf[i] = 0; + buf[i] = 8; for (b = i; b; b >>= 1) - buf[i] += b&1; + buf[i] -= b&1; } return(troot); } @@ -161,9 +161,11 @@ incr_counter(uby8 *ctrp, int n) { n = tree_br[n].cntr_siz; - while (n-- > 0) /* LSB first */ - if (++(*ctrp++)) - break; + while (! ++(*ctrp++)) /* LSB first */ + if (--n <= 0) { + fputs("Shuffle occupancy overflow!\n", stderr); + exit(1); /* means we sized something wrong */ + } } @@ -190,17 +192,17 @@ eat_nth_leaf(uby8 *brp, long ski) incr_counter(brp, lvl); /* we intend to eat one */ brp += tree_br[lvl--].cntr_siz; /* drop a level */ } - b = 0; /* browse the leaves */ - while (ski >= 8-buf[*brp]) { /* buf contains bit counts */ + while (ski >= buf[*brp]) { /* browse the leaves */ pos += 8; - ski -= 8-buf[*brp++]; + ski -= buf[*brp++]; /* buf contains 0-bit counts */ } - while (ski >= 0) { /* target zero leaf bit */ + b = 0; /* find target bit in byte */ + while ((ski -= !(*brp & 1<= 0) { pos++; - ski -= !(*brp & 1< 0) /* allocate and print random array entries */ - print_shuf(n, eat_nth_leaf(tree_root, random() % alen--)); + print_shuf(n, eat_nth_leaf(tree_root, irandom(alen--))); free(tree_root); /* all done */ } @@ -251,7 +253,7 @@ shuffle(long *n) myshuf[i] = i; /* perform Fisher-Yates shuffle */ for (i = 0; i < alen-1; i++) { - int ix = random()%(alen-i) + i; + int ix = irandom(alen-i) + i; int ndx = myshuf[i]; myshuf[i] = myshuf[ix]; myshuf[ix] = ndx;