Sets in C (?) :idea from PASCAL

Leo de Wit leo at philmds.UUCP
Wed Aug 17 19:12:20 AEST 1988


In article <8808121452.AA14152 at ucbvax.berkeley.edu> U23405 at UICVM.BITNET writes:
>(Please don't flame at me, I'm just being curious.) :-)
>
>In PASCAL, there is a datatype called SET that (obviously) deals with sets.
>What I would like to know is whether anyone has tried to implement this
>datatype in C (either by a system header file or by typedefs). Any comments
>would be helpful. (And I mean *ANY* comments)

An obvious way to implement this in C could be using an (array of)
integer(s) and the logical operations &, |, ~, and ^ to perform
operations on these values. The bits of the integer(s) should each
correspond to a data element of a certain type (the type in: set of
type).  So the GAME of this implementation is such that each bit of the
SET MATCHes a value of the base type (been watching tennis a bit
too much lately 8-).

Of course this rules out type checking - unless you do
something like putting the integer(s) into a struct and adding a type
identifier to this struct. The nice thing about using the logical
operators on integers is that this opens the possibility to code the
stuff using macro's, thus improving speed.
The use of bit arrays forbids any large base type (for example: set of
integer).  Dunno how that's handled in Pascal, but I seem to remember
that the language does not pose any restrictions on the base type but
implementations do.  In this case one will have to resort to linked
lists or something alike.

Sounds interesting ... might even give it a try myself 8-).

              Leo.



More information about the Comp.lang.c mailing list