unordered_set.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*-
  2. * Copyright 2012 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 "buffer.h"
  29. #include "hash.h"
  30. #include "hash_base.h"
  31. namespace tinystl {
  32. template<typename Key, typename Alloc = TINYSTL_ALLOCATOR>
  33. class unordered_set {
  34. public:
  35. unordered_set();
  36. unordered_set(const unordered_set& other);
  37. ~unordered_set();
  38. unordered_set& operator=(const unordered_set& other);
  39. typedef unordered_hash_iterator<const unordered_hash_node<Key, void> > const_iterator;
  40. typedef const_iterator iterator;
  41. iterator begin() const;
  42. iterator end() const;
  43. void clear();
  44. bool empty() const;
  45. size_t size() const;
  46. iterator find(const Key& key) const;
  47. pair<iterator, bool> insert(const Key& key);
  48. void erase(iterator where);
  49. size_t erase(const Key& key);
  50. void swap(unordered_set& other);
  51. private:
  52. typedef unordered_hash_node<Key, void>* pointer;
  53. size_t m_size;
  54. buffer<pointer, Alloc> m_buckets;
  55. };
  56. template<typename Key, typename Alloc>
  57. unordered_set<Key, Alloc>::unordered_set()
  58. : m_size(0)
  59. {
  60. buffer_init<pointer, Alloc>(&m_buckets);
  61. buffer_resize<pointer, Alloc>(&m_buckets, 9, 0);
  62. }
  63. template<typename Key, typename Alloc>
  64. unordered_set<Key, Alloc>::unordered_set(const unordered_set& other)
  65. : m_size(other.m_size)
  66. {
  67. const size_t nbuckets = (size_t)(other.m_buckets.last - other.m_buckets.first);
  68. buffer_init<pointer, Alloc>(&m_buckets);
  69. buffer_resize<pointer, Alloc>(&m_buckets, nbuckets, 0);
  70. for (pointer it = *other.m_buckets.first; it; it = it->next) {
  71. unordered_hash_node<Key, void>* newnode = new(placeholder(), Alloc::static_allocate(sizeof(unordered_hash_node<Key, void>))) unordered_hash_node<Key, void>(*it);
  72. newnode->next = newnode->prev = 0;
  73. unordered_hash_node_insert(newnode, hash(it->first), m_buckets.first, nbuckets - 1);
  74. }
  75. }
  76. template<typename Key, typename Alloc>
  77. unordered_set<Key, Alloc>::~unordered_set() {
  78. clear();
  79. buffer_destroy<pointer, Alloc>(&m_buckets);
  80. }
  81. template<typename Key, typename Alloc>
  82. unordered_set<Key, Alloc>& unordered_set<Key, Alloc>::operator=(const unordered_set<Key, Alloc>& other) {
  83. unordered_set<Key, Alloc>(other).swap(*this);
  84. return *this;
  85. }
  86. template<typename Key, typename Alloc>
  87. inline typename unordered_set<Key, Alloc>::iterator unordered_set<Key, Alloc>::begin() const {
  88. iterator cit;
  89. cit.node = *m_buckets.first;
  90. return cit;
  91. }
  92. template<typename Key, typename Alloc>
  93. inline typename unordered_set<Key, Alloc>::iterator unordered_set<Key, Alloc>::end() const {
  94. iterator cit;
  95. cit.node = 0;
  96. return cit;
  97. }
  98. template<typename Key, typename Alloc>
  99. inline bool unordered_set<Key, Alloc>::empty() const {
  100. return m_size == 0;
  101. }
  102. template<typename Key, typename Alloc>
  103. inline size_t unordered_set<Key, Alloc>::size() const {
  104. return m_size;
  105. }
  106. template<typename Key, typename Alloc>
  107. inline void unordered_set<Key, Alloc>::clear() {
  108. pointer it = *m_buckets.first;
  109. while (it) {
  110. const pointer next = it->next;
  111. it->~unordered_hash_node<Key, void>();
  112. Alloc::static_deallocate(it, sizeof(unordered_hash_node<Key, void>));
  113. it = next;
  114. }
  115. m_buckets.last = m_buckets.first;
  116. buffer_resize<pointer, Alloc>(&m_buckets, 9, 0);
  117. m_size = 0;
  118. }
  119. template<typename Key, typename Alloc>
  120. inline typename unordered_set<Key, Alloc>::iterator unordered_set<Key, Alloc>::find(const Key& key) const {
  121. iterator result;
  122. result.node = unordered_hash_find(key, m_buckets.first, (size_t)(m_buckets.last - m_buckets.first));
  123. return result;
  124. }
  125. template<typename Key, typename Alloc>
  126. inline pair<typename unordered_set<Key, Alloc>::iterator, bool> unordered_set<Key, Alloc>::insert(const Key& key) {
  127. pair<iterator, bool> result;
  128. result.second = false;
  129. result.first = find(key);
  130. if (result.first.node != 0)
  131. return result;
  132. unordered_hash_node<Key, void>* newnode = new(placeholder(), Alloc::static_allocate(sizeof(unordered_hash_node<Key, void>))) unordered_hash_node<Key, void>(key);
  133. newnode->next = newnode->prev = 0;
  134. const size_t nbuckets = (size_t)(m_buckets.last - m_buckets.first);
  135. unordered_hash_node_insert(newnode, hash(key), m_buckets.first, nbuckets - 1);
  136. ++m_size;
  137. if (m_size + 1 > 4 * nbuckets) {
  138. pointer root = *m_buckets.first;
  139. const size_t newnbuckets = ((size_t)(m_buckets.last - m_buckets.first) - 1) * 8;
  140. m_buckets.last = m_buckets.first;
  141. buffer_resize<pointer, Alloc>(&m_buckets, newnbuckets + 1, 0);
  142. unordered_hash_node<Key, void>** buckets = m_buckets.first;
  143. while (root) {
  144. const pointer next = root->next;
  145. root->next = root->prev = 0;
  146. unordered_hash_node_insert(root, hash(root->first), buckets, newnbuckets);
  147. root = next;
  148. }
  149. }
  150. result.first.node = newnode;
  151. result.second = true;
  152. return result;
  153. }
  154. template<typename Key, typename Alloc>
  155. inline void unordered_set<Key, Alloc>::erase(iterator where) {
  156. unordered_hash_node_erase(where.node, hash(where.node->first), m_buckets.first, (size_t)(m_buckets.last - m_buckets.first) - 1);
  157. where.node->~unordered_hash_node<Key, void>();
  158. Alloc::static_deallocate((void*)where.node, sizeof(unordered_hash_node<Key, void>));
  159. --m_size;
  160. }
  161. template<typename Key, typename Alloc>
  162. inline size_t unordered_set<Key, Alloc>::erase(const Key& key) {
  163. const iterator it = find(key);
  164. if (it.node == 0)
  165. return 0;
  166. erase(it);
  167. return 1;
  168. }
  169. template <typename Key, typename Alloc>
  170. void unordered_set<Key, Alloc>::swap(unordered_set& other) {
  171. size_t tsize = other.m_size;
  172. other.m_size = m_size, m_size = tsize;
  173. buffer_swap(&m_buckets, &other.m_buckets);
  174. }
  175. }
  176. #endif