网站公告列表

  没有公告

加入收藏
设为首页
联系站长
您现在的位置: 网络学院 >> 程序设计 >> Delphi编程 >> 文章正文
  在 Delphi 6 中使用 Hashtable            【字体:
在 Delphi 6 中使用 Hashtable
作者:佚名    文章来源:不详    点击数:    更新时间:2007-9-12    
Java 中的 Hashtable 类小巧好用,尤其是因为采用了哈希算法,查找的速度奇快。
正在装载数据……
后来因
工作需要,使用 Delphi 实施一些项目,特别不习惯没有哈希表的日子。于是决定自己动手做
一个。
  不必白手起家,Delphi 中有一个 THashedStringList 类,它位于 IniFiles 单元中。它里面
有一个不错的哈希算法,解决冲突的机制是“链地址”。把这个类改装改装,就成了自己的
了。新鲜热辣,火爆出炉。下面就来看看新的 Hashtable 长得何许模样。

                    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;           //撤消

本程序在Delphi 6.0下通过。(其它版本应该也可以,不过没做测试)

 


本文来源:http://blog.csdn.net/lee576/archive/2007/09/05/1773748.aspx
站内文章搜索 高级搜索
文章录入:admin    责任编辑:admin 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
     在delphi中使用xml文档有…
     初探delphi 7 中的插件编…
     delphi 2006(dexter) & …
     获得windows的版本信息。
     “序列号输入助手”源代…
     rs232串口通讯模块
     ado方式下判断数据表是否…
  • Ant入门-配置和使用     选…

  • 浅析Spring框架下PropertyPl…

  • SPRING+STRUTS+HIBERNATE登录…

  • MVP——Model-Viewer-Presen…

  • C++ Object Model

  • constructor and destructor

  • 绑定HGE到AngelScript脚本系…

  • delegate C#关键字 (委托类型…

  • Boyer-Moore String Searchi…

  • 【游戏制作基础】网络游戏设…

  •   网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    网络学院©2007 www.23book.net
    为您提供web编程,vb编程,vc编程,服务器架设管理,数据库设计等方面的知识 站长:David