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


准备做一个爬虫程序,目测需要查找的数量级略大,使用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.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值