Delphi 中 "位" 的使用(2) - 集合


集合中的每个元素其实只占一个二进制位, 不足 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 集合的测试效果图:

Delphi 中 "位" 的使用(2) - 集合

接下来学习 TBits 类; 对 "位" 的操作 TBits 应该是最直观的.

你可能感兴趣的:(Delphi)