Delphi中的THashTable
在Delphi中,Inifiles单元中有一个TStringHash的类,不过它的Value仅支持Integer(其实也不是问题,有其它类型可以将变量变为Pointer),有点不舒服,今天没事做就把它替换为variant了,其中Key的名称大小写无关,就是为了加快开发速度!
使用Hashtable,查找和删除复杂度都是常数级别的!
type
PPHashItem = ^PHashItem;
PHashItem = ^THashItem;
THashItem = record
Next: PHashItem;
Key: String;
Value: Variant;
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: Cardinal = );
destructor Destroy; 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;
begin
for I := to Length(Buckets) - do
begin
P := Buckets[I];
while P <> nil do
begin
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 <> nil then
begin
Result := True;
end
else
Result := False;
end;
constructor THashTable.Create(Size: Cardinal);
begin
inherited Create;
SetLength(Buckets, Size);
end;
destructor THashTable.Destroy;
begin
Clear;
inherited Destroy;
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.Get(const Key: String): Variant;
var
P: PHashItem;
begin
P := Find(AnsiUpperCase(Key))^;
if P <> nil then
Result := P^.Value
else
Result := -;
end;
function THashTable.HashOf(const Key: String): Cardinal;
var
I: Integer;
begin
Result := ;
for I := to Length(Key) do
Result := ((Result shl ) or (Result shr (SizeOf(Result) * - ))) xor
Ord(Key[I]);
end;
function THashTable.Modify(const Key: String; Value: Variant): 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.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 <> nil then
begin
Prev^ := P^.Next;
Dispose(P);
end;
end;
使用:
var
Demo:THashTable;
begin
Demo:=THashTable.Create();
try
Demo.Put('id',);
ShowMessage(Demo.Get('id'));
finally
Demo.Free;
end;
end;
参考:http://www.cnblogs.com/key-ok/p/3358929.html
Delphi中的THashTable的更多相关文章
- Delphi中stringlist分割字符串的用法
Delphi中stringlist分割字符串的用法 TStrings是一个抽象类,在实际开发中,是除了基本类型外,应用得最多的. 常规的用法大家都知道,现在来讨论它的一些高级的用法. 1.CommaT ...
- delphi中exit,abort,break,continue 的区别
from:http://www.cnblogs.com/taofengli288/archive/2011/09/05/2167553.html delphi中表示跳出的有break,continue ...
- Delphi中使用比较少的一些语法
本文是为了加强记忆而写,这里写的大多数内容都是在编程的日常工作中使用频率不高的东西,但是又十分重要. ---Murphy 1,构造和析构函数: a,构造函数: 一般基于TComponent组件的派生类 ...
- 如何在 Delphi 中静态链接 SQLite
搞了我几个小时,终于成功在 Delphi 中静态链接了 SQLite (v3.5.4),下一步就是研究加密了,呵呵中间其实遇到很多问题,今天累了,就不说了,改天补上 下载测试工程 下面说说方法 1.当 ...
- 翻箱倒柜,《Delphi中建议使用的语句》
(*//标题:Delphi中建议使用的语句整理:Zswang连接:http://www.csdn.net/Expert/TopicView1.asp?id=724036日期:2002-06-22支持: ...
- delphi中break,continue, exit,abort, halt, runerror的异同
delphi中表示跳出的有break,continue, exit,abort, halt, runerror. 1.break 强制退出循环(只能放在循环中),用于从For语句,while语句或re ...
- delphi中midas是什么
Delphi中MIDAS到底是什么呢?和他相关组件是什么呢? MIDAS(Multitiered Distributed Application Services)多层分布式应用服务. Del ...
- Delphi中window消息截获的实现方式(2)
Delphi是Borland公司提供的一种全新的WINDOWS编程开发工具.由于它采用了具有弹性的和可重用的面向对象Pascal(object-orientedpascal)语言,并有强大的数据库引擎 ...
- Delphi 中的 procedure of object
转载:http://www.cnblogs.com/ywangzi/archive/2012/08/28/2659811.html 总结:TMyEvent = procedure of object; ...
随机推荐
- hibernate批量删除和更新数据
转载自:http://blog.csdn.net/yuhua3272004/article/details/2909538 Hibernate3.0 採用新的基于ANTLR的HQL/SQL查询翻译器, ...
- javascript 高级程序设计(二)-在html中使用javascript
<script> async 可选 charset 可选 defer 可选 language 已废弃 src 可选 type 可选
- css考核点整理(九)-有几种文字替换方式,之间的优缺点
有几种文字替换方式,之间的优缺点
- linux系统时间同步更新
linux中的时钟分为系统时钟和硬件时钟.硬件时钟通过bioss进行设置,系统时钟通过linux kernel进行设置.当linux启动时,bioss会读取系统时钟的时钟设置.在linux中涉及时间设 ...
- Python之路【第十一篇】:CSS --暂无内容-待更新
Python之路[第十一篇]:CSS --暂无内容-待更新
- Java批量文件打包下载zip
网上看了很多,本文使用ant.jar中的org.apache.tools.zip,页面用js表单提交 代码供参考: ACTION: /* * 另存为 */ @RequestMapping(" ...
- oo面向对象原则
1.单一职责原则 一个类,最好只做一件事,只有一个引起他变化的原因否则就应该考虑重构. 2.开放封闭原则 软件实体应该是可扩展的,而不是可修改的.也就是说对扩展开放,对修改封闭.主要体现在两个方面: ...
- weex APIs
1.通过这个$vm()上下文访问这些api在脚本的方法 <script> module.exports = { methods: { somemethod: function() { th ...
- 关于this 的一个问题
var name = "the window"; var object = { name:"my object"; getName:function(){ re ...
- cocos2dx 各种环境的搭建
http://www.cocos.com/doc/tutorial/index?type=cocos2d-x Windows7上搭建Cocos2d-x 3.4开发环境 这里需要注意的是,如果是搭建VS ...