集合中的每個元素其實只占一個二進制位, 不足 8 個元素的集合只需要 1 個字節.
先觀察集合的大小:
Type
TSet1 = set of (a1,a2,a3,a4,a5,a6,a7,a8); {剛好對應一個字節的 8 個位}
TSet2 = set of (b1,b2,b3); {只用一個字節中的 3 個位, 也要占一個字節}
TSet3 = set of (c1,c2,c3,c4,c5,c6,c7,c8,c9); {需要 9 個位, 一個字節容不下了}
TSet4 = set of Char;
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessageFmt('%d, %d, %d, %d' , [SizeOf(TSet1), //1
SizeOf(TSet2), //1
SizeOf(TSet3), //2
SizeOf(TSet4) //32
]);
end;
洞察集合的二進制表示:
{查看二進制的函數}
function ToBin(p: PByteArray; b: Integer): string;
var
i,j: Integer;
begin
Result := StringOfChar('0', b * 8);
for i := 0 to b - 1 do for j := 0 to 7 do
if Odd(p^[b-1-i] shr j) then Result[i*8 + 8 - j] := '1';
end;
procedure TForm1.Button1Click(Sender: TObject);
var
set1: set of (A,B,C,D,E,F,G,H);
begin
set1 := [];
ShowMessage(ToBin(@set1, SizeOf(set1))); //00000000
set1 := [A,B,C,D,E,F,G,H];
ShowMessage(ToBin(@set1, SizeOf(set1))); //11111111
set1 := [A,B,C];
ShowMessage(ToBin(@set1, SizeOf(set1))); //00000111
set1 := [A,B,C,H];
ShowMessage(ToBin(@set1, SizeOf(set1))); //10000111
end;
甚至可以把集合看成一個數字:
procedure TForm1.Button1Click(Sender: TObject);
type
TSet = set of (A,B,C,D,E,F,G,H);
var
s1,s2,s3,s4: TSet;
begin
s1 := [];
s2 := [A,B,C,D,E,F,G,H];
s3 := [A,B,C];
s4 := [A,B,C,H];
ShowMessage(IntToStr(Byte(s1))); //0
ShowMessage(IntToStr(Byte(s2))); //255
ShowMessage(IntToStr(Byte(s3))); //7
ShowMessage(IntToStr(Byte(s4))); //135
end;
用集合的方式重新做前一次的例子(窗體設計與測試效果同前):
var set1: set of 0..7; {准備用自定義的集合變量 set1 儲存下面的 8 種狀態}
procedure TForm1.FormCreate(Sender: TObject);
begin
CheckListBox1.Items.CommaText := 'A,B,C,D,E,F,G,H';
Button1.Caption := '保存狀態';
Button2.Caption := '恢復狀態';
Button3.Caption := '全選';
Button4.Caption := '全不選';
Button1.Tag := 1;
Button2.Tag := 2;
Button3.Tag := 3;
Button4.Tag := 4;
Button2.OnClick := Button1.OnClick;
Button3.OnClick := Button1.OnClick;
Button4.OnClick := Button1.OnClick;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
begin
if TButton(Sender).Tag = 1 then set1 := [];
for i := 0 to CheckListBox1.Count - 1 do
case TButton(Sender).Tag of
1: if CheckListBox1.Checked[i] then Include(set1, i);
2: CheckListBox1.Checked[i] := i in set1;
3: CheckListBox1.Checked[i] := True;
4: CheckListBox1.Checked[i] := False;
end;
end;
實例觀察 TFontStyles 集合:
{查看二進制的函數}
function ToBin(p: PByteArray; b: Integer): string;
var
i,j: Integer;
begin
Result := StringOfChar('0', b * 8);
for i := 0 to b - 1 do for j := 0 to 7 do
if Odd(p^[b-1-i] shr j) then Result[i*8 + 8 - j] := '1';
end;
procedure TForm1.Button1Click(Sender: TObject);
var
fs: TFontStyles;
begin
Font.Style := [fsBold, fsItalic, fsUnderline];
fs := Font.Style;
Text := ToBin(@fs, SizeOf(fs));
end;
procedure TForm1.Button2Click(Sender: TObject);
var
fs: TFontStyles;
begin
Font.Style := [];
fs := Font.Style;
Text := ToBin(@fs, SizeOf(fs));
end;
TFontStyles 集合的測試效果圖:
接下來學習 TBits 類; 對 "位" 的操作 TBits 應該是最直觀的.