Hi
Here is patch to tBits ans test program.
Its repair FindnextBit.
I also introduce FindNextRaw,
based on it faster version FindNextBit can be done
I can add:
[code]
function  FindFirstBit;
begin
 result:=findNextRaw(-1, state);
end;
function  FindNextBit;
begin
 result:=findNextRaw(findIndex, findState);
end;
[/code]



and can be helpful in  enumerator.

 With this I have and question:
 For me function FindNextRaw should be in protected section
But then we cant use them in enumerator, except when something like friend classes can be used in FPC.
 I only read about this concept, is it possible/plan  in FPC.


--
 Darek




program tbits1;

// test operator Enumerator support for classes

{$mode objfpc}{$H+}
{$APPTYPE CONSOLE}
uses

  unitbits;


var
  Bits: TBits;

procedure test1;
begin
  Bits := TBits.Create(10);

  bits[1]:=true;
  bits[6]:=true;
  writeln(bits.findFirstBit(true));
  writeln(bits.findNextBit);
  writeln(bits.findNextBit);

  bits.Free;
end;



procedure test2;
var
  i : integer;
begin
  Bits := TBits.Create(10);

  bits[1]:=true;
  bits[6]:=true;
  i:=-1;
  repeat
  i:=bits.findNextRaw(i,true);
  writeln(i);
  until i=-1;
  bits.Free;
end;

begin
  test1;
  test2;
end.
Index: bits.inc
===================================================================
--- bits.inc    (wersja 14086)
+++ bits.inc    (kopia robocza)
@@ -282,6 +282,45 @@
 { and when it is set to False it looks for bits that are turned      }
 { off (0).                                                           }
 
+function TBits.FindNextRaw(start : longint;state : boolean) : longint;
+var
+   loop : longint;
+   loop2 : longint;
+   startIndex : longint;
+   stopIndex : longint;
+   compareVal : cardinal;
+begin
+   result := -1; {should only occur if none are set}
+   if state = False then
+      compareVal := $FFFFFFFF  { looking for off bits }
+   else
+      compareVal := $00000000; { looking for on bits }
+   inc(start);
+   loop := (start) div 32;
+   if loop>=fSize then exit;
+   repeat
+      if FBits^[loop] <> compareVal then begin
+
+         startIndex := loop shl BITSHIFT;
+         stopIndex:= startIndex +MASK+1;
+         if start > startIndex then startIndex:=start;
+         if stopIndex>(FBSize ) then stopIndex:=fbSize;
+         writeln(startIndex,' ',stopIndex);
+         for loop2 := startIndex to stopIndex -1 do
+         begin
+            if get(loop2) = state then
+            begin
+               exit(loop2);
+            end;
+         end;
+      end;
+      inc(loop);
+   until (loop>=FSize);
+
+end;
+
+
+
 function TBits.FindFirstBit(state : boolean) : longint;
 var
    loop : longint;
@@ -323,16 +362,14 @@
 function TBits.FindNextBit : longint;
 var
    loop : longint;
-   maxVal : 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              }
-      maxVal := (FSize * 32) - 1;
 
-      for loop := findIndex + 1 to maxVal  do
+      for loop := findIndex + 1 to FBSize-1  do
       begin
          if get(loop) = findState then
          begin
_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply via email to