i having some problem with TList and BinarySearch. I have this structure:
PDoubleEstr = record
Double: array [1..2] of Integer;
Count: Integer;
end;
TDoubleEstr = TList<PDoubleEstr>;
and declare:
var oDoubleEstr: TDoubleEstr;
Then, i Initialize the list correctly using this function:
procedure Initialize;
var
iIndex1, iIndex2: Integer;
rDoubleEstr: PDoubleEstr;
begin
oDoubleEstr.Clear;
for iIndex1 := 1 to 89 do
for iIndex2 := Succ(iIndex1) to 90 do
begin
with rDoubleEstr do
begin
Double[1] := iIndex1;
Double[2] := iIndex2;
Count := 0;
end;
oDoubleEstr.Add(rDoubleEstr);
end;
end;
Now, i define this procedure:
procedure Element(const First: Integer; const Second: Integer; var Value: PDoubleEstr);
begin
with Value do
begin
Double[1] := First;
Double[2] := Second;
end;
end;
then in my main procedure:
procedure Main;
var
Value: PDoubleEstr;
Index: Integer;
flag: boolean;
begin
Element(89, 90, Value);
flag := oDoubleEstr.BinarySearch(Value, Index, TDelegatedComparer<PDoubleEstr>.Construct(Compare));
Writeln(Flag:5, oDoubleEstr[Index].Double[1]:5, oDoubleEstr[Index].Double[2]:5);
end;
It turn me an error. In sense that elements with index "Index" not correspond to element that i have typed. Of course, oDoubleEstr is sorted correctly, and not understand where i mistake. The construct Compare is so defined:
function TDouble.Compare(const Left, Right: PDoubleEstr): Integer;
begin
Result := Sign(Left.Double[1] - Right.Double[2]);
end;
and i think that error is in construct, but not understood as solve it. In general i want check if element exist, and if exist get the index. As element i mean only field Double in my case. I try to explain better, my list is so populate:
1 2 // element 0
1 3
......
1 90
......
88 89
88 90
89 90 // element 4004
if i set Element as (89,90) it should be turn me as index the value: 4004 and true if found it or false otherwise. Thanks for help.