Put your code... (was Re: gotos
kenny at uiucdcsb.cs.uiuc.edu
kenny at uiucdcsb.cs.uiuc.edu
Tue Apr 26 10:07:00 AEST 1988
[Part 3: Operations on parallel data structures]
EXAMPLE 5 looks like:
search: if (A[i] < x) {
if (L[i] != 0) { i = L[i]; goto search; }
else { L[i] = j; goto insert; }
}
else
if (R[i] != 0) { i = R[i]; goto search; }
else { R[i] = j; goto insert; }
}
insert: A[j] = x;
L[j] = 0;
R[j] = 0;
Knuth claims that the _goto_ structure brings out the nature of the
parallel operations on the L and R links. If this be true, why not
collapse the code for these parallel operations? I would much rather
see:
for (;;) {
register int *ptr;
if (A[i] < x)
ptr = & L [i];
else
ptr = & R [i];
if (!*ptr) {
*ptr = j;
break;
}
i = *ptr;
}
A [j] = x;
L [j] = 0;
R [j] = 0;
We have handled this by generating one reference to the member of the
L or R array in question, and then using this reference, rather than
duplicating the code.
The argument about performance is difficult to address in this case,
since it is heavily dependent on the machine architecture. Anyone who
is worried about performance for code like this, though, will use
pointers rather than array indices and make the L and R structure
components rather than arrays, so the question is moot.
Moral: If you're doing the same thing to parallel data structures, run
the same code on the two structures rather than complicating your
control flow by duplicating the code.
[End of part 3.]
More information about the Comp.lang.c
mailing list