The following operators take sets as operands.
Operator
|
Operation |
Operand types |
Result type |
Example |
|
+ |
union |
set |
set
|
Set1 + Set2 |
|
- |
difference |
set |
set
|
S – T |
|
* |
intersection |
set |
set
|
S * T |
|
<= |
subset |
set |
Boolean
|
Q <= MySet |
|
>= |
superset |
set |
Boolean
|
S1 >= S2 |
|
= |
equality |
set |
Boolean
|
S2 = MySet |
|
<> |
inequality |
set |
Boolean
|
MySet <> S1 |
|
in |
membership |
ordinal, set |
Boolean
|
A in Set1 |
The following
rules apply to +, -, and *.
· An ordinal O is in X + Y if and only if O is in X or Y (or both). O
is in X - Y if and only if O is in X but not in Y. O is in X * Y if and only if
O is in both X and Y.
· The result of a +, -, or * operation is of the type set of
A..B, where A is the smallest ordinal value in the result set and B is the
largest.
The
following rules apply to <=, >=, =, <>,
and in.
· X <= Y is True just in case every member of X is a
member of Y; Z >= W is equivalent to W <= Z. U = V
is True just in case U and V contain exactly the same members;
otherwise, U <> V is True.
· For an ordinal O and a set S, O in S is True just in case O is a member of S.
下列运算符接受集合为操作数:
运算符
|
操作 |
操作数类型 |
结果类型 |
范例 |
|
+ |
并集 |
集合 |
集合
|
Set1 + Set2 |
|
- |
差集 |
集合 |
集合
|
S – T |
|
* |
交集 |
集合 |
集合
|
S * T |
|
<= |
子集 |
集合 |
Boolean
|
Q <= MySet |
|
>= |
超集 |
集合 |
Boolean
|
S1 >= S2 |
|
= |
相等 |
集合 |
Boolean
|
S2 = MySet |
|
<> |
不等 |
集合 |
Boolean
|
MySet <> S1 |
|
in |
成员 |
序数,集合 |
Boolean
|
A in Set1 |
下列规则适用于+、-、* 等运算符:
· 序数O是X + Y的成员,当且仅当O是X的成员或O是Y的成员(或者都是)。O是X - Y的成员当且仅当O是X的成员并且不是Y的成员。O是X * Y的成员当且仅当O是X的成员并且O是Y的成员。
· 运算符+、-或*对集合运算的结果形如set of A..B,这里的A是结果中最小的序数,B是最大的序数。
下列规则适用于<=、>=、=、<>、in等运算符:
· 当X中的每个成员都是Y的成员时,X <= Y的结果为True;Z >= W 等价于W <= Z;当U和V包括完全相同的成员时,U = V的值为True,否则U <> V的值为True。
· 对于序数O和集合S,当O是S的成员时,表达式O in S的值为True。
编者注
序数(Ordinal)和序数型(Ordinal Types)并非仅指如Byte、Word、Integer等整数和整数类型,也包括Char、Boolean等类型。因此“序数”也并非仅指“数”。下面的代码举例说明了序数和集合的类型定义、变量声明和一般使用:
type
TMyBoolean = Boolean;
TMyBooleanSet = set of
TMyBoolean;
TWeekDay = (Mon, Tue, Wed, Thu, Fri,
Sat, Sun);
TOffDays = set of TWeekDay;
TWorkDays = set of
TWeekDay;
var
BSet: TMyBooleanSet;
ODays: TOffDays;
WDays: TWorkDays;
...
BSet := [True];
ODays := [Sat, Sun];
WDays := [Mon, Tue] + [Wed, Thu, Fri] +
ODays - [Sun];
...