2021년 1월 5일 화요일

레코드 형태의 행렬에서 검색하는 방법(TArray.BinarySearch)

type
  TBoll = record
    count: integer;
    name: string;
  end;
var     iArrayBoll: TArray<TBoll>;
procedure TForm1.Button2Click(Sender: TObject);
begin
  SetLength(iArrayBoll, Memo1.Lines.count);
  for var idx := low(iArrayBoll) to high(iArrayBoll) do begin
    iArrayBoll[idx].count := 25;
    iArrayBoll[idx].name := Memo1.Lines[idx];
  end;
  TArray.Sort<TBoll>(iArrayBoll, TComparer<TBoll>.Construct(
    function(const a, b: TBoll): integer begin
      // Result := StrToint(a.name) - StrToint(b.name); //숫자크기우선 정렬
      Result:= CompareText(a.name, b.name); //스트링 정상 정렬
    end));
  for var idx := low(iArrayBoll) to High(iArrayBoll) do begin
    Memo2.Lines.Add(iArrayBoll[idx].name + '=' + iArrayBoll[idx].count.ToString);
  end;
end;


procedure TForm1.Button4Click(Sender: TObject);
var
  idx: integer;
  iBoll: TBoll;
  Found: boolean;
begin
  if rbLineair.Checked then begin
    for idx := 0 to High(iArrayBoll) do
      if Edit1.Text = iArrayBoll[idx].name then begin
        Caption := intToStr(idx);
        Exit;
      end;
  end else begin
    iBoll.name := Edit1.Text;
    iBoll.count := 250;
    Found := TArray.BinarySearch<TBoll>(iArrayBoll, iBoll, idx, TDelegatedComparer<TBoll>.Construct(
      function(const a, b: TBoll): integer begin
        Result := CompareText(a.name, b.name);
      end));
    if Found then Caption := intToStr(idx) else Caption := 'No';
  end;
end;

사전에 정상 정렬되어 있지 않은 경우 아래처럼 오류가 나온다. BinarySearch 전에 스트링의 경우 정상 정렬(숫자크기우선 정렬이면 안됨)이되어 있어야 한다. 이것 때문에 두어시간 해메었었네요.

댓글 없음:

tensorflow gpu 사용하기에서

 tensorflow 설치시 주의해야 한다. # Anything above 2.10 is not supported on the GPU on Windows Native python - m pip install "tensorflow<2.11...