X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Fshared%2Fhashtable.pas;h=9f72e3ab2f5fb547da371473cc30e87e8b47887b;hb=679461cd7908bd408cf384840ef1c7af78e5c597;hp=f3331d816e68d994a3bd234972172c3733184554;hpb=053a1b71fc1667d29e9c518be03866aa546078a4;p=d2df-sdl.git diff --git a/src/shared/hashtable.pas b/src/shared/hashtable.pas index f3331d8..9f72e3a 100644 --- a/src/shared/hashtable.pas +++ b/src/shared/hashtable.pas @@ -52,36 +52,38 @@ type private type + TEntryArray = array of TEntry; + TValEnumerator = record private - mEntries: PEntry; + mEntries: TEntryArray; mFirstEntry, mLastEntry, cur: Integer; public - constructor Create (aents: PEntry; afirst, alast: Integer); - function MoveNext: Boolean; - function getCurrent (): ValueT; + constructor Create (const aents: TEntryArray; afirst, alast: Integer); + function MoveNext (): Boolean; inline; + function getCurrent (): ValueT; inline; property Current: ValueT read getCurrent; end; TKeyEnumerator = record private - mEntries: PEntry; + mEntries: TEntryArray; mFirstEntry, mLastEntry, cur: Integer; public - constructor Create (aents: PEntry; afirst, alast: Integer); - function MoveNext: Boolean; - function getCurrent (): KeyT; + constructor Create (const aents: TEntryArray; afirst, alast: Integer); + function MoveNext (): Boolean; inline; + function getCurrent (): KeyT; inline; property Current: KeyT read getCurrent; end; TKeyValEnumerator = record private - mEntries: PEntry; + mEntries: TEntryArray; mFirstEntry, mLastEntry, cur: Integer; public - constructor Create (aents: PEntry; afirst, alast: Integer); - function MoveNext: Boolean; - function getCurrent (): PEntry; + constructor Create (const aents: TEntryArray; afirst, alast: Integer); + function MoveNext (): Boolean; inline; + function getCurrent (): PEntry; inline; property Current: PEntry read getCurrent; end; @@ -90,7 +92,7 @@ type equfn: TEquFn; mBuckets: array of PEntry; // entries, points to mEntries elements mBucketsUsed: Integer; - mEntries: array of TEntry; + mEntries: TEntryArray; {$IFDEF RBHASH_SANITY_CHECKS} mEntriesUsed: Integer; {$ENDIF} @@ -158,8 +160,10 @@ type type THashIntInt = specialize THashBase; + THashStrInt = specialize THashBase; function hashNewIntInt (): THashIntInt; +function hashNewStrInt (): THashStrInt; function u32Hash (a: LongWord): LongWord; inline; @@ -194,6 +198,7 @@ end; // ////////////////////////////////////////////////////////////////////////// // 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} @@ -208,6 +213,11 @@ begin 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} @@ -217,6 +227,12 @@ begin end; +function hashNewStrInt (): THashStrInt; +begin + result := THashStrInt.Create(hsihash, hsiequ); +end; + + // ////////////////////////////////////////////////////////////////////////// // {$PUSH} {$RANGECHECKS OFF} @@ -905,31 +921,31 @@ end; // enumerators function THashBase.GetEnumerator (): TValEnumerator; begin - if (Length(mEntries) > 0) then result := TValEnumerator.Create(@mEntries[0], mFirstEntry, mLastEntry) + if (Length(mEntries) > 0) then result := TValEnumerator.Create(mEntries, mFirstEntry, mLastEntry) else result := TValEnumerator.Create(nil, -1, -1); end; function THashBase.byKey (): TKeyEnumerator; begin - if (Length(mEntries) > 0) then result := TKeyEnumerator.Create(@mEntries[0], mFirstEntry, mLastEntry) + if (Length(mEntries) > 0) then result := TKeyEnumerator.Create(mEntries, mFirstEntry, mLastEntry) else result := TKeyEnumerator.Create(nil, -1, -1); end; function THashBase.byValue (): TValEnumerator; begin - if (Length(mEntries) > 0) then result := TValEnumerator.Create(@mEntries[0], mFirstEntry, mLastEntry) + if (Length(mEntries) > 0) then result := TValEnumerator.Create(mEntries, mFirstEntry, mLastEntry) else result := TValEnumerator.Create(nil, -1, -1); end; function THashBase.byKeyValue (): TKeyValEnumerator; // PEntry begin - if (Length(mEntries) > 0) then result := TKeyValEnumerator.Create(@mEntries[0], mFirstEntry, mLastEntry) + if (Length(mEntries) > 0) then result := TKeyValEnumerator.Create(mEntries, mFirstEntry, mLastEntry) else result := TKeyValEnumerator.Create(nil, -1, -1); end; // ////////////////////////////////////////////////////////////////////////// // -constructor THashBase.TValEnumerator.Create (aents: PEntry; afirst, alast: Integer); +constructor THashBase.TValEnumerator.Create (const aents: TEntryArray; afirst, alast: Integer); begin mEntries := aents; mFirstEntry := afirst; @@ -937,7 +953,7 @@ begin cur := mFirstEntry-1; end; -function THashBase.TValEnumerator.MoveNext: Boolean; +function THashBase.TValEnumerator.MoveNext (): Boolean; inline; begin Inc(cur); while (cur <= mLastEntry) do @@ -947,14 +963,14 @@ begin result := false; end; -function THashBase.TValEnumerator.getCurrent (): ValueT; +function THashBase.TValEnumerator.getCurrent (): ValueT; inline; begin result := mEntries[cur].value; end; // ////////////////////////////////////////////////////////////////////////// // -constructor THashBase.TKeyEnumerator.Create (aents: PEntry; afirst, alast: Integer); +constructor THashBase.TKeyEnumerator.Create (const aents: TEntryArray; afirst, alast: Integer); begin mEntries := aents; mFirstEntry := afirst; @@ -962,7 +978,7 @@ begin cur := mFirstEntry-1; end; -function THashBase.TKeyEnumerator.MoveNext: Boolean; +function THashBase.TKeyEnumerator.MoveNext (): Boolean; inline; begin Inc(cur); while (cur <= mLastEntry) do @@ -972,14 +988,14 @@ begin result := false; end; -function THashBase.TKeyEnumerator.getCurrent (): KeyT; +function THashBase.TKeyEnumerator.getCurrent (): KeyT; inline; begin result := mEntries[cur].key; end; // ////////////////////////////////////////////////////////////////////////// // -constructor THashBase.TKeyValEnumerator.Create (aents: PEntry; afirst, alast: Integer); +constructor THashBase.TKeyValEnumerator.Create (const aents: TEntryArray; afirst, alast: Integer); begin mEntries := aents; mFirstEntry := afirst; @@ -987,7 +1003,7 @@ begin cur := mFirstEntry-1; end; -function THashBase.TKeyValEnumerator.MoveNext: Boolean; +function THashBase.TKeyValEnumerator.MoveNext (): Boolean; inline; begin Inc(cur); while (cur <= mLastEntry) do @@ -997,9 +1013,9 @@ begin result := false; end; -function THashBase.TKeyValEnumerator.getCurrent (): PEntry; +function THashBase.TKeyValEnumerator.getCurrent (): PEntry; inline; begin - result := mEntries+cur; + result := @mEntries[cur]; end;