X-Git-Url: https://deadsoftware.ru/gitweb?a=blobdiff_plain;ds=sidebyside;f=src%2Fshared%2Fhashtable.pas;h=cb6d35c0f8c684b58320c369907b9a3141e1d664;hb=c7827dd408b445f025117f2c5df2a3c0f4622298;hp=a51fc91d27519874d875a6b2799ab85b7230c599;hpb=fa51b0a96da1c9d7a3c08f19e5a7adce658873ba;p=d2df-sdl.git diff --git a/src/shared/hashtable.pas b/src/shared/hashtable.pas index a51fc91..cb6d35c 100644 --- a/src/shared/hashtable.pas +++ b/src/shared/hashtable.pas @@ -1,9 +1,8 @@ -(* Copyright (C) DooM 2D:Forever Developers +(* Copyright (C) Doom 2D: Forever Developers * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * the Free Software Foundation, version 3 of the License ONLY. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -26,20 +25,20 @@ unit hashtable; interface - +(* + * HashObjT: class that contains class methods: + * class function hash (const[ref] k: KeyT): LongWord; + * class function equ (const[ref] a, b: KeyT): Boolean; + * class procedure freekey (var k: KeyT); // this may free key + *) type // WARNING! don't put structures into hash, use ponters or ids! - generic THashBase = class(TObject) + generic THashBase = class(TObject) private const InitSize = {$IF DEFINED(RBHASH_SANITY_CHECKS)}16{$ELSE}256{$ENDIF}; // *MUST* be power of two const LoadFactorPrc = 90; // it is ok for robin hood hashes public - type THashFn = function (constref o: KeyT): LongWord; - type TEquFn = function (constref a, b: KeyT): Boolean; - type TIteratorFn = function (constref k: KeyT; constref v: ValueT): Boolean is nested; // return `true` to stop - type TIteratorExFn = function (constref k: KeyT; constref v: ValueT; keyhash: LongWord): Boolean is nested; // return `true` to stop - type PEntry = ^TEntry; TEntry = record @@ -49,10 +48,17 @@ type private hash: LongWord; // key hash or 0 nextFree: PEntry; // next free entry + private + function getEmpty (): Boolean; inline; public - property keyhash: LongWord read hash; + property empty: Boolean read getEmpty; + property keyhash: LongWord read hash; // cannot be 0 end; + type TFreeValueFn = procedure (var v: ValueT); // this may free value + type TIteratorFn = function (constref k: KeyT; constref v: ValueT): Boolean is nested; // return `true` to stop + type TIteratorExFn = function (constref k: KeyT; constref v: ValueT; keyhash: LongWord): Boolean is nested; // return `true` to stop + private type TEntryArray = array of TEntry; @@ -96,8 +102,7 @@ type end; private - hashfn: THashFn; - equfn: TEquFn; + freevalfn: TFreeValueFn; mBuckets: array of PEntry; // entries, points to mEntries elements mBucketsUsed: Integer; mEntries: TEntryArray; @@ -112,14 +117,16 @@ type function allocEntry (): PEntry; procedure releaseEntry (e: PEntry); - //function distToStIdx (idx: LongWord): LongWord; inline; + function distToStIdx (idx: LongWord): LongWord; inline; procedure putEntryInternal (swpe: PEntry); function getCapacity (): Integer; inline; + procedure freeEntries (); + public - constructor Create (ahashfn: THashFn; aequfn: TEquFn); + constructor Create (afreevalfn: TFreeValueFn=nil); destructor Destroy (); override; procedure clear (); @@ -172,39 +179,58 @@ type type - THashIntInt = specialize THashBase; - THashStrInt = specialize THashBase; - THashStrStr = specialize THashBase; + THashKeyInt = class + public + class function hash (const k: Integer): LongWord; inline; + class function equ (const a, b: Integer): Boolean; inline; + class procedure freekey (k: Integer); inline; + end; -function hashNewIntInt (): THashIntInt; -function hashNewStrInt (): THashStrInt; -function hashNewStrStr (): THashStrStr; + THashKeyStr = class + public + class function hash (const k: AnsiString): LongWord; inline; + class function equ (const a, b: AnsiString): Boolean; inline; + class procedure freekey (var k: AnsiString); inline; + end; + // case-insensitive (ansi) + THashKeyStrAnsiCI = class + public + class function hash (const k: AnsiString): LongWord; inline; + class function equ (const a, b: AnsiString): Boolean; inline; + class procedure freekey (var k: AnsiString); inline; + end; -function u32Hash (a: LongWord): LongWord; inline; -function fnvHash (constref buf; len: LongWord): LongWord; -function joaatHash (constref buf; len: LongWord): LongWord; +type + THashIntInt = specialize THashBase; + THashStrInt = specialize THashBase; + THashStrCIInt = specialize THashBase; + THashIntStr = specialize THashBase; + THashStrStr = specialize THashBase; + THashStrCIStr = specialize THashBase; + THashStrVariant = specialize THashBase; + THashStrCIVariant = specialize THashBase; -function nextPOT (x: LongWord): LongWord; inline; +function u32Hash (a: LongWord): LongWord; inline; +function fnvHash (constref buf; len: LongWord): LongWord; +function joaatHash (constref buf; len: LongWord; seed: LongWord=0): LongWord; +function joaatHashPtr (buf: Pointer; len: LongWord; seed: LongWord=0): LongWord; -// for integer keys -function hiiequ (constref a, b: Integer): Boolean; -function hiihash (constref k: Integer): LongWord; -function hsiequ (constref a, b: AnsiString): Boolean; -function hsihash (constref k: AnsiString): LongWord; +// has to be public due to FPC generics limitation +function nextPOTU32 (x: LongWord): LongWord; inline; implementation uses - SysUtils; + SysUtils, Variants; // ////////////////////////////////////////////////////////////////////////// // {$PUSH} {$RANGECHECKS OFF} -function nextPOT (x: LongWord): LongWord; inline; +function nextPOTU32 (x: LongWord): LongWord; inline; begin result := x; result := result or (result shr 1); @@ -218,49 +244,6 @@ end; {$POP} -// ////////////////////////////////////////////////////////////////////////// // -function hiiequ (constref a, b: Integer): Boolean; begin result := (a = b); end; -function hsiequ (constref a, b: AnsiString): Boolean; begin result := (a = b); end; - -{$PUSH} -{$RANGECHECKS OFF} -function hiihash (constref k: Integer): LongWord; -begin - result := LongWord(k); - result -= (result shl 6); - result := result xor (result shr 17); - result -= (result shl 9); - result := result xor (result shl 4); - result -= (result shl 3); - result := result xor (result shl 10); - result := result xor (result shr 15); -end; - -function hsihash (constref k: AnsiString): LongWord; -begin - if (Length(k) > 0) then result := fnvHash(PAnsiChar(k)^, Length(k)) else result := 0; -end; -{$POP} - - -function hashNewIntInt (): THashIntInt; -begin - result := THashIntInt.Create(hiihash, hiiequ); -end; - - -function hashNewStrInt (): THashStrInt; -begin - result := THashStrInt.Create(hsihash, hsiequ); -end; - - -function hashNewStrStr (): THashStrStr; -begin - result := THashStrStr.Create(hsihash, hsiequ); -end; - - // ////////////////////////////////////////////////////////////////////////// // {$PUSH} {$RANGECHECKS OFF} @@ -269,20 +252,17 @@ begin reset(aseed); end; - procedure TJoaatHasher.reset (); inline; overload; begin hash := seed; end; - procedure TJoaatHasher.reset (aseed: LongWord); inline; overload; begin seed := aseed; hash := aseed; end; - procedure TJoaatHasher.put (constref buf; len: LongWord); var bytes: PByte; @@ -302,7 +282,6 @@ begin hash := h; end; - function TJoaatHasher.value: LongWord; inline; begin result := hash; @@ -313,17 +292,50 @@ end; {$POP} -function joaatHash (constref buf; len: LongWord): LongWord; +// ////////////////////////////////////////////////////////////////////////// // +{$PUSH} +{$RANGECHECKS OFF} +function joaatHash (constref buf; len: LongWord; seed: LongWord=0): LongWord; var - h: TJoaatHasher; + b: PByte; + f: LongWord; begin - h := TJoaatHasher.Create(0); - h.put(PByte(@buf)^, len); - result := h.value; + result := seed; + b := PByte(@buf); + for f := 1 to len do + begin + result += b^; + result += (result shl 10); + result := result xor (result shr 6); + Inc(b); + end; + // finalize + result += (result shl 3); + result := result xor (result shr 11); + result += (result shl 15); end; +function joaatHashPtr (buf: Pointer; len: LongWord; seed: LongWord=0): LongWord; +var + b: PByte; + f: LongWord; +begin + result := seed; + b := PByte(buf); + for f := 1 to len do + begin + result += b^; + result += (result shl 10); + result := result xor (result shr 6); + Inc(b); + end; + // finalize + result += (result shl 3); + result := result xor (result shr 11); + result += (result shl 15); +end; +{$POP} -// ////////////////////////////////////////////////////////////////////////// // {$PUSH} {$RANGECHECKS OFF} // fnv-1a: http://www.isthe.com/chongo/tech/comp/fnv/ @@ -343,7 +355,6 @@ begin end; {$POP} - {$PUSH} {$RANGECHECKS OFF} function u32Hash (a: LongWord): LongWord; inline; @@ -359,73 +370,205 @@ begin end; {$POP} +function locase1251 (ch: AnsiChar): AnsiChar; inline; +begin + if ch < #128 then + begin + if (ch >= 'A') and (ch <= 'Z') then Inc(ch, 32); + end + else + begin + if (ch >= #192) and (ch <= #223) then + begin + Inc(ch, 32); + end + else + begin + case ch of + #168, #170, #175: Inc(ch, 16); + #161, #178: Inc(ch); + end; + end; + end; + result := ch; +end; + + +// ////////////////////////////////////////////////////////////////////////// // +// THashKeyInt +class function THashKeyInt.hash (const k: Integer): LongWord; inline; +begin + result := LongWord(k); + result -= (result shl 6); + result := result xor (result shr 17); + result -= (result shl 9); + result := result xor (result shl 4); + result -= (result shl 3); + result := result xor (result shl 10); + result := result xor (result shr 15); +end; + +class function THashKeyInt.equ (const a, b: Integer): Boolean; inline; begin result := (a = b); end; +class procedure THashKeyInt.freekey (k: Integer); inline; begin end; + + +// ////////////////////////////////////////////////////////////////////////// // +// THashKeyStr +class function THashKeyStr.hash (const k: AnsiString): LongWord; inline; begin if (Length(k) > 0) then result := fnvHash((@k[1])^, Length(k)) else result := 0; end; +class function THashKeyStr.equ (const a, b: AnsiString): Boolean; inline; begin result := (a = b); end; +class procedure THashKeyStr.freekey (var k: AnsiString); inline; begin k := ''; end; + // ////////////////////////////////////////////////////////////////////////// // -constructor THashBase.Create (ahashfn: THashFn; aequfn: TEquFn); +// case-insensitive (ansi) +{$PUSH} +{$RANGECHECKS OFF} +// fnv-1a: http://www.isthe.com/chongo/tech/comp/fnv/ +function fnvHashLo (constref buf; len: LongWord): LongWord; +var + b: PAnsiChar; begin - if not assigned(ahashfn) then raise Exception.Create('cannot create hash without hash function'); - if not assigned(aequfn) then raise Exception.Create('cannot create hash without equality function'); + b := @buf; + result := 2166136261; // fnv offset basis + while (len > 0) do + begin + result := result xor Byte(locase1251(b^)); + result := result*16777619; // 32-bit fnv prime + Inc(b); + Dec(len); + end; +end; +{$POP} + +class function THashKeyStrAnsiCI.hash (const k: AnsiString): LongWord; inline; begin if (Length(k) > 0) then result := fnvHashLo((@k[1])^, Length(k)) else result := 0; end; +class function THashKeyStrAnsiCI.equ (const a, b: AnsiString): Boolean; inline; +var + f: Integer; +begin + result := false; + if (Length(a) = Length(b)) then + begin + for f := 1 to Length(a) do if (locase1251(a[f]) <> locase1251(b[f])) then exit; + end; + result := true; +end; +class procedure THashKeyStrAnsiCI.freekey (var k: AnsiString); inline; begin k := ''; end; + + +// ////////////////////////////////////////////////////////////////////////// // +function THashBase.TEntry.getEmpty (): Boolean; inline; begin result := (hash = 0); end; + + +// ////////////////////////////////////////////////////////////////////////// // +function THashBase.getCapacity (): Integer; inline; begin result := Length(mBuckets); end; - hashfn := ahashfn; - equfn := aequfn; + +constructor THashBase.Create (afreevalfn: TFreeValueFn=nil); +begin + freevalfn := afreevalfn; mSeed := u32Hash($29a); + mFreeEntryHead := nil; + mFirstEntry := -1; + mLastEntry := -1; clear(); end; destructor THashBase.Destroy (); begin + freeEntries(); mBuckets := nil; mEntries := nil; inherited; end; -procedure THashBase.clear (); +procedure THashBase.freeEntries (); var - idx: Integer; + f: Integer; + e: PEntry; begin - SetLength(mBuckets, InitSize); - for idx := 0 to High(mBuckets) do mBuckets[idx] := nil; - SetLength(mEntries, Length(mBuckets)); - mBucketsUsed := 0; + if (mFirstEntry >= 0) then + begin + for f := mFirstEntry to mLastEntry do + begin + e := @mEntries[f]; + if not e.empty then + begin + HashObjT.freekey(e.key); + if assigned(freevalfn) then freevalfn(e.value) else e.value := Default(ValueT); + e.key := Default(KeyT); + e.value := Default(ValueT); + e.hash := 0; + end; + end; + end + else if (Length(mEntries) > 0) then + begin + FillChar(mEntries[0], Length(mEntries)*sizeof(mEntries[0]), 0); + end; + mFreeEntryHead := nil; + mFirstEntry := -1; + mLastEntry := -1; {$IFDEF RBHASH_SANITY_CHECKS} mEntriesUsed := 0; {$ENDIF} - mFreeEntryHead := nil; //@mEntries[0]; +end; + + +procedure THashBase.clear (); +begin + freeEntries(); + { + SetLength(mBuckets, InitSize); + FillChar(mBuckets[0], InitSize*sizeof(mBuckets[0]), 0); + SetLength(mEntries, InitSize); + FillChar(mEntries[0], InitSize*sizeof(mEntries[0]), 0); + } + mFreeEntryHead := nil; + mBuckets := nil; + mEntries := nil; mFirstEntry := -1; mLastEntry := -1; + mBucketsUsed := 0; end; procedure THashBase.reset (); -var - idx: Integer; +//var idx: Integer; begin + freeEntries(); if (mBucketsUsed > 0) then begin - for idx := 0 to High(mBuckets) do mBuckets[idx] := nil; + //for idx := 0 to High(mBuckets) do mBuckets[idx] := nil; + FillChar(mBuckets[0], Length(mBuckets)*sizeof(mBuckets[0]), 0); mBucketsUsed := 0; - {$IFDEF RBHASH_SANITY_CHECKS} - mEntriesUsed := 0; - {$ENDIF} - mFreeEntryHead := nil; //@mEntries[0]; - mFirstEntry := -1; - mLastEntry := -1; end; end; -function THashBase.getCapacity (): Integer; inline; begin result := Length(mBuckets); end; - - function THashBase.allocEntry (): PEntry; var idx: Integer; begin if (mFreeEntryHead = nil) then begin + // nothing was allocated, so allocate something now + if (Length(mBuckets) = 0) then + begin + assert(Length(mEntries) = 0); + assert(mFirstEntry = -1); + assert(mLastEntry = -1); + assert(mBucketsUsed = 0); + {$IFDEF RBHASH_SANITY_CHECKS} + mEntriesUsed := 0; + {$ENDIF} + SetLength(mBuckets, InitSize); + FillChar(mBuckets[0], InitSize*sizeof(mBuckets[0]), 0); + SetLength(mEntries, InitSize); + FillChar(mEntries[0], InitSize*sizeof(mEntries[0]), 0); + end; if (mLastEntry = High(mEntries)) then raise Exception.Create('internal error in hash entry allocator (0.0)'); Inc(mLastEntry); if (mFirstEntry = -1) then @@ -442,7 +585,7 @@ begin end; {$IFDEF RBHASH_SANITY_CHECKS} if (mFreeEntryHead = nil) then raise Exception.Create('internal error in hash entry allocator (0)'); - if (mFreeEntryHead.hash <> 0) then raise Exception.Create('internal error in hash entry allocator (1)'); + if (not mFreeEntryHead.empty) then raise Exception.Create('internal error in hash entry allocator (1)'); {$ENDIF} result := mFreeEntryHead; mFreeEntryHead := result.nextFree; @@ -469,19 +612,23 @@ begin if (mEntriesUsed = 0) then raise Exception.Create('internal error in hash entry allocator'); if (mEntriesUsed <> mBucketsUsed) then raise Exception.Create('internal error in hash entry allocator (entry/bucket count mismatch)'); if (e = nil) then raise Exception.Create('internal error in hash entry allocator (trying to release nil entry)'); - if (e.hash = 0) then raise Exception.Create('internal error in hash entry allocator (trying to release unallocated entry)'); + if (e.empty) then raise Exception.Create('internal error in hash entry allocator (trying to release unallocated entry)'); {$ENDIF} idx := Integer((PtrUInt(e)-PtrUInt(@mEntries[0])) div sizeof(mEntries[0])); {$IFDEF RBHASH_SANITY_CHECKS} if (idx < 0) or (idx > High(mEntries)) then raise Exception.Create('internal error in hash entry allocator (invalid entry address)'); if (e <> @mEntries[idx]) then raise Exception.Create('internal error in hash entry allocator (wtf?!)'); {$ENDIF} + HashObjT.freekey(e.key); + if assigned(freevalfn) then freevalfn(e.value) else e.value := Default(ValueT); {$IFDEF RBHASH_SANITY_CHECKS} Dec(mEntriesUsed); {$ENDIF} + e.key := Default(KeyT); + e.value := Default(ValueT); e.hash := 0; e.nextFree := mFreeEntryHead; - mFreeEntryHead := e; //idx; + mFreeEntryHead := e; // fix mFirstEntry and mLastEntry {$IFDEF RBHASH_SANITY_CHECKS} if (mFirstEntry < 0) or (mLastEntry < 0) then raise Exception.Create('internal error in hash entry allocator (invalid first/last range; 0)'); @@ -491,6 +638,7 @@ begin {$IFDEF RBHASH_SANITY_CHECKS} if (mEntriesUsed <> 0) then raise Exception.Create('internal error in hash entry allocator (invalid first/last range; 1)'); {$ENDIF} + mFreeEntryHead := nil; mFirstEntry := -1; mLastEntry := -1; end @@ -503,7 +651,7 @@ begin if (idx = mFirstEntry) then begin cidx := idx+1; - while (mEntries[cidx].hash = 0) do Inc(cidx); + while (mEntries[cidx].empty) do Inc(cidx); {$IFDEF RBHASH_SANITY_CHECKS} if (cidx > High(mEntries)) then raise Exception.Create('internal error in hash entry allocator (invalid first/last range; 3)'); {$ENDIF} @@ -513,7 +661,7 @@ begin if (idx = mLastEntry) then begin cidx := idx-1; - while (mEntries[cidx].hash = 0) do Dec(cidx); + while (mEntries[cidx].empty) do Dec(cidx); {$IFDEF RBHASH_SANITY_CHECKS} if (cidx < 0) then raise Exception.Create('internal error in hash entry allocator (invalid first/last range; 3)'); {$ENDIF} @@ -523,7 +671,6 @@ begin end; -(* function THashBase.distToStIdx (idx: LongWord): LongWord; inline; begin {$IFDEF RBHASH_SANITY_CHECKS} @@ -533,7 +680,6 @@ begin result := (mBuckets[idx].hash xor mSeed) and High(mBuckets); if (result <= idx) then result := idx-result else result := idx+(Length(mBuckets)-result); end; -*) function THashBase.has (constref akey: KeyT; keyhashin: PLongWord=nil): Boolean; @@ -551,11 +697,11 @@ begin if (keyhashin <> nil) then begin khash := keyhashin^; - if (khash = 0) then khash := hashfn(akey); + if (khash = 0) then khash := HashObjT.hash(akey); end else begin - khash := hashfn(akey); + khash := HashObjT.hash(akey); end; if (khash = 0) then khash := $29a; @@ -565,12 +711,9 @@ begin for dist := 0 to bhigh do begin if (mBuckets[idx] = nil) then break; - //pdist := distToStIdx(idx); - pdist := (mBuckets[idx].hash xor xseed) and bhigh; - if (pdist <= idx) then pdist := idx-pdist else pdist := idx+((bhigh+1)-pdist); - // + pdist := distToStIdx(idx); if (dist > pdist) then break; - result := (mBuckets[idx].hash = khash) and equfn(mBuckets[idx].key, akey); + result := (mBuckets[idx].hash = khash) and HashObjT.equ(mBuckets[idx].key, akey); if result then break; idx := (idx+1) and bhigh; end; @@ -592,11 +735,11 @@ begin if (keyhashin <> nil) then begin khash := keyhashin^; - if (khash = 0) then khash := hashfn(akey); + if (khash = 0) then khash := HashObjT.hash(akey); end else begin - khash := hashfn(akey); + khash := HashObjT.hash(akey); end; if (khash = 0) then khash := $29a; @@ -605,12 +748,9 @@ begin for dist := 0 to bhigh do begin if (mBuckets[idx] = nil) then break; - //pdist := distToStIdx(idx); - pdist := (mBuckets[idx].hash xor xseed) and bhigh; - if (pdist <= idx) then pdist := idx-pdist else pdist := idx+((bhigh+1)-pdist); - // + pdist := distToStIdx(idx); if (dist > pdist) then break; - result := (mBuckets[idx].hash = khash) and equfn(mBuckets[idx].key, akey); + result := (mBuckets[idx].hash = khash) and HashObjT.equ(mBuckets[idx].key, akey); if result then begin rval := mBuckets[idx].value; break; end; idx := (idx+1) and bhigh; end; @@ -622,7 +762,7 @@ end; procedure THashBase.putEntryInternal (swpe: PEntry); var idx, dist, pcur, pdist: LongWord; - tmpe: PEntry; // current entry to swap (or nothing) + tmpe: PEntry; bhigh, xseed: LongWord; begin bhigh := High(mBuckets); @@ -640,10 +780,7 @@ begin Inc(mBucketsUsed); break; end; - //pdist := distToStIdx(idx); - pdist := (mBuckets[idx].hash xor xseed) and bhigh; - if (pdist <= idx) then pdist := idx-pdist else pdist := idx+((bhigh+1)-pdist); - // + pdist := distToStIdx(idx); if (pcur > pdist) then begin // swapping the current bucket with the one to insert @@ -669,7 +806,7 @@ begin bhigh := High(mBuckets); xseed := mSeed; - khash := hashfn(akey); + khash := HashObjT.hash(akey); if (khash = 0) then khash := $29a; if (keyhashout <> nil) then keyhashout^ := khash; idx := (khash xor xseed) and bhigh; @@ -680,16 +817,15 @@ begin for dist := 0 to bhigh do begin if (mBuckets[idx] = nil) then break; - //pdist := distToStIdx(idx); - pdist := (mBuckets[idx].hash xor xseed) and bhigh; - if (pdist <= idx) then pdist := idx-pdist else pdist := idx+((bhigh+1)-pdist); - // + pdist := distToStIdx(idx); if (dist > pdist) then break; - result := (mBuckets[idx].hash = khash) and equfn(mBuckets[idx].key, akey); + result := (mBuckets[idx].hash = khash) and HashObjT.equ(mBuckets[idx].key, akey); if result then begin // replace element - //mBuckets[idx].key := akey; + HashObjT.freekey(mBuckets[idx].key); + if assigned(freevalfn) then freevalfn(mBuckets[idx].value) else mBuckets[idx].value := Default(ValueT); + mBuckets[idx].key := akey; mBuckets[idx].value := aval; exit; end; @@ -741,11 +877,11 @@ begin if (keyhashin <> nil) then begin khash := keyhashin^; - if (khash = 0) then khash := hashfn(akey); + if (khash = 0) then khash := HashObjT.hash(akey); end else begin - khash := hashfn(akey); + khash := HashObjT.hash(akey); end; if (khash = 0) then khash := $29a; @@ -756,12 +892,9 @@ begin for dist := 0 to bhigh do begin if (mBuckets[idx] = nil) then break; - //pdist := distToStIdx(idxcur); - pdist := (mBuckets[idx].hash xor xseed) and bhigh; - if (pdist <= idx) then pdist := idx-pdist else pdist := idx+((bhigh+1)-pdist); - // + pdist := distToStIdx(idx); if (dist > pdist) then break; - result := (mBuckets[idx].hash = khash) and equfn(mBuckets[idx].key, akey); + result := (mBuckets[idx].hash = khash) and HashObjT.equ(mBuckets[idx].key, akey); if result then break; idx := (idx+1) and bhigh; end; @@ -787,10 +920,7 @@ begin writeln(' dist=', dist, '; idx=', idx, '; idxnext=', idxnext, '; ce=', (mBuckets[idx] <> nil), '; ne=', (mBuckets[idxnext] <> nil)); {$ENDIF} if (mBuckets[idxnext] = nil) then begin {$IFDEF RBHASH_DEBUG_DELETE}writeln(' idxnext nil');{$ENDIF} mBuckets[idx] := nil; break; end; - //pdist := distToStIdx(idxnext); - pdist := (mBuckets[idxnext].hash xor xseed) and bhigh; - if (pdist <= idxnext) then pdist := idxnext-pdist else pdist := idxnext+((bhigh+1)-pdist); - // + pdist := distToStIdx(idxnext); if (pdist = 0) then begin {$IFDEF RBHASH_DEBUG_DELETE}writeln(' pdist is zero');{$ENDIF} mBuckets[idx] := nil; break; end; {$IFDEF RBHASH_DEBUG_DELETE}writeln(' pdist=', pdist);{$ENDIF} mBuckets[idx] := mBuckets[idxnext]; @@ -816,7 +946,8 @@ begin if (mSeed = 0) then mSeed := $29a; mSeed := u32Hash(mSeed); // clear buckets - for idx := 0 to High(mBuckets) do mBuckets[idx] := nil; + //for idx := 0 to High(mBuckets) do mBuckets[idx] := nil; + FillChar(mBuckets[0], Length(mBuckets)*sizeof(mBuckets[0]), 0); mBucketsUsed := 0; // reinsert entries mFreeEntryHead := nil; @@ -824,7 +955,7 @@ begin for idx := 0 to High(mEntries) do begin e := @mEntries[idx]; - if (e.hash <> 0) then + if (not e.empty) then begin {$IFDEF RBHASH_SANITY_CHECKS} if (e.nextFree <> nil) then raise Exception.Create('internal error in rehash: inconsistent'); @@ -833,7 +964,6 @@ begin if (cnt = mBucketsUsed) and (idx <> mLastEntry) then raise Exception.Create('internal error in rehash: inconsistent (2)'); {$ENDIF} // no need to recalculate hash - //e.hash := hashfn(e.key) xor mSeed; if (e.hash = 0) then e.hash := $29a; putEntryInternal(e); end else @@ -842,7 +972,7 @@ begin lastfree := e; end; end; - if (lastfree <> nil) then e.nextFree := nil; + if (lastfree <> nil) then lastfree.nextFree := nil; {$IFDEF RBHASH_SANITY_CHECKS} if (cnt <> mBucketsUsed) then raise Exception.Create('internal error in hash table resize (invalid first/last range; 0)'); if (cnt <> mEntriesUsed) then raise Exception.Create('internal error in hash table resize (invalid first/last range; 1)'); @@ -857,7 +987,7 @@ var cnt: Integer; {$ENDIF} begin - newsz := nextPOT(LongWord(mBucketsUsed)); + newsz := nextPOTU32(LongWord(mBucketsUsed)); if (newsz >= 1024*1024*1024) then exit; if (newsz*2 >= Length(mBuckets)) then exit; if (newsz*2 < 128) then exit; @@ -870,12 +1000,12 @@ begin if (mBucketsUsed < 1) then raise Exception.Create('internal error in hash table (invalid bucket count; 0)'); {$ENDIF} didx := 0; - while (didx < Length(mEntries)) do if (mEntries[didx].hash <> 0) then Inc(didx) else break; + while (didx < Length(mEntries)) do if (not mEntries[didx].empty) then Inc(didx) else break; f := didx+1; // copy entries while true do begin - if (mEntries[f].hash <> 0) then + if (not mEntries[f].empty) then begin {$IFDEF RBHASH_SANITY_CHECKS} if (didx >= f) then raise Exception.Create('internal error in hash: inconsistent'); @@ -884,7 +1014,7 @@ begin mEntries[f].hash := 0; Inc(didx); if (f = mLastEntry) then break; - while (didx < Length(mEntries)) do if (mEntries[didx].hash <> 0) then Inc(didx) else break; + while (didx < Length(mEntries)) do if (not mEntries[didx].empty) then Inc(didx) else break; end; Inc(f); end; @@ -897,14 +1027,14 @@ begin cnt := 0; for f := mFirstEntry to mLastEntry do begin - if (mEntries[f].hash = 0) then raise Exception.Create('internal error in hash table (invalid first/last range; 2)'); + if (mEntries[f].empty) then raise Exception.Create('internal error in hash table (invalid first/last range; 2)'); Inc(cnt); end; if (cnt <> mBucketsUsed) then raise Exception.Create('internal error in hash table (invalid first/last range; 3)'); if (cnt <> mEntriesUsed) then raise Exception.Create('internal error in hash table (invalid first/last range; 4)'); for f := mLastEntry+1 to High(mEntries) do begin - if (mEntries[f].hash <> 0) then raise Exception.Create('internal error in hash table (invalid first/last range; 5)'); + if (not mEntries[f].empty) then raise Exception.Create('internal error in hash table (invalid first/last range; 5)'); end; {$ENDIF} end @@ -925,39 +1055,33 @@ end; function THashBase.forEach (it: TIteratorFn): Boolean; overload; var - i: Integer; + f: Integer; begin result := false; - if not assigned(it) then exit; - i := mFirstEntry; - if (i < 0) then exit; - while (i <= mLastEntry) do + if not assigned(it) or (mFirstEntry < 0) then exit; + for f := mFirstEntry to mLastEntry do begin - if (mEntries[i].hash <> 0) then + if (not mEntries[f].empty) then begin - result := it(mEntries[i].key, mEntries[i].value); + result := it(mEntries[f].key, mEntries[f].value); if result then exit; end; - Inc(i); end; end; function THashBase.forEach (it: TIteratorExFn): Boolean; overload; var - i: Integer; + f: Integer; begin result := false; - if not assigned(it) then exit; - i := mFirstEntry; - if (i < 0) then exit; - while (i <= mLastEntry) do + if not assigned(it) or (mFirstEntry < 0) then exit; + for f := mFirstEntry to mLastEntry do begin - if (mEntries[i].hash <> 0) then + if (not mEntries[f].empty) then begin - result := it(mEntries[i].key, mEntries[i].value, mEntries[i].hash); + result := it(mEntries[f].key, mEntries[f].value, mEntries[f].hash); if result then exit; end; - Inc(i); end; end; @@ -1007,7 +1131,7 @@ begin Inc(cur); while (cur <= mLastEntry) do begin - if (mEntries[cur].hash <> 0) then begin result := true; exit; end; + if (not mEntries[cur].empty) then begin result := true; exit; end; end; result := false; end; @@ -1032,7 +1156,7 @@ begin Inc(cur); while (cur <= mLastEntry) do begin - if (mEntries[cur].hash <> 0) then begin result := true; exit; end; + if (not mEntries[cur].empty) then begin result := true; exit; end; end; result := false; end; @@ -1057,7 +1181,7 @@ begin Inc(cur); while (cur <= mLastEntry) do begin - if (mEntries[cur].hash <> 0) then begin result := true; exit; end; + if (not mEntries[cur].empty) then begin result := true; exit; end; end; result := false; end;