我有一个 THashedStringList,上面有近 20 万个项目(字符串和对象)。该列表将大量用于搜索其上的项目。在某些情况下,我需要对其进行增量搜索。我已经为使用StartsText例程的增量搜索编写了一个基本示例
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
System.IniFiles, Vcl.StdCtrls,
System.StrUtils;
type
TForm1 = class(TForm)
Memo1: TMemo;
Edit1: TEdit;
Memo2: TMemo;
procedure FormCreate(Sender: TObject);
procedure Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
private
{ Private declarations }
public
ahsStrLst : THashedStringList;
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function generate(cantidad: integer): string;
const
letras_mi = 'abcdefghijklmnopqrstuvwxyz';
var
i: Integer;
begin
Result := '';
for I := 0 to cantidad-1 do
Result := Result + letras_mi[Random(Length(letras_mi)) + 1];
end;
procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
var iPos: Integer;
begin
Memo2.Clear;
Memo2.Update;
for ipos := 0 to ahsStrLst.Count-1 do
if StartsText(Edit1.Text,ahsStrLst[iPos]) then
Memo2.Lines.Add(ahsStrLst[iPos])
end;
procedure TForm1.FormCreate(Sender: TObject);
var ipos:Integer;
begin
ahsStrLst := THashedStringList.Create;
for iPos := 0 to 50000 do
ahsStrLst.AddObject(generate(4),TObject(ipos));//here there will be diffent type of objects
for iPos := 0 to 50000 do
ahsStrLst.AddObject(generate(6),TObject(ipos));
for iPos := 0 to 50000 do
ahsStrLst.AddObject(generate(8),TObject(ipos));
for iPos := 0 to 50000 do
ahsStrLst.AddObject(generate(10),TObject(ipos));
Memo1.Lines.AddStrings(ahsStrLst);
end;
end.
有什么方法可以加快这种增量搜索?