易截截图软件、单文件、免安装、纯绿色、仅160KB

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 = 256);
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 := 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;
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;
functio


相关文档:

Delphi in a Unicode World Part I

 ---
Delphi in a Unicode World Part I: What is Unicode, Why do you need
it, and How do you work with it in Delphi?
By: Nick
Hodges
原文链接:http://dn.codegear.com/article/38437
Abstract: This article discusses Unicode, how Delphi developers
can benefit from using Unicode, and ho ......

Delphi in a Unicode World Part II

Delphi in a Unicode World Part II:  New RTL Features and
Classes to Support Unicode
By: Nick
Hodges
原文链接:http://dn.codegear.com/article/38498
Abstract: This article will cover the new features of the Tiburon
Runtime Library that will help handle Unicode strings.
//
 & ......

关于delphi Move函数的用法详解

  使用delphi多年,前些天忽然遇到不会string转pbyte,很是失落,此时对于编程基本功的重要性深有体会.这其中用到MOVE函数.
 搞了好一会才搞明白其用法.所以想贴出来帮助需要帮助的人.
var
   s:string;
   ps:Pchar;
   b:pbyte;
   len:integer;
begin
 &nb ......

delphi中国际化的几种方案及比较(转)

delphi中国际化的几种方案及比较(转)
随着全球化程度加深,软件越来越像蒲公英,到处飘散、扎根。这其中要解决的是不同语言的显示问题。我们当然希望一套程序,可以不修改代码就可以支持不同的语言,不要去维护很多的版本。
        首先要谈到的一个问题是乱码问题,因为delphi
wi ......

Delphi中的数组

不论数组元素是什么类型,静态数组的局部变量总会在栈上分配。如果堆栈大小不够将会导致异常。下例暗藏着一个危险的bug
procedure stackover;
var
s:array [0..4100] of string[255];
begin
end; 
delphi默认栈最大为$100000字节,上例中的栈上分配的大侠为256*4100>$10000。测试必然会出现异常Stack overf ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号