![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IniFiles, StdCtrls;
type
PIpRecRord = ^TIpRecRord;
TIpRecRord = packed record
IP1: string;
Ip2: Pointer;
Count: Integer;
end;
THashIp = class(TObject)
private
FStringHash: TStringHash;
Flist: TList;
public
constructor Create(AMax: Integer = 250);
destructor Destroy;
procedure AddIp(const Ip: string);
function GetIpNum(const I: Integer): Integer;
end;
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Memo2: TMemo;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Test: THashIp;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Test := THashIp.Create();
end;
{ THashIp }
procedure THashIp.AddIp(const Ip: string);
var
P1: PIpRecRord;
begin
if Length(Ip) = 0 then Exit;
if FStringHash.ValueOf(Ip) = -1 then
begin
New(P1);
P1.IP1 := Ip;
P1.Count := 1;
FStringHash.Add(P1.IP1, Flist.Count);
Flist.Add(P1);
end
else
PIpRecRord(Flist[FStringHash.ValueOf(iP)]).Count := PIpRecRord(Flist[FStringHash.ValueOf(iP)]).Count + 1;
end;
constructor THashIp.Create(AMax: Integer);
begin
FStringHash := TStringHash.Create(AMax);
Flist := TList.Create;
end;
destructor THashIp.Destroy;
begin
if Flist.Count > 0 then
begin
while Assigned(Flist[0]) do
begin
Dispose(Flist[0]);
Flist.Delete(0);
end;
end;
FStringHash.Free;
end;
function THashIp.GetIpNum(const I: Integer): Integer;
var
Index: Integer;
begin
Result := 0;
for Index := 0 to Flist.Count - 1 do
begin
if PIpRecRord(Flist[Index])^.Count = I then
Inc(Result);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
I, Data: Integer;
begin
for I := 0 to 20 do
begin
Data := Random(20);
Test.AddIp(IntToStr(Data));
Memo1.Lines.Add(IntToStr(Data));
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
I, Data: Integer;
begin
for I := 0 to 8 do
Memo2.Lines.Add(IntToStr(Test.GetIpNum(I)));
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Test.Free;
end;
end.