index b847720ba90a3ed86977b6adbc2531a4c4ce4b9c..8e6158fcefbff31d5d6bcff14d499d5d621a5b77 100644 (file)
--- a/src/shared/hashtable.pas
+++ b/src/shared/hashtable.pas
nextFree: PEntry; // next free entry
end;
+ TValEnumerator = class
+ private
+ mEntries: PEntry;
+ mFirstEntry, mLastEntry, cur: Integer;
+ public
+ constructor Create (aents: PEntry; afirst, alast: Integer);
+ function MoveNext: Boolean;
+ function getCurrent (): ValueT;
+ property Current: ValueT read getCurrent;
+ end;
+
private
hashfn: THashFn;
equfn: TEquFn;
//WARNING! don't modify table in iterator (queries are ok, though)
function forEach (it: TIteratorFn): Boolean;
+ function GetEnumerator (): TValEnumerator;
+
property count: Integer read mBucketsUsed;
property capacity: Integer read getCapacity;
end;
-
type
TJoaatHasher = record
private
procedure reset (); inline; overload;
procedure reset (aseed: LongWord); inline; overload;
- procedure put (const buf; len: LongWord);
+ procedure put (constref buf; len: LongWord);
// current hash value
// you can continue putting data, as this is not destructive
function u32Hash (a: LongWord): LongWord; inline;
-function fnvHash (const buf; len: LongWord): LongWord;
-function joaatHash (const buf; len: LongWord): LongWord;
+function fnvHash (constref buf; len: LongWord): LongWord;
+function joaatHash (constref buf; len: LongWord): LongWord;
function nextPOT (x: LongWord): LongWord; inline;
end;
-procedure TJoaatHasher.put (const buf; len: LongWord);
+procedure TJoaatHasher.put (constref buf; len: LongWord);
var
bytes: PByte;
h: LongWord;
{$POP}
-function joaatHash (const buf; len: LongWord): LongWord;
+function joaatHash (constref buf; len: LongWord): LongWord;
var
h: TJoaatHasher;
begin
h := TJoaatHasher.Create(0);
- h.put(buf, len);
+ h.put(PByte(@buf)^, len);
result := h.value;
end;
{$PUSH}
{$RANGECHECKS OFF}
// fnv-1a: http://www.isthe.com/chongo/tech/comp/fnv/
-function fnvHash (const buf; len: LongWord): LongWord;
+function fnvHash (constref buf; len: LongWord): LongWord;
var
b: PByte;
begin
end;
+function THashBase.GetEnumerator (): TValEnumerator;
+begin
+ if (Length(mEntries) > 0) then
+ begin
+ result := TValEnumerator.Create(@mEntries[0], mFirstEntry, mLastEntry);
+ end
+ else
+ begin
+ result := TValEnumerator.Create(nil, -1, -1);
+ end;
+end;
+
+constructor THashBase.TValEnumerator.Create (aents: PEntry; afirst, alast: Integer);
+begin
+ mEntries := aents;
+ mFirstEntry := afirst;
+ mLastEntry := alast;
+ cur := mFirstEntry-1;
+end;
+
+function THashBase.TValEnumerator.MoveNext: Boolean;
+begin
+ Inc(cur);
+ while (cur <= mLastEntry) do
+ begin
+ if (mEntries[cur].hash <> 0) then begin result := true; exit; end;
+ end;
+ result := false;
+end;
+
+function THashBase.TValEnumerator.getCurrent (): ValueT;
+begin
+ result := mEntries[cur].value;
+end;
+
+
end.