HashTable.hh 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. // Generic Hash Table
  16. // C++ header
  17. #ifndef _HASH_TABLE_HH
  18. #define _HASH_TABLE_HH
  19. #ifndef _BOOLEAN_HH
  20. #include "Boolean.hh"
  21. #endif
  22. class HashTable {
  23. public:
  24. virtual ~HashTable();
  25. // The following must be implemented by a particular
  26. // implementation (subclass):
  27. static HashTable* create(int keyType);
  28. virtual void* Add(char const* key, void* value) = 0;
  29. // Returns the old value if different, otherwise 0
  30. virtual Boolean Remove(char const* key) = 0;
  31. virtual void* Lookup(char const* key) const = 0;
  32. // Returns 0 if not found
  33. virtual unsigned numEntries() const = 0;
  34. Boolean IsEmpty() const { return numEntries() == 0; }
  35. // Used to iterate through the members of the table:
  36. class Iterator {
  37. public:
  38. // The following must be implemented by a particular
  39. // implementation (subclass):
  40. static Iterator* create(HashTable const& hashTable);
  41. virtual ~Iterator();
  42. virtual void* next(char const*& key) = 0; // returns 0 if none
  43. protected:
  44. Iterator(); // abstract base class
  45. };
  46. // A shortcut that can be used to successively remove each of
  47. // the entries in the table (e.g., so that their values can be
  48. // deleted, if they happen to be pointers to allocated memory).
  49. void* RemoveNext();
  50. // Returns the first entry in the table.
  51. // (This is useful for deleting each entry in the table, if the entry's destructor also removes itself from the table.)
  52. void* getFirst();
  53. protected:
  54. HashTable(); // abstract base class
  55. };
  56. // Warning: The following are deliberately the same as in
  57. // Tcl's hash table implementation
  58. int const STRING_HASH_KEYS = 0;
  59. int const ONE_WORD_HASH_KEYS = 1;
  60. #endif