places where sets are useful
Doug Moen
kdmoen at watcgl.UUCP
Thu Feb 28 03:34:10 AEST 1985
> type
> mymodes = (insert, error, append, ...);
> var
> status = set of mymodes;
> begin
> ...
> if (status * [insert, append]) <> [] then begin....
> ...
> end
> ...
> end.
>
> Is much more readable and therefore less likely to get mangled in software
> rewrites then is the exhaustive "if" phrasing.
I agree. Here's how I would write this in C:
enum {insert, error, append, ... };
#define elem(n) (1 << (int)(n))
short status;
...
if (status & (elem(insert) | elem(append))) {
...
/* add 'error' to set 'status' */
status |= elem(error);
/* remove 'error' from set 'status' */
status &= ~elem(error);
More information about the Comp.lang.c
mailing list