首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 网络安全-> 黑客攻防
改良版tstringlist类_delphi教程
作者:网友供稿 点击:0
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 
 

{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at
http://www.mozilla.org/NPL/NPL-1_1Final.html

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.

The Original Code is: mwStringHashList.pas, released December 18, 2000.

The Initial Developer of the Original Code is Martin Waldenburg
(Martin.Waldenburg@T-Online.de).
Portions created by Martin Waldenburg are Copyright (C) 2000 Martin Waldenburg.
All Rights Reserved.

Contributor(s): ___________________.

Last Modified: 18/12/2000
Current Version: 1.1

Notes: This is a very fast Hash list for strings.
       The TinyHash functions should be in most cases suffizient

Known Issues:
-----------------------------------------------------------------------------}

unit mwStringHashList;

interface

uses Classes, SysUtils;

var
  mwHashTable: array[#0..#255] of Byte;
  mwInsensitiveHashTable: array[#0..#255] of Byte;

type
  TmwStringHash = function (const aString: String): Integer;
  TmwStringHashCompare = function (const Str1: String; const Str2: String): Boolean;

  TmwHashWord = class
    S: String;
    constructor Create(aString: String);
  end;

  PHashPointerList = ^THashPointerList;
  THashPointerList = array[1..1] of Pointer;

  TmwBaseStringHashList = class(TObject)
    FList: PHashPointerList;
    fCapacity: Integer;
  protected
    function Get(Index: Integer): Pointer;
    procedure Put(Index: Integer; Item: Pointer);
    procedure SetCapacity(NewCapacity: Integer);
  public
    destructor Destroy; override;
    property Capacity: Integer read fCapacity;
    property Items[Index: Integer]: Pointer read Get write Put; default;
  end;

  TmwHashStrings = class(TList)
  public
    destructor Destroy; override;
    procedure AddString(S: String);
  end;

  TmwHashItems = class(TmwBaseStringHashList)
  public
    procedure AddString(S: String);
  end;

  TmwStringHashList = class(TmwBaseStringHashList)
  private
    fHash: TmwStringHash;
    fCompare: TmwStringHashCompare;
  public
    constructor Create(aHash: TmwStringHash; aCompare: TmwStringHashCompare);
    procedure AddString(S: String);
    function Hash(S: String): Boolean;
    function HashEX(S: String; HashValue: Integer): Boolean;
  end;

  function SimpleHash(const aString: String): Integer;
  function ISimpleHash(const aString: String): Integer;
  function TinyHash(const aString: String): Integer;
  function ITinyHash(const aString: String): Integer;
  function HashCompare(const Str1: String; const Str2: String): Boolean;
  function IHashCompare(const Str1: String; const Str2: String): Boolean;

implementation

procedure InitTables;
var
  I: Char;
begin
  for I:= #0 to #255 do
  begin
    mwHashTable[I]:= Ord(I);
    mwInsensitiveHashTable[I]:= Ord(UpperCase(String(I))[1]);
  end;
end;

function SimpleHash(const aString: String): Integer;
var
  I: Integer;
begin
  Result:= Length(aString);
  for I:= 1 to Length(aString) do
  inc(Result, mwHashTable[aString[I]]);
end;

function ISimpleHash(const aString: String): Integer;
var
  I: Integer;
begin
  Result:= Length(aString);
  for I:= 1 to Length(aString) do
  inc(Result, mwInsensitiveHashTable[aString[I]]);
end;

function TinyHash(const aString: String): Integer;
var
  I: Integer;
begin
  Result:= Length(aString);
  for I:= 1 to Length(aString) do
  begin
    inc(Result, mwHashTable[aString[I]]);
    if I = 2 then Break;
  end;
end;

function ITinyHash(const aString: String): Integer;
var
  I: Integer;
begin
  Result:= Length(aString);
  for I:= 1 to Length(aString) do
  begin
    inc(Result, mwInsensitiveHashTable[aString[I]]);
    if I = 2 then Break;
  end;
end;

function HashCompare(const Str1: String; const Str2: String): Boolean;
var
  I: Integer;
begin
  if Length(Str1) <> Length(Str2) then
  begin
    Result:= False;
    Exit;
  end;
  Result:= True;
  for I:= 1 to Length(Str1) do
  if Str1[I] <> Str2[I] then
  begin
    Result:= False;
    Exit;
  end;
end;

function IHashCompare(const Str1: String; const Str2: String): Boolean;
var
  I: Integer;
begin
  if Length(Str1) <> Length(Str2) then
  begin
    Result:= False;
    Exit;
  end;
  Result:= True;
  for I:= 1 to Length(Str1) do
  if mwInsensitiveHashTable[Str1[I]] <> mwInsensitiveHashTable[Str2[I]] then
  begin
    Result:= False;
    Exit;
  end;
end;

{ TmwHashString }

constructor TmwHashWord.Create(aString: String);
begin
  inherited Create;
  S:= aString;
end;

{ TmwBaseStringHashList }

destructor TmwBaseStringHashList.Destroy;
var
  I: Integer;
begin
  for I:= 1 to fCapacity do
    if Items[I] <> nil then TObject(Items[I]).Free;
    ReallocMem(FList, 0);
  inherited Destroy;
end;

function TmwBaseStringHashList.Get(Index: Integer): Pointer;
begin
  Result:= nil;
  if (Index > 0) and (Index <= fCapacity) then
  Result:= fList[Index];
end;

procedure TmwBaseStringHashList.Put(Index: Integer; Item: Pointer);
begin
  if (Index > 0) and (Index <= fCapacity) then
  fList[Index]:= Item;
end;

procedure TmwBaseStringHashList.SetCapacity(NewCapacity: Integer);
var
  I, OldCapacity: Integer;
begin
  if NewCapacity > fCapacity then
  begin
    ReallocMem(FList, (NewCapacity) * SizeOf(Pointer));
    OldCapacity:= fCapacity;
    FCapacity := NewCapacity;
    for I:= OldCapacity+1 to NewCapacity do Items[I]:= nil;
  end;
end;

{ TmwHashStrings }

procedure TmwHashStrings.AddString(S: String);
begin
  Add(TmwHashWord.Create(S));
end;

destructor TmwHashStrings.Destroy;
var
  I: Integer;
begin
  for I:= 0 to Count - 1 do
  if Items[I] <> nil then TObject(Items[I]).Free;
  inherited Destroy;
end;

{ TmwHashItems }

procedure TmwHashItems.AddString(S: String);
var
  HashWord: TmwHashWord;
  HashStrings: TmwHashStrings;
begin
  SetCapacity(Length(S));
  if Items[Length(S)] = nil then
  begin
    Items[Length(S)]:= TmwHashWord.Create(S);
  end else
  if TObject(Items[Length(S)]) is TmwHashStrings then
  begin
    TmwHashStrings(Items[Length(S)]).AddString(S);
  end else
  begin
    HashWord:= Items[Length(S)];
    HashStrings:= TmwHashStrings.Create;
    Items[Length(S)]:= HashStrings;
    HashStrings.AddString(HashWord.S);
    HashWord.Free;
    HashStrings.AddString(S)
  end;
end;

{ TmwStringHashList }

constructor TmwStringHashList.Create(aHash: TmwStringHash; aCompare: TmwStringHashCompare);
begin
  inherited Create;
  fHash:= aHash;
  fCompare:= aCompare;
end;

procedure TmwStringHashList.AddString(S: String);
var
  HashWord: TmwHashWord;
  HashValue: Integer;
  HashItems: TmwHashItems;
begin
  HashValue:= fHash(S);
  if HashEx(S, HashValue) then exit;
  if HashValue >= fCapacity then SetCapacity(HashValue);
  if Items[HashValue] = nil then
  begin
    Items[HashValue]:= TmwHashWord.Create(S);
  end else
  if TObject(Items[HashValue]) is TmwHashItems then
  begin
    TmwHashItems(Items[HashValue]).AddString(S);
  end else
  begin
    HashWord:= Items[HashValue];
    HashItems:= TmwHashItems.Create;
    Items[HashValue]:= HashItems;
    HashItems.AddString(HashWord.S);
    HashWord.Free;
    HashItems.AddString(S);
  end;
end;

function TmwStringHashList.Hash(S: String): Boolean;
begin
  Result:= HashEX(S, fHash(S));
end;

function TmwStringHashList.HashEX(S: String; HashValue: Integer): Boolean;
var
  Temp: TObject;
  Hashword: TmwHashWord;
  HashItems: TmwHashItems;
  I: Integer;
begin
  Result:= False;
  if HashValue < 1 then Exit;
  if HashValue > Capacity  then Exit;
  if Items[HashValue] <> nil then
  begin
    if TObject(Items[HashValue]) is TmwHashWord then
    begin
      Result:= fCompare(TmwHashWord(Items[HashValue]).S, S);
    end else
    begin
      HashItems:= Items[HashValue];
      if Length(S) > HashItems.Capacity  then Exit;
      Temp:= HashItems[Length(S)];
      if Temp <> nil then
      if Temp is TmwHashWord then
      begin
        Result:= fCompare(TmwHashWord(Temp).S, S);
      end else
      for I:= 0 to TmwHashStrings(Temp).Count -1 do
      begin
        HashWord:= TmwHashStrings(Temp)[I];
        Result:= fCompare(HashWord.S, S);
        if Result then exit;
      end;
    end;
  end;
end;

Initialization
InitTables;
end. 


文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·J2EE平台介绍-JSP教程,J2ME开发
·自己制作友好的页面“确认对话框”-ASP教程,ASP应用
·DataAdapter是如何更新数据库数据的?-.NET教程,数据库应用
·jsp项目中实现关闭浏览器后吊线,只要不关闭不操作也保持在线-JSP教程,Jsp/Servlet
·企业Java技术开发技巧2则-JSP教程,Java技巧及代码
·刚刚整理好-汉字转拼音缩写的函数-.NET教程,评论及其它
·关于数据返回给调用页面-ASP教程,数据库相关
·如何使用C#压缩文件及注意的问题!-.NET教程,C#语言
·数据库安装程序-.NET教程,数据库应用
·DataGrid的自定义分页UserControl-.NET教程,数据库应用
最新文章
·王通:网站推广的18种秘笈_营销推广
·什么是mfa,以及停止mfa的好处_网赚技巧
·google adsense 域名保护功能是否作恶_网赚技巧
·关于无效点击-adsense团队资深专员如是说_网赚技巧
·google adsense将以人民币支付_站长访谈
·bbmao的神秘配方:打破中文聚类搜索的低迷_站长访谈
·你的网站为什么粘不住流量?_站长心得
·决定网站成败重要的六个因素_站长心得
·widnows vista是吃内存大户 优化vista攻略_windows vista
·首页所应该达到的几个目标(1)_站长心得
相关主题
西部数码虚拟主机

友情链接
CNNIC 西部数码
万网 自助建站
虚拟主机 asp空间
域名注册 域名
域名申请 主页空间
论坛空间 网站空间
国际域名 虚拟空间
空间租用 DDOS防火墙
成都主机托管 四川主机托管
主机租用 服务器租用
网站目录 自助建站
虚拟主机 网址大全
软件下载
自助链接
虚拟主机资讯 特价虚拟主机
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
发表评论 打印  刷新     关闭