[net.lang.c] Set type

jss@sjuvax.UUCP (J. Shapiro) (02/16/85)

[Aren't you hungry...?]

	Though my last posting (a ^= b ^= a ^= b) was a joke, this one is
serious... I am finding pascal's set type to be very useful in my efforts
at writing a compiler generator. So much so that I am using Pascal instead
of C (normally I prefer C because it takes less typing).

	Has anyone implemented a good set of set operators? My compiler (on the
Mac) doesn't have enum...?

	Should not a set type be considered as an addition to the language
which might be worthwhile?

Jon Shapiro
Haverford College

ka@hou3c.UUCP (Kenneth Almquist) (02/20/85)

Creating sets in C is easy.  To declare set element, use #define:

	#define RED 01
	#define BLUE 02
	#define GREEN 04

C has a union operator ("|"), an intersection operator ("&"), and even
a set difference operator ("&~").  Of course, this approach limits you
to sets which contain no more elements than are contained in an integer,
but this limitation also tends to apply to PASCAL sets.

It may be fun to talk about set operators instead of bitwise operators,
but you aren't going to eliminate the existing bit operators from C,
and we certainly don't need *both* set operators and bit operators.
				Kenneth Almquist

ndiamond@watdaisy.UUCP (Norman Diamond) (02/21/85)

> Creating sets in C is easy.  To declare set element, use #define:
> 
> 	#define RED 01
> 	#define BLUE 02
> 	#define GREEN 04
> 
> C has a union operator ("|"), an intersection operator ("&"), and even
> a set difference operator ("&~").  Of course, this approach limits you
> to sets which contain no more elements than are contained in an integer,
> but this limitation also tends to apply to PASCAL sets.
> 				Kenneth Almquist

That limitation does not apply to most PASCAL sets.  The vast majority of
PASCAL compilers allow sets large enough to support SET OF CHAR, but their
integers are not >= 127 bits in size.
-- 

   Norman Diamond

UUCP:  {decvax|utzoo|ihnp4|allegra|clyde}!watmath!watdaisy!ndiamond
CSNET: ndiamond%watdaisy@waterloo.csnet
ARPA:  ndiamond%watdaisy%waterloo.csnet@csnet-relay.arpa

"Opinions are those of the keyboard, and do not reflect on me or higher-ups."

ag4@pucc-h (Angus Greiswald the fourth) (02/21/85)

> C has a union operator ("|"), an intersection operator ("&"), and even
> a set difference operator ("&~").  Of course, this approach limits you
> to sets which contain no more elements than are contained in an integer,
> but this limitation also tends to apply to PASCAL sets.

Well, that doesn't limit you, just like it *doesn't* limit you in Pascal.
Use an array of ints.  Of course, then the code can get crufty.  That's
why sets are usually so inefficient in Pascal.  They are an interesting
idea, though.  Can anyone think of a good example where using sets seems
to be the best solution?  If so, perhaps we could work at coming up
with a good set of macros to handle sets right.

--
Jeff Lewis                                                     vvvvvvvvvvvv
{decvax | hao | cbosgd | masscomp | uiucdcs | sequent | ihnp4}!pur-ee!lewie
                                                               ^^^^^^^^^^^^

gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) (02/21/85)

> 	Should not a set type be considered as an addition to the language
> which might be worthwhile?

Bit-field arrays, anyone?

gwyn@Brl-Vld.ARPA (VLD/VMB) (02/23/85)

I'm not thrilled with Pascal sets, but Boolean matrices can be very
useful, especially in bottom-up parser design.  Arbitrary bit-field
arrays would be useful for this; arrays of chars are too large for
this application.

alexis@reed.UUCP (Alexis Dimitriadis) (02/26/85)

> Creating sets in C is easy.  To declare set element, use #define:
> 
> 	#define RED 01
> 	#define BLUE 02
> 	#define GREEN 04
> 
> C has a union operator ("|"), an intersection operator ("&"), and even
> a set difference operator ("&~").  Of course, this approach limits you
> to sets which contain no more elements than are contained in an integer,
> but this limitation also tends to apply to PASCAL sets.

  Actually, it is easy to have bitfields of arbitrary length.  (And no,
Pascal sets are not necessarily limited to one word).  Define each
element to correspond to an *integer*.  Then define a set as an array of
enough integers to contain a bit for each potential element.  Then
membership of an element is implemented by turning on the bit of  <elementh>
order.  I saw somewhere the following macro that will do the job:

#define setbit(field, bit) (field[(bit)/WORDSIZ] |= (1<< ((bit)%WORDSIZ)))
     Which, for WORDSIZ == 32, can be optimized to
#define setbit(field, bit) (field[(bit)>>5] |= (1 << ((bit) & 037))
  Similar macros can test or unset bits.  Operations between sets can be 
optimized with functions that mask entire words at a time...

  Sorry if I contribute to the torrent of messages that will soon say the
same thing...
			Alexis Dimitriadis