git-svn-id: https://spexeah.com:8443/svn/Asuro@1243 6dbc8c32-bb84-406f-8558-d1cf31a0ab0c

This commit is contained in:
kieron 2020-07-17 16:01:53 +00:00
parent fb904274b0
commit 57f5e6928d
2 changed files with 29 additions and 11 deletions

View File

@ -11,6 +11,7 @@ type
THashItem = record THashItem = record
Next : PHashItem; Next : PHashItem;
Key : pchar; Key : pchar;
Hash : uint32;
Data : void; Data : void;
end; end;
PHashMap = ^THashMap; PHashMap = ^THashMap;
@ -28,20 +29,25 @@ procedure printMap(map : PHashMap);
implementation implementation
function hashIndex(size : uint32; key : pchar) : uint32; function KeyHash(key : pchar) : uint32;
var var
KeyLength : uint32; KeyLength : uint32;
Hash : PMD5Digest; MD5_Hash : PMD5Digest;
Hash128 : puint128; Hash128 : puint128;
Hash32 : uint32; Hash32 : uint32;
begin begin
KeyLength:= StringSize(key); KeyLength:= StringSize(key);
Hash:= MD5Buffer(puint8(key), KeyLength); MD5_Hash:= MD5Buffer(puint8(key), KeyLength);
Hash128:= puint128(Hash); Hash128:= puint128(MD5_Hash);
Hash32:= MD5To32(Hash128); Hash32:= MD5To32(Hash128);
hashIndex:= Hash32 mod size; KeyHash:= Hash32;
kfree(void(Hash)); kfree(void(MD5_Hash));
end;
function hashIndex(size : uint32; hash : uint32) : uint32;
begin
hashIndex:= hash mod size;
end; end;
function newItem : PHashItem; function newItem : PHashItem;
@ -50,17 +56,20 @@ begin
newItem^.Next:= nil; newItem^.Next:= nil;
newItem^.Key:= nil; newItem^.Key:= nil;
newItem^.Data:= nil; newItem^.Data:= nil;
newItem^.Hash:= 0;
end; end;
procedure add(map : PHashMap; key : pchar; value : void); procedure add(map : PHashMap; key : pchar; value : void);
var var
Idx : uint32; Idx : uint32;
hash : uint32;
Item : PHashItem; Item : PHashItem;
nItem : PHashItem; nItem : PHashItem;
begin begin
if (map <> nil) and (key <> nil) then begin if (map <> nil) and (key <> nil) then begin
Idx:= hashIndex(map^.size, key); hash:= KeyHash(key);
Idx:= hashIndex(map^.size, hash);
if map^.Table[Idx] = nil then begin if map^.Table[Idx] = nil then begin
Item:= newItem; Item:= newItem;
Item^.Next:= nil; Item^.Next:= nil;
@ -82,18 +91,21 @@ begin
end; end;
if Item^.Key = nil then Item^.Key:= stringCopy(key); if Item^.Key = nil then Item^.Key:= stringCopy(key);
Item^.Data:= value; Item^.Data:= value;
Item^.Hash:= hash;
end; end;
end; end;
function get(map : PHashMap; key : pchar) : void; function get(map : PHashMap; key : pchar) : void;
var var
Idx : uint32; Idx : uint32;
hash : uint32;
Item : PHashItem; Item : PHashItem;
begin begin
get:= nil; get:= nil;
if (map <> nil) and (key <> nil) then begin if (map <> nil) and (key <> nil) then begin
Idx:= hashIndex(map^.size, key); hash:= KeyHash(key);
Idx:= hashIndex(map^.size, hash);
Item:= map^.table[Idx]; Item:= map^.table[Idx];
if Item = nil then exit; if Item = nil then exit;
while not StringEquals(Item^.key, key) do begin while not StringEquals(Item^.key, key) do begin
@ -122,13 +134,15 @@ end;
procedure deleteAndFree(map : PHashMap; key : pchar); procedure deleteAndFree(map : PHashMap; key : pchar);
var var
Idx : uint32; Idx : uint32;
hash : uint32;
Item : PHashItem; Item : PHashItem;
Prev : PHashItem; Prev : PHashItem;
begin begin
Prev:= nil; Prev:= nil;
if (map <> nil) and (key <> nil) then begin if (map <> nil) and (key <> nil) then begin
Idx:= hashIndex(map^.size, key); hash:= KeyHash(key);
Idx:= hashIndex(map^.size, hash);
Item:= map^.table[Idx]; Item:= map^.table[Idx];
while not StringEquals(Item^.key, key) do begin while not StringEquals(Item^.key, key) do begin
Prev:= Item; Prev:= Item;
@ -153,13 +167,15 @@ end;
procedure delete(map : PHashMap; key : pchar); procedure delete(map : PHashMap; key : pchar);
var var
Idx : uint32; Idx : uint32;
hash : uint32;
Item : PHashItem; Item : PHashItem;
Prev : PHashItem; Prev : PHashItem;
begin begin
Prev:= nil; Prev:= nil;
if (map <> nil) and (key <> nil) then begin if (map <> nil) and (key <> nil) then begin
Idx:= hashIndex(map^.size, key); hash:= KeyHash(key);
Idx:= hashIndex(map^.size, hash);
Item:= map^.table[Idx]; Item:= map^.table[Idx];
while not StringEquals(Item^.key, key) do begin while not StringEquals(Item^.key, key) do begin
Prev:= Item; Prev:= Item;

View File

@ -127,6 +127,8 @@ type
end; end;
PRGB565Pair = ^TRGB565Pair; PRGB565Pair = ^TRGB565Pair;
PText = ^Text;
implementation implementation
end. end.