tb_hashtable.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. #include "tb_hashtable.h"
  6. #include "tb_system.h"
  7. #include "tb_tempbuffer.h"
  8. namespace tb {
  9. //FIX: reduce memory (block allocation of ITEM)
  10. //FIX: should shrink when deleting single items (but not when adding items!)
  11. //FIX: should grow when about 70% full instead of 100%
  12. // == TBHashTable =======================================================================
  13. TBHashTable::TBHashTable()
  14. : m_buckets(0)
  15. , m_num_buckets(0)
  16. , m_num_items(0)
  17. {
  18. }
  19. TBHashTable::~TBHashTable()
  20. {
  21. RemoveAll();
  22. }
  23. void TBHashTable::RemoveAll(bool delete_content)
  24. {
  25. #ifdef TB_RUNTIME_DEBUG_INFO
  26. //Debug();
  27. #endif
  28. for (uint32 i = 0; i < m_num_buckets; i++)
  29. {
  30. ITEM *item = m_buckets[i];
  31. while (item)
  32. {
  33. ITEM *item_next = item->next;
  34. if (delete_content)
  35. DeleteContent(item->content);
  36. delete item;
  37. item = item_next;
  38. }
  39. }
  40. delete [] m_buckets;
  41. m_buckets = nullptr;
  42. m_num_buckets = m_num_items = 0;
  43. }
  44. bool TBHashTable::Rehash(uint32 new_num_buckets)
  45. {
  46. if (new_num_buckets == m_num_buckets)
  47. return true;
  48. if (ITEM **new_buckets = new ITEM*[new_num_buckets])
  49. {
  50. memset(new_buckets, 0, sizeof(ITEM*) * new_num_buckets);
  51. // Rehash all items into the new buckets
  52. for (uint32 i = 0; i < m_num_buckets; i++)
  53. {
  54. ITEM *item = m_buckets[i];
  55. while (item)
  56. {
  57. ITEM *item_next = item->next;
  58. // Add it to new_buckets
  59. uint32 bucket = item->key & (new_num_buckets - 1);
  60. item->next = new_buckets[bucket];
  61. new_buckets[bucket] = item;
  62. item = item_next;
  63. }
  64. }
  65. // Delete old buckets and update
  66. delete [] m_buckets;
  67. m_buckets = new_buckets;
  68. m_num_buckets = new_num_buckets;
  69. return true;
  70. }
  71. return false;
  72. }
  73. bool TBHashTable::NeedRehash() const
  74. {
  75. // Grow if more items than buckets
  76. return !m_num_buckets || m_num_items >= m_num_buckets;
  77. }
  78. uint32 TBHashTable::GetSuitableBucketsCount() const
  79. {
  80. // As long as we use FNV for TBID (in TBGetHash), power of two hash sizes are the best.
  81. if (!m_num_items)
  82. return 16;
  83. return m_num_items * 2;
  84. }
  85. void *TBHashTable::Get(uint32 key) const
  86. {
  87. if (!m_num_buckets)
  88. return nullptr;
  89. uint32 bucket = key & (m_num_buckets - 1);
  90. ITEM *item = m_buckets[bucket];
  91. while (item)
  92. {
  93. if (item->key == key)
  94. return item->content;
  95. item = item->next;
  96. }
  97. return nullptr;
  98. }
  99. bool TBHashTable::Add(uint32 key, void *content)
  100. {
  101. if (NeedRehash() && !Rehash(GetSuitableBucketsCount()))
  102. return false;
  103. assert(!Get(key));
  104. if (ITEM *item = new ITEM)
  105. {
  106. uint32 bucket = key & (m_num_buckets - 1);
  107. item->key = key;
  108. item->content = content;
  109. item->next = m_buckets[bucket];
  110. m_buckets[bucket] = item;
  111. m_num_items++;
  112. return true;
  113. }
  114. return false;
  115. }
  116. void *TBHashTable::Remove(uint32 key)
  117. {
  118. if (!m_num_buckets)
  119. return nullptr;
  120. uint32 bucket = key & (m_num_buckets - 1);
  121. ITEM *item = m_buckets[bucket];
  122. ITEM *prev_item = nullptr;
  123. while (item)
  124. {
  125. if (item->key == key)
  126. {
  127. if (prev_item)
  128. prev_item->next = item->next;
  129. else
  130. m_buckets[bucket] = item->next;
  131. void *content = item->content;
  132. delete item;
  133. return content;
  134. }
  135. prev_item = item;
  136. item = item->next;
  137. }
  138. assert(!"This hash table didn't contain the given key!");
  139. return nullptr;
  140. }
  141. void TBHashTable::Delete(uint32 key)
  142. {
  143. DeleteContent(Remove(key));
  144. }
  145. #ifdef TB_RUNTIME_DEBUG_INFO
  146. void TBHashTable::Debug()
  147. {
  148. TBTempBuffer line;
  149. line.AppendString("Hash table: ");
  150. int total_count = 0;
  151. for (uint32 i = 0; i < m_num_buckets; i++)
  152. {
  153. int count = 0;
  154. ITEM *item = m_buckets[i];
  155. while (item)
  156. {
  157. count++;
  158. item = item->next;
  159. }
  160. TBStr tmp; tmp.SetFormatted("%d ", count);
  161. line.AppendString(tmp);
  162. total_count += count;
  163. }
  164. TBStr tmp; tmp.SetFormatted(" (total: %d of %d buckets)\n", total_count, m_num_buckets);
  165. line.AppendString(tmp);
  166. TBDebugOut(line.GetData());
  167. }
  168. #endif // TB_RUNTIME_DEBUG_INFO
  169. // == TBHashTableIterator ===============================================================
  170. TBHashTableIterator::TBHashTableIterator(TBHashTable *hash_table)
  171. : m_hash_table(hash_table)
  172. , m_current_bucket(0)
  173. , m_current_item(nullptr)
  174. {
  175. }
  176. void *TBHashTableIterator::GetNextContent()
  177. {
  178. if (m_current_bucket == m_hash_table->m_num_buckets)
  179. return nullptr;
  180. if (m_current_item && m_current_item->next)
  181. m_current_item = m_current_item->next;
  182. else
  183. {
  184. if (m_current_item)
  185. m_current_bucket++;
  186. if (m_current_bucket == m_hash_table->m_num_buckets)
  187. return nullptr;
  188. while (m_current_bucket < m_hash_table->m_num_buckets)
  189. {
  190. m_current_item = m_hash_table->m_buckets[m_current_bucket];
  191. if (m_current_item)
  192. break;
  193. m_current_bucket++;
  194. }
  195. }
  196. return m_current_item ? m_current_item->content : nullptr;
  197. }
  198. }; // namespace tb