Delphi排序字符串列表,第一个字段上有两个字段
我正在实现一个本地缓存来加速DNS查找IP->hostname。 缓存从CSV文件1.1.1.1host.example.com加载到带有两个字段的TStringList中:Delphi排序字符串列表,第一个字段上有两个字段,delphi,delphi-xe2,tstringlist,Delphi,Delphi Xe2,Tstringlist,我正在实现一个本地缓存来加速DNS查找IP->hostname。 缓存从CSV文件1.1.1.1host.example.com加载到带有两个字段的TStringList中: TStringList[0] := IPAddress; TStringList[1] := HostName; 由于我将通过IP查询TStringList,因此我希望对第一个字段进行排序: TStringList.sorted := True; 这样我就可以更快地找到它了吗 IPResolved:=TStringLi
TStringList[0] := IPAddress;
TStringList[1] := HostName;
由于我将通过IP查询TStringList,因此我希望对第一个字段进行排序:
TStringList.sorted := True;
这样我就可以更快地找到它了吗
IPResolved:=TStringList[TStringList.IndexOf('1.1.1.1'),1];
?
谢谢 免责声明:
这不会回答如何对字符串列表进行排序或如何将数据加载到字符串列表中。它将为您提供一个哈希表,这比为您的目的使用字符串列表40k名称、值对和按名称搜索更有效
备选方案:
因为您有DelphiXe2,所以可以使用泛型类。它将包含IP地址作为密钥,主机名作为值。下面的代码显示了如何填写字典以及如何通过给定的密钥IP地址搜索值host name:
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.
免责声明:
这不会回答如何对字符串列表进行排序或如何将数据加载到字符串列表中。它将为您提供一个哈希表,这比为您的目的使用字符串列表40k名称、值对和按名称搜索更有效
备选方案:
因为您有DelphiXe2,所以可以使用泛型类。它将包含IP地址作为密钥,主机名作为值。下面的代码显示了如何填写字典以及如何通过给定的密钥IP地址搜索值host name:
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.
显然,CSV文件在IP和主机名之间有一个空格。Tringlist绝对是错误的数据类型。您需要一个包含两个字符串字段的记录,即IPAddress和HostName。然后您想使用TList保存这些记录。@David,最好使用字典,因为OP想按IP搜索,那么键是什么。您可以使用IndexOfName和这样的名称=值配对字符串列表。或者使用泛型的TDictionary代替我在下面发布的字符串列表……TDictionary听起来不错。我的想法是使用二进制搜索对TList进行排序。字典可能更好。使用汇编程序的想法简直太可怕了。嘘嘘!显然,CSV文件在IP和主机名之间有一个空格。Tringlist绝对是错误的数据类型。您需要一个包含两个字符串字段的记录,即IPAddress和HostName。然后您想使用TList保存这些记录。@David,最好使用字典,因为OP想按IP搜索,那么键是什么。您可以使用IndexOfName和这样的名称=值配对字符串列表。或者使用泛型的TDictionary代替我在下面发布的字符串列表……TDictionary听起来不错。我的想法是使用二进制搜索对TList进行排序。字典可能更好。使用汇编程序的想法简直太可怕了。嘘嘘!