Difference between revisions of "Set"
m (→Bitmasks: corrected enum. to reflect values in comment) |
(Describe direct set assignation) |
||
(5 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
{{Set}} | {{Set}} | ||
+ | |||
+ | |||
+ | Back to [[Data type|data types]]. | ||
+ | |||
+ | Back to [[Reserved words]]. | ||
+ | |||
== Introduction == | == Introduction == | ||
Line 7: | Line 13: | ||
For example let's consider this enumeration: | For example let's consider this enumeration: | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang="pascal"> |
− | TSpeed = (spVerySlow,spSlow, | + | TSpeed = (spVerySlow,spSlow,spAverage,spFast,spVeryFast); |
</syntaxhighlight> | </syntaxhighlight> | ||
And this set: | And this set: | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang="pascal"> |
TPossibleSpeeds = set of TSpeed | TPossibleSpeeds = set of TSpeed | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | Constant instances of TPossibleSpeeds can be defined using brackets to hold set elements: | + | [[Constant]] instances of TPossibleSpeeds can be defined using [[square brackets|brackets]] to hold set elements: |
− | <syntaxhighlight> | + | <syntaxhighlight lang="pascal"> |
const | const | ||
RatherSlow = [spVerySlow,spSlow]; | RatherSlow = [spVerySlow,spSlow]; | ||
Line 29: | Line 35: | ||
== Manipulating sets == | == Manipulating sets == | ||
− | + | You can assign a set's contents directly: | |
− | <syntaxhighlight> | + | <syntaxhighlight lang="pascal"> |
+ | var | ||
+ | SomeSpeeds: TPossibleSpeeds = [spAverage]; | ||
+ | begin | ||
+ | SomeSpeeds := [spSlow,spVerySlow]; | ||
+ | end; | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Two [[Function|functions]] defined in the [[RTL]] [[System unit]] are used to manipulate set elements individually: [[Include]](ASet,AValue) and [[Exclude]](ASet,AValue). | ||
+ | |||
+ | <syntaxhighlight lang="pascal"> | ||
var | var | ||
SomeSpeeds: TPossibleSpeeds; | SomeSpeeds: TPossibleSpeeds; | ||
Line 44: | Line 60: | ||
then to call the setter. | then to call the setter. | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang="pascal"> |
procedure TSomething.DoSomething(Sender: TFarObject); | procedure TSomething.DoSomething(Sender: TFarObject); | ||
var | var | ||
Line 55: | Line 71: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | The | + | The [[Reserved_word|reserved word]] [[In|<syntaxhighlight lang="pascal" inline>in</syntaxhighlight>]] is also used to test if a value is in a set. It's usually used in this fashion: |
− | <syntaxhighlight> | + | <syntaxhighlight lang="pascal"> |
var | var | ||
CanBeSlow: Boolean; | CanBeSlow: Boolean; | ||
Line 64: | Line 80: | ||
begin | begin | ||
CanBeSlow := (spVerySlow in SomeSpeeds) or (spSlow in SomeSpeeds); | CanBeSlow := (spVerySlow in SomeSpeeds) or (spSlow in SomeSpeeds); | ||
+ | end; | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | 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: | ||
+ | |||
+ | <syntaxhighlight lang="pascal"> | ||
+ | var | ||
+ | SomeSpeeds: TPossibleSpeeds; | ||
+ | begin | ||
+ | if SomeSpeeds = [] then SomeSpeeds := [spAverage]; | ||
end; | end; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 71: | Line 97: | ||
Sets can be used to create bitmasks as shown in the example. | Sets can be used to create bitmasks as shown in the example. | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang="pascal"> |
(* | (* | ||
− | FLAG_A | + | The set [FLAG_A, FLAG_C] will be stored like this: |
− | FLAG_B | + | |
− | + | TFlags : 0000'0101 | |
+ | │││ | ||
+ | FLAG_C ──────────┘││ | ||
+ | FLAG_B ───────────┘│ | ||
+ | FLAG_A ────────────┘ | ||
*) | *) | ||
type | type | ||
− | TFlag = (FLAG_A | + | TFlag = (FLAG_A, FLAG_B, FLAG_C); |
TFlags = set of TFlag; | TFlags = set of TFlag; | ||
Latest revision as of 21:39, 16 July 2022
│
Deutsch (de) │
English (en) │
suomi (fi) │
français (fr) │
русский (ru) │
Back to data types.
Back to Reserved words.
Introduction
A Set encodes many values from an enumeration into an Ordinal type.
For example let's consider this enumeration:
TSpeed = (spVerySlow,spSlow,spAverage,spFast,spVeryFast);
And this set:
TPossibleSpeeds = set of TSpeed
Constant instances of TPossibleSpeeds can be defined using brackets to hold set elements:
const
RatherSlow = [spVerySlow,spSlow];
RatherFast = [spFast,spVeryFast];
RatherSlow and RatherFast are some Set of TSpeed.
Manipulating sets
You can assign a set's contents directly:
var
SomeSpeeds: TPossibleSpeeds = [spAverage];
begin
SomeSpeeds := [spSlow,spVerySlow];
end;
Two functions defined in the RTL System unit are used to manipulate set elements individually: Include(ASet,AValue) and Exclude(ASet,AValue).
var
SomeSpeeds: TPossibleSpeeds;
begin
SomeSpeeds := [];
Include(SomeSpeeds,spVerySlow);
Include(SomeSpeeds,spVeryFast);
end;
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.
procedure TSomething.DoSomething(Sender: TFarObject);
var
LocalCopy: TPossibleSpeeds;
begin
LocalCopy := Sender.PossibleSpeeds; // getter to local
Include(LocalCopy,spVerySlow);
Sender.PossibleSpeeds := LocalCopy; // local to setter.
end;
The reserved word in
is also used to test if a value is in a set. It's usually used in this fashion:
var
CanBeSlow: Boolean;
const
SomeSpeeds = [Low(TSpeed)..High(TSpeed)];
begin
CanBeSlow := (spVerySlow in SomeSpeeds) or (spSlow in SomeSpeeds);
end;
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:
var
SomeSpeeds: TPossibleSpeeds;
begin
if SomeSpeeds = [] then SomeSpeeds := [spAverage];
end;
Bitmasks
Sets can be used to create bitmasks as shown in the example.
(*
The set [FLAG_A, FLAG_C] will be stored like this:
TFlags : 0000'0101
│││
FLAG_C ──────────┘││
FLAG_B ───────────┘│
FLAG_A ────────────┘
*)
type
TFlag = (FLAG_A, FLAG_B, FLAG_C);
TFlags = set of TFlag;
var
Flags: TFlags;
[..]
Flags:= [FLAG_A, FLAG_C];
if FLAG_A in Flags then .. // check FLAG_A is set in flags variable
simple data types |
|
---|---|
complex data types |