Lazarus

Free Pascal => FPC development => Topic started by: edgarrod71 on August 12, 2017, 01:28:27 am

Title: A propose for TStringList.Find...
Post by: edgarrod71 on August 12, 2017, 01:28:27 am
Code: Pascal  [Select][+][-]
  1. function TStringList.Find(const S: string; out Index: Integer): Boolean;
  2.  
  3. var
  4.   L, R, I: Integer;
  5.   CompareRes: PtrInt;
  6. begin
  7.   Result := false;
  8.   Index:=-1;
  9.   if Not Sorted then
  10.     Raise EListError.Create(SErrFindNeedsSortedList);
  11.   // Use binary search.
  12.   L := 0;
  13.   R := Pred(Count);  // Count - 1;
  14.   if L<=R then  //  while (L<=R) do
  15.   repeat    // repeat is 5-10% faster than while, so
  16.     I := L + (R - L) shr 1; // div 2;  shr is faster than div...
  17.     CompareRes := DoCompareText(S, Flist^[I].FString);
  18.     if (CompareRes>0) then
  19.       L := Succ(I); // I+1;  instead of adding, Succ or Pred only checks... so faster code.
  20.     else begin
  21.       R := Pred(I); // I-1;
  22.       if (CompareRes=0) then begin
  23.          Result := true;
  24.          if (Duplicates<>dupAccept) then
  25.             L := I; // forces end of while loop
  26.       end;
  27.     end;
  28.   until L>R;
  29.   Index := L;
  30. end;
Title: Re: A propose for TStringList.Find...
Post by: marcov on August 12, 2017, 01:44:43 am
So where is the benchmark code to compare ?
Title: Re: A propose for TStringList.Find...
Post by: RAW on August 12, 2017, 10:16:26 am
If the normal TStringlist is too slow, then it should be no problem at all to find a good HashList...
There are several improved TStringlists and several HashLists out there...

Just pick one and play trial and error...  :)
TinyPortal © 2005-2018