unordered_map.h 8.1 KB

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