tb_hashtable.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. #ifndef TB_HASHTABLE_H
  6. #define TB_HASHTABLE_H
  7. #include "tb_core.h"
  8. #include <assert.h>
  9. namespace tb {
  10. /** TBHashTable is a minimal hash table, for hashing anything using a uint32 key. */
  11. class TBHashTable
  12. {
  13. public:
  14. TBHashTable();
  15. virtual ~TBHashTable();
  16. /** Remove all items without deleting the content. */
  17. void RemoveAll() { RemoveAll(false); }
  18. /** Remove all items and delete the content.
  19. This requires TBHashTable to be subclassed and implementing DeleteContent.
  20. You would typically do this by using TBHashTableOf or TBHashTableAutoDeleteOf. */
  21. void DeleteAll() { RemoveAll(true); }
  22. /** Get the content for the given key, or nullptr if not found. */
  23. void *Get(uint32 key) const;
  24. /** Add content with the given key.
  25. Returns false if out of memory. */
  26. bool Add(uint32 key, void *content);
  27. /** Remove the content with the given key. */
  28. void *Remove(uint32 key);
  29. /** Delete the content with the given key. */
  30. void Delete(uint32 key);
  31. /** Rehash the table so use the given number of buckets.
  32. Returns false if out of memory. */
  33. bool Rehash(uint32 num_buckets);
  34. /** Return true if the hashtable itself think it's time to rehash. */
  35. bool NeedRehash() const;
  36. /** Get the number of buckets the hashtable itself thinks is suitable for
  37. the current number of items. */
  38. uint32 GetSuitableBucketsCount() const;
  39. /** Get the number of items in the hash table. */
  40. uint32 GetNumItems() const { return m_num_items; }
  41. #ifdef TB_RUNTIME_DEBUG_INFO
  42. /** Print out some debug info about the hash table. */
  43. void Debug();
  44. #endif
  45. protected:
  46. /** Delete the content of a item. This is called if calling DeleteAll, and must be
  47. implemented in a subclass that knows about the content type. */
  48. virtual void DeleteContent(void *content) { assert(!"You need to subclass and implement!"); }
  49. private:
  50. friend class TBHashTableIterator;
  51. void RemoveAll(bool delete_content);
  52. struct ITEM {
  53. uint32 key;
  54. ITEM *next;
  55. void *content;
  56. } **m_buckets;
  57. uint32 m_num_buckets;
  58. uint32 m_num_items;
  59. };
  60. /** TBHashTableIterator is a iterator for stepping through all content stored in a TBHashTable. */
  61. //FIX: make it safe in case the current item is removed from the hashtable
  62. class TBHashTableIterator
  63. {
  64. public:
  65. TBHashTableIterator(TBHashTable *hash_table);
  66. void *GetNextContent();
  67. private:
  68. TBHashTable *m_hash_table;
  69. uint32 m_current_bucket;
  70. TBHashTable::ITEM *m_current_item;
  71. };
  72. /** TBHashTableIteratorOf is a TBHashTableIterator which auto cast to the class type. */
  73. template<class T>
  74. class TBHashTableIteratorOf : private TBHashTableIterator
  75. {
  76. public:
  77. TBHashTableIteratorOf(TBHashTable *hash_table) : TBHashTableIterator(hash_table) {}
  78. T *GetNextContent() { return (T*) TBHashTableIterator::GetNextContent(); }
  79. };
  80. /** TBHashTableOf is a TBHashTable with the given class type as content. */
  81. template<class T>
  82. class TBHashTableOf : public TBHashTable
  83. {
  84. // FIX: Don't do public inheritance! Either inherit privately and forward, or use a private member backend!
  85. public:
  86. T *Get(uint32 key) const { return (T*) TBHashTable::Get(key); }
  87. T *Remove(uint32 key) { return (T*) TBHashTable::Remove(key); }
  88. protected:
  89. virtual void DeleteContent(void *content) { delete (T*) content; }
  90. };
  91. /** TBHashTableOf is a TBHashTable with the given class type as content.
  92. It will delete all content automaticallt on destruction. */
  93. template<class T>
  94. class TBHashTableAutoDeleteOf : public TBHashTable
  95. {
  96. public:
  97. ~TBHashTableAutoDeleteOf() { DeleteAll(); }
  98. T *Get(uint32 key) const { return (T*) TBHashTable::Get(key); }
  99. protected:
  100. virtual void DeleteContent(void *content) { delete (T*) content; }
  101. };
  102. } // namespace tb
  103. #endif // TB_HASHTABLE_H