DEADSOFTWARE

save/load fixes
[d2df-sdl.git] / src / shared / hashtable.pas
index 190d4e62b57cb9c9b9941bc78dec401239aaf0d2..fa6ec85f4c7d6d9e14a58298c3e0450275c72463 100644 (file)
@@ -54,14 +54,17 @@ type
     type
       TEntryArray = array of TEntry;
 
+  public
+    type
       TValEnumerator = record
       private
         mEntries: TEntryArray;
         mFirstEntry, mLastEntry, cur: Integer;
       public
-        constructor Create (aents: TEntryArray; afirst, alast: Integer);
+        constructor Create (const aents: TEntryArray; afirst, alast: Integer);
         function MoveNext (): Boolean; inline;
         function getCurrent (): ValueT; inline;
+        function GetEnumerator (): TValEnumerator; inline;
         property Current: ValueT read getCurrent;
       end;
 
@@ -70,9 +73,10 @@ type
         mEntries: TEntryArray;
         mFirstEntry, mLastEntry, cur: Integer;
       public
-        constructor Create (aents: TEntryArray; afirst, alast: Integer);
+        constructor Create (const aents: TEntryArray; afirst, alast: Integer);
         function MoveNext (): Boolean; inline;
         function getCurrent (): KeyT; inline;
+        function GetEnumerator (): TKeyEnumerator; inline;
         property Current: KeyT read getCurrent;
       end;
 
@@ -81,9 +85,10 @@ type
         mEntries: TEntryArray;
         mFirstEntry, mLastEntry, cur: Integer;
       public
-        constructor Create (aents: TEntryArray; afirst, alast: Integer);
+        constructor Create (const aents: TEntryArray; afirst, alast: Integer);
         function MoveNext (): Boolean; inline;
         function getCurrent (): PEntry; inline;
+        function GetEnumerator (): TKeyValEnumerator; inline;
         property Current: PEntry read getCurrent;
       end;
 
@@ -161,9 +166,11 @@ type
 type
   THashIntInt = specialize THashBase<Integer, Integer>;
   THashStrInt = specialize THashBase<AnsiString, Integer>;
+  THashStrStr = specialize THashBase<AnsiString, AnsiString>;
 
 function hashNewIntInt (): THashIntInt;
 function hashNewStrInt (): THashStrInt;
+function hashNewStrStr (): THashStrStr;
 
 
 function u32Hash (a: LongWord): LongWord; inline;
@@ -173,6 +180,13 @@ function joaatHash (constref buf; len: LongWord): LongWord;
 function nextPOT (x: LongWord): LongWord; inline;
 
 
+// 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;
+
+
 implementation
 
 uses
@@ -204,7 +218,7 @@ function hsiequ (constref a, b: AnsiString): Boolean; begin result := (a = b); e
 {$RANGECHECKS OFF}
 function hiihash (constref k: Integer): LongWord;
 begin
-  result := k;
+  result := LongWord(k);
   result -= (result shl 6);
   result := result xor (result shr 17);
   result -= (result shl 9);
@@ -233,6 +247,12 @@ begin
 end;
 
 
+function hashNewStrStr (): THashStrStr;
+begin
+  result := THashStrStr.Create(hsihash, hsiequ);
+end;
+
+
 // ////////////////////////////////////////////////////////////////////////// //
 {$PUSH}
 {$RANGECHECKS OFF}
@@ -944,8 +964,13 @@ begin
 end;
 
 
+function THashBase.TValEnumerator.GetEnumerator (): TValEnumerator; inline; begin result.mEntries := self.mEntries; result.mFirstEntry := self.mFirstEntry; result.mLastEntry := self.mLastEntry; result.cur := self.cur; end;
+function THashBase.TKeyEnumerator.GetEnumerator (): TKeyEnumerator; inline; begin result.mEntries := self.mEntries; result.mFirstEntry := self.mFirstEntry; result.mLastEntry := self.mLastEntry; result.cur := self.cur; end;
+function THashBase.TKeyValEnumerator.GetEnumerator (): TKeyValEnumerator; inline; begin result.mEntries := self.mEntries; result.mFirstEntry := self.mFirstEntry; result.mLastEntry := self.mLastEntry; result.cur := self.cur; end;
+
+
 // ////////////////////////////////////////////////////////////////////////// //
-constructor THashBase.TValEnumerator.Create (aents: TEntryArray; afirst, alast: Integer);
+constructor THashBase.TValEnumerator.Create (const aents: TEntryArray; afirst, alast: Integer);
 begin
   mEntries := aents;
   mFirstEntry := afirst;
@@ -970,7 +995,7 @@ end;
 
 
 // ////////////////////////////////////////////////////////////////////////// //
-constructor THashBase.TKeyEnumerator.Create (aents: TEntryArray; afirst, alast: Integer);
+constructor THashBase.TKeyEnumerator.Create (const aents: TEntryArray; afirst, alast: Integer);
 begin
   mEntries := aents;
   mFirstEntry := afirst;
@@ -995,7 +1020,7 @@ end;
 
 
 // ////////////////////////////////////////////////////////////////////////// //
-constructor THashBase.TKeyValEnumerator.Create (aents: TEntryArray; afirst, alast: Integer);
+constructor THashBase.TKeyValEnumerator.Create (const aents: TEntryArray; afirst, alast: Integer);
 begin
   mEntries := aents;
   mFirstEntry := afirst;