Hasty Badger
Small UI library (a branch of Turbo Badger)
 All Classes Namespaces Functions Variables Enumerations Enumerator Friends Groups Pages
tb_hashtable.h
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 
6 #ifndef TB_HASHTABLE_H
7 #define TB_HASHTABLE_H
8 
9 #include "tb_core.h"
10 #include <assert.h>
11 
12 namespace tb {
13 
17 {
18 public:
19  TBHashTable();
20  virtual ~TBHashTable();
21 
23  void RemoveAll() { RemoveAll(false); }
24 
28  void DeleteAll() { RemoveAll(true); }
29 
31  void *Get(uint32_t key) const;
32 
35  bool Add(uint32_t key, void *content);
36 
38  void *Remove(uint32_t key);
39 
41  void Delete(uint32_t key);
42 
45  bool Rehash(uint32_t num_buckets);
46 
48  bool NeedRehash() const;
49 
52  uint32_t GetSuitableBucketsCount() const;
53 
54 #ifdef TB_RUNTIME_DEBUG_INFO
55 
56  void Debug();
57 #endif
58 
59 protected:
62  virtual void DeleteContent(void * /*content*/) { assert(!"You need to subclass and implement!"); }
63 private:
64  friend class TBHashTableIterator;
65  void RemoveAll(bool delete_content);
66  struct ITEM {
67  uint32_t key;
68  ITEM *next;
69  void *content;
70  } **m_buckets;
71  uint32_t m_num_buckets;
72  uint32_t m_num_items;
73 };
74 
76 //FIX: make it safe in case the current item is removed from the hashtable
78 {
79 public:
80  TBHashTableIterator(TBHashTable *hash_table);
81  void *GetNextContent();
82 private:
83  TBHashTable *m_hash_table;
84  uint32_t m_current_bucket;
85  TBHashTable::ITEM *m_current_item;
86 };
87 
89 template<class T>
91 {
92 public:
93  TBHashTableIteratorOf(TBHashTable *hash_table) : TBHashTableIterator(hash_table) {}
94  T *GetNextContent() { return (T*) TBHashTableIterator::GetNextContent(); }
95 };
96 
98 template<class T>
99 class TBHashTableOf : public TBHashTable
100 {
101 // FIX: Don't do public inheritance! Either inherit privately and forward, or use a private member backend!
102 public:
103  T *Get(uint32_t key) const { return (T*) TBHashTable::Get(key); }
104 
105 protected:
106  virtual void DeleteContent(void *content) { delete (T*) content; }
107 };
108 
111 template<class T>
113 {
114 public:
116 
117  T *Get(uint32_t key) const { return (T*) TBHashTable::Get(key); }
118 
119 protected:
120  virtual void DeleteContent(void *content) { delete (T*) content; }
121 };
122 
123 } // namespace tb
124 
125 #endif // TB_HASHTABLE_H
TBHashTableOf is a TBHashTable with the given class type as content.
Definition: tb_hashtable.h:112
bool NeedRehash() const
Return true if the hashtable itself think it&#39;s time to rehash.
Definition: tb_hashtable.cpp:82
virtual void DeleteContent(void *content)
Delete the content of a item.
Definition: tb_hashtable.h:106
TBHashTableIterator is a iterator for stepping through all content stored in a TBHashTable.
Definition: tb_hashtable.h:77
virtual void DeleteContent(void *)
Delete the content of a item.
Definition: tb_hashtable.h:62
bool Add(uint32_t key, void *content)
Add content with the given key.
Definition: tb_hashtable.cpp:111
void * Remove(uint32_t key)
Remove the content with the given key.
Definition: tb_hashtable.cpp:129
TBHashTableIteratorOf is a TBHashTableIterator which auto cast to the class type. ...
Definition: tb_hashtable.h:90
TBHashTableOf is a TBHashTable with the given class type as content.
Definition: tb_hashtable.h:99
bool Rehash(uint32_t num_buckets)
Rehash the table so use the given number of buckets.
Definition: tb_hashtable.cpp:52
void Delete(uint32_t key)
Delete the content with the given key.
Definition: tb_hashtable.cpp:155
uint32_t GetSuitableBucketsCount() const
Get the number of buckets the hashtable itself thinks is suitable for the current number of items...
Definition: tb_hashtable.cpp:88
virtual void DeleteContent(void *content)
Delete the content of a item.
Definition: tb_hashtable.h:120
void RemoveAll()
Remove all items without deleting the content.
Definition: tb_hashtable.h:23
void DeleteAll()
Remove all items and delete the content.
Definition: tb_hashtable.h:28
void * Get(uint32_t key) const
Get the content for the given key, or nullptr if not found.
Definition: tb_hashtable.cpp:96
TBHashTable is a minimal hash table, for hashing anything using a uint32_t key.
Definition: tb_hashtable.h:16