{-----------------------------------------------------------------------------
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);
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




