vtv_map.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* Copyright (C) 2012-2013
  2. Free Software Foundation
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GCC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #ifndef _VTV_MAP_H
  20. #define _VTV_MAP_H 1
  21. #include <string.h>
  22. #ifdef __MINGW32__
  23. #include <stdint.h>
  24. #include "vtv_utils.h"
  25. #else
  26. #include <vtv_utils.h>
  27. #endif
  28. inline uint64_t
  29. load8bytes (const void *p)
  30. {
  31. uint64_t result;
  32. memcpy (&result, p, 8);
  33. return result;
  34. }
  35. /* Insert_only_hash_map maps keys to values. The implementation is a
  36. basic hash table with open addressing. The keys are not "owned" by
  37. the table; it only stores pointers to keys. The key type is
  38. specified below (see insert_only_hash_map::key_type) and is,
  39. roughly speaking, a string of any length with the string length and
  40. a hash code stored at the front. The code here does not compute
  41. any hash codes, but rather uses what's given. */
  42. template<typename T, typename Alloc>
  43. class insert_only_hash_map
  44. {
  45. public:
  46. typedef size_t size_type;
  47. typedef T value_type;
  48. typedef Alloc alloc_type;
  49. enum { min_capacity = 4 };
  50. #if HASHMAP_STATS
  51. enum { stats = true };
  52. #else
  53. enum { stats = false };
  54. #endif
  55. /* Keys are a byte string (up to 2^32 - 1 long) plus a uint32_t
  56. that's used as a hash code. The latter can encode arbitrary
  57. information at the client's discretion, so, e.g., multiple keys
  58. that are the same string still "differ" if the hash codes differ.
  59. Keys are equal if the first 8 bytes are equal and the next n
  60. bytes are equal. */
  61. struct key_type
  62. {
  63. uint32_t n;
  64. uint32_t hash;
  65. char bytes[0];
  66. bool
  67. equals (const key_type *k) const;
  68. };
  69. /* Create an empty map with a reasonable number of buckets for the
  70. expected size. Returns NULL if the allocator fails. */
  71. static insert_only_hash_map *
  72. create (size_type expected_size);
  73. /* The opposite of create(). Free the memory for the given map. */
  74. static void
  75. destroy (insert_only_hash_map *m)
  76. { Alloc().dealloc (m, m->size_in_bytes_); }
  77. /* Return a map identical to this except that *k is mapped to v.
  78. Typcially it's done by modifying this in place, but if a resize
  79. is necessary then this is deallocated and a new map is returned.
  80. Requires k to be non-NULL. Does nothing and returns NULL if the
  81. allocator fails. */
  82. insert_only_hash_map*
  83. put (const key_type *k, const value_type &v)
  84. { return this->put_internal (k, v, false); }
  85. /* If *k is a key in this then set *v to point to the corresponding
  86. value. Otherwise, do the equivalent of insert(k, value_type())
  87. and, if that succeeds, set *v to point to the inserted value.
  88. Requires k to be non-NULL. Does nothing and returns NULL if the
  89. allocator fails. Typically returns this, but will return a new
  90. insert_only_hash_map if a resize occurs. If the return value is
  91. non-NULL, *v is set and it's valid until a resize of the map that
  92. is the return value. */
  93. insert_only_hash_map *
  94. find_or_add_key (const key_type *k, value_type **v);
  95. /* Get the value corresponding to *k. Returns NULL if there is
  96. none. Requires k to be non-NULL. The return value is valid
  97. until any resize. */
  98. const value_type *get (const key_type *k) const;
  99. size_type
  100. size () const
  101. { return num_entries_; }
  102. bool
  103. empty () const
  104. { return this->size () == 0; }
  105. size_type
  106. bucket_count () const
  107. { return num_buckets_; }
  108. private:
  109. typedef std::pair <const key_type *, value_type> bucket_type;
  110. insert_only_hash_map *put_internal (const key_type *, const value_type &,
  111. bool);
  112. /* This function determines when to resize the table. */
  113. bool
  114. is_too_full (size_type entries) const
  115. { return entries > (this->bucket_count () * 0.7); }
  116. /* Return a copy with double the number of buckets. Returns NULL if
  117. the allocator fails. Otherwise, calls destroy (this). */
  118. insert_only_hash_map *destructive_copy ();
  119. /* Must be a power of 2 not less than min_capacity. */
  120. size_type num_buckets_;
  121. size_type num_entries_;
  122. size_type size_in_bytes_;
  123. bucket_type buckets[0]; /* Actual array size is num_buckets. */
  124. };
  125. template <typename T, typename Alloc>
  126. insert_only_hash_map <T, Alloc> *
  127. insert_only_hash_map <T, Alloc>::create (size_type expected_size)
  128. {
  129. size_t cap = min_capacity;
  130. while (expected_size >= cap)
  131. {
  132. cap *= 2;
  133. }
  134. size_t size_in_bytes = sizeof (insert_only_hash_map <T, Alloc>)
  135. + cap * sizeof (bucket_type);
  136. insert_only_hash_map <T, Alloc>* result =
  137. static_cast <insert_only_hash_map <T, Alloc>*> (Alloc ()
  138. .alloc (size_in_bytes));
  139. if (result != NULL)
  140. {
  141. result->size_in_bytes_ = size_in_bytes;
  142. result->num_buckets_ = cap;
  143. result->num_entries_ = 0;
  144. memset (result->buckets, 0, cap * sizeof (bucket_type));
  145. }
  146. return result;
  147. }
  148. template <typename T, typename Alloc>
  149. insert_only_hash_map <T, Alloc>*
  150. insert_only_hash_map <T, Alloc>::destructive_copy ()
  151. {
  152. insert_only_hash_map* copy = create (this->bucket_count ());
  153. if (copy == NULL)
  154. return NULL;
  155. VTV_DEBUG_ASSERT (copy->bucket_count () == 2 * this->bucket_count ());
  156. for (size_type i = 0; i < this->bucket_count (); i++)
  157. if (this->buckets[i].first != NULL)
  158. copy->put_internal (this->buckets[i].first, this->buckets[i].second,
  159. true);
  160. VTV_DEBUG_ASSERT (copy->size () == this->size ());
  161. destroy (this);
  162. return copy;
  163. }
  164. template <typename T, typename Alloc>
  165. insert_only_hash_map <T, Alloc>*
  166. insert_only_hash_map <T, Alloc>::find_or_add_key (const key_type *k,
  167. value_type **v)
  168. {
  169. /* Table size is always a power of 2. */
  170. const size_type mask = this->bucket_count () - 1;
  171. size_type bucket_index = k->hash & mask;
  172. size_type step = 1;
  173. for (;;)
  174. {
  175. bucket_type &bucket = this->buckets[bucket_index];
  176. if (bucket.first == NULL)
  177. {
  178. /* Key was not present. */
  179. if (this->is_too_full (this->size () + 1))
  180. {
  181. insert_only_hash_map <T, Alloc>* result =
  182. this->destructive_copy ();
  183. return result == NULL
  184. ? NULL
  185. : result->find_or_add_key (k, v);
  186. }
  187. else
  188. {
  189. bucket.first = k;
  190. bucket.second = T ();
  191. this->num_entries_++;
  192. *v = &bucket.second;
  193. return this;
  194. }
  195. }
  196. else if (bucket.first->equals (k))
  197. {
  198. /* Key was present. */
  199. *v = &bucket.second;
  200. return this;
  201. }
  202. else
  203. bucket_index = (bucket_index + step++) & mask;
  204. }
  205. }
  206. template <typename T, typename Alloc>
  207. insert_only_hash_map <T, Alloc>*
  208. insert_only_hash_map <T, Alloc>::put_internal (
  209. const insert_only_hash_map::key_type *k,
  210. const insert_only_hash_map::value_type &v,
  211. bool unique_key_and_resize_not_needed)
  212. {
  213. /* Table size is always a power of 2. */
  214. const size_type mask = this->bucket_count () - 1;
  215. size_type bucket_index = k->hash & mask;
  216. size_type step = 1;
  217. for (;;)
  218. {
  219. bucket_type &bucket = this->buckets[bucket_index];
  220. if (bucket.first == NULL)
  221. {
  222. /* Key was not present. */
  223. if (!unique_key_and_resize_not_needed
  224. && this->is_too_full (this->size () + 1))
  225. {
  226. insert_only_hash_map <T, Alloc>* result =
  227. this->destructive_copy ();
  228. return result == NULL
  229. ? NULL
  230. : result->put_internal (k, v, true);
  231. }
  232. else
  233. {
  234. bucket.first = k;
  235. bucket.second = v;
  236. this->num_entries_++;
  237. return this;
  238. }
  239. }
  240. else if (!unique_key_and_resize_not_needed && bucket.first->equals (k))
  241. {
  242. /* Key was present. Just change the value. */
  243. bucket.second = v;
  244. return this;
  245. }
  246. else
  247. bucket_index = (bucket_index + step++) & mask;
  248. }
  249. }
  250. template <typename T, typename Alloc>
  251. inline const typename insert_only_hash_map <T, Alloc>::value_type*
  252. insert_only_hash_map <T, Alloc>::get (const insert_only_hash_map::key_type *k)
  253. const
  254. {
  255. /* Table size is always a power of 2. */
  256. const size_type mask = this->bucket_count () - 1;
  257. size_type bucket_index = k->hash & mask;
  258. size_type step = 1;
  259. for (;;)
  260. {
  261. const bucket_type &bucket = this->buckets[bucket_index];
  262. if (bucket.first == NULL)
  263. return NULL;
  264. else if (bucket.first->equals (k))
  265. return &bucket.second;
  266. else
  267. bucket_index = (bucket_index + step++) & mask;
  268. }
  269. }
  270. template <typename T, typename Alloc>
  271. inline bool
  272. insert_only_hash_map <T, Alloc>::key_type::equals (
  273. const typename insert_only_hash_map <T, Alloc>::key_type *k) const
  274. {
  275. const char* x = reinterpret_cast <const char *> (k);
  276. const char* y = reinterpret_cast <const char *> (this);
  277. return (load8bytes (x) == load8bytes (y)
  278. && memcmp (x + 8, y + 8, this->n) == 0);
  279. }
  280. #endif /* _VTV_MAP_H */