Mega Code Archive

 
Categories / Delphi / Algorithm Math
 

Using Tbits the built in large boolean array class

Title: Using Tbits- the built in large boolean array class Question: If I want Really large boolean arrays, whats a good way to implement them- the answer- don't- instead use tbits. Answer: Large Boolean arrays In article 1013, the author looked at ways of doing bit manipulation. Delphi also comes with a dynamic large Boolean bit array the class is called tbits and is found in the classes unit. The main advantage of tbits is that the array of bits can be very very large. While you could do it as an array of bytes, anding to extract a particular bit, there is no need- Borland have already implemented it. The array size is set by the size property. All bits are clear by default. The program below demonstrates requesting a 400 million bit array (50 Mb in size) and then setting every bit true. On my 700 Mhz NT box at work it takes about 10 seconds to do this but I did have a couple of things open and its a 128 Mb box so some file swapping took place. You can access the state of any bit by its index so with a variable Bits:tbits. Just access Bits[n] as a Boolean where n is 0..bits.size-1. Finally there is a function OpenBit which gives the location of the first non true bit. Just add a button and a label to a form, add the following unit, cook with Delphi and bingo instant large boolean arrays. unit xxx type TForm1 = class(TForm) Button1: TButton; Label1: TLabel; procedure Button1Click(Sender: TObject); procedure FormDestroy(Sender: TObject); private { Private declarations } public { Public declarations } b:tbits; end; var Form1: TForm1; implementation {$R *.DFM} uses mmsystem; Const Numbits = 400000000; // yes 400 million! procedure TForm1.Button1Click(Sender: TObject); var i:integer; lt : cardinal; begin lt := timegettime; b := tbits.create; b.size := Numbits; for i := 0 to Numbits-1 do b[i]:= true; label1.caption := inttostr(timegettime-lt); end; procedure TForm1.FormDestroy(Sender: TObject); begin b.free; end;