vtv_map.h 9.9 KB

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