您的位置:首页 > 编程语言 > Delphi

使用THashedStringList管理结构体数据()

2015-09-02 23:54 495 查看
准备做一个爬虫程序,目测需要查找的数量级略大,使用THashedStringList来管理,方便直接name,value这样查找数据并快速得到结果。写个小demo备忘。

直接上代码,比较简单,多线程使用时加个互斥,不知道效率如何。

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;

type
TUrlInfo=packed record
sKey:string[20];
sTitle:string[255];
sUrl:string[255];
iDepth:Integer;
end;
PUrlInfo=^TUrlInfo;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Button3: TButton;
Button4: TButton;
Button5: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
myHashList:THashedStringList;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);{添加}
var
myPUrlInfo:PUrlInfo;
begin
New(myPUrlInfo);
myPUrlInfo.sKey:=Edit1.Text;
myPUrlInfo.sTitle:=Format('sTitle<%d>',[Random(9999)]);
myPUrlInfo.sUrl:=  Format('URL<%d>',[Random(9999)]);
myPUrlInfo.iDepth:=1;
if myHashList.IndexOf(myPUrlInfo.sKey)=-1 then
begin
myHashList.AddObject(myPUrlInfo.sKey,TObject(myPUrlInfo))
end
else
begin
Memo1.Lines.Add('无法添加,已经存在。');
end;
end;

procedure TForm1.Button2Click(Sender: TObject); {查找}
var
iIndex:Integer;
myPUrlInfo:PUrlInfo;
begin
iIndex:=myHashList.IndexOf(Edit1.Text);
if iIndex<>-1 then
begin
with PUrlInfo(myHashList.Objects[iIndex])^ do
begin
Memo1.Lines.Add(format('找到指定项目:Title:%s Url:%s',[sTitle,sUrl]));
end;
end
else
begin
Memo1.Lines.Add('未找到指定项目。');
end;
end;

procedure TForm1.Button3Click(Sender: TObject); {删除}
var
iIndex:Integer;
myPUrlInfo:PUrlInfo;
begin
iIndex:=myHashList.IndexOf(Edit1.Text);
if iIndex<>-1 then
begin
myPUrlInfo:=PUrlInfo(myHashList.Objects[iIndex]);
myHashList.Delete(iIndex);
Dispose(myPUrlInfo);
Memo1.Lines.Add('删除完毕。')
end
else
begin
Memo1.Lines.Add('未找到指定项目。');
end;
end;

procedure TForm1.Button4Click(Sender: TObject); {修改}
var
iIndex:Integer;
myPUrlInfo:PUrlInfo;
begin
iIndex:=myHashList.IndexOf(Edit1.Text);
if iIndex<>-1 then
begin
with PUrlInfo(myHashList.Objects[iIndex])^ do
begin
sTitle:=Format('NewsTitle<%d>',[Random(9999)]);
sUrl:=  Format('NewURL<%d>',[Random(9999)]);
Memo1.Lines.Add(format('找到指定项目:Title:%s Url:%s',[sTitle,sUrl]));
end;
end
else
begin
Memo1.Lines.Add('未找到指定项目。');
end;
end;

procedure TForm1.Button5Click(Sender: TObject);{遍历}
var
i:Integer;
begin
for i := 0 to myHashList.Count-1 do
begin
with PUrlInfo(myHashList.Objects[i])^ do
begin
Memo1.Lines.Add(Format('[%d]:Key:%s Title:%s  Url:%s',[I+1,sKey,sTitle,sUrl]));
end;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
myHashList:=THashedStringList.Create;
Randomize;
end;

end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息