hash_set.h - Engine C API Reference

hash_set.h
  1. #pragma once
  2. #include "vector.h"
  3. #include "hash_function.h"
  4. #include "bucket_iterator.h"
  5. #include "allocator.h"
  6. #include "template_tools.h"
  7. #include "functional.h"
  8. namespace stingray_plugin_foundation {
  9. // Implementation of set using the same technique as HashMap.
  10. // see HashMap
  11. template <class K, class HASH = default_hash<K>, class EQUAL = equal_to >
  12. class HashSet
  13. {
  14. public:
  15. ALLOCATOR_AWARE;
  16. typedef K key_type;
  17. typedef HASH key_hash;
  18. typedef EQUAL key_equal;
  19. typedef HashSet<K, HASH, EQUAL> this_type;
  20. typedef ConstBucketIterator<this_type, key_type> const_iterator;
  21. typedef BucketIterator<this_type, key_type> iterator;
  22. enum {END_OF_LIST = 0x7fffffff, UNUSED = 0xfffffffe, END_OF_FREELIST = 0xffffffff};
  23. HashSet(Allocator &a);
  24. HashSet(unsigned buckets, unsigned spill, Allocator &a);
  25. HashSet(const HashSet<K,HASH,EQUAL> &o);
  26. ~HashSet();
  27. void operator=(const HashSet<K,HASH,EQUAL> &o);
  28. void reserve(unsigned size);
  29. void swap(HashSet<K,HASH,EQUAL> &o);
  30. Allocator & allocator() const;
  31. unsigned size() const;
  32. bool empty() const;
  33. template <class KEY_EQ> bool has(const KEY_EQ &k) const;
  34. void clear();
  35. template <class KEY_EQ> void insert(const KEY_EQ &k);
  36. template <class KEY_EQ> void erase(const KEY_EQ &k);
  37. void rehash(unsigned new_buckets);
  38. const_iterator begin() const;
  39. const_iterator end() const;
  40. unsigned num_buckets() const;
  41. bool bucket_valid(unsigned i) const;
  42. const key_type &bucket_value(unsigned i) const;
  43. key_type &bucket_value(unsigned i);
  44. template <class STREAM> void serialize(STREAM &stream);
  45. private:
  46. template <class KEY_EQ> unsigned find_or_fail(const KEY_EQ &k) const;
  47. template <class KEY_EQ> unsigned find_or_make(const KEY_EQ &k);
  48. template <class KEY_EQ> void find_and_erase(const KEY_EQ &k);
  49. bool full() const;
  50. void grow();
  51. template <class KEY_EQ> unsigned hash(const KEY_EQ &k) const;
  52. unsigned allocate_spill();
  53. void free_spill(unsigned i);
  54. void clear_freelist();
  55. void allocate_data(unsigned count);
  56. void construct(key_type &p, const Int2Type<true> &) { new (&p) key_type(_allocator); }
  57. void construct(key_type &p, const Int2Type<false> &) { new (&p) key_type(); }
  58. void destruct(key_type &p) { p.~key_type(); }
  59. struct Data {
  60. Data() : size(), marker(), key() {}
  61. unsigned size;
  62. unsigned *marker;
  63. key_type *key;
  64. };
  65. key_hash _hash;
  66. key_equal _equal;
  67. Data _data;
  68. unsigned _used;
  69. unsigned _buckets;
  70. unsigned _spill_unused;
  71. unsigned _spill_freelist;
  72. Allocator &_allocator;
  73. };
  74. } // namespace stingray_plugin_foundation
  75. #include "hash_set.inl"