3.3.3 Set types

Free Pascal supports the set types as in Turbo Pascal. The prototype of a set declaration is:

_________________________________________________________________________________________________________ Set Types
-- --set type-set- of -ordinal type------------------------------------
___________________________________________________________________

Each of the elements of SetType must be of type TargetType. TargetType can be any ordinal type with a range between 0 and 255. A set can contain maximally 255 elements. The following are valid set declaration:

Type  
  Junk = Set of Char;  
 
  Days = (Mon, Tue, Wed, Thu, Fri, Sat, Sun);  
  WorkDays : Set of days;

Given this set declarations, the following assignment is legal:

WorkDays := [ Mon, Tue, Wed, Thu, Fri];

The operators and functions for manipulations of sets are listed in table (3.6).


Table 3.6: Set Manipulation operators

Operation Operator
Union +
Dierence -
Intersection *
Add element include
Delete element exclude

Two sets can be compared with the <> and = operators, but not (yet) with the < and > operators. The compiler stores small sets (less than 32 elements) in a Longint, if the type range allows it. This allows for faster processing and decreases program size. Otherwise, sets are stored in 32 bytes.

include and exclude operators

The include and exclude operators are in fact procedures (like Inc and Dec), i.e. use the statements
  Include(Set, Element);
  Exclude(Set, Element);
to include/exclude Element to/from Set.

Note: You could also do

  Set += [Element];
  Set -= [Element];

-- Bram Kuijvenhoven on March 27, 2006 12:29 PM (view details)