Sets

d d at nomail.com
Thu Feb 8 08:11:18 PST 2007


> I think creating a whole different data structure just for the set isn't
worth the
> trouble.

Sets done properly:

type
  TCarManufacturer = (cmFord, cmRenault, cmVauxhall, cmFiat); //
enumeration - each element has an ordinal

// value - not numerical
type
  TCarManufacturers = set of TCarManufacturer;

const
  DEFAULT_CAR_MANUF = [cmFord, cmVauhall]; //square brackets denote "set" in
code

var
  myset: TCarManufacturers;
  myintersection: TCarManufacturers;
begin
  myset := [cmFord]; //Assign "ford" to the set

  myset := myset + DEFAULT_CAR_MANUF; //still only contains one "cmFord"

  myintersection := []; //empty set - sets don't need to be initialised
however...

  myintersection := myset * [cmFord]; //intersection

  myset := myset - myintersection; //removes the common values

end;

The following operators take sets as operands.

Operator     Operation      Operand types   Result type    Example
+                 union              set                     set
Set1 + Set2
-                 difference       set                      set
S - T
*                 intersection     set                     set
S * T
<=               subset            set                      Boolean       Q
<= MySet
>=               superset         set                      Boolean       S1
>= S2
=                 equality          set                       Boolean
S2 = MySet
<>               inequality        set                      Boolean
MySet <> S1
in                 membership   ordinal, set           Boolean       A in
Set1





More information about the Digitalmars-d mailing list