From 479d7fbea9acb65cd2d1e86545d48038529d7273 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Fri, 11 Nov 2016 21:52:31 +0000 Subject: [PATCH] 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 --- components/callite/source/calendarlite.pas | 31 +++++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/components/callite/source/calendarlite.pas b/components/callite/source/calendarlite.pas index 4371dfc8d..703e267cf 100644 --- a/components/callite/source/calendarlite.pas +++ b/components/callite/source/calendarlite.pas @@ -515,16 +515,33 @@ begin end; function TCalDateList.IndexOfDate(ADate: TDate): Integer; -// to do: Since the list is always ordered use a binary search here 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 - for i:=0 to FList.Count-1 do - if SameDate(GetDate(i), ADate) then begin - Result := i; - exit; + lower := 0; + higher := Pred(FList.Count); + truncADate := trunc(ADate); + 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; - Result := -1; + end; + Exit(-1); end; procedure TCalDateList.Insert(AIndex: Integer; ADate: TDate);