views:

103

answers:

1
+1  Q: 

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?

+2  A: 

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;
skamradt
I would exit after finding the first occurrence.
Svein Bringsli
@sveinbringsli, you are correct. updated example.
skamradt