tb_hashtable.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #ifdef TB_RUNTIME_DEBUG_INFO
  40. /** Print out some debug info about the hash table. */
  41. void Debug();
  42. #endif
  43. protected:
  44. /** Delete the content of a item. This is called if calling DeleteAll, and must be
  45. implemented in a subclass that knows about the content type. */
  46. virtual void DeleteContent(void *content) { assert(!"You need to subclass and implement!"); }
  47. private:
  48. friend class TBHashTableIterator;
  49. void RemoveAll(bool delete_content);
  50. struct ITEM {
  51. uint32 key;
  52. ITEM *next;
  53. void *content;
  54. } **m_buckets;
  55. uint32 m_num_buckets;
  56. uint32 m_num_items;
  57. };
  58. /** TBHashTableIterator is a iterator for stepping through all content stored in a TBHashTable. */
  59. //FIX: make it safe in case the current item is removed from the hashtable
  60. class TBHashTableIterator
  61. {
  62. public:
  63. TBHashTableIterator(TBHashTable *hash_table);
  64. void *GetNextContent();
  65. private:
  66. TBHashTable *m_hash_table;
  67. uint32 m_current_bucket;
  68. TBHashTable::ITEM *m_current_item;
  69. };
  70. /** TBHashTableIteratorOf is a TBHashTableIterator which auto cast to the class type. */
  71. template<class T>
  72. class TBHashTableIteratorOf : private TBHashTableIterator
  73. {
  74. public:
  75. TBHashTableIteratorOf(TBHashTable *hash_table) : TBHashTableIterator(hash_table) {}
  76. T *GetNextContent() { return (T*) TBHashTableIterator::GetNextContent(); }
  77. };
  78. /** TBHashTableOf is a TBHashTable with the given class type as content. */
  79. template<class T>
  80. class TBHashTableOf : public TBHashTable
  81. {
  82. // FIX: Don't do public inheritance! Either inherit privately and forward, or use a private member backend!
  83. public:
  84. T *Get(uint32 key) const { return (T*) TBHashTable::Get(key); }
  85. protected:
  86. virtual void DeleteContent(void *content) { delete (T*) content; }
  87. };
  88. /** TBHashTableOf is a TBHashTable with the given class type as content.
  89. It will delete all content automaticallt on destruction. */
  90. template<class T>
  91. class TBHashTableAutoDeleteOf : public TBHashTable
  92. {
  93. public:
  94. ~TBHashTableAutoDeleteOf() { DeleteAll(); }
  95. T *Get(uint32 key) const { return (T*) TBHashTable::Get(key); }
  96. protected:
  97. virtual void DeleteContent(void *content) { delete (T*) content; }
  98. };
  99. }; // namespace tb
  100. #endif // TB_HASHTABLE_H