type
TTimeRecord<T> =
record
Time: TDateTime;
Value: T;
end;
type
TDataRecord = TTimeRecord<Double>;
TDataArray = TArray<TDataRecord>;
TIndexRecord = TTimeRecord<Int64>;
TIndexArray = TArray<TIndexRecord>;
function MakeIndexRecordComparer: IComparer<TIndexRecord>;
begin
Result := TComparer<TIndexRecord>.Construct(
function(
const Left, Right: TIndexRecord): Integer
begin
Result := CompareDateTime(Left.Time, Right.Time);
end
);
end;
var
FIndex: TIndexArray;
procedure SortIndex;
begin
TArray.Sort<TIndexRecord>(FIndexArray, MakeIndexRecordComparer);
end;
function FindIndexValue(
const ATime: TDateTime;
out AIndex: Int64): Boolean;
var
foundIndex: NativeInt;
begin
var item: TIndexRecord;
item.Time := ATime;
Result := TArray.BinarySearch<TIndexRecord>(FIndexArray, item, foundIndex, MakeIndexRecordComparer);
{ falls bei nicht gefunden der kleinere Record verwendet werden soll }
// if not Result then
// Dec(foundIndex);
if not Result
and (foundIndex < 0)
then begin
AIndex := -1;
Exit;
end;
AIndex := FIndexArray[foundIndex].Value;
end;