Sign in Join
Wiki copytemporary

Set: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Set

OldNewLine
1+Back to [[Data type|data types]].
2+
3+Back to [[Reserved words]].
4+
5+## Introduction
6+
7+A Set encodes many values from an enumeration into an Ordinal type.
8+
9+For example let's consider this enumeration:
10+
11+``` pascal
12+ TSpeed = (spVerySlow,spSlow,spAverage,spFast,spVeryFast);
13+```
14+
15+And this set:
16+
17+``` pascal
18+ TPossibleSpeeds = set of TSpeed
19+```
20+
21+[[Constant]] instances of TPossibleSpeeds can be defined using [[square brackets|brackets]] to hold set elements:
22+
23+``` pascal
24+ const
25+ RatherSlow = [spVerySlow,spSlow];
26+ RatherFast = [spFast,spVeryFast];
27+```
28+
29+RatherSlow and RatherFast are some Set of TSpeed.
30+
31+## Manipulating sets
32+
33+You can assign a set's contents directly:
34+
35+``` pascal
36+ var
37+ SomeSpeeds: TPossibleSpeeds = [spAverage];
38+ begin
39+ SomeSpeeds := [spSlow,spVerySlow];
40+ end;
41+```
42+
43+Two [[Function|functions]] defined in the [[RTL]] [[System unit]] are used to manipulate set elements individually: [[Include]](ASet,AValue) and [[Exclude]](ASet,AValue).
44+
45+``` pascal
46+ var
47+ SomeSpeeds: TPossibleSpeeds;
48+ begin
49+ SomeSpeeds := [];
50+ Include(SomeSpeeds,spVerySlow);
51+ Include(SomeSpeeds,spVeryFast);
52+ end;
53+```
54+
55+Sets cannot be directly manipulated if they are published. You usually have to make a local copy, change the local copy and then to call the setter.
56+
57+``` pascal
58+ procedure TSomething.DoSomething(Sender: TFarObject);
59+ var
60+ LocalCopy: TPossibleSpeeds;
61+ begin
62+ LocalCopy := Sender.PossibleSpeeds; // getter to local
63+ Include(LocalCopy,spVerySlow);
64+ Sender.PossibleSpeeds := LocalCopy; // local to setter.
65+ end;
66+```
67+
68+The [[Reserved word|reserved word]] [[In|in]] is also used to test if a value is in a set. It's usually used in this fashion:
69+
70+``` pascal
71+ var
72+ CanBeSlow: Boolean;
73+ const
74+ SomeSpeeds = [Low(TSpeed)..High(TSpeed)];
75+ begin
76+ CanBeSlow := (spVerySlow in SomeSpeeds) or (spSlow in SomeSpeeds);
77+ end;
78+```
79+
80+Empty square brackets represent an empty set. You can test if a set currently contains no values at all by comparing against an empty set:
81+
82+``` pascal
83+ var
84+ SomeSpeeds: TPossibleSpeeds;
85+ begin
86+ if SomeSpeeds = [] then SomeSpeeds := [spAverage];
87+ end;
88+```
89+
90+## Bitmasks
91+
92+Sets can be used to create bitmasks as shown in the example.
93+
94+``` pascal
95+(*
96+ The set [FLAG_A, FLAG_C] will be stored like this:
97+
98+ TFlags : 0000'0101
99+ │││
100+ FLAG_C ──────────┘││
101+ FLAG_B ───────────┘│
102+ FLAG_A ────────────┘
103+*)
104+
105+type
106+ TFlag = (FLAG_A, FLAG_B, FLAG_C);
107+ TFlags = set of TFlag;
108+
109+var
110+ Flags: TFlags;
111+
112+[..]
113+ Flags:= [FLAG_A, FLAG_C];
114+ if FLAG_A in Flags then .. // check FLAG_A is set in flags variable
115+```
116+
117+---
118+*From the Free Pascal wiki, page "Set", as it stood on 2022-07-16 after a change by Kirinn, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history