places where sets are useful

J. Shapiro jss at sjuvax.UUCP
Tue Mar 5 16:38:29 AEST 1985


>> type
>> 	mymodes = (insert, error, append, ...);
>> var
>> 	status = set of mymodes;
>> begin
>> ...
>> if (status * [insert, append]) <> [] then begin....
>> ...
>> end
>> ...
>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);

	As the original poster, I thought I had said that enums are problematic
because many compilers don't support them.  Also, this leaves one with the
limit that the number of elements of the set must be <= number of bits in a
long.

It has been suggested that this is equally much a problem in pascal, but in
reality every compiler I have worked with handles the code extensions
automatically. Why should the programmer be obliged to write his own
bitfield manipulation code when this is something the compiler could (and
in pascal does) do perfectly well for itself.

	I am all in favor of hands on control, but only where doing the work
yourself really buys you something.  Any compiler which generates worse
code than I do by hand to handle large bitsets is making a concerted effort
to generate bad code.

	Incidentally, many people provided macros that handle everything for
sets which can be described with the number of bits in a long. *All* of
them said that "extending this for longer sets is trivial."  No one to date
has bothered to cough up code I believe (with one exception - but I would
need to go searching my mailbox to find out who).

	If C is really so adequate to do the job, would somebody cough up a
working set of macros (or a preprocessor - which I believe is the necessary
item) which:

	a) all of us will agree to use
	b) all of us will be satisfied with as something to augment the
		language.
	c) will be useable with any C compiler (excludes enums...)

I can't think of anything which meets all of these qualifications.... I
hope I am wrong ;-)

Jon Shapiro
happily provided



More information about the Comp.lang.c mailing list