hash_map_set.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #ifndef __HASH_MAP_SET_H__
  2. #define __HASH_MAP_SET_H__
  3. #include <set>
  4. #include "fixed_types.h"
  5. template <class T>
  6. class HashMapSet
  7. {
  8. private:
  9. std::set<T>** m_set_list;
  10. UInt32 m_num_buckets;
  11. UInt32 (*m_hash_fn)(T, UInt32, UInt32);
  12. UInt32 m_hash_fn_param;
  13. public:
  14. HashMapSet(UInt32 num_buckets, UInt32 (*hash_fn)(T, UInt32, UInt32), UInt32 hash_fn_param) :
  15. m_num_buckets(num_buckets),
  16. m_hash_fn(hash_fn),
  17. m_hash_fn_param(hash_fn_param)
  18. {
  19. m_set_list = new std::set<T>*[m_num_buckets];
  20. for (UInt32 i = 0; i < m_num_buckets; i++)
  21. {
  22. m_set_list[i] = new std::set<T>;
  23. }
  24. }
  25. ~HashMapSet() {}
  26. void insert(T elem)
  27. {
  28. UInt32 bucket = m_hash_fn(elem, m_hash_fn_param, m_num_buckets);
  29. m_set_list[bucket]->insert(elem);
  30. // LOG_ASSERT_WARNING(m_set_list[bucket]->size() <= 100,
  31. // "m_set_list[%u] : (%u > 100)", bucket, m_set_list[bucket]->size());
  32. }
  33. UInt32 count(T elem)
  34. {
  35. UInt32 bucket = m_hash_fn(elem, m_hash_fn_param, m_num_buckets);
  36. return m_set_list[bucket]->count(elem);
  37. }
  38. void erase(T elem)
  39. {
  40. UInt32 bucket = m_hash_fn(elem, m_hash_fn_param, m_num_buckets);
  41. m_set_list[bucket]->erase(elem);
  42. }
  43. void clear()
  44. {
  45. for (UInt32 i = 0; i < m_num_buckets; i++)
  46. m_set_list[i]->clear();
  47. }
  48. };
  49. #endif /* __HASH_MAP_SET_H__ */