Delphi 实现 hashTable

dr_lou 2007-07-12 10:09:07
clientSocket不断接收String变量
我要从中解析出手机号和一种特殊信号,如果有就给这个手机号的记录+1
我想用hashTable

以前学java的 hashTable很好用 不知道delphi有没有类似的东西可用

hashTable
key value
手机号:13001370000 出现特殊信号次数:1
手机号:13001370001 出现特殊信号次数:3
手机号:13001371000 出现特殊信号次数:5

要求key不能有重复值,可以快速通过key查找value,如果某一手机号第一次出现特殊信号就在hashtable里面添加该key/value

...全文
745 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
dr_lou 2007-07-13
  • 打赏
  • 举报
回复
弱弱的问一句 哈西表怎么遍历?
zhangl_cn 2007-07-12
  • 打赏
  • 举报
回复
楼主可以试试 DGL库

http://community.csdn.net/Expert/topic/5604/5604520.xml?temp=.6217615
dr_lou 2007-07-12
  • 打赏
  • 举报
回复
我会了 插入之前查找一下
dr_lou 2007-07-12
  • 打赏
  • 举报
回复
大哥 我问问题之前能不baidu一下么

那个我试了 能插入重复的值
hongqi162 2007-07-12
  • 打赏
  • 举报
回复
Hashtable.pas
--------------------------------------------------------------------------------
unit Hashtable;

interface

uses SysUtils, Classes;

type
{ THashTable }

PPHashItem = ^PHashItem;
PHashItem = ^THashItem;
THashItem = record
Next: PHashItem;
Key: string;
Value: String;
end;

THashTable = class
private
Buckets: array of PHashItem;
protected
function Find(const Key: string): PPHashItem;
function HashOf(const Key: string): Cardinal; virtual;
public
constructor Create(Size: Integer = 256);
destructor Destroy; override;
procedure Put(const Key: string; Value: String);
procedure Clear;
procedure Remove(const Key: string);
function Modify(const Key: string; Value: String): Boolean;
function Get(const Key: string): String;
end;

implementation

{ THashTable }

procedure THashTable.Put(const Key: string; Value: String);
var
Hash: Integer;
Bucket: PHashItem;
begin
Hash := HashOf(Key) mod Cardinal(Length(Buckets));
New(Bucket);
Bucket^.Key := Key;
Bucket^.Value := Value;
Bucket^.Next := Buckets[Hash];
Buckets[Hash] := Bucket;
end;

procedure THashTable.Clear;
var
I: Integer;
P, N: PHashItem;
begin
for I := 0 to Length(Buckets) - 1 do
begin
P := Buckets[I];
while P <> nil do
begin
N := P^.Next;
Dispose(P);
P := N;
end;
Buckets[I] := nil;
end;
end;

constructor THashTable.Create(Size: Integer);
begin
inherited Create;
SetLength(Buckets, Size);
end;

destructor THashTable.Destroy;
begin
Clear;
inherited;
end;

function THashTable.Find(const Key: string): PPHashItem;
var
Hash: Integer;
begin
Hash := HashOf(Key) mod Cardinal(Length(Buckets));
Result := @Buckets[Hash];
while Result^ <> nil do
begin
if Result^.Key = Key then
Exit
else
Result := @Result^.Next;
end;
end;

function THashTable.HashOf(const Key: string): Cardinal;
var
I: Integer;
begin
Result := 0;
for I := 1 to Length(Key) do
Result := ((Result shl 2) or (Result shr (SizeOf(Result) * 8 - 2))) xor
Ord(Key[I]);
end;

function THashTable.Modify(const Key: string; Value: String): Boolean;
var
P: PHashItem;
begin
P := Find(Key)^;
if P <> nil then
begin
Result := True;
P^.Value := Value;
end
else
Result := False;
end;

procedure THashTable.Remove(const Key: string);
var
P: PHashItem;
Prev: PPHashItem;
begin
Prev := Find(Key);
P := Prev^;
if P <> nil then
begin
Prev^ := P^.Next;
Dispose(P);
end;
end;

function THashTable.Get(const Key: string): String;
var
P: PHashItem;
begin
P := Find(Key)^;
if P <> nil then
Result := P^.Value else
Result := '';
end;

end.
--------------------------------------------------------------------------------

使用起来就简单了:
HashTable := THashTable.Create(); //创建
HashTable.Put(Key, Value); //赋值
Value=HashTable.Get(Key); //取值
HashTable.Destroy; //撤消
dr_lou 2007-07-12
  • 打赏
  • 举报
回复
不用hashtable能实现类似功能也行

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧