Sets
From The Oxygene Language Wiki
This is a Language topic
Feel free to add your notes to this topic below.
A set is a collection of ordinal values of the same type. If you are coming to Delphi Prism via C#, the concept of sets may be new to you. Sets are a convenient way of storing and accessing values that belong together.
For example, instead of defining day, month and year as 1, 2 and 3, it is more convenient to declare them as a set.
Although you can fill sets with integers, you will typically use them to hold Enums:
Type MySet1 = set of 1000; // set with elements 0..999 MySet2 = set of (Abc, Def, Ghi); // implicit enums
You can also declare a set using an explicit enum:
type MyEnum = (Abc, Def, Ghi); MySet3 = Set of MyEnum; // creates a copy of all possible values in the enum;
A set is a value type and typical syntax is as follows:
var x: MySet3 := [MySet.Def, MySet.Abc]; // note MySet, not MyEnum
Additional Syntax
set of byte // same as set of 256 set of 256 // same as set of 0..255 set of Word // same as set of 65536 set of 65536 // same as set of 0..65535
Set Operators
The following operators take two sets as operands and return a third set:
| | | |
|---|---|---|
|
+ |
union |
[a,b,e]+[c,e,f] = [a,b,c,e,f] |
|
- |
difference |
[a,b,c,d]-[a,c] = [b,d] |
|
* |
intersection |
[a,b,e]*[c,e,f] = [e] |
These operators compare two sets:
| | | |
|---|---|---|
|
= |
equality |
[a,b,c] = [a,b,c] // true [a,b,c] = [a,b,c,d] // false [a,b,e] = [a,b,d] // false [a,b,c,d] = [a,b,c] // false |
|
<> |
inequality |
[a,b,c] <> [a,b,c] // false [a,b,c] <> [a,b,c,d] // true [a,b,e] <> [a,b,d] // true [a,b,c,d] <> [a,b,c] // true |
|
<= |
subset |
[a,b,c] <= [a,b,c] // true [a,b,c] <= [a,b,c,d] // true [a,b,e] <= [a,b,d] // false [a,b,c,d] <= [a,b,c] // false |
|
>= |
superset |
[a,b,c] >= [a,b,c] // true [a,b,c] >= [a,b,c,d] // false [a,b,e] >= [a,b,d] // false [a,b,c,d] >= [a,b,c] // true |
Finally, the in operator lets you test set membership:
| | | |
|---|---|---|
|
in |
membership |
if a in [a,b,c] then .. // True if x not in foo then... // shortcut for if not (x in foo) then |
Set Rules
- Elements of the set cannot have a value < 0 (this is why signed datatypes are not supported).
- The set uses 1 bit possible values, 8 bits per byte.
- The maximum number of set values is limited to the available memory required to store that bit-set.
- The generic class used for sets is immutable, set1 + set2 returns a new set.
- A set with nothing does not occupy memory for all possible bit values, only the highest bit that is set will define how much byte memory will be allocated.
- The supported set sub-types are: byte, word, char and enumerated types.
External use of Sets
Sets may be used from non-Delphi Prism code, including all operators except 'in'.
In needs to be called via the op_in method or via the regular methods on the set class in C#.
See Also
Area: Oxygene Language
Compiler version: Oxygene 5
Language Glossary — Keywords — Types — FAQ — How To