转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
改良版TStringList类         ★★★★

改良版TStringList类

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1384 更新时间:2009/4/23 18:26:59

{-----------------------------------------------------------------------------
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;
&

[1] [2]  下一页


没有相关教程
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台