unordered_set.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*-
  2. * Copyright 2012-2018 Matthew Endsley
  3. * All rights reserved
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted providing that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  18. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  22. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  23. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. * POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifndef TINYSTL_UNORDERED_SET_H
  27. #define TINYSTL_UNORDERED_SET_H
  28. #include <tinystl/allocator.h>
  29. #include <tinystl/buffer.h>
  30. #include <tinystl/hash.h>
  31. #include <tinystl/hash_base.h>
  32. namespace tinystl {
  33. template<typename Key, typename Alloc = TINYSTL_ALLOCATOR>
  34. class unordered_set {
  35. public:
  36. unordered_set();
  37. unordered_set(const unordered_set& other);
  38. unordered_set(unordered_set&& other);
  39. ~unordered_set();
  40. unordered_set& operator=(const unordered_set& other);
  41. unordered_set& operator=(unordered_set&& other);
  42. typedef unordered_hash_iterator<const unordered_hash_node<Key, void> > const_iterator;
  43. typedef const_iterator iterator;
  44. iterator begin() const;
  45. iterator end() const;
  46. void clear();
  47. bool empty() const;
  48. size_t size() const;
  49. iterator find(const Key& key) const;
  50. pair<iterator, bool> insert(const Key& key);
  51. pair<iterator, bool> emplace(Key&& key);
  52. void erase(iterator where);
  53. size_t erase(const Key& key);
  54. void swap(unordered_set& other);
  55. private:
  56. void rehash(size_t nbuckets);
  57. typedef unordered_hash_node<Key, void>* pointer;
  58. size_t m_size;
  59. tinystl::buffer<pointer, Alloc> m_buckets;
  60. };
  61. template<typename Key, typename Alloc>
  62. inline unordered_set<Key, Alloc>::unordered_set()
  63. : m_size(0)
  64. {
  65. buffer_init<pointer, Alloc>(&m_buckets);
  66. }
  67. template<typename Key, typename Alloc>
  68. inline unordered_set<Key, Alloc>::unordered_set(const unordered_set& other)
  69. : m_size(other.m_size)
  70. {
  71. const size_t nbuckets = (size_t)(other.m_buckets.last - other.m_buckets.first);
  72. buffer_init<pointer, Alloc>(&m_buckets);
  73. buffer_resize<pointer, Alloc>(&m_buckets, nbuckets, 0);
  74. if (other.m_buckets.first) {
  75. for (pointer it = *other.m_buckets.first; it; it = it->next) {
  76. unordered_hash_node<Key, void>* newnode = new(placeholder(), Alloc::static_allocate(sizeof(unordered_hash_node<Key, void>))) unordered_hash_node<Key, void>(*it);
  77. newnode->next = newnode->prev = 0;
  78. unordered_hash_node_insert(newnode, hash(it->first), m_buckets.first, nbuckets - 1);
  79. }
  80. }
  81. }
  82. template<typename Key, typename Alloc>
  83. inline unordered_set<Key, Alloc>::unordered_set(unordered_set&& other)
  84. : m_size(other.m_size)
  85. {
  86. buffer_move(&m_buckets, &other.m_buckets);
  87. other.m_size = 0;
  88. }
  89. template<typename Key, typename Alloc>
  90. inline unordered_set<Key, Alloc>::~unordered_set() {
  91. if (m_buckets.first != m_buckets.last)
  92. clear();
  93. buffer_destroy<pointer, Alloc>(&m_buckets);
  94. }
  95. template<typename Key, typename Alloc>
  96. inline unordered_set<Key, Alloc>& unordered_set<Key, Alloc>::operator=(const unordered_set<Key, Alloc>& other) {
  97. unordered_set<Key, Alloc>(other).swap(*this);
  98. return *this;
  99. }
  100. template<typename Key, typename Alloc>
  101. inline unordered_set<Key, Alloc>& unordered_set<Key, Alloc>::operator=(unordered_set&& other) {
  102. unordered_set(static_cast<unordered_set&&>(other)).swap(*this);
  103. return *this;
  104. }
  105. template<typename Key, typename Alloc>
  106. inline typename unordered_set<Key, Alloc>::iterator unordered_set<Key, Alloc>::begin() const {
  107. iterator it;
  108. if (m_buckets.first) {
  109. it.node = *m_buckets.first;
  110. } else {
  111. it.node = nullptr;
  112. }
  113. return it;
  114. }
  115. template<typename Key, typename Alloc>
  116. inline typename unordered_set<Key, Alloc>::iterator unordered_set<Key, Alloc>::end() const {
  117. iterator cit;
  118. cit.node = nullptr;
  119. return cit;
  120. }
  121. template<typename Key, typename Alloc>
  122. inline bool unordered_set<Key, Alloc>::empty() const {
  123. return m_size == 0;
  124. }
  125. template<typename Key, typename Alloc>
  126. inline size_t unordered_set<Key, Alloc>::size() const {
  127. return m_size;
  128. }
  129. template<typename Key, typename Alloc>
  130. inline void unordered_set<Key, Alloc>::clear() {
  131. if (m_buckets.first) {
  132. pointer it = *m_buckets.first;
  133. while (it) {
  134. const pointer next = it->next;
  135. it->~unordered_hash_node<Key, void>();
  136. Alloc::static_deallocate(it, sizeof(unordered_hash_node<Key, void>));
  137. it = next;
  138. }
  139. }
  140. m_buckets.last = m_buckets.first;
  141. buffer_resize<pointer, Alloc>(&m_buckets, 9, 0);
  142. m_size = 0;
  143. }
  144. template<typename Key, typename Alloc>
  145. inline typename unordered_set<Key, Alloc>::iterator unordered_set<Key, Alloc>::find(const Key& key) const {
  146. iterator result;
  147. result.node = unordered_hash_find(key, m_buckets.first, (size_t)(m_buckets.last - m_buckets.first));
  148. return result;
  149. }
  150. template<typename Key, typename Alloc>
  151. inline void unordered_set<Key, Alloc>::rehash(size_t nbuckets) {
  152. if (!m_buckets.first) return;
  153. if (m_size + 1 > 4 * nbuckets) {
  154. pointer root = *m_buckets.first;
  155. const size_t newnbuckets = ((size_t)(m_buckets.last - m_buckets.first) - 1) * 8;
  156. m_buckets.last = m_buckets.first;
  157. buffer_resize<pointer, Alloc>(&m_buckets, newnbuckets + 1, 0);
  158. unordered_hash_node<Key, void>** buckets = m_buckets.first;
  159. while (root) {
  160. const pointer next = root->next;
  161. root->next = root->prev = nullptr;
  162. unordered_hash_node_insert(root, hash(root->first), buckets, newnbuckets);
  163. root = next;
  164. }
  165. }
  166. }
  167. template<typename Key, typename Alloc>
  168. inline pair<typename unordered_set<Key, Alloc>::iterator, bool> unordered_set<Key, Alloc>::insert(const Key& key) {
  169. pair<iterator, bool> result;
  170. result.second = false;
  171. result.first = find(key);
  172. if (result.first.node != nullptr)
  173. return result;
  174. unordered_hash_node<Key, void>* newnode = new(placeholder(), Alloc::static_allocate(sizeof(unordered_hash_node<Key, void>))) unordered_hash_node<Key, void>(key);
  175. newnode->next = newnode->prev = nullptr;
  176. if(!m_buckets.first) buffer_resize<pointer, Alloc>(&m_buckets, 9, 0);
  177. const size_t nbuckets = (size_t)(m_buckets.last - m_buckets.first);
  178. unordered_hash_node_insert(newnode, hash(key), m_buckets.first, nbuckets - 1);
  179. ++m_size;
  180. rehash(nbuckets);
  181. result.first.node = newnode;
  182. result.second = true;
  183. return result;
  184. }
  185. template<typename Key, typename Alloc>
  186. inline pair<typename unordered_set<Key, Alloc>::iterator, bool> unordered_set<Key, Alloc>::emplace(Key&& key) {
  187. pair<iterator, bool> result;
  188. result.second = false;
  189. result.first = find(key);
  190. if (result.first.node != nullptr)
  191. return result;
  192. const size_t keyhash = hash(key);
  193. unordered_hash_node<Key, void>* newnode = new(placeholder(), Alloc::static_allocate(sizeof(unordered_hash_node<Key, void>))) unordered_hash_node<Key, void>(static_cast<Key&&>(key));
  194. newnode->next = newnode->prev = nullptr;
  195. if(!m_buckets.first) buffer_resize<pointer, Alloc>(&m_buckets, 9, 0);
  196. const size_t nbuckets = (size_t)(m_buckets.last - m_buckets.first);
  197. unordered_hash_node_insert(newnode, keyhash, m_buckets.first, nbuckets - 1);
  198. ++m_size;
  199. rehash(nbuckets);
  200. result.first.node = newnode;
  201. result.second = true;
  202. return result;
  203. }
  204. template<typename Key, typename Alloc>
  205. inline void unordered_set<Key, Alloc>::erase(iterator where) {
  206. unordered_hash_node_erase(where.node, hash(where.node->first), m_buckets.first, (size_t)(m_buckets.last - m_buckets.first) - 1);
  207. where.node->~unordered_hash_node<Key, void>();
  208. Alloc::static_deallocate((void*)where.node, sizeof(unordered_hash_node<Key, void>));
  209. --m_size;
  210. }
  211. template<typename Key, typename Alloc>
  212. inline size_t unordered_set<Key, Alloc>::erase(const Key& key) {
  213. const iterator it = find(key);
  214. if (it.node == nullptr)
  215. return 0;
  216. erase(it);
  217. return 1;
  218. }
  219. template <typename Key, typename Alloc>
  220. void unordered_set<Key, Alloc>::swap(unordered_set& other) {
  221. size_t tsize = other.m_size;
  222. other.m_size = m_size, m_size = tsize;
  223. buffer_swap(&m_buckets, &other.m_buckets);
  224. }
  225. }
  226. #endif