FrankCM
FrankCM

Reputation: 207

Outline searching

IDE: Delphi 1 16-bit (Yeah it's old, no I can't afford a newer version or the hardware needed to run it. Besides I'm just learning Delphi so it serves the purpose fine.)

I've loaded a TOutline with names (to represent an address book). They're in sorted order. I want to be able to search the outline.

Possible searches:

  1. the whole name (eg. Frank Allan Smith)
  2. a partial name (eg. Allan)

What's a good, fast way to search the outline?

Upvotes: 1

Views: 230

Answers (1)

skamradt
skamradt

Reputation: 15548

For the sorted list, you can use a binary search for your "BEGINS WITH", but for the contains, you will need to do a linear search (evaluating every item). Its been awhile since I've worked with Delphi 1, but here is the linear search:

The linear search:

function OutlineContains(aOutline:tOutline;aText:string;Repos:boolean):boolean;
var
  aSearch : string;
begin
  Result := false;
  aSearch := uppercase(aText);
  for I := 0 to aOutline.Lines.Count-1 do
  begin
    if Pos(aSearch,Uppercase(aOutline.Lines.Text[i])) <> 0 then
      begin
        Result := true;
        if Repos then
          aOutline.SelectedIndex := i;
        exit;
      end;
  end;
end;

Upvotes: 2

Related Questions