Next: , Previous: Complex Number Operations, Up: Library Routines



6.10.7 Set Operations

GPC supports Standard Pascal set operations. In addition it supports the Extended Pascal set operation symmetric difference (set1 >< set2) operation whose result consists of those elements which are in exactly one of the operannds.

It also has a function that counts the elements in the set: a := Card (set1).

In the following description, S1 and S2 are variables of set type, s is of the base type of the set.

S1 := S2
Assign a set to a set variable.
S1 + S2
Union of sets.
S1 - S2
Difference between two sets.
S1 * S2
Intersection of two sets.
S1 >< S2
Symmetric difference
S1 = S2
Comparison between two sets. Returns boolean result. True if S1 has the same elements as S2.
S1 <> S2
Comparison between two sets. Returns boolean result. True if S1 does not have the same elements as S2.
S1 < S2
S2 > S1
Comparison between two sets. Returns boolean result. True if S1 is a strict subset of S2.
S1 <= S2
S2 >= S1
Comparison between two sets. Returns boolean result. True if S1 is a subset of (or equal to) S2.
s in S1
Set membership test between an element s and a set. Returns boolean result. True if s is an element of S1.

The following example demonstrates some set operations. The results of the operations are given in the comments.

     program SetOpDemo;
     
     type
       TCharSet = set of Char;
     
     var
       S1, S2, S3: TCharSet;
       Result: Boolean;
     
     begin
       S1 := ['a', 'b', 'c'];
       S2 := ['c', 'd', 'e'];
       S3 := S1 + S2;       { S3 = ['a', 'b', 'c', 'd', 'e'] }
       S3 := S1 * S2;       { S3 = ['c'] }
       S3 := S1 - S2;       { S3 = ['a', 'b'] }
       S3 := S1 >< S2;      { S3 = ['a', 'b', 'd', 'e'] }
     
       S1 := ['c', 'd', 'e'];
       Result := S1 = S2;   { False }
       Result := S1 < S2;   { False }
       Result := S1 <= S2;  { True }
     
       S1 := ['c', 'd'];
       Result := S1 <> S2;  { True }
       Result := S2 > S1;   { True }
       Result := S2 >= S1   { True }
     end.