CalLite: Use binary search in TCalDateList (patch by howardpc). Some cleanup.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@5342 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2016-11-11 21:52:31 +00:00
parent a7f21d2f4c
commit 479d7fbea9

View File

@ -515,16 +515,33 @@ begin
end; end;
function TCalDateList.IndexOfDate(ADate: TDate): Integer; function TCalDateList.IndexOfDate(ADate: TDate): Integer;
// to do: Since the list is always ordered use a binary search here
var var
i: Integer; lower, higher, mid, truncADate, truncMidDate: integer;
function Compare: integer;
begin
if (truncMidDate < truncADate) then
Exit(-1)
else if (truncMidDate > truncADate) then
Exit(+1)
else
Exit(0);
end;
begin begin
for i:=0 to FList.Count-1 do lower := 0;
if SameDate(GetDate(i), ADate) then begin higher := Pred(FList.Count);
Result := i; truncADate := trunc(ADate);
exit; while (lower <= higher) do begin
mid := (lower + higher) shr 1;
truncMidDate:=trunc(GetDate(mid));
case Compare of
-1: lower := Succ(mid);
+1: higher := Pred(mid);
0: Exit(mid);
end; end;
Result := -1; end;
Exit(-1);
end; end;
procedure TCalDateList.Insert(AIndex: Integer; ADate: TDate); procedure TCalDateList.Insert(AIndex: Integer; ADate: TDate);