1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
|
{
This file is part of the Free Component Library (FCL)
Copyright (c) 1999-2008 by the Free Pascal development team
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
{****************************************************************************}
{* TBits *}
{****************************************************************************}
Procedure BitsError (const Msg : string);
begin
Raise EBitsError.Create(Msg) at get_caller_addr(get_frame), get_caller_frame(get_frame);
end;
Procedure BitsErrorFmt (const Msg : string; const Args : array of const);
begin
Raise EBitsError.CreateFmt(Msg,args) at get_caller_addr(get_frame), get_caller_frame(get_frame);
end;
{Min function for Longint}
Function liMin(X, Y: Longint): Longint;
begin
Result := X;
if X > Y then Result := Y;
end;
procedure TBits.CheckBitIndex (Bit : longint;CurrentSize : Boolean);
begin
if (bit<0) or (CurrentSize and (Bit >= FBSize)) then
BitsErrorFmt(SErrInvalidBitIndex,[bit]);
if (bit>=MaxBitFlags) then
BitsErrorFmt(SErrIndexTooLarge,[bit])
end;
{ ************* functions to match TBits class ************* }
procedure TBits.setSize(value: longint);
var
newSize, loop: LongInt;
begin
CheckBitIndex(value, false);
if value <> 0 then
newSize := (value shr BITSHIFT) + 1
else
newSize := 0;
if newSize <> FSize then
begin
ReAllocMem(FBits, newSize * SizeOf(longint));
if FBits <> nil then
begin
if newSize > FSize then
for loop := FSize to newSize - 1 do
FBits^[loop] := 0;
end
else if newSize > 0 then
BitsError(SErrOutOfMemory); { isn't ReallocMem supposed to throw EOutOfMemory? }
FSize := newSize;
end;
FBSize := value;
end;
procedure TBits.SetBit(bit : longint; value : Boolean);
var
n: Integer;
begin
grow(bit+1); { validates bit range and adjusts FBSize if necessary }
n := bit shr BITSHIFT;
if value then
FBits^[n] := FBits^[n] or (longword(1) shl (bit and MASK))
else
FBits^[n] := FBits^[n] and not (longword(1) shl (bit and MASK));
end;
function TBits.OpenBit : longint;
var
loop : longint;
loop2 : longint;
begin
result := -1; {should only occur if the whole array is set}
{ map 0 to -1, 1..32 to 0, etc }
for loop := 0 to ((FBSize + MASK) shr BITSHIFT) - 1 do
begin
if FBits^[loop] <> $FFFFFFFF then
begin
for loop2 := 0 to MASK do
begin
if (FBits^[loop] and (longint(1) shl loop2)) = 0 then
begin
result := (loop shl BITSHIFT) + loop2;
if result > FBSize then
result := FBSize;
Exit;
end;
end;
end;
end;
if FSize < MaxBitRec then
result := FBSize; {first bit of next record}
end;
{ ******************** TBits ***************************** }
constructor TBits.Create(theSize : longint = 0 );
begin
FSize := 0;
FBSize := 0;
FBits := nil;
findIndex := -1;
findState := True; { no reason just setting it to something }
if TheSize > 0 then grow(theSize);
end;
destructor TBits.Destroy;
begin
if FBits <> nil then
FreeMem(FBits, FSize * SizeOf(longint));
FBits := nil;
inherited Destroy;
end;
procedure TBits.grow(nbit: longint);
begin
if nbit > FBSize then
SetSize(nbit);
end;
function TBits.getFSize : longint;
begin
result := FSize;
end;
procedure TBits.seton(bit : longint);
begin
SetBit(bit, True);
end;
procedure TBits.clear(bit : longint);
begin
SetBit(bit, False);
end;
procedure TBits.clearall;
var
loop : longint;
begin
for loop := 0 to FSize - 1 do
FBits^[loop] := 0;
{ don't clear FBSize here, it will cause exceptions on subsequent reading bit values }
{ use 'Size := 0' to reset everything and deallocate storage }
end;
function TBits.get(bit : longint) : Boolean;
var
n : longint;
begin
CheckBitIndex(bit,true);
result := False;
n := bit shr BITSHIFT;
if (n < FSize) then
result := (FBits^[n] and (longint(1) shl (bit and MASK))) <> 0;
end;
procedure TBits.CopyBits(BitSet : TBits);
begin
setSize(bitset.Size);
Move(bitset.FBits^,FBits^,FSize*SizeOf(cardinal));
end;
procedure TBits.andbits(bitset : TBits);
var
n : longint;
loop : longint;
begin
if FSize < bitset.getFSize then
n := FSize - 1
else
n := bitset.getFSize - 1;
for loop := 0 to n do
FBits^[loop] := FBits^[loop] and bitset.FBits^[loop];
for loop := n + 1 to FSize - 1 do
FBits^[loop] := 0;
end;
procedure TBits.notbits(bitset : TBits);
var
n : longint;
loop : longint;
begin
if FSize < bitset.getFSize then
n := FSize - 1
else
n := bitset.getFSize - 1;
for loop := 0 to n do
FBits^[loop] := FBits^[loop] xor bitset.FBits^[loop];
end;
procedure TBits.orbits(bitset : TBits);
var
loop : longint;
begin
grow(bitset.Size);
for loop := 0 to bitset.getFSize-1 do
FBits^[loop] := FBits^[loop] or bitset.FBits^[loop];
end;
procedure TBits.xorbits(bitset : TBits);
var
loop : longint;
begin
grow(bitset.Size);
for loop := 0 to bitset.getFSize-1 do
FBits^[loop] := FBits^[loop] xor bitset.FBits^[loop];
end;
function TBits.Equals(Obj : TObject): Boolean;
begin
if Obj is TBits then
Result := Equals(TBits(Obj))
else
Result := inherited Equals(Obj);
end;
function TBits.equals(bitset : TBits) : Boolean;
var
n : longint;
loop : longint;
begin
result := False;
if FSize < bitset.getFSize then
n := FSize - 1
else
n := bitset.getFSize - 1;
for loop := 0 to n do
if FBits^[loop] <> bitset.FBits^[loop] then exit;
if FSize - 1 > n then
begin
for loop := n to FSize - 1 do
if FBits^[loop] <> 0 then exit;
end
else if bitset.getFSize - 1 > n then
for loop := n to bitset.getFSize - 1 do
if bitset.FBits^[loop] <> 0 then exit;
result := True; {passed all tests}
end;
{ us this in place of calling FindFirstBit. It sets the current }
{ index used by FindNextBit and FindPrevBit }
procedure TBits.SetIndex(index : longint);
begin
CheckBitIndex(index,true);
findIndex := index;
end;
{ When state is set to True it looks for bits that are turned On (1) }
{ and when it is set to False it looks for bits that are turned }
{ off (0). }
function TBits.FindFirstBit(state : boolean) : longint;
var
loop : longint;
loop2 : longint;
startIndex : longint;
stopIndex : Longint;
compareVal : cardinal;
begin
result := -1; {should only occur if none are set}
findState := state;
if state = False then
compareVal := $FFFFFFFF { looking for off bits }
else
compareVal := $00000000; { looking for on bits }
for loop := 0 to FSize - 1 do
begin
if FBits^[loop] <> compareVal then
begin
startIndex := loop * 32;
stopIndex:= liMin(StartIndex+31,FBSize -1);
for loop2 := startIndex to stopIndex do
begin
if get(loop2) = state then
begin
result := loop2;
break; { use this as the index to return }
end;
end;
break; {stop looking for bit in records }
end;
end;
findIndex := result;
end;
function TBits.FindNextBit : longint;
var
loop : longint;
begin
result := -1; { will occur only if no other bits set to }
{ current findState }
if findIndex > -1 then { must have called FindFirstBit first }
begin { or set the start index }
for loop := findIndex + 1 to FBSize-1 do
begin
if get(loop) = findState then
begin
result := loop;
break;
end;
end;
findIndex := result;
end;
end;
function TBits.FindPrevBit : longint;
var
loop : longint;
begin
result := -1; { will occur only if no other bits set to }
{ current findState }
if findIndex > -1 then { must have called FindFirstBit first }
begin { or set the start index }
for loop := findIndex - 1 downto 0 do
begin
if get(loop) = findState then
begin
result := loop;
break;
end;
end;
findIndex := result;
end;
end;
|