Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.2 9/5/84; site reed.UUCP
Path: utzoo!watmath!clyde!cbosgd!ihnp4!houxm!mhuxj!mhuxr!ulysses!allegra!mit-eddie!genrad!decvax!tektronix!reed!alexis
From: alexis@reed.UUCP (Alexis Dimitriadis)
Newsgroups: net.lang.c
Subject: Re: Set type
Message-ID: <989@reed.UUCP>
Date: Tue, 26-Feb-85 02:08:15 EST
Article-I.D.: reed.989
Posted: Tue Feb 26 02:08:15 1985
Date-Received: Tue, 5-Mar-85 14:22:40 EST
References: <10983@watmath.UUCP> <21000020@uiucuxc.UUCP>
Organization: Reed College, Portland, Oregon
Lines: 28

> 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  
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