【问题标题】:Delphi sort Stringlist with two fields on firstDelphi 对 Stringlist 排序,首先有两个字段
【发布时间】:2012-08-14 14:20:18
【问题描述】:

我正在实现一个本地缓存来加速 DNS 查找(IP->主机名)。 缓存从 CSV 文件(“1.1.1.1host.example.com”)加载到具有两个字段的 TStringList 中:

TStringList[0] := IPAddress;
TStringList[1] := HostName;

由于我将通过 IP 查询 TStringList,所以我很自然地希望对第一个字段进行排序:

TStringList.sorted := True;

那会照顾好它,以便我可以更快地找到

IPResolved:=TStringList[TStringList.IndexOf('1.1.1.1'),1];

?

谢谢!

【问题讨论】:

  • 显然 CSV 文件在 IP 和主机名之间有一个空格(已被剪切)
  • TStringList 绝对是错误的数据类型。您需要一个包含两个字符串字段 IPAddress 和 HostName 的记录。然后你想使用 TList 来保存这些记录。
  • @David,最好使用字典,因为 OP 想通过 IP 搜索,关键是什么。
  • 您可以将IndexOfName 与这样的Name=Value 配对字符串列表一起使用。或者使用泛型的TDictionary 而不是我在下面发布的字符串列表...
  • TDictionary 听起来不错。使用二进制搜索排序的 TList 是我的想法。字典可能更好。使用汇编程序的想法简直太糟糕了。嘘!

标签: delphi delphi-xe2 tstringlist


【解决方案1】:

免责声明:

这不会回答您如何对字符串列表进行排序或如何将数据加载到字符串列表中。它将为您提供使用哈希表,这比使用字符串列表更有效(40k 名称,按名称搜索的值对)。

替代方案:

由于你有 Delphi XE2,你可以使用泛型 TDictionary 类。它将包含 IP 地址作为键和主机名作为值。在下面的代码中,显示了如何填充字典以及如何通过给定的键(IP 地址)搜索值(主机名):

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Generics.Collections;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    IPList: TDictionary<string, string>;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  // create the TDictionary instance
  IPList := TDictionary<string, string>.Create;
  // here you will read your CSV file and add the items in a loop
  // I've used here some of the major IP addresses for Sweden
  IPList.Add('77.244.224.0', 'Insat Net AB');
  IPList.Add('79.138.128.0', 'Hi3G Access AB');
  IPList.Add('62.181.192.0', 'DGC Access AB');
  IPList.Add('81.216.128.0', 'TDC Swerige AB');
  IPList.Add('80.252.176.0', 'Phonera Networks AB');
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  // release a dictionary instance
  IPList.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  HostName: string;
begin
  // and how to search by the IP address and get the host name if found
  if IPList.TryGetValue('81.216.128.0', HostName) then
    ShowMessage(HostName)
  else
    ShowMessage('IP address not found!');
end;

end.

扩展:

然后您可以简单地扩展上述解决方案以使用结构来存储不仅仅是主机名,例如也是一个主机位置:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Generics.Collections;

type
  TIPData = record
    HostName: string;
    HostLocation: string;
  end;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    IPList: TDictionary<string, TIPData>;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  IPData: TIPData;
begin
  IPList := TDictionary<string, TIPData>.Create;

  IPData.HostName := 'Broadnet Europe France';
  IPData.HostLocation := 'France';
  IPList.Add('78.155.128.0', IPData);

  IPData.HostName := 'DNA Palvelut Oy';
  IPData.HostLocation := 'Finland';
  IPList.Add('62.113.160.0', IPData);

  IPData.HostName := 'CD-Telematika a.s.';
  IPData.HostLocation := 'Czech republic';
  IPList.Add('89.203.128.0', IPData);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  IPList.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  IPData: TIPData;
begin
  if IPList.TryGetValue('89.203.128.0', IPData) then
    ShowMessage('Provider ' + IPData.HostName + ' from ' + IPData.HostLocation)
  else
    ShowMessage('IP address not found!');
end;

end.

【讨论】:

    猜你喜欢
    • 2018-10-04
    • 2012-12-15
    • 1970-01-01
    • 1970-01-01
    • 2013-06-29
    • 1970-01-01
    • 1970-01-01
    • 2012-12-30
    • 2021-12-28
    相关资源
    最近更新 更多