Difference between revisions of "Set"
From Free Pascal wiki
Jump to navigationJump to searchm (wiki syntaxhighlight) |
|||
Line 5: | Line 5: | ||
For example let's consider this enumaration: | For example let's consider this enumaration: | ||
− | < | + | <syntaxhighlight> |
TSpeed = (spVerySlow,spSlow,spAVerage,spFast,spVeryFast); | TSpeed = (spVerySlow,spSlow,spAVerage,spFast,spVeryFast); | ||
− | </ | + | </syntaxhighlight> |
And this set: | And this set: | ||
− | < | + | <syntaxhighlight> |
TPossibleSpeeds = set of TSpeed | TPossibleSpeeds = set of TSpeed | ||
− | </ | + | </syntaxhighlight> |
The TPossibleSpeeds can be defined as constant in right brackets: | The TPossibleSpeeds can be defined as constant in right brackets: | ||
− | < | + | <syntaxhighlight> |
const | const | ||
RatherSlow = [spVerySlow,spSlow]; | RatherSlow = [spVerySlow,spSlow]; | ||
RatherFast = [spFast,spVeryFast]; | RatherFast = [spFast,spVeryFast]; | ||
− | </ | + | </syntaxhighlight> |
RatherSlow and RatherFast are some Set of TSpeed. | RatherSlow and RatherFast are some Set of TSpeed. | ||
Line 29: | Line 29: | ||
Usually two compiler functions are used to manipulate a set: '''Include(ASet,AValue)''' and '''Exclude(ASet,AValue)'''. | Usually two compiler functions are used to manipulate a set: '''Include(ASet,AValue)''' and '''Exclude(ASet,AValue)'''. | ||
− | < | + | <syntaxhighlight> |
var | var | ||
SomeSpeeds = TPossibleSpeeds; | SomeSpeeds = TPossibleSpeeds; | ||
Line 37: | Line 37: | ||
Include(SomeSpeeds,spVeryFast); | Include(SomeSpeeds,spVeryFast); | ||
end; | end; | ||
− | </ | + | </syntaxhighlight> |
Sets cannot be directly manipulated if they are published. You usually have to make a local copy, change the local copy and | 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. | then to call the setter. | ||
− | < | + | <syntaxhighlight> |
procedure TSomething.DoSomething(Sender: TFarObject); | procedure TSomething.DoSomething(Sender: TFarObject); | ||
var | var | ||
Line 51: | Line 51: | ||
Sender.PossibleSpeeds := LocalCopy; // local to setter. | Sender.PossibleSpeeds := LocalCopy; // local to setter. | ||
end; | end; | ||
− | </ | + | </syntaxhighlight> |
The Keyword '''in''' is also used to test if a value is in a set. It's usually used in this fashion: | The Keyword '''in''' is also used to test if a value is in a set. It's usually used in this fashion: | ||
− | < | + | <syntaxhighlight> |
var | var | ||
CanBeSlow: Boolean; | CanBeSlow: Boolean; | ||
Line 63: | Line 63: | ||
CanBeSlow := (spVerySlow in SomeSpeeds) or (spSlow in SomeSpeeds); | CanBeSlow := (spVerySlow in SomeSpeeds) or (spSlow in SomeSpeeds); | ||
end; | end; | ||
− | </ | + | </syntaxhighlight> |
Revision as of 13:09, 10 February 2013
Introduction
A Set encodes many values from an enumeration into an Ordinal type.
For example let's consider this enumaration:
TSpeed = (spVerySlow,spSlow,spAVerage,spFast,spVeryFast);
And this set:
TPossibleSpeeds = set of TSpeed
The TPossibleSpeeds can be defined as constant in right brackets:
const
RatherSlow = [spVerySlow,spSlow];
RatherFast = [spFast,spVeryFast];
RatherSlow and RatherFast are some Set of TSpeed.
Manipulating sets
Usually two compiler functions are used to manipulate a set: 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 Keyword 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;