Delphi中的THashTable
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Delphi中的THashTable,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2949字,纯文字阅读大概需要5分钟。
内容图文
![Delphi中的THashTable](/upload/InfoBanner/zyjiaocheng/1309/69dc19fd653444beb10c1a52959a4576.jpg)
在Delphi中,Inifiles单元中有一个TStringHash的类,不过它的Value仅支持Integer(其实也不是问题,有其它类型可以将变量变为Pointer),有点不舒服,今天没事做就把它替换为variant了,其中Key的名称大小写无关,就是为了加快开发速度!
使用Hashtable,查找和删除复杂度都是常数级别的!
type PPHashItem = ^PHashItem; PHashItem = ^THashItem; THashItem = record Next: PHashItem; Key: String; Value: Variant; end; THashTable = classprivate Buckets: arrayof PHashItem; protectedfunction Find(const Key: String): PPHashItem; function HashOf(const Key: String): Cardinal; virtual; publicconstructorCreate(Size: Cardinal = 256); destructorDestroy; override; procedure Put(const Key: String; Value: Variant); procedure Clear; procedure Remove(const Key: String); function Modify(const Key: String; Value: Variant): Boolean; function Get(const Key: String): Variant; function ContainKey(const Key: String):boolean; end; procedure THashTable.Clear; var I: Integer; P, N: PHashItem; beginfor I := 0to Length(Buckets) - 1dobegin P := Buckets[I]; while P <> nildobegin N := P^.Next; Dispose(P); P := N; end; Buckets[I] := nil; end; end; function THashTable.ContainKey(const Key: String): boolean; var P: PHashItem; begin P := Find(Key)^; if P <> nilthenbegin Result := True; endelse Result := False; end; constructor THashTable.Create(Size: Cardinal); begininheritedCreate; SetLength(Buckets, Size); end; destructor THashTable.Destroy; begin Clear; inheritedDestroy; end; function THashTable.Find(const Key: String): PPHashItem; var Hash: Integer; begin Hash := HashOf(Key) mod Cardinal(Length(Buckets)); Result := @Buckets[Hash]; while Result^ <> nildobeginif Result^.Key = Key then Exit else Result := @Result^.Next; end; end; function THashTable.Get(const Key: String): Variant; var P: PHashItem; begin P := Find(AnsiUpperCase(Key))^; if P <> nilthen Result := P^.Value else Result := -1; end; function THashTable.HashOf(const Key: String): Cardinal; var I: Integer; begin Result := 0; for I := 1to Length(Key) do Result := ((Result shl2) or (Result shr (SizeOf(Result) * 8 - 2))) xor Ord(Key[I]); end; function THashTable.Modify(const Key: String; Value: Variant): Boolean; var P: PHashItem; begin P := Find(Key)^; if P <> nilthenbegin Result := True; P^.Value := Value; endelse Result := False; end; procedure THashTable.Put(const Key: String; Value: Variant); var Hash: Integer; Bucket: PHashItem; begin Hash := HashOf(AnsiUpperCase(Key)) mod Cardinal(Length(Buckets)); New(Bucket); Bucket^.Key := AnsiUpperCase(Key); Bucket^.Value := Value; Bucket^.Next := Buckets[Hash]; Buckets[Hash] := Bucket; end; procedure THashTable.Remove(const Key: String); var P: PHashItem; Prev: PPHashItem; begin Prev := Find(Key); P := Prev^; if P <> nilthenbegin Prev^ := P^.Next; Dispose(P); end; end;
使用:
var Demo:THashTable; begin Demo:=THashTable.Create(); try Demo.Put(‘id‘,1); ShowMessage(Demo.Get(‘id‘)); finally Demo.Free; end; end;
参考:http://www.cnblogs.com/key-ok/p/3358929.html
原文:http://www.cnblogs.com/findumars/p/4748612.html
内容总结
以上是互联网集市为您收集整理的Delphi中的THashTable全部内容,希望文章能够帮你解决Delphi中的THashTable所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。