tb_hashtable.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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(nullptr)
  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. m_num_items--;
  132. void *content = item->content;
  133. delete item;
  134. return content;
  135. }
  136. prev_item = item;
  137. item = item->next;
  138. }
  139. assert(!"This hash table didn't contain the given key!");
  140. return nullptr;
  141. }
  142. void TBHashTable::Delete(uint32 key)
  143. {
  144. DeleteContent(Remove(key));
  145. }
  146. #ifdef TB_RUNTIME_DEBUG_INFO
  147. void TBHashTable::Debug()
  148. {
  149. TBTempBuffer line;
  150. line.AppendString("Hash table: ");
  151. int total_count = 0;
  152. for (uint32 i = 0; i < m_num_buckets; i++)
  153. {
  154. int count = 0;
  155. ITEM *item = m_buckets[i];
  156. while (item)
  157. {
  158. count++;
  159. item = item->next;
  160. }
  161. TBStr tmp; tmp.SetFormatted("%d ", count);
  162. line.AppendString(tmp);
  163. total_count += count;
  164. }
  165. TBStr tmp; tmp.SetFormatted(" (total: %d of %d buckets)\n", total_count, m_num_buckets);
  166. line.AppendString(tmp);
  167. TBDebugOut(line.GetData());
  168. }
  169. #endif // TB_RUNTIME_DEBUG_INFO
  170. // == TBHashTableIterator ===============================================================
  171. TBHashTableIterator::TBHashTableIterator(TBHashTable *hash_table)
  172. : m_hash_table(hash_table)
  173. , m_current_bucket(0)
  174. , m_current_item(nullptr)
  175. {
  176. }
  177. void *TBHashTableIterator::GetNextContent()
  178. {
  179. if (m_current_bucket == m_hash_table->m_num_buckets)
  180. return nullptr;
  181. if (m_current_item && m_current_item->next)
  182. m_current_item = m_current_item->next;
  183. else
  184. {
  185. if (m_current_item)
  186. m_current_bucket++;
  187. if (m_current_bucket == m_hash_table->m_num_buckets)
  188. return nullptr;
  189. while (m_current_bucket < m_hash_table->m_num_buckets)
  190. {
  191. m_current_item = m_hash_table->m_buckets[m_current_bucket];
  192. if (m_current_item)
  193. break;
  194. m_current_bucket++;
  195. }
  196. }
  197. return m_current_item ? m_current_item->content : nullptr;
  198. }
  199. } // namespace tb