vtv_set.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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 it
  4. 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, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  9. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  10. 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
  15. and a copy of the GCC Runtime Library Exception along with this
  16. program; see the files COPYING3 and COPYING.RUNTIME respectively.
  17. If not, see <http://www.gnu.org/licenses/>. */
  18. #ifndef _VTV_SET_H
  19. #define _VTV_SET_H 1
  20. /* Code in this file manages a collection of insert-only sets. We
  21. have only tested the case where Key is uintptr_t, though it
  22. theoretically should work for some other cases. All odd keys are
  23. reserved, and must not be inserted into any of the sets. This code
  24. is intended primarily for sets of pointers, and the code is
  25. optimized for small sets (including size 0 and 1), but regardless
  26. of the set size, insert() and contains() have close to O(1) speed
  27. in practice.
  28. TODO(gpike): fix this comment.
  29. Recommended multithreaded use of a set:
  30. For speed, we want to use a lock-free test for set membership. The
  31. code handles simultaneous reads and inserts, as long as at most one
  32. insertion is in progress at a time. After an insert, other threads
  33. may not immediately "see" the inserted key if they perform a
  34. lock-free read, so we recommend retrying, as explained below.
  35. Also, to make data corruption less likely, we recommend using a
  36. "normal" RW page as well as one or pages that are typically RO
  37. but that can be switched to RW and back as needed. The latter
  38. pages should contain sets. The former should contain a lock, L,
  39. and an int or similar, num_writers. Then, to insert, something
  40. like this would be safe:
  41. o Acquire L.
  42. o Increment num_writers; if that made it 1, change pages to RW.
  43. o Release L.
  44. o while (there are insertions to do in some set, S) {
  45. acquire L;
  46. do some insertions in S;
  47. release L;
  48. }
  49. o Acquire L.
  50. o Decrement num_writers; if that made it 0, change pages to RO.
  51. o Release L.
  52. And to check if the set contains some key, one could use
  53. set.contains(key) ||
  54. ({ Acquire L; bool b = set.contains(key); Release L; b; })
  55. In this scheme, the number of threads with reads in progress isn't
  56. tracked, so old sets can never be deleted. In addition, on some
  57. architectures the intentionally racy reads might cause contains()
  58. to return true when it should have returned false. This should be
  59. no problem on x86, and most other machines, where reading or
  60. writing an aligned uintptr_t is atomic. E.g., on those machines,
  61. if *p is 0 and one thread does *p = x while another reads *p, the
  62. read will see either 0 or x.
  63. To make the above easier, the insert_only_hash_sets class provides
  64. an interface to manipulate any number of hash sets. One shouldn't
  65. create objects of that class, as it has no member data and its
  66. methods are static.
  67. So the recommended model is to have a single lock, a single
  68. num_writers variable, and some number of sets. If lock contention
  69. becomes a problem then the sets can be divided into k groups, each
  70. of which has a lock and a num_writers variable; or each set can be
  71. represented as a set of values that equal 0 mod m, a set of values
  72. that equal 1 mod m, ..., plus a set of values that equal m-1 mod m.
  73. However, we expect most or all uses of this code to call contains()
  74. much more frequently than anything else, so lock contention is
  75. likely to be low. */
  76. #include <algorithm>
  77. #ifndef HASHTABLE_STATS
  78. #define HASHTABLE_STATS 0
  79. #endif
  80. #ifndef HASHTABLE_STATS_ATOMIC
  81. #define HASHTABLE_STATS_ATOMIC 0
  82. #endif
  83. #if HASHTABLE_STATS
  84. #if HASHTABLE_STATS_ATOMIC
  85. /* Stat counters, with atomics. */
  86. #include <bits/atomic_word.h>
  87. typedef _Atomic_word _AtomicStatCounter;
  88. void
  89. inc_by (_AtomicStatCounter &stat, int amount)
  90. {
  91. __atomic_add_fetch (&stat, amount, __ATOMIC_ACQ_REL);
  92. }
  93. #else
  94. /* Stat counters, but without atomics. */
  95. typedef int _AtomicStatCounter;
  96. void
  97. inc_by (_AtomicStatCounter& stat, int amount)
  98. {
  99. stat += amount;
  100. }
  101. #endif
  102. /* Number of calls to contains(), insert(), etc. */
  103. extern _AtomicStatCounter stat_insert;
  104. extern _AtomicStatCounter stat_contains;
  105. extern _AtomicStatCounter stat_resize;
  106. extern _AtomicStatCounter stat_create;
  107. /* Sum of set size over all calls to contains(). */
  108. extern _AtomicStatCounter stat_contains_sizes;
  109. /* contains() calls in a set whose capacity is more than 1. */
  110. extern _AtomicStatCounter stat_contains_in_non_trivial_set;
  111. /* Probes in a set whose capacity is more than 1. Ideally, this will
  112. be pretty close to stat_contains_in_non_trivial_set. That will
  113. happen if our hash function is good and/or important keys were
  114. inserted before unimportant keys. */
  115. extern _AtomicStatCounter stat_probes_in_non_trivial_set;
  116. /* number of calls to contains() with size=0, 1, etc. */
  117. extern _AtomicStatCounter stat_contains_size0;
  118. extern _AtomicStatCounter stat_contains_size1;
  119. extern _AtomicStatCounter stat_contains_size2;
  120. extern _AtomicStatCounter stat_contains_size3;
  121. extern _AtomicStatCounter stat_contains_size4;
  122. extern _AtomicStatCounter stat_contains_size5;
  123. extern _AtomicStatCounter stat_contains_size6;
  124. extern _AtomicStatCounter stat_contains_size7;
  125. extern _AtomicStatCounter stat_contains_size8;
  126. extern _AtomicStatCounter stat_contains_size9;
  127. extern _AtomicStatCounter stat_contains_size10;
  128. extern _AtomicStatCounter stat_contains_size11;
  129. extern _AtomicStatCounter stat_contains_size12;
  130. extern _AtomicStatCounter stat_contains_size13_or_more;
  131. extern _AtomicStatCounter stat_grow_from_size0_to_1;
  132. extern _AtomicStatCounter stat_grow_from_size1_to_2;
  133. extern _AtomicStatCounter stat_double_the_number_of_buckets;
  134. extern _AtomicStatCounter stat_insert_key_that_was_already_present;
  135. /* Hash collisions detected during insert_no_resize(). Only counts
  136. hasher(k) == hasher(k'); hasher(k) % tablesize == hasher(k') %
  137. tablesize is not sufficient. Will count collisions that are
  138. detected during table resizes etc., so the same two keys may add to
  139. this stat multiple times. */
  140. extern _AtomicStatCounter stat_insert_found_hash_collision;
  141. #include <string>
  142. struct insert_only_hash_sets_logger
  143. {
  144. static char *
  145. log (char c, char *buf)
  146. {
  147. *buf++ = c;
  148. return buf;
  149. }
  150. static char *
  151. log (const char *s, char *buf)
  152. { return strcpy (buf, s) + strlen (s); }
  153. static char *
  154. log (_AtomicStatCounter i, char *buf)
  155. {
  156. if (i < 10)
  157. return log ((char) ('0' + i), buf);
  158. else
  159. return log ((char) ('0' + i % 10), log (i / 10, buf));
  160. }
  161. static char *
  162. log (const char *label, _AtomicStatCounter i, char *buf)
  163. {
  164. buf = log (label, buf);
  165. buf = log (": ", buf);
  166. buf = log (i, buf);
  167. return log ('\n', buf);
  168. }
  169. };
  170. // Write stats to the given buffer, which should be at least 4000 bytes.
  171. static inline void
  172. insert_only_hash_tables_stats (char *buf)
  173. {
  174. buf = insert_only_hash_sets_logger::log ("insert", stat_insert, buf);
  175. buf = insert_only_hash_sets_logger::log ("contains", stat_contains, buf);
  176. buf = insert_only_hash_sets_logger::log ("resize", stat_resize, buf);
  177. buf = insert_only_hash_sets_logger::log ("create", stat_create, buf);
  178. buf = insert_only_hash_sets_logger::log ("insert_key_that_was_already_"
  179. "present",
  180. stat_insert_key_that_was_already_present,
  181. buf);
  182. buf = insert_only_hash_sets_logger::log ("contains_sizes",
  183. stat_contains_sizes, buf);
  184. buf = insert_only_hash_sets_logger::log ("contains_in_non_trivial_set",
  185. stat_contains_in_non_trivial_set,
  186. buf);
  187. buf = insert_only_hash_sets_logger::log ("probes_in_non_trivial_set",
  188. stat_probes_in_non_trivial_set,
  189. buf);
  190. buf = insert_only_hash_sets_logger::log ("contains_size0",
  191. stat_contains_size0, buf);
  192. buf = insert_only_hash_sets_logger::log ("contains_size1",
  193. stat_contains_size1, buf);
  194. buf = insert_only_hash_sets_logger::log ("contains_size2",
  195. stat_contains_size2, buf);
  196. buf = insert_only_hash_sets_logger::log ("contains_size3",
  197. stat_contains_size3, buf);
  198. buf = insert_only_hash_sets_logger::log ("contains_size4",
  199. stat_contains_size4, buf);
  200. buf = insert_only_hash_sets_logger::log ("contains_size5",
  201. stat_contains_size5, buf);
  202. buf = insert_only_hash_sets_logger::log ("contains_size6",
  203. stat_contains_size6, buf);
  204. buf = insert_only_hash_sets_logger::log ("contains_size7",
  205. stat_contains_size7, buf);
  206. buf = insert_only_hash_sets_logger::log ("contains_size8",
  207. stat_contains_size8, buf);
  208. buf = insert_only_hash_sets_logger::log ("contains_size9",
  209. stat_contains_size9, buf);
  210. buf = insert_only_hash_sets_logger::log ("contains_size10",
  211. stat_contains_size10, buf);
  212. buf = insert_only_hash_sets_logger::log ("contains_size11",
  213. stat_contains_size11, buf);
  214. buf = insert_only_hash_sets_logger::log ("contains_size12",
  215. stat_contains_size12, buf);
  216. buf = insert_only_hash_sets_logger::log ("contains_size13_or_more",
  217. stat_contains_size13_or_more, buf);
  218. buf = insert_only_hash_sets_logger::log ("grow_from_size0_to_1",
  219. stat_grow_from_size0_to_1, buf);
  220. buf = insert_only_hash_sets_logger::log ("grow_from_size1_to_2",
  221. stat_grow_from_size1_to_2, buf);
  222. buf = insert_only_hash_sets_logger::log ("insert_found_hash_collision",
  223. stat_insert_found_hash_collision,
  224. buf);
  225. buf = insert_only_hash_sets_logger::log ("double_the_number_of_buckets",
  226. stat_double_the_number_of_buckets,
  227. buf);
  228. *buf = '\0';
  229. }
  230. #else
  231. /* No stats. */
  232. #define inc_by(statname, amount) do { } while (false && (amount))
  233. #endif
  234. #define inc(statname) inc_by (statname, 1)
  235. template <typename Key, class HashFcn, class Alloc>
  236. class insert_only_hash_sets
  237. {
  238. public:
  239. typedef Key key_type;
  240. typedef size_t size_type;
  241. typedef Alloc alloc_type;
  242. enum { illegal_key = 1 };
  243. enum { min_capacity = 4 };
  244. #if HASHTABLE_STATS
  245. enum { stats = true };
  246. #else
  247. enum { stats = false };
  248. #endif
  249. /* Do not directly use insert_only_hash_set. Instead, use the
  250. static methods below to create and manipulate objects of the
  251. following class.
  252. Implementation details: each set is represented by a pointer
  253. plus, perhaps, out-of-line data, which would be an object of type
  254. insert_only_hash_set. For a pointer, s, the interpretation is: s
  255. == NULL means empty set, lsb(s) == 1 means a set with one
  256. element, which is (uintptr_t)s - 1, and otherwise s is a pointer
  257. of type insert_only_hash_set*. So, to increase the size of a set
  258. we have to change s and/or *s. To check if a set contains some
  259. key we have to examine s and possibly *s. */
  260. class insert_only_hash_set
  261. {
  262. public:
  263. /* Insert a key. The key must not be a reserved key. */
  264. static inline insert_only_hash_set *insert (key_type key,
  265. insert_only_hash_set *s);
  266. /* Create an empty set. */
  267. static inline insert_only_hash_set *create (size_type capacity);
  268. /* Return whether the given key is present. If key is illegal_key
  269. then either true or false may be returned, but for all other
  270. reserved keys false will be returned. */
  271. static bool
  272. contains (key_type key, const insert_only_hash_set *s)
  273. {
  274. if (stats)
  275. {
  276. inc (stat_contains);
  277. switch (size (s))
  278. {
  279. case 0: inc (stat_contains_size0); break;
  280. case 1: inc (stat_contains_size1); break;
  281. case 2: inc (stat_contains_size2); break;
  282. case 3: inc (stat_contains_size3); break;
  283. case 4: inc (stat_contains_size4); break;
  284. case 5: inc (stat_contains_size5); break;
  285. case 6: inc (stat_contains_size6); break;
  286. case 7: inc (stat_contains_size7); break;
  287. case 8: inc (stat_contains_size8); break;
  288. case 9: inc (stat_contains_size9); break;
  289. case 10: inc (stat_contains_size10); break;
  290. case 11: inc (stat_contains_size11); break;
  291. case 12: inc (stat_contains_size12); break;
  292. default: inc (stat_contains_size13_or_more); break;
  293. }
  294. inc_by (stat_contains_sizes, size (s));
  295. }
  296. return (singleton (s) ?
  297. singleton_key (key) == s :
  298. ((s != NULL) && s->contains (key)));
  299. }
  300. /* Return a set's size. */
  301. static size_type
  302. size (const insert_only_hash_set *s)
  303. { return (s == NULL) ? 0 : (singleton (s) ? 1 : s->num_entries); }
  304. static inline insert_only_hash_set *resize (size_type target_num_buckets,
  305. insert_only_hash_set *s);
  306. private:
  307. /* Return whether a set has size 1. */
  308. static bool
  309. singleton (const insert_only_hash_set *s)
  310. { return (uintptr_t) s & 1; }
  311. /* Return the representation of a singleton set containing the
  312. given key. */
  313. static insert_only_hash_set *
  314. singleton_key (key_type key)
  315. { return (insert_only_hash_set *) ((uintptr_t) key + 1); }
  316. /* Given a singleton set, what key does it contain? */
  317. static key_type
  318. extract_singleton_key (const insert_only_hash_set *s)
  319. {
  320. VTV_DEBUG_ASSERT (singleton (s));
  321. return (key_type) ((uintptr_t) s - 1);
  322. }
  323. volatile key_type &
  324. key_at_index (size_type index)
  325. { return buckets[index]; }
  326. key_type
  327. key_at_index (size_type index) const
  328. { return buckets[index]; }
  329. size_type
  330. next_index (size_type index, size_type indices_examined) const
  331. { return (index + indices_examined) & (num_buckets - 1); }
  332. inline void insert_no_resize (key_type key);
  333. inline bool contains (key_type key) const;
  334. inline insert_only_hash_set *resize_if_necessary (void);
  335. size_type num_buckets; /* Must be a power of 2 not less than
  336. min_capacity. */
  337. volatile size_type num_entries;
  338. volatile key_type buckets[0]; /* Actual array size is num_buckets. */
  339. };
  340. /* Create an empty set with the given capacity. Requires that n be
  341. 0 or a power of 2. If 1 < n < min_capacity then treat n as
  342. min_capacity. Sets *handle. Returns true unless the allocator
  343. fails. Subsequent operations on this set should use the same
  344. handle. */
  345. static inline bool create (size_type n, insert_only_hash_set **handle);
  346. /* Force the capacity of a set to be n, unless it was more than n
  347. already. Requires that n be 0 or a power of 2. Sets *handle
  348. unless the current capacity is n or more. Returns true unless
  349. the allocator fails. */
  350. static inline bool resize (size_type n, insert_only_hash_set **handle);
  351. /* Insert a key. *handle is unmodified unless (1) a resize occurs,
  352. or (2) the set was initially empty. Returns true unless the
  353. allocator fails during a resize. If the allocator fails during a
  354. resize then the set is reset to be the empty set. The key must
  355. not be a reserved key. */
  356. static inline bool insert (key_type key, insert_only_hash_set **handle);
  357. /* Check for the presence of a key. If key is illegal_key then
  358. either true or false may be returned, but for all other reserved
  359. keys false will be returned. */
  360. static inline bool
  361. contains (key_type key, /* const */ insert_only_hash_set **handle)
  362. { return insert_only_hash_set::contains (key, *handle); }
  363. /* Return the size of the given set. */
  364. static size_type
  365. size (const insert_only_hash_set **handle)
  366. { return insert_only_hash_set::size (*handle); }
  367. static bool
  368. is_reserved_key (key_type key)
  369. { return ((uintptr_t) key % 2) == 1; }
  370. };
  371. template <typename Key, class HashFcn, class Alloc>
  372. typename insert_only_hash_sets <Key, HashFcn, Alloc>::insert_only_hash_set *
  373. insert_only_hash_sets <Key, HashFcn, Alloc>::insert_only_hash_set::resize
  374. (size_type n, insert_only_hash_set *s)
  375. {
  376. if (s == NULL)
  377. return create (n);
  378. size_type capacity = singleton (s) ? 1 : s->num_buckets;
  379. if (n <= capacity)
  380. return s;
  381. insert_only_hash_set *result =
  382. create (std::max<size_type> (n, min_capacity));
  383. if (result != NULL)
  384. {
  385. if (singleton (s))
  386. {
  387. result->insert_no_resize (extract_singleton_key (s));
  388. }
  389. else
  390. {
  391. for (size_type i = 0; i < s->num_buckets; i++)
  392. if (s->buckets[i] != (key_type) illegal_key)
  393. result->insert_no_resize (s->buckets[i]);
  394. }
  395. VTV_DEBUG_ASSERT (size (result) == size (s));
  396. }
  397. return result;
  398. }
  399. template <typename Key, class HashFcn, class Alloc>
  400. typename insert_only_hash_sets <Key, HashFcn, Alloc>::insert_only_hash_set *
  401. insert_only_hash_sets <Key, HashFcn, Alloc>::insert_only_hash_set::insert
  402. (key_type key, insert_only_hash_set *s)
  403. {
  404. VTV_DEBUG_ASSERT (!is_reserved_key (key));
  405. inc_by (stat_grow_from_size0_to_1, s == NULL);
  406. if (s == NULL)
  407. return singleton_key (key);
  408. if (singleton (s))
  409. {
  410. const key_type old_key = extract_singleton_key (s);
  411. if (old_key == key)
  412. return s;
  413. /* Grow from size 1 to size 2. */
  414. inc (stat_grow_from_size1_to_2);
  415. s = create (2);
  416. if (s == NULL)
  417. return NULL;
  418. s->insert_no_resize (old_key);
  419. s->insert_no_resize (key);
  420. VTV_DEBUG_ASSERT (size (s) == 2);
  421. return s;
  422. }
  423. s = s->resize_if_necessary();
  424. if (s != NULL)
  425. s->insert_no_resize (key);
  426. return s;
  427. }
  428. template <typename Key, class HashFcn, class Alloc>
  429. typename insert_only_hash_sets <Key, HashFcn, Alloc>::insert_only_hash_set *
  430. insert_only_hash_sets <Key, HashFcn, Alloc>::insert_only_hash_set::create
  431. (size_type capacity)
  432. {
  433. if (capacity <= 1)
  434. return NULL;
  435. VTV_DEBUG_ASSERT (capacity > 1 && (capacity & (capacity - 1)) == 0);
  436. VTV_DEBUG_ASSERT (sizeof (insert_only_hash_set) == 2 * sizeof (size_type));
  437. capacity = std::max <size_type> (capacity, min_capacity);
  438. const size_t num_bytes = sizeof (insert_only_hash_set) +
  439. sizeof (key_type) * capacity;
  440. alloc_type alloc;
  441. insert_only_hash_set *result = (insert_only_hash_set *) alloc (num_bytes);
  442. result->num_buckets = capacity;
  443. result->num_entries = 0;
  444. for (size_type i = 0; i < capacity; i++)
  445. result->buckets[i] = (key_type) illegal_key;
  446. return result;
  447. }
  448. template <typename Key, class HashFcn, class Alloc>
  449. void
  450. insert_only_hash_sets<Key, HashFcn,
  451. Alloc>::insert_only_hash_set::insert_no_resize
  452. (key_type key)
  453. {
  454. HashFcn hasher;
  455. const size_type capacity = num_buckets;
  456. VTV_DEBUG_ASSERT (capacity >= min_capacity);
  457. VTV_DEBUG_ASSERT (!is_reserved_key (key));
  458. size_type index = hasher (key) & (capacity - 1);
  459. key_type k = key_at_index (index);
  460. size_type indices_examined = 0;
  461. while (k != key)
  462. {
  463. ++indices_examined;
  464. if (k == (key_type) illegal_key)
  465. {
  466. key_at_index (index) = key;
  467. ++num_entries;
  468. return;
  469. }
  470. else
  471. {
  472. inc_by (stat_insert_found_hash_collision,
  473. hasher (k) == hasher (key));
  474. }
  475. VTV_DEBUG_ASSERT (indices_examined < capacity);
  476. index = next_index (index, indices_examined);
  477. k = key_at_index (index);
  478. }
  479. }
  480. template<typename Key, class HashFcn, class Alloc>
  481. bool
  482. insert_only_hash_sets<Key, HashFcn, Alloc>::insert_only_hash_set::contains
  483. (key_type key) const
  484. {
  485. inc (stat_contains_in_non_trivial_set);
  486. HashFcn hasher;
  487. const size_type capacity = num_buckets;
  488. size_type index = hasher (key) & (capacity - 1);
  489. key_type k = key_at_index (index);
  490. size_type indices_examined = 0;
  491. inc (stat_probes_in_non_trivial_set);
  492. while (k != key)
  493. {
  494. ++indices_examined;
  495. if (/*UNLIKELY*/(k == (key_type) illegal_key
  496. || indices_examined == capacity))
  497. return false;
  498. index = next_index (index, indices_examined);
  499. k = key_at_index (index);
  500. inc (stat_probes_in_non_trivial_set);
  501. }
  502. return true;
  503. }
  504. template <typename Key, class HashFcn, class Alloc>
  505. typename insert_only_hash_sets <Key, HashFcn, Alloc>::insert_only_hash_set *
  506. insert_only_hash_sets<Key, HashFcn,
  507. Alloc>::insert_only_hash_set::resize_if_necessary (void)
  508. {
  509. VTV_DEBUG_ASSERT (num_buckets >= min_capacity);
  510. size_type unused = num_buckets - num_entries;
  511. if (unused < (num_buckets >> 2))
  512. {
  513. inc (stat_double_the_number_of_buckets);
  514. size_type new_num_buckets = num_buckets * 2;
  515. insert_only_hash_set *s = create (new_num_buckets);
  516. for (size_type i = 0; i < num_buckets; i++)
  517. if (buckets[i] != (key_type) illegal_key)
  518. s->insert_no_resize (buckets[i]);
  519. VTV_DEBUG_ASSERT (size (this) == size (s));
  520. return s;
  521. }
  522. else
  523. return this;
  524. }
  525. template<typename Key, class HashFcn, class Alloc>
  526. bool
  527. insert_only_hash_sets<Key, HashFcn, Alloc>::create (size_type n,
  528. insert_only_hash_set **handle)
  529. {
  530. inc (stat_create);
  531. *handle = insert_only_hash_set::create (n);
  532. return (n <= 1) || (*handle != NULL);
  533. }
  534. template<typename Key, class HashFcn, class Alloc>
  535. bool
  536. insert_only_hash_sets<Key, HashFcn, Alloc>::resize (size_type n,
  537. insert_only_hash_set **handle)
  538. {
  539. inc (stat_resize);
  540. *handle = insert_only_hash_set::resize (n, *handle);
  541. return (n <= 1) || (*handle != NULL);
  542. }
  543. template<typename Key, class HashFcn, class Alloc>
  544. bool
  545. insert_only_hash_sets<Key, HashFcn, Alloc>::insert (key_type key,
  546. insert_only_hash_set **handle)
  547. {
  548. inc (stat_insert);
  549. const size_type old_size = insert_only_hash_set::size (*handle);
  550. *handle = insert_only_hash_set::insert (key, *handle);
  551. if (*handle != NULL)
  552. {
  553. const size_type delta = insert_only_hash_set::size (*handle) - old_size;
  554. inc_by (stat_insert_key_that_was_already_present, delta == 0);
  555. }
  556. return *handle != NULL;
  557. }
  558. #endif /* VTV_SET_H */