改良版TStringList类

2008-04-09 04:21:32来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

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

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:获取 Windows 特殊文件夹函数

下一篇:角点检测算子的代码描述