Hash table ,也叫做字典,是任何数据结构教材都会提到,O(0)的访问性能让它得到很广阔的应用,STL中也有map,hashmap,当然map一般是使用平衡BST来实现的,但是外部使用接口和hash table差不多。dict和list,scalar(string和numbers)组成了python,perl等脚本语言中基本数据元素。在各种程序应用中,相当大部分计算是在处理字符串,很多情况下,hash table会给字符串处理提供极大的方便。
        Delphi 自带的Contnrs中TBucketList实现了Key-value的访问接口,但它内部实现却是o(n)的数组。JEDI提供了一个TStringHashMap,这个到是真正的hashmp,我没有用过,但是按照jedi的正常品质,性能应该不错。我要推荐的是Hashes.pas,作者是Ciaran McCreesh,早就停止更新了,它提供了3个类,TIntegerHash,TObjectHash,TStringHash,从名字就可以知道它们k-v对中的value类型分别为integer,object,string。我在n个项目中都使用了它,用法很简单

Delphi中的hash tableuses Hashes
Delphi中的hash table
Delphi中的hash tablevar
Delphi中的hash table hash:TObjectHash
Delphi中的hash tablebegin
Delphi中的hash table 
//new
Delphi中的hash table hash:
=TObjectHash.Create;
Delphi中的hash table
Delphi中的hash table 
//add value
Delphi中的hash table hash[
'test']:= TObject.create;
Delphi中的hash table

Delphi中的hash table 
//search
Delphi中的hash table 
if hash.Exists('test') then
Delphi中的hash table

Delphi中的hash table 
//foreach
Delphi中的hash table hash.Restart;
Delphi中的hash table 
while hash.Next do
Delphi中的hash table begin
Delphi中的hash table   key:
=Hash.CurrentKey;
Delphi中的hash table   value:
=hash[key];
Delphi中的hash table   Delphi中的hash table
Delphi中的hash table 
end
Delphi中的hash table
Delphi中的hash table 
//delete
Delphi中的hash table hash.Free;
Delphi中的hash table
end

可以从这里下载Hashes.rar
我做了一点修改,TObjectHash['key']如果不存在,返回nil,原文件是抛出一个异常。
程序=数据结构+定义在数据结构上的运算。
delphi多是用来处理用户界面和数据库相关应用,delphi程序员很少自己创建各种数据结构,最多的是使用TStringlist和各种TQuery,TTable在内存中存放数据,或者就是定义各种record,用一个TList来存放,这中间有delphi自己的缺陷,但是Delphi 'er们是否也应该思考一下呢?

相关文章:

  • 2021-07-02
  • 2022-12-23
  • 2021-11-28
  • 2021-08-22
  • 2021-11-11
  • 2022-12-23
  • 2022-02-10
  • 2021-10-26
猜你喜欢
  • 2022-02-01
  • 2021-06-08
  • 2021-06-08
  • 2021-09-06
  • 2021-06-01
  • 2021-10-10
  • 2021-08-20
相关资源
相似解决方案