BasicHashTable.hh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**********
  2. This library is free software; you can redistribute it and/or modify it under
  3. the terms of the GNU Lesser General Public License as published by the
  4. Free Software Foundation; either version 3 of the License, or (at your
  5. option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
  6. This library is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  9. more details.
  10. You should have received a copy of the GNU Lesser General Public License
  11. along with this library; if not, write to the Free Software Foundation, Inc.,
  12. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  13. **********/
  14. // Copyright (c) 1996-2019 Live Networks, Inc. All rights reserved.
  15. // Basic Hash Table implementation
  16. // C++ header
  17. #ifndef _BASIC_HASH_TABLE_HH
  18. #define _BASIC_HASH_TABLE_HH
  19. #ifndef _HASH_TABLE_HH
  20. #include "HashTable.hh"
  21. #endif
  22. #ifndef _NET_COMMON_H
  23. #include <NetCommon.h> // to ensure that "uintptr_t" is defined
  24. #endif
  25. // A simple hash table implementation, inspired by the hash table
  26. // implementation used in Tcl 7.6: <http://www.tcl.tk/>
  27. #define SMALL_HASH_TABLE_SIZE 4
  28. class BasicHashTable: public HashTable {
  29. private:
  30. class TableEntry; // forward
  31. public:
  32. BasicHashTable(int keyType);
  33. virtual ~BasicHashTable();
  34. // Used to iterate through the members of the table:
  35. class Iterator; friend class Iterator; // to make Sun's C++ compiler happy
  36. class Iterator: public HashTable::Iterator {
  37. public:
  38. Iterator(BasicHashTable const& table);
  39. private: // implementation of inherited pure virtual functions
  40. void* next(char const*& key); // returns 0 if none
  41. private:
  42. BasicHashTable const& fTable;
  43. unsigned fNextIndex; // index of next bucket to be enumerated after this
  44. TableEntry* fNextEntry; // next entry in the current bucket
  45. };
  46. private: // implementation of inherited pure virtual functions
  47. virtual void* Add(char const* key, void* value);
  48. // Returns the old value if different, otherwise 0
  49. virtual Boolean Remove(char const* key);
  50. virtual void* Lookup(char const* key) const;
  51. // Returns 0 if not found
  52. virtual unsigned numEntries() const;
  53. private:
  54. class TableEntry {
  55. public:
  56. TableEntry* fNext;
  57. char const* key;
  58. void* value;
  59. };
  60. TableEntry* lookupKey(char const* key, unsigned& index) const;
  61. // returns entry matching "key", or NULL if none
  62. Boolean keyMatches(char const* key1, char const* key2) const;
  63. // used to implement "lookupKey()"
  64. TableEntry* insertNewEntry(unsigned index, char const* key);
  65. // creates a new entry, and inserts it in the table
  66. void assignKey(TableEntry* entry, char const* key);
  67. // used to implement "insertNewEntry()"
  68. void deleteEntry(unsigned index, TableEntry* entry);
  69. void deleteKey(TableEntry* entry);
  70. // used to implement "deleteEntry()"
  71. void rebuild(); // rebuilds the table as its size increases
  72. unsigned hashIndexFromKey(char const* key) const;
  73. // used to implement many of the routines above
  74. unsigned randomIndex(uintptr_t i) const {
  75. return (unsigned)(((i*1103515245) >> fDownShift) & fMask);
  76. }
  77. private:
  78. TableEntry** fBuckets; // pointer to bucket array
  79. TableEntry* fStaticBuckets[SMALL_HASH_TABLE_SIZE];// used for small tables
  80. unsigned fNumBuckets, fNumEntries, fRebuildSize, fDownShift, fMask;
  81. int fKeyType;
  82. };
  83. #endif