unordered_map.h 8.1 KB

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