Subj : Sets
To   : Sean Dennis
From : Scott Little
Date : Mon Oct 12 2009 05:45 pm

[ On 2009-10-09 at 11:31:50, Sean Dennis wrote to All ]

SD> Telegard (the BBS software I use) uses sets to do some
SD> "bit-twiddling" with certain settings on a user's record, such as if
SD> the user is "hidden", et al.

Sets are fancy bitfields.  A set is made of one or more values of an ordinal
type, which can be reduced to an integer and therefore a bit number.

eg. an enumerated (ordinal) type, specifying the days of the week:

 type
  Day = (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
  Days = set of Day;

 var
  d: Days;

Then you can set one or more days:

 d := [Mon, Wed, Fri];

Since ord(Mon) = 0, ord(Wed) = 2, ord(Fri) = 4, the above will flip bits 0, 2
and 4 to on.  Byte(d) = 21.

The compiler will automatically size the set to accomodate all the potential
values.  Since there are seven days, sizeof(Days) = 1. A set of Char would be
32 bytes (256 possible values divided by 8 bits per byte).

You can also do set math, eg.

 if Mon in [Mon, Wed, Fri] - [Wed, Fri] then writeln('Wed and Fri are gone,
Mon is left');
 if not (Mon in [Mon, Wed, Fri] - [Mon]) then writeln('Mon is gone, Wed and
Fri are left');
 if Mon in [Mon, Wed, Fri] * [Mon, Fri] then writeln('Mon and Fri are common
to both sets');

The same can be done manually with bitwise operations, but sets are easier to
read.

Clear as mud? ;)


-- Scott Little  [fidonet#3:712/848 / [email protected]]

--- The government solution to a problem is usually as bad as the problem.
* Origin: What the hell are you waiting for? Christmas? (3:712/848)