You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2923 lines
110 KiB

  1. // ----------------------------------------------------------------------
  2. // Copyright (c) 2016, Steven Gregory Popovitch - greg7mdp@gmail.com
  3. // All rights reserved.
  4. //
  5. // This work is derived from Google's sparsehash library
  6. // (see https://github.com/sparsehash/sparsehash) whose copyright appears
  7. // below this one.
  8. //
  9. // Redistribution and use in source and binary forms, with or without
  10. // modification, are permitted provided that the following conditions are
  11. // met:
  12. //
  13. // * Redistributions of source code must retain the above copyright
  14. // notice, this list of conditions and the following disclaimer.
  15. // * Redistributions in binary form must reproduce the above
  16. // copyright notice, this list of conditions and the following disclaimer
  17. // in the documentation and/or other materials provided with the
  18. // distribution.
  19. // * The name of Steven Gregory Popovitch may not be used to
  20. // endorse or promote products derived from this software without
  21. // specific prior written permission.
  22. //
  23. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. // ----------------------------------------------------------------------
  35. // ----------------------------------------------------------------------
  36. // Copyright (c) 2010, Google Inc.
  37. // All rights reserved.
  38. //
  39. // Redistribution and use in source and binary forms, with or without
  40. // modification, are permitted provided that the following conditions are
  41. // met:
  42. //
  43. // * Redistributions of source code must retain the above copyright
  44. // notice, this list of conditions and the following disclaimer.
  45. // * Redistributions in binary form must reproduce the above
  46. // copyright notice, this list of conditions and the following disclaimer
  47. // in the documentation and/or other materials provided with the
  48. // distribution.
  49. // * Neither the name of Google Inc. nor the names of its
  50. // contributors may be used to endorse or promote products derived from
  51. // this software without specific prior written permission.
  52. //
  53. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  54. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  55. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  56. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  57. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  58. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  59. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  60. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  61. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  62. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  63. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  64. // ----------------------------------------------------------------------
  65. #ifdef _MSC_VER
  66. #pragma warning( disable : 4820 ) // '6' bytes padding added after data member...
  67. #pragma warning( disable : 4710 ) // function not inlined
  68. #pragma warning( disable : 4514 ) // unreferenced inline function has been removed
  69. #pragma warning( disable : 4996 ) // 'fopen': This function or variable may be unsafe
  70. #endif
  71. #include "sparsepp.h"
  72. #ifdef _MSC_VER
  73. #pragma warning( disable : 4127 ) // conditional expression is constant
  74. #pragma warning(push, 0)
  75. #endif
  76. #include <math.h>
  77. #include <stddef.h> // for size_t
  78. #include <stdio.h>
  79. #include <stdlib.h>
  80. #include <string.h>
  81. #include <iostream>
  82. #include <set>
  83. #include <sstream>
  84. #include <typeinfo> // for class typeinfo (returned by typeid)
  85. #include <vector>
  86. #include <stdexcept> // for length_error
  87. namespace sparsehash_internal = SPP_NAMESPACE::sparsehash_internal;
  88. using SPP_NAMESPACE::sparsetable;
  89. using SPP_NAMESPACE::sparse_hashtable;
  90. using SPP_NAMESPACE::sparse_hash_map;
  91. using SPP_NAMESPACE::sparse_hash_set;
  92. // ---------------------------------------------------------------------
  93. // ---------------------------------------------------------------------
  94. #ifndef _MSC_VER // windows defines its own version
  95. #define _strdup strdup
  96. #ifdef __MINGW32__ // mingw has trouble writing to /tmp
  97. static std::string TmpFile(const char* basename)
  98. {
  99. return std::string("./#") + basename;
  100. }
  101. #endif
  102. #else
  103. #pragma warning(disable : 4996)
  104. #define snprintf sprintf_s
  105. #define WIN32_LEAN_AND_MEAN /* We always want minimal includes */
  106. #include <windows.h>
  107. std::string TmpFile(const char* basename)
  108. {
  109. char tmppath_buffer[1024];
  110. int tmppath_len = GetTempPathA(sizeof(tmppath_buffer), tmppath_buffer);
  111. if (tmppath_len <= 0 || tmppath_len >= sizeof(tmppath_buffer))
  112. return basename; // an error, so just bail on tmppath
  113. sprintf_s(tmppath_buffer + tmppath_len, 1024 - tmppath_len, "\\%s", basename);
  114. return tmppath_buffer;
  115. }
  116. #endif
  117. #ifdef _MSC_VER
  118. #pragma warning(pop)
  119. #endif
  120. // ---------------------------------------------------------------------
  121. // This is the "default" interface, which just passes everything
  122. // through to the underlying hashtable. You'll need to subclass it to
  123. // specialize behavior for an individual hashtable.
  124. // ---------------------------------------------------------------------
  125. template <class HT>
  126. class BaseHashtableInterface
  127. {
  128. public:
  129. virtual ~BaseHashtableInterface() {}
  130. typedef typename HT::key_type key_type;
  131. typedef typename HT::value_type value_type;
  132. typedef typename HT::hasher hasher;
  133. typedef typename HT::key_equal key_equal;
  134. typedef typename HT::allocator_type allocator_type;
  135. typedef typename HT::size_type size_type;
  136. typedef typename HT::difference_type difference_type;
  137. typedef typename HT::pointer pointer;
  138. typedef typename HT::const_pointer const_pointer;
  139. typedef typename HT::reference reference;
  140. typedef typename HT::const_reference const_reference;
  141. class const_iterator;
  142. class iterator : public HT::iterator
  143. {
  144. public:
  145. iterator() : parent_(NULL) { } // this allows code like "iterator it;"
  146. iterator(typename HT::iterator it, const BaseHashtableInterface* parent)
  147. : HT::iterator(it), parent_(parent) { }
  148. key_type key() { return parent_->it_to_key(*this); }
  149. private:
  150. friend class BaseHashtableInterface::const_iterator; // for its ctor
  151. const BaseHashtableInterface* parent_;
  152. };
  153. class const_iterator : public HT::const_iterator
  154. {
  155. public:
  156. const_iterator() : parent_(NULL) { }
  157. const_iterator(typename HT::const_iterator it,
  158. const BaseHashtableInterface* parent)
  159. : HT::const_iterator(it), parent_(parent) { }
  160. const_iterator(typename HT::iterator it,
  161. BaseHashtableInterface* parent)
  162. : HT::const_iterator(it), parent_(parent) { }
  163. // The parameter type here *should* just be "iterator", but MSVC
  164. // gets confused by that, so I'm overly specific.
  165. const_iterator(typename BaseHashtableInterface<HT>::iterator it)
  166. : HT::const_iterator(it), parent_(it.parent_) { }
  167. key_type key() { return parent_->it_to_key(*this); }
  168. private:
  169. const BaseHashtableInterface* parent_;
  170. };
  171. class const_local_iterator;
  172. class local_iterator : public HT::local_iterator
  173. {
  174. public:
  175. local_iterator() : parent_(NULL) { }
  176. local_iterator(typename HT::local_iterator it,
  177. const BaseHashtableInterface* parent)
  178. : HT::local_iterator(it), parent_(parent) { }
  179. key_type key() { return parent_->it_to_key(*this); }
  180. private:
  181. friend class BaseHashtableInterface::const_local_iterator; // for its ctor
  182. const BaseHashtableInterface* parent_;
  183. };
  184. class const_local_iterator : public HT::const_local_iterator
  185. {
  186. public:
  187. const_local_iterator() : parent_(NULL) { }
  188. const_local_iterator(typename HT::const_local_iterator it,
  189. const BaseHashtableInterface* parent)
  190. : HT::const_local_iterator(it), parent_(parent) { }
  191. const_local_iterator(typename HT::local_iterator it,
  192. BaseHashtableInterface* parent)
  193. : HT::const_local_iterator(it), parent_(parent) { }
  194. const_local_iterator(local_iterator it)
  195. : HT::const_local_iterator(it), parent_(it.parent_) { }
  196. key_type key() { return parent_->it_to_key(*this); }
  197. private:
  198. const BaseHashtableInterface* parent_;
  199. };
  200. iterator begin() { return iterator(ht_.begin(), this); }
  201. iterator end() { return iterator(ht_.end(), this); }
  202. const_iterator begin() const { return const_iterator(ht_.begin(), this); }
  203. const_iterator end() const { return const_iterator(ht_.end(), this); }
  204. local_iterator begin(size_type i) { return local_iterator(ht_.begin(i), this); }
  205. local_iterator end(size_type i) { return local_iterator(ht_.end(i), this); }
  206. const_local_iterator begin(size_type i) const { return const_local_iterator(ht_.begin(i), this); }
  207. const_local_iterator end(size_type i) const { return const_local_iterator(ht_.end(i), this); }
  208. hasher hash_funct() const { return ht_.hash_funct(); }
  209. hasher hash_function() const { return ht_.hash_function(); }
  210. key_equal key_eq() const { return ht_.key_eq(); }
  211. allocator_type get_allocator() const { return ht_.get_allocator(); }
  212. BaseHashtableInterface(size_type expected_max_items_in_table,
  213. const hasher& hf,
  214. const key_equal& eql,
  215. const allocator_type& alloc)
  216. : ht_(expected_max_items_in_table, hf, eql, alloc) { }
  217. // Not all ht_'s support this constructor: you should only call it
  218. // from a subclass if you know your ht supports it. Otherwise call
  219. // the previous constructor, followed by 'insert(f, l);'.
  220. template <class InputIterator>
  221. BaseHashtableInterface(InputIterator f, InputIterator l,
  222. size_type expected_max_items_in_table,
  223. const hasher& hf,
  224. const key_equal& eql,
  225. const allocator_type& alloc)
  226. : ht_(f, l, expected_max_items_in_table, hf, eql, alloc) {
  227. }
  228. // This is the version of the constructor used by dense_*, which
  229. // requires an empty key in the constructor.
  230. template <class InputIterator>
  231. BaseHashtableInterface(InputIterator f, InputIterator l, key_type empty_k,
  232. size_type expected_max_items_in_table,
  233. const hasher& hf,
  234. const key_equal& eql,
  235. const allocator_type& alloc)
  236. : ht_(f, l, empty_k, expected_max_items_in_table, hf, eql, alloc) {
  237. }
  238. // This is the constructor appropriate for {dense,sparse}hashtable.
  239. template <class ExtractKey, class SetKey>
  240. BaseHashtableInterface(size_type expected_max_items_in_table,
  241. const hasher& hf,
  242. const key_equal& eql,
  243. const ExtractKey& ek,
  244. const SetKey& sk,
  245. const allocator_type& alloc)
  246. : ht_(expected_max_items_in_table, hf, eql, ek, sk, alloc) { }
  247. void clear() { ht_.clear(); }
  248. void swap(BaseHashtableInterface& other) { ht_.swap(other.ht_); }
  249. // Only part of the API for some hashtable implementations.
  250. void clear_no_resize() { clear(); }
  251. size_type size() const { return ht_.size(); }
  252. size_type max_size() const { return ht_.max_size(); }
  253. bool empty() const { return ht_.empty(); }
  254. size_type bucket_count() const { return ht_.bucket_count(); }
  255. size_type max_bucket_count() const { return ht_.max_bucket_count(); }
  256. size_type bucket_size(size_type i) const {
  257. return ht_.bucket_size(i);
  258. }
  259. size_type bucket(const key_type& key) const {
  260. return ht_.bucket(key);
  261. }
  262. float load_factor() const { return ht_.load_factor(); }
  263. float max_load_factor() const { return ht_.max_load_factor(); }
  264. void max_load_factor(float grow) { ht_.max_load_factor(grow); }
  265. float min_load_factor() const { return ht_.min_load_factor(); }
  266. void min_load_factor(float shrink) { ht_.min_load_factor(shrink); }
  267. void set_resizing_parameters(float shrink, float grow) {
  268. ht_.set_resizing_parameters(shrink, grow);
  269. }
  270. void resize(size_type hint) { ht_.resize(hint); }
  271. void rehash(size_type hint) { ht_.rehash(hint); }
  272. iterator find(const key_type& key) {
  273. return iterator(ht_.find(key), this);
  274. }
  275. const_iterator find(const key_type& key) const {
  276. return const_iterator(ht_.find(key), this);
  277. }
  278. // Rather than try to implement operator[], which doesn't make much
  279. // sense for set types, we implement two methods: bracket_equal and
  280. // bracket_assign. By default, bracket_equal(a, b) returns true if
  281. // ht[a] == b, and false otherwise. (Note that this follows
  282. // operator[] semantics exactly, including inserting a if it's not
  283. // already in the hashtable, before doing the equality test.) For
  284. // sets, which have no operator[], b is ignored, and bracket_equal
  285. // returns true if key is in the set and false otherwise.
  286. // bracket_assign(a, b) is equivalent to ht[a] = b. For sets, b is
  287. // ignored, and bracket_assign is equivalent to ht.insert(a).
  288. template<typename AssignValue>
  289. bool bracket_equal(const key_type& key, const AssignValue& expected) {
  290. return ht_[key] == expected;
  291. }
  292. template<typename AssignValue>
  293. void bracket_assign(const key_type& key, const AssignValue& value) {
  294. ht_[key] = value;
  295. }
  296. size_type count(const key_type& key) const { return ht_.count(key); }
  297. std::pair<iterator, iterator> equal_range(const key_type& key)
  298. {
  299. std::pair<typename HT::iterator, typename HT::iterator> r
  300. = ht_.equal_range(key);
  301. return std::pair<iterator, iterator>(iterator(r.first, this),
  302. iterator(r.second, this));
  303. }
  304. std::pair<const_iterator, const_iterator> equal_range(const key_type& key) const
  305. {
  306. std::pair<typename HT::const_iterator, typename HT::const_iterator> r
  307. = ht_.equal_range(key);
  308. return std::pair<const_iterator, const_iterator>(
  309. const_iterator(r.first, this), const_iterator(r.second, this));
  310. }
  311. const_iterator random_element(class ACMRandom* r) const {
  312. return const_iterator(ht_.random_element(r), this);
  313. }
  314. iterator random_element(class ACMRandom* r) {
  315. return iterator(ht_.random_element(r), this);
  316. }
  317. std::pair<iterator, bool> insert(const value_type& obj) {
  318. std::pair<typename HT::iterator, bool> r = ht_.insert(obj);
  319. return std::pair<iterator, bool>(iterator(r.first, this), r.second);
  320. }
  321. template <class InputIterator>
  322. void insert(InputIterator f, InputIterator l) {
  323. ht_.insert(f, l);
  324. }
  325. void insert(typename HT::const_iterator f, typename HT::const_iterator l) {
  326. ht_.insert(f, l);
  327. }
  328. iterator insert(typename HT::iterator, const value_type& obj) {
  329. return iterator(insert(obj).first, this);
  330. }
  331. // These will commonly need to be overridden by the child.
  332. void set_empty_key(const key_type& k) { ht_.set_empty_key(k); }
  333. void clear_empty_key() { ht_.clear_empty_key(); }
  334. key_type empty_key() const { return ht_.empty_key(); }
  335. void set_deleted_key(const key_type& k) { ht_.set_deleted_key(k); }
  336. void clear_deleted_key() { ht_.clear_deleted_key(); }
  337. key_type deleted_key() const { return ht_.deleted_key(); }
  338. size_type erase(const key_type& key) { return ht_.erase(key); }
  339. void erase(typename HT::iterator it) { ht_.erase(it); }
  340. void erase(typename HT::iterator f, typename HT::iterator l) {
  341. ht_.erase(f, l);
  342. }
  343. bool operator==(const BaseHashtableInterface& other) const {
  344. return ht_ == other.ht_;
  345. }
  346. bool operator!=(const BaseHashtableInterface& other) const {
  347. return ht_ != other.ht_;
  348. }
  349. template <typename ValueSerializer, typename OUTPUT>
  350. bool serialize(ValueSerializer serializer, OUTPUT *fp) {
  351. return ht_.serialize(serializer, fp);
  352. }
  353. template <typename ValueSerializer, typename INPUT>
  354. bool unserialize(ValueSerializer serializer, INPUT *fp) {
  355. return ht_.unserialize(serializer, fp);
  356. }
  357. template <typename OUTPUT>
  358. bool write_metadata(OUTPUT *fp) {
  359. return ht_.write_metadata(fp);
  360. }
  361. template <typename INPUT>
  362. bool read_metadata(INPUT *fp) {
  363. return ht_.read_metadata(fp);
  364. }
  365. template <typename OUTPUT>
  366. bool write_nopointer_data(OUTPUT *fp) {
  367. return ht_.write_nopointer_data(fp);
  368. }
  369. template <typename INPUT>
  370. bool read_nopointer_data(INPUT *fp) {
  371. return ht_.read_nopointer_data(fp);
  372. }
  373. // low-level stats
  374. int num_table_copies() const { return (int)ht_.num_table_copies(); }
  375. // Not part of the hashtable API, but is provided to make testing easier.
  376. virtual key_type get_key(const value_type& value) const = 0;
  377. // All subclasses should define get_data(value_type) as well. I don't
  378. // provide an abstract-virtual definition here, because the return type
  379. // differs between subclasses (not all subclasses define data_type).
  380. //virtual data_type get_data(const value_type& value) const = 0;
  381. //virtual data_type default_data() const = 0;
  382. // These allow introspection into the interface. "Supports" means
  383. // that the implementation of this functionality isn't a noop.
  384. virtual bool supports_clear_no_resize() const = 0;
  385. virtual bool supports_empty_key() const = 0;
  386. virtual bool supports_deleted_key() const = 0;
  387. virtual bool supports_brackets() const = 0; // has a 'real' operator[]
  388. virtual bool supports_readwrite() const = 0;
  389. virtual bool supports_num_table_copies() const = 0;
  390. virtual bool supports_serialization() const = 0;
  391. protected:
  392. HT ht_;
  393. // These are what subclasses have to define to get class-specific behavior
  394. virtual key_type it_to_key(const iterator& it) const = 0;
  395. virtual key_type it_to_key(const const_iterator& it) const = 0;
  396. virtual key_type it_to_key(const local_iterator& it) const = 0;
  397. virtual key_type it_to_key(const const_local_iterator& it) const = 0;
  398. };
  399. // ---------------------------------------------------------------------
  400. // ---------------------------------------------------------------------
  401. template <class Key, class T,
  402. class HashFcn = SPP_HASH_CLASS<Key>,
  403. class EqualKey = std::equal_to<Key>,
  404. class Alloc = spp::libc_allocator_with_realloc<std::pair<const Key, T> > >
  405. class HashtableInterface_SparseHashMap
  406. : public BaseHashtableInterface< sparse_hash_map<Key, T, HashFcn,
  407. EqualKey, Alloc> >
  408. {
  409. private:
  410. typedef sparse_hash_map<Key, T, HashFcn, EqualKey, Alloc> ht;
  411. typedef BaseHashtableInterface<ht> p; // parent
  412. public:
  413. explicit HashtableInterface_SparseHashMap(
  414. typename p::size_type expected_max_items = 0,
  415. const typename p::hasher& hf = typename p::hasher(),
  416. const typename p::key_equal& eql = typename p::key_equal(),
  417. const typename p::allocator_type& alloc = typename p::allocator_type())
  418. : BaseHashtableInterface<ht>(expected_max_items, hf, eql, alloc) { }
  419. template <class InputIterator>
  420. HashtableInterface_SparseHashMap(
  421. InputIterator f, InputIterator l,
  422. typename p::size_type expected_max_items = 0,
  423. const typename p::hasher& hf = typename p::hasher(),
  424. const typename p::key_equal& eql = typename p::key_equal(),
  425. const typename p::allocator_type& alloc = typename p::allocator_type())
  426. : BaseHashtableInterface<ht>(f, l, expected_max_items, hf, eql, alloc) { }
  427. typename p::key_type get_key(const typename p::value_type& value) const {
  428. return value.first;
  429. }
  430. typename ht::data_type get_data(const typename p::value_type& value) const {
  431. return value.second;
  432. }
  433. typename ht::data_type default_data() const {
  434. return typename ht::data_type();
  435. }
  436. bool supports_clear_no_resize() const { return false; }
  437. bool supports_empty_key() const { return false; }
  438. bool supports_deleted_key() const { return false; }
  439. bool supports_brackets() const { return true; }
  440. bool supports_readwrite() const { return true; }
  441. bool supports_num_table_copies() const { return false; }
  442. bool supports_serialization() const { return true; }
  443. void set_empty_key(const typename p::key_type&) { }
  444. void clear_empty_key() { }
  445. typename p::key_type empty_key() const { return typename p::key_type(); }
  446. int num_table_copies() const { return 0; }
  447. typedef typename ht::NopointerSerializer NopointerSerializer;
  448. protected:
  449. template <class K2, class T2, class H2, class E2, class A2>
  450. friend void swap(HashtableInterface_SparseHashMap<K2,T2,H2,E2,A2>& a,
  451. HashtableInterface_SparseHashMap<K2,T2,H2,E2,A2>& b);
  452. typename p::key_type it_to_key(const typename p::iterator& it) const {
  453. return it->first;
  454. }
  455. typename p::key_type it_to_key(const typename p::const_iterator& it) const {
  456. return it->first;
  457. }
  458. typename p::key_type it_to_key(const typename p::local_iterator& it) const {
  459. return it->first;
  460. }
  461. typename p::key_type it_to_key(const typename p::const_local_iterator& it) const {
  462. return it->first;
  463. }
  464. };
  465. // ---------------------------------------------------------------------
  466. // ---------------------------------------------------------------------
  467. template <class K, class T, class H, class E, class A>
  468. void swap(HashtableInterface_SparseHashMap<K,T,H,E,A>& a,
  469. HashtableInterface_SparseHashMap<K,T,H,E,A>& b)
  470. {
  471. swap(a.ht_, b.ht_);
  472. }
  473. // ---------------------------------------------------------------------
  474. // ---------------------------------------------------------------------
  475. template <class Value,
  476. class HashFcn = SPP_HASH_CLASS<Value>,
  477. class EqualKey = std::equal_to<Value>,
  478. class Alloc = spp::libc_allocator_with_realloc<Value> >
  479. class HashtableInterface_SparseHashSet
  480. : public BaseHashtableInterface< sparse_hash_set<Value, HashFcn,
  481. EqualKey, Alloc> >
  482. {
  483. private:
  484. typedef sparse_hash_set<Value, HashFcn, EqualKey, Alloc> ht;
  485. typedef BaseHashtableInterface<ht> p; // parent
  486. public:
  487. explicit HashtableInterface_SparseHashSet(
  488. typename p::size_type expected_max_items = 0,
  489. const typename p::hasher& hf = typename p::hasher(),
  490. const typename p::key_equal& eql = typename p::key_equal(),
  491. const typename p::allocator_type& alloc = typename p::allocator_type())
  492. : BaseHashtableInterface<ht>(expected_max_items, hf, eql, alloc) { }
  493. template <class InputIterator>
  494. HashtableInterface_SparseHashSet(
  495. InputIterator f, InputIterator l,
  496. typename p::size_type expected_max_items = 0,
  497. const typename p::hasher& hf = typename p::hasher(),
  498. const typename p::key_equal& eql = typename p::key_equal(),
  499. const typename p::allocator_type& alloc = typename p::allocator_type())
  500. : BaseHashtableInterface<ht>(f, l, expected_max_items, hf, eql, alloc) { }
  501. template<typename AssignValue>
  502. bool bracket_equal(const typename p::key_type& key, const AssignValue&) {
  503. return this->ht_.find(key) != this->ht_.end();
  504. }
  505. template<typename AssignValue>
  506. void bracket_assign(const typename p::key_type& key, const AssignValue&) {
  507. this->ht_.insert(key);
  508. }
  509. typename p::key_type get_key(const typename p::value_type& value) const {
  510. return value;
  511. }
  512. // For sets, the only 'data' is that an item is actually inserted.
  513. bool get_data(const typename p::value_type&) const {
  514. return true;
  515. }
  516. bool default_data() const {
  517. return true;
  518. }
  519. bool supports_clear_no_resize() const { return false; }
  520. bool supports_empty_key() const { return false; }
  521. bool supports_deleted_key() const { return false; }
  522. bool supports_brackets() const { return false; }
  523. bool supports_readwrite() const { return true; }
  524. bool supports_num_table_copies() const { return false; }
  525. bool supports_serialization() const { return true; }
  526. void set_empty_key(const typename p::key_type&) { }
  527. void clear_empty_key() { }
  528. typename p::key_type empty_key() const { return typename p::key_type(); }
  529. int num_table_copies() const { return 0; }
  530. typedef typename ht::NopointerSerializer NopointerSerializer;
  531. protected:
  532. template <class K2, class H2, class E2, class A2>
  533. friend void swap(HashtableInterface_SparseHashSet<K2,H2,E2,A2>& a,
  534. HashtableInterface_SparseHashSet<K2,H2,E2,A2>& b);
  535. typename p::key_type it_to_key(const typename p::iterator& it) const {
  536. return *it;
  537. }
  538. typename p::key_type it_to_key(const typename p::const_iterator& it) const {
  539. return *it;
  540. }
  541. typename p::key_type it_to_key(const typename p::local_iterator& it) const {
  542. return *it;
  543. }
  544. typename p::key_type it_to_key(const typename p::const_local_iterator& it)
  545. const {
  546. return *it;
  547. }
  548. };
  549. // ---------------------------------------------------------------------
  550. // ---------------------------------------------------------------------
  551. template <class K, class H, class E, class A>
  552. void swap(HashtableInterface_SparseHashSet<K,H,E,A>& a,
  553. HashtableInterface_SparseHashSet<K,H,E,A>& b)
  554. {
  555. swap(a.ht_, b.ht_);
  556. }
  557. // ---------------------------------------------------------------------
  558. // ---------------------------------------------------------------------
  559. template <class Value, class Key, class HashFcn, class ExtractKey,
  560. class SetKey, class EqualKey, class Alloc>
  561. class HashtableInterface_SparseHashtable
  562. : public BaseHashtableInterface< sparse_hashtable<Value, Key, HashFcn,
  563. ExtractKey, SetKey,
  564. EqualKey, Alloc> >
  565. {
  566. private:
  567. typedef sparse_hashtable<Value, Key, HashFcn, ExtractKey, SetKey,
  568. EqualKey, Alloc> ht;
  569. typedef BaseHashtableInterface<ht> p; // parent
  570. public:
  571. explicit HashtableInterface_SparseHashtable(
  572. typename p::size_type expected_max_items = 0,
  573. const typename p::hasher& hf = typename p::hasher(),
  574. const typename p::key_equal& eql = typename p::key_equal(),
  575. const typename p::allocator_type& alloc = typename p::allocator_type())
  576. : BaseHashtableInterface<ht>(expected_max_items, hf, eql,
  577. ExtractKey(), SetKey(), alloc) { }
  578. template <class InputIterator>
  579. HashtableInterface_SparseHashtable(
  580. InputIterator f, InputIterator l,
  581. typename p::size_type expected_max_items = 0,
  582. const typename p::hasher& hf = typename p::hasher(),
  583. const typename p::key_equal& eql = typename p::key_equal(),
  584. const typename p::allocator_type& alloc = typename p::allocator_type())
  585. : BaseHashtableInterface<ht>(expected_max_items, hf, eql,
  586. ExtractKey(), SetKey(), alloc) {
  587. this->insert(f, l);
  588. }
  589. float max_load_factor() const {
  590. float shrink, grow;
  591. this->ht_.get_resizing_parameters(&shrink, &grow);
  592. return grow;
  593. }
  594. void max_load_factor(float new_grow) {
  595. float shrink, grow;
  596. this->ht_.get_resizing_parameters(&shrink, &grow);
  597. this->ht_.set_resizing_parameters(shrink, new_grow);
  598. }
  599. float min_load_factor() const {
  600. float shrink, grow;
  601. this->ht_.get_resizing_parameters(&shrink, &grow);
  602. return shrink;
  603. }
  604. void min_load_factor(float new_shrink) {
  605. float shrink, grow;
  606. this->ht_.get_resizing_parameters(&shrink, &grow);
  607. this->ht_.set_resizing_parameters(new_shrink, grow);
  608. }
  609. template<typename AssignValue>
  610. bool bracket_equal(const typename p::key_type&, const AssignValue&) {
  611. return false;
  612. }
  613. template<typename AssignValue>
  614. void bracket_assign(const typename p::key_type&, const AssignValue&) {
  615. }
  616. typename p::key_type get_key(const typename p::value_type& value) const {
  617. return extract_key(value);
  618. }
  619. typename p::value_type get_data(const typename p::value_type& value) const {
  620. return value;
  621. }
  622. typename p::value_type default_data() const {
  623. return typename p::value_type();
  624. }
  625. bool supports_clear_no_resize() const { return false; }
  626. bool supports_empty_key() const { return false; }
  627. bool supports_deleted_key() const { return false; }
  628. bool supports_brackets() const { return false; }
  629. bool supports_readwrite() const { return true; }
  630. bool supports_num_table_copies() const { return true; }
  631. bool supports_serialization() const { return true; }
  632. void set_empty_key(const typename p::key_type&) { }
  633. void clear_empty_key() { }
  634. typename p::key_type empty_key() const { return typename p::key_type(); }
  635. // These tr1 names aren't defined for sparse_hashtable.
  636. typename p::hasher hash_function() { return this->hash_funct(); }
  637. void rehash(typename p::size_type hint) { this->resize(hint); }
  638. // TODO(csilvers): also support/test destructive_begin()/destructive_end()?
  639. typedef typename ht::NopointerSerializer NopointerSerializer;
  640. protected:
  641. template <class V2, class K2, class HF2, class EK2, class SK2, class Eq2,
  642. class A2>
  643. friend void swap(
  644. HashtableInterface_SparseHashtable<V2,K2,HF2,EK2,SK2,Eq2,A2>& a,
  645. HashtableInterface_SparseHashtable<V2,K2,HF2,EK2,SK2,Eq2,A2>& b);
  646. typename p::key_type it_to_key(const typename p::iterator& it) const {
  647. return extract_key(*it);
  648. }
  649. typename p::key_type it_to_key(const typename p::const_iterator& it) const {
  650. return extract_key(*it);
  651. }
  652. typename p::key_type it_to_key(const typename p::local_iterator& it) const {
  653. return extract_key(*it);
  654. }
  655. typename p::key_type it_to_key(const typename p::const_local_iterator& it)
  656. const {
  657. return extract_key(*it);
  658. }
  659. private:
  660. ExtractKey extract_key;
  661. };
  662. // ---------------------------------------------------------------------
  663. // ---------------------------------------------------------------------
  664. template <class V, class K, class HF, class EK, class SK, class Eq, class A>
  665. void swap(HashtableInterface_SparseHashtable<V,K,HF,EK,SK,Eq,A>& a,
  666. HashtableInterface_SparseHashtable<V,K,HF,EK,SK,Eq,A>& b) {
  667. swap(a.ht_, b.ht_);
  668. }
  669. void EXPECT_TRUE(bool cond)
  670. {
  671. if (!cond)
  672. {
  673. ::fputs("Test failed:\n", stderr);
  674. ::exit(1);
  675. }
  676. }
  677. SPP_START_NAMESPACE
  678. namespace testing
  679. {
  680. #define EXPECT_FALSE(a) EXPECT_TRUE(!(a))
  681. #define EXPECT_EQ(a, b) EXPECT_TRUE((a) == (b))
  682. #define EXPECT_NE(a, b) EXPECT_TRUE((a) != (b))
  683. #define EXPECT_LT(a, b) EXPECT_TRUE((a) < (b))
  684. #define EXPECT_GT(a, b) EXPECT_TRUE((a) > (b))
  685. #define EXPECT_LE(a, b) EXPECT_TRUE((a) <= (b))
  686. #define EXPECT_GE(a, b) EXPECT_TRUE((a) >= (b))
  687. #define EXPECT_DEATH(cmd, expected_error_string) \
  688. try { \
  689. cmd; \
  690. EXPECT_FALSE("did not see expected error: " #expected_error_string); \
  691. } catch (const std::length_error&) { \
  692. /* Good, the cmd failed. */ \
  693. }
  694. #define TEST(suitename, testname) \
  695. class TEST_##suitename##_##testname { \
  696. public: \
  697. TEST_##suitename##_##testname() { \
  698. ::fputs("Running " #suitename "." #testname "\n", stderr); \
  699. Run(); \
  700. } \
  701. void Run(); \
  702. }; \
  703. static TEST_##suitename##_##testname \
  704. test_instance_##suitename##_##testname; \
  705. void TEST_##suitename##_##testname::Run()
  706. template<typename C1, typename C2, typename C3>
  707. struct TypeList3
  708. {
  709. typedef C1 type1;
  710. typedef C2 type2;
  711. typedef C3 type3;
  712. };
  713. // I need to list 9 types here, for code below to compile, though
  714. // only the first 3 are ever used.
  715. #define TYPED_TEST_CASE_3(classname, typelist) \
  716. typedef typelist::type1 classname##_type1; \
  717. typedef typelist::type2 classname##_type2; \
  718. typedef typelist::type3 classname##_type3; \
  719. SPP_ATTRIBUTE_UNUSED static const int classname##_numtypes = 3; \
  720. typedef typelist::type1 classname##_type4; \
  721. typedef typelist::type1 classname##_type5; \
  722. typedef typelist::type1 classname##_type6; \
  723. typedef typelist::type1 classname##_type7; \
  724. typedef typelist::type1 classname##_type8; \
  725. typedef typelist::type1 classname##_type9;
  726. template<typename C1, typename C2, typename C3, typename C4, typename C5,
  727. typename C6, typename C7, typename C8, typename C9>
  728. struct TypeList9
  729. {
  730. typedef C1 type1;
  731. typedef C2 type2;
  732. typedef C3 type3;
  733. typedef C4 type4;
  734. typedef C5 type5;
  735. typedef C6 type6;
  736. typedef C7 type7;
  737. typedef C8 type8;
  738. typedef C9 type9;
  739. };
  740. #define TYPED_TEST_CASE_9(classname, typelist) \
  741. typedef typelist::type1 classname##_type1; \
  742. typedef typelist::type2 classname##_type2; \
  743. typedef typelist::type3 classname##_type3; \
  744. typedef typelist::type4 classname##_type4; \
  745. typedef typelist::type5 classname##_type5; \
  746. typedef typelist::type6 classname##_type6; \
  747. typedef typelist::type7 classname##_type7; \
  748. typedef typelist::type8 classname##_type8; \
  749. typedef typelist::type9 classname##_type9; \
  750. static const int classname##_numtypes = 9;
  751. #define TYPED_TEST(superclass, testname) \
  752. template<typename TypeParam> \
  753. class TEST_onetype_##superclass##_##testname : \
  754. public superclass<TypeParam> { \
  755. public: \
  756. TEST_onetype_##superclass##_##testname() { \
  757. Run(); \
  758. } \
  759. private: \
  760. void Run(); \
  761. }; \
  762. class TEST_typed_##superclass##_##testname { \
  763. public: \
  764. explicit TEST_typed_##superclass##_##testname() { \
  765. if (superclass##_numtypes >= 1) { \
  766. ::fputs("Running " #superclass "." #testname ".1\n", stderr); \
  767. TEST_onetype_##superclass##_##testname<superclass##_type1> t; \
  768. } \
  769. if (superclass##_numtypes >= 2) { \
  770. ::fputs("Running " #superclass "." #testname ".2\n", stderr); \
  771. TEST_onetype_##superclass##_##testname<superclass##_type2> t; \
  772. } \
  773. if (superclass##_numtypes >= 3) { \
  774. ::fputs("Running " #superclass "." #testname ".3\n", stderr); \
  775. TEST_onetype_##superclass##_##testname<superclass##_type3> t; \
  776. } \
  777. if (superclass##_numtypes >= 4) { \
  778. ::fputs("Running " #superclass "." #testname ".4\n", stderr); \
  779. TEST_onetype_##superclass##_##testname<superclass##_type4> t; \
  780. } \
  781. if (superclass##_numtypes >= 5) { \
  782. ::fputs("Running " #superclass "." #testname ".5\n", stderr); \
  783. TEST_onetype_##superclass##_##testname<superclass##_type5> t; \
  784. } \
  785. if (superclass##_numtypes >= 6) { \
  786. ::fputs("Running " #superclass "." #testname ".6\n", stderr); \
  787. TEST_onetype_##superclass##_##testname<superclass##_type6> t; \
  788. } \
  789. if (superclass##_numtypes >= 7) { \
  790. ::fputs("Running " #superclass "." #testname ".7\n", stderr); \
  791. TEST_onetype_##superclass##_##testname<superclass##_type7> t; \
  792. } \
  793. if (superclass##_numtypes >= 8) { \
  794. ::fputs("Running " #superclass "." #testname ".8\n", stderr); \
  795. TEST_onetype_##superclass##_##testname<superclass##_type8> t; \
  796. } \
  797. if (superclass##_numtypes >= 9) { \
  798. ::fputs("Running " #superclass "." #testname ".9\n", stderr); \
  799. TEST_onetype_##superclass##_##testname<superclass##_type9> t; \
  800. } \
  801. } \
  802. }; \
  803. static TEST_typed_##superclass##_##testname \
  804. test_instance_typed_##superclass##_##testname; \
  805. template<class TypeParam> \
  806. void TEST_onetype_##superclass##_##testname<TypeParam>::Run()
  807. // This is a dummy class just to make converting from internal-google
  808. // to opensourcing easier.
  809. class Test { };
  810. } // namespace testing
  811. SPP_END_NAMESPACE
  812. namespace testing = SPP_NAMESPACE::testing;
  813. using std::cout;
  814. using std::pair;
  815. using std::set;
  816. using std::string;
  817. using std::vector;
  818. typedef unsigned char uint8;
  819. #ifdef _MSC_VER
  820. // Below, we purposefully test having a very small allocator size.
  821. // This causes some "type conversion too small" errors when using this
  822. // allocator with sparsetable buckets. We're testing to make sure we
  823. // handle that situation ok, so we don't need the compiler warnings.
  824. #pragma warning(disable:4244)
  825. #define ATTRIBUTE_UNUSED
  826. #else
  827. #define ATTRIBUTE_UNUSED __attribute__((unused))
  828. #endif
  829. namespace {
  830. #ifndef _MSC_VER // windows defines its own version
  831. # ifdef __MINGW32__ // mingw has trouble writing to /tmp
  832. static string TmpFile(const char* basename) {
  833. return string("./#") + basename;
  834. }
  835. # else
  836. static string TmpFile(const char* basename) {
  837. string kTmpdir = "/tmp";
  838. return kTmpdir + "/" + basename;
  839. }
  840. # endif
  841. #endif
  842. // Used as a value in some of the hashtable tests. It's just some
  843. // arbitrary user-defined type with non-trivial memory management.
  844. // ---------------------------------------------------------------
  845. struct ValueType
  846. {
  847. public:
  848. ValueType() : s_(kDefault) { }
  849. ValueType(const char* init_s) : s_(kDefault) { set_s(init_s); }
  850. ~ValueType() { set_s(NULL); }
  851. ValueType(const ValueType& that) : s_(kDefault) { operator=(that); }
  852. void operator=(const ValueType& that) { set_s(that.s_); }
  853. bool operator==(const ValueType& that) const {
  854. return strcmp(this->s(), that.s()) == 0;
  855. }
  856. void set_s(const char* new_s) {
  857. if (s_ != kDefault)
  858. free(const_cast<char*>(s_));
  859. s_ = (new_s == NULL ? kDefault : reinterpret_cast<char*>(_strdup(new_s)));
  860. }
  861. const char* s() const { return s_; }
  862. private:
  863. const char* s_;
  864. static const char* const kDefault;
  865. };
  866. const char* const ValueType::kDefault = "hi";
  867. // This is used by the low-level sparse/dense_hashtable classes,
  868. // which support the most general relationship between keys and
  869. // values: the key is derived from the value through some arbitrary
  870. // function. (For classes like sparse_hash_map, the 'value' is a
  871. // key/data pair, and the function to derive the key is
  872. // FirstElementOfPair.) KeyToValue is the inverse of this function,
  873. // so GetKey(KeyToValue(key)) == key. To keep the tests a bit
  874. // simpler, we've chosen to make the key and value actually be the
  875. // same type, which is why we need only one template argument for the
  876. // types, rather than two (one for the key and one for the value).
  877. template<class KeyAndValueT, class KeyToValue>
  878. struct SetKey
  879. {
  880. void operator()(KeyAndValueT* value, const KeyAndValueT& new_key) const
  881. {
  882. *value = KeyToValue()(new_key);
  883. }
  884. };
  885. // A hash function that keeps track of how often it's called. We use
  886. // a simple djb-hash so we don't depend on how STL hashes. We use
  887. // this same method to do the key-comparison, so we can keep track
  888. // of comparison-counts too.
  889. struct Hasher
  890. {
  891. explicit Hasher(int i=0) : id_(i), num_hashes_(0), num_compares_(0) { }
  892. int id() const { return id_; }
  893. int num_hashes() const { return num_hashes_; }
  894. int num_compares() const { return num_compares_; }
  895. size_t operator()(int a) const {
  896. num_hashes_++;
  897. return static_cast<size_t>(a);
  898. }
  899. size_t operator()(const char* a) const {
  900. num_hashes_++;
  901. size_t hash = 0;
  902. for (size_t i = 0; a[i]; i++ )
  903. hash = 33 * hash + a[i];
  904. return hash;
  905. }
  906. size_t operator()(const string& a) const {
  907. num_hashes_++;
  908. size_t hash = 0;
  909. for (size_t i = 0; i < a.length(); i++ )
  910. hash = 33 * hash + a[i];
  911. return hash;
  912. }
  913. size_t operator()(const int* a) const {
  914. num_hashes_++;
  915. return static_cast<size_t>(reinterpret_cast<uintptr_t>(a));
  916. }
  917. bool operator()(int a, int b) const {
  918. num_compares_++;
  919. return a == b;
  920. }
  921. bool operator()(const string& a, const string& b) const {
  922. num_compares_++;
  923. return a == b;
  924. }
  925. bool operator()(const char* a, const char* b) const {
  926. num_compares_++;
  927. // The 'a == b' test is necessary, in case a and b are both NULL.
  928. return (a == b || (a && b && strcmp(a, b) == 0));
  929. }
  930. private:
  931. mutable int id_;
  932. mutable int num_hashes_;
  933. mutable int num_compares_;
  934. };
  935. // Allocator that allows controlling its size in various ways, to test
  936. // allocator overflow. Because we use this allocator in a vector, we
  937. // need to define != and swap for gcc.
  938. // ------------------------------------------------------------------
  939. template<typename T,
  940. typename SizeT = size_t,
  941. SizeT MAX_SIZE = static_cast<SizeT>(~0)>
  942. struct Alloc
  943. {
  944. typedef T value_type;
  945. typedef SizeT size_type;
  946. typedef ptrdiff_t difference_type;
  947. typedef T* pointer;
  948. typedef const T* const_pointer;
  949. typedef T& reference;
  950. typedef const T& const_reference;
  951. explicit Alloc(int i=0, int* count=NULL) : id_(i), count_(count) {}
  952. ~Alloc() {}
  953. pointer address(reference r) const { return &r; }
  954. const_pointer address(const_reference r) const { return &r; }
  955. pointer allocate(size_type n, const_pointer = 0) {
  956. if (count_) ++(*count_);
  957. return static_cast<pointer>(malloc(n * sizeof(value_type)));
  958. }
  959. void deallocate(pointer p, size_type) {
  960. free(p);
  961. }
  962. pointer reallocate(pointer p, size_type n) {
  963. if (count_) ++(*count_);
  964. return static_cast<pointer>(realloc(p, n * sizeof(value_type)));
  965. }
  966. size_type max_size() const {
  967. return static_cast<size_type>(MAX_SIZE);
  968. }
  969. void construct(pointer p, const value_type& val) {
  970. new(p) value_type(val);
  971. }
  972. void destroy(pointer p) { p->~value_type(); }
  973. bool is_custom_alloc() const { return true; }
  974. template <class U>
  975. Alloc(const Alloc<U, SizeT, MAX_SIZE>& that)
  976. : id_(that.id_), count_(that.count_) {
  977. }
  978. template <class U>
  979. struct rebind {
  980. typedef Alloc<U, SizeT, MAX_SIZE> other;
  981. };
  982. bool operator==(const Alloc& that) const {
  983. return this->id_ == that.id_ && this->count_ == that.count_;
  984. }
  985. bool operator!=(const Alloc& that) const {
  986. return !this->operator==(that);
  987. }
  988. int id() const { return id_; }
  989. // I have to make these public so the constructor used for rebinding
  990. // can see them. Normally, I'd just make them private and say:
  991. // template<typename U, typename U_SizeT, U_SizeT U_MAX_SIZE> friend struct Alloc;
  992. // but MSVC 7.1 barfs on that. So public it is. But no peeking!
  993. public:
  994. int id_;
  995. int* count_;
  996. };
  997. // Below are a few fun routines that convert a value into a key, used
  998. // for dense_hashtable and sparse_hashtable. It's our responsibility
  999. // to make sure, when we insert values into these objects, that the
  1000. // values match the keys we insert them under. To allow us to use
  1001. // these routines for SetKey as well, we require all these functions
  1002. // be their own inverse: f(f(x)) == x.
  1003. template<class Value>
  1004. struct Negation {
  1005. typedef Value result_type;
  1006. Value operator()(Value& v) { return -v; }
  1007. const Value operator()(const Value& v) const { return -v; }
  1008. };
  1009. struct Capital
  1010. {
  1011. typedef string result_type;
  1012. string operator()(string& s) {
  1013. return string(1, s[0] ^ 32) + s.substr(1);
  1014. }
  1015. const string operator()(const string& s) const {
  1016. return string(1, s[0] ^ 32) + s.substr(1);
  1017. }
  1018. };
  1019. struct Identity
  1020. { // lame, I know, but an important case to test.
  1021. typedef const char* result_type;
  1022. const char* operator()(const char* s) const {
  1023. return s;
  1024. }
  1025. };
  1026. // This is just to avoid memory leaks -- it's a global pointer to
  1027. // all the memory allocated by UniqueObjectHelper. We'll use it
  1028. // to semi-test sparsetable as well. :-)
  1029. sparsetable<char*> g_unique_charstar_objects(16);
  1030. // This is an object-generator: pass in an index, and it will return a
  1031. // unique object of type ItemType. We provide specializations for the
  1032. // types we actually support.
  1033. template <typename ItemType> ItemType UniqueObjectHelper(int index);
  1034. template<> int UniqueObjectHelper(int index)
  1035. {
  1036. return index;
  1037. }
  1038. template<> string UniqueObjectHelper(int index)
  1039. {
  1040. char buffer[64];
  1041. snprintf(buffer, sizeof(buffer), "%d", index);
  1042. return buffer;
  1043. }
  1044. template<> char* UniqueObjectHelper(int index)
  1045. {
  1046. // First grow the table if need be.
  1047. sparsetable<char*>::size_type table_size = g_unique_charstar_objects.size();
  1048. while (index >= static_cast<int>(table_size)) {
  1049. assert(table_size * 2 > table_size); // avoid overflow problems
  1050. table_size *= 2;
  1051. }
  1052. if (table_size > g_unique_charstar_objects.size())
  1053. g_unique_charstar_objects.resize(table_size);
  1054. if (!g_unique_charstar_objects.test((size_t)index)) {
  1055. char buffer[64];
  1056. snprintf(buffer, sizeof(buffer), "%d", index);
  1057. g_unique_charstar_objects[(size_t)index] = _strdup(buffer);
  1058. }
  1059. return g_unique_charstar_objects.get((size_t)index);
  1060. }
  1061. template<> const char* UniqueObjectHelper(int index) {
  1062. return UniqueObjectHelper<char*>(index);
  1063. }
  1064. template<> ValueType UniqueObjectHelper(int index) {
  1065. return ValueType(UniqueObjectHelper<string>(index).c_str());
  1066. }
  1067. template<> pair<const int, int> UniqueObjectHelper(int index) {
  1068. return pair<const int,int>(index, index + 1);
  1069. }
  1070. template<> pair<const string, string> UniqueObjectHelper(int index)
  1071. {
  1072. return pair<const string,string>(
  1073. UniqueObjectHelper<string>(index), UniqueObjectHelper<string>(index + 1));
  1074. }
  1075. template<> pair<const char* const,ValueType> UniqueObjectHelper(int index)
  1076. {
  1077. return pair<const char* const,ValueType>(
  1078. UniqueObjectHelper<char*>(index), UniqueObjectHelper<ValueType>(index+1));
  1079. }
  1080. class ValueSerializer
  1081. {
  1082. public:
  1083. bool operator()(FILE* fp, const int& value) {
  1084. return fwrite(&value, sizeof(value), 1, fp) == 1;
  1085. }
  1086. bool operator()(FILE* fp, int* value) {
  1087. return fread(value, sizeof(*value), 1, fp) == 1;
  1088. }
  1089. bool operator()(FILE* fp, const string& value) {
  1090. const size_t size = value.size();
  1091. return (*this)(fp, (int)size) && fwrite(value.c_str(), size, 1, fp) == 1;
  1092. }
  1093. bool operator()(FILE* fp, string* value) {
  1094. int size;
  1095. if (!(*this)(fp, &size)) return false;
  1096. char* buf = new char[(size_t)size];
  1097. if (fread(buf, (size_t)size, 1, fp) != 1) {
  1098. delete[] buf;
  1099. return false;
  1100. }
  1101. new (value) string(buf, (size_t)size);
  1102. delete[] buf;
  1103. return true;
  1104. }
  1105. template <typename OUTPUT>
  1106. bool operator()(OUTPUT* fp, const ValueType& v) {
  1107. return (*this)(fp, string(v.s()));
  1108. }
  1109. template <typename INPUT>
  1110. bool operator()(INPUT* fp, ValueType* v) {
  1111. string data;
  1112. if (!(*this)(fp, &data)) return false;
  1113. new(v) ValueType(data.c_str());
  1114. return true;
  1115. }
  1116. template <typename OUTPUT>
  1117. bool operator()(OUTPUT* fp, const char* const& value) {
  1118. // Just store the index.
  1119. return (*this)(fp, atoi(value));
  1120. }
  1121. template <typename INPUT>
  1122. bool operator()(INPUT* fp, const char** value) {
  1123. // Look up via index.
  1124. int index;
  1125. if (!(*this)(fp, &index)) return false;
  1126. *value = UniqueObjectHelper<char*>(index);
  1127. return true;
  1128. }
  1129. template <typename OUTPUT, typename First, typename Second>
  1130. bool operator()(OUTPUT* fp, std::pair<const First, Second>* value) {
  1131. return (*this)(fp, const_cast<First*>(&value->first))
  1132. && (*this)(fp, &value->second);
  1133. }
  1134. template <typename INPUT, typename First, typename Second>
  1135. bool operator()(INPUT* fp, const std::pair<const First, Second>& value) {
  1136. return (*this)(fp, value.first) && (*this)(fp, value.second);
  1137. }
  1138. };
  1139. template <typename HashtableType>
  1140. class HashtableTest : public ::testing::Test
  1141. {
  1142. public:
  1143. HashtableTest() : ht_() { }
  1144. // Give syntactically-prettier access to UniqueObjectHelper.
  1145. typename HashtableType::value_type UniqueObject(int index) {
  1146. return UniqueObjectHelper<typename HashtableType::value_type>(index);
  1147. }
  1148. typename HashtableType::key_type UniqueKey(int index) {
  1149. return this->ht_.get_key(this->UniqueObject(index));
  1150. }
  1151. protected:
  1152. HashtableType ht_;
  1153. };
  1154. }
  1155. // These are used to specify the empty key and deleted key in some
  1156. // contexts. They can't be in the unnamed namespace, or static,
  1157. // because the template code requires external linkage.
  1158. extern const string kEmptyString("--empty string--");
  1159. extern const string kDeletedString("--deleted string--");
  1160. extern const int kEmptyInt = 0;
  1161. extern const int kDeletedInt = -1234676543; // an unlikely-to-pick int
  1162. extern const char* const kEmptyCharStar = "--empty char*--";
  1163. extern const char* const kDeletedCharStar = "--deleted char*--";
  1164. namespace {
  1165. #define INT_HASHTABLES \
  1166. HashtableInterface_SparseHashMap<int, int, Hasher, Hasher, \
  1167. Alloc<int> >, \
  1168. HashtableInterface_SparseHashSet<int, Hasher, Hasher, \
  1169. Alloc<int> >, \
  1170. /* This is a table where the key associated with a value is -value */ \
  1171. HashtableInterface_SparseHashtable<int, int, Hasher, Negation<int>, \
  1172. SetKey<int, Negation<int> >, \
  1173. Hasher, Alloc<int> >
  1174. #define STRING_HASHTABLES \
  1175. HashtableInterface_SparseHashMap<string, string, Hasher, Hasher, \
  1176. Alloc<string> >, \
  1177. HashtableInterface_SparseHashSet<string, Hasher, Hasher, \
  1178. Alloc<string> >, \
  1179. /* This is a table where the key associated with a value is Cap(value) */ \
  1180. HashtableInterface_SparseHashtable<string, string, Hasher, Capital, \
  1181. SetKey<string, Capital>, \
  1182. Hasher, Alloc<string> >
  1183. // ---------------------------------------------------------------------
  1184. // I'd like to use ValueType keys for SparseHashtable<> and
  1185. // DenseHashtable<> but I can't due to memory-management woes (nobody
  1186. // really owns the char* involved). So instead I do something simpler.
  1187. // ---------------------------------------------------------------------
  1188. #define CHARSTAR_HASHTABLES \
  1189. HashtableInterface_SparseHashMap<const char*, ValueType, \
  1190. Hasher, Hasher, Alloc<const char*> >, \
  1191. HashtableInterface_SparseHashSet<const char*, Hasher, Hasher, \
  1192. Alloc<const char*> >, \
  1193. HashtableInterface_SparseHashtable<const char*, const char*, \
  1194. Hasher, Identity, \
  1195. SetKey<const char*, Identity>, \
  1196. Hasher, Alloc<const char*> >
  1197. // ---------------------------------------------------------------------
  1198. // This is the list of types we run each test against.
  1199. // We need to define the same class 4 times due to limitations in the
  1200. // testing framework. Basically, we associate each class below with
  1201. // the set of types we want to run tests on it with.
  1202. // ---------------------------------------------------------------------
  1203. template <typename HashtableType> class HashtableIntTest
  1204. : public HashtableTest<HashtableType> { };
  1205. template <typename HashtableType> class HashtableStringTest
  1206. : public HashtableTest<HashtableType> { };
  1207. template <typename HashtableType> class HashtableCharStarTest
  1208. : public HashtableTest<HashtableType> { };
  1209. template <typename HashtableType> class HashtableAllTest
  1210. : public HashtableTest<HashtableType> { };
  1211. typedef testing::TypeList3<INT_HASHTABLES> IntHashtables;
  1212. typedef testing::TypeList3<STRING_HASHTABLES> StringHashtables;
  1213. typedef testing::TypeList3<CHARSTAR_HASHTABLES> CharStarHashtables;
  1214. typedef testing::TypeList9<INT_HASHTABLES, STRING_HASHTABLES,
  1215. CHARSTAR_HASHTABLES> AllHashtables;
  1216. TYPED_TEST_CASE_3(HashtableIntTest, IntHashtables);
  1217. TYPED_TEST_CASE_3(HashtableStringTest, StringHashtables);
  1218. TYPED_TEST_CASE_3(HashtableCharStarTest, CharStarHashtables);
  1219. TYPED_TEST_CASE_9(HashtableAllTest, AllHashtables);
  1220. // ------------------------------------------------------------------------
  1221. // First, some testing of the underlying infrastructure.
  1222. #if 0
  1223. TEST(HashtableCommonTest, HashMunging)
  1224. {
  1225. const Hasher hasher;
  1226. // We don't munge the hash value on non-pointer template types.
  1227. {
  1228. const sparsehash_internal::sh_hashtable_settings<int, Hasher, size_t, 1>
  1229. settings(hasher, 0.0, 0.0);
  1230. const int v = 1000;
  1231. EXPECT_EQ(hasher(v), settings.hash(v));
  1232. }
  1233. {
  1234. // We do munge the hash value on pointer template types.
  1235. const sparsehash_internal::sh_hashtable_settings<int*, Hasher, size_t, 1>
  1236. settings(hasher, 0.0, 0.0);
  1237. int* v = NULL;
  1238. v += 0x10000; // get a non-trivial pointer value
  1239. EXPECT_NE(hasher(v), settings.hash(v));
  1240. }
  1241. {
  1242. const sparsehash_internal::sh_hashtable_settings<const int*, Hasher,
  1243. size_t, 1>
  1244. settings(hasher, 0.0, 0.0);
  1245. const int* v = NULL;
  1246. v += 0x10000; // get a non-trivial pointer value
  1247. EXPECT_NE(hasher(v), settings.hash(v));
  1248. }
  1249. }
  1250. #endif
  1251. // ------------------------------------------------------------------------
  1252. // If the first arg to TYPED_TEST is HashtableIntTest, it will run
  1253. // this test on all the hashtable types, with key=int and value=int.
  1254. // Likewise, HashtableStringTest will have string key/values, and
  1255. // HashtableCharStarTest will have char* keys and -- just to mix it up
  1256. // a little -- ValueType values. HashtableAllTest will run all three
  1257. // key/value types on all 6 hashtables types, for 9 test-runs total
  1258. // per test.
  1259. //
  1260. // In addition, TYPED_TEST makes available the magic keyword
  1261. // TypeParam, which is the type being used for the current test.
  1262. // This first set of tests just tests the public API, going through
  1263. // the public typedefs and methods in turn. It goes approximately
  1264. // in the definition-order in sparse_hash_map.h.
  1265. // ------------------------------------------------------------------------
  1266. TYPED_TEST(HashtableIntTest, Typedefs)
  1267. {
  1268. // Make sure all the standard STL-y typedefs are defined. The exact
  1269. // key/value types don't matter here, so we only bother testing on
  1270. // the int tables. This is just a compile-time "test"; nothing here
  1271. // can fail at runtime.
  1272. this->ht_.set_deleted_key(-2); // just so deleted_key succeeds
  1273. typename TypeParam::key_type kt;
  1274. typename TypeParam::value_type vt;
  1275. typename TypeParam::hasher h;
  1276. typename TypeParam::key_equal ke;
  1277. typename TypeParam::allocator_type at;
  1278. typename TypeParam::size_type st;
  1279. typename TypeParam::difference_type dt;
  1280. typename TypeParam::pointer p;
  1281. typename TypeParam::const_pointer cp;
  1282. // I can't declare variables of reference-type, since I have nothing
  1283. // to point them to, so I just make sure that these types exist.
  1284. ATTRIBUTE_UNUSED typedef typename TypeParam::reference r;
  1285. ATTRIBUTE_UNUSED typedef typename TypeParam::const_reference cf;
  1286. typename TypeParam::iterator i;
  1287. typename TypeParam::const_iterator ci;
  1288. typename TypeParam::local_iterator li;
  1289. typename TypeParam::const_local_iterator cli;
  1290. // Now make sure the variables are used, so the compiler doesn't
  1291. // complain. Where possible, I "use" the variable by calling the
  1292. // method that's supposed to return the unique instance of the
  1293. // relevant type (eg. get_allocator()). Otherwise, I try to call a
  1294. // different, arbitrary function that returns the type. Sometimes
  1295. // the type isn't used at all, and there's no good way to use the
  1296. // variable.
  1297. kt = this->ht_.deleted_key();
  1298. (void)vt; // value_type may not be copyable. Easiest not to try.
  1299. h = this->ht_.hash_funct();
  1300. ke = this->ht_.key_eq();
  1301. at = this->ht_.get_allocator();
  1302. st = this->ht_.size();
  1303. (void)dt;
  1304. (void)p;
  1305. (void)cp;
  1306. i = this->ht_.begin();
  1307. ci = this->ht_.begin();
  1308. li = this->ht_.begin(0);
  1309. cli = this->ht_.begin(0);
  1310. }
  1311. TYPED_TEST(HashtableAllTest, NormalIterators)
  1312. {
  1313. EXPECT_TRUE(this->ht_.begin() == this->ht_.end());
  1314. this->ht_.insert(this->UniqueObject(1));
  1315. {
  1316. typename TypeParam::iterator it = this->ht_.begin();
  1317. EXPECT_TRUE(it != this->ht_.end());
  1318. ++it;
  1319. EXPECT_TRUE(it == this->ht_.end());
  1320. }
  1321. }
  1322. TEST(HashtableTest, ModifyViaIterator)
  1323. {
  1324. // This only works for hash-maps, since only they have non-const values.
  1325. {
  1326. sparse_hash_map<int, int> ht;
  1327. ht[1] = 2;
  1328. sparse_hash_map<int, int>::iterator it = ht.find(1);
  1329. EXPECT_TRUE(it != ht.end());
  1330. EXPECT_EQ(1, it->first);
  1331. EXPECT_EQ(2, it->second);
  1332. it->second = 5;
  1333. it = ht.find(1);
  1334. EXPECT_TRUE(it != ht.end());
  1335. EXPECT_EQ(5, it->second);
  1336. }
  1337. }
  1338. TYPED_TEST(HashtableAllTest, ConstIterators)
  1339. {
  1340. this->ht_.insert(this->UniqueObject(1));
  1341. typename TypeParam::const_iterator it = this->ht_.begin();
  1342. EXPECT_TRUE(it != (typename TypeParam::const_iterator)this->ht_.end());
  1343. ++it;
  1344. EXPECT_TRUE(it == (typename TypeParam::const_iterator)this->ht_.end());
  1345. }
  1346. TYPED_TEST(HashtableAllTest, LocalIterators)
  1347. {
  1348. // Now, tr1 begin/end (the local iterator that takes a bucket-number).
  1349. // ht::bucket() returns the bucket that this key would be inserted in.
  1350. this->ht_.insert(this->UniqueObject(1));
  1351. const typename TypeParam::size_type bucknum =
  1352. this->ht_.bucket(this->UniqueKey(1));
  1353. typename TypeParam::local_iterator b = this->ht_.begin(bucknum);
  1354. typename TypeParam::local_iterator e = this->ht_.end(bucknum);
  1355. EXPECT_TRUE(b != e);
  1356. b++;
  1357. EXPECT_TRUE(b == e);
  1358. // Check an empty bucket. We can just xor the bottom bit and be sure
  1359. // of getting a legal bucket, since #buckets is always a power of 2.
  1360. EXPECT_TRUE(this->ht_.begin(bucknum ^ 1) == this->ht_.end(bucknum ^ 1));
  1361. // Another test, this time making sure we're using the right types.
  1362. typename TypeParam::local_iterator b2 = this->ht_.begin(bucknum ^ 1);
  1363. typename TypeParam::local_iterator e2 = this->ht_.end(bucknum ^ 1);
  1364. EXPECT_TRUE(b2 == e2);
  1365. }
  1366. TYPED_TEST(HashtableAllTest, ConstLocalIterators)
  1367. {
  1368. this->ht_.insert(this->UniqueObject(1));
  1369. const typename TypeParam::size_type bucknum =
  1370. this->ht_.bucket(this->UniqueKey(1));
  1371. typename TypeParam::const_local_iterator b = this->ht_.begin(bucknum);
  1372. typename TypeParam::const_local_iterator e = this->ht_.end(bucknum);
  1373. EXPECT_TRUE(b != e);
  1374. b++;
  1375. EXPECT_TRUE(b == e);
  1376. typename TypeParam::const_local_iterator b2 = this->ht_.begin(bucknum ^ 1);
  1377. typename TypeParam::const_local_iterator e2 = this->ht_.end(bucknum ^ 1);
  1378. EXPECT_TRUE(b2 == e2);
  1379. }
  1380. TYPED_TEST(HashtableAllTest, Iterating)
  1381. {
  1382. // Test a bit more iterating than just one ++.
  1383. this->ht_.insert(this->UniqueObject(1));
  1384. this->ht_.insert(this->UniqueObject(11));
  1385. this->ht_.insert(this->UniqueObject(111));
  1386. this->ht_.insert(this->UniqueObject(1111));
  1387. this->ht_.insert(this->UniqueObject(11111));
  1388. this->ht_.insert(this->UniqueObject(111111));
  1389. this->ht_.insert(this->UniqueObject(1111111));
  1390. this->ht_.insert(this->UniqueObject(11111111));
  1391. this->ht_.insert(this->UniqueObject(111111111));
  1392. typename TypeParam::iterator it = this->ht_.begin();
  1393. for (int i = 1; i <= 9; i++) { // start at 1 so i is never 0
  1394. // && here makes it easier to tell what loop iteration the test failed on.
  1395. EXPECT_TRUE(i && (it++ != this->ht_.end()));
  1396. }
  1397. EXPECT_TRUE(it == this->ht_.end());
  1398. }
  1399. TYPED_TEST(HashtableIntTest, Constructors)
  1400. {
  1401. // The key/value types don't matter here, so I just test on one set
  1402. // of tables, the ones with int keys, which can easily handle the
  1403. // placement-news we have to do below.
  1404. Hasher hasher(1); // 1 is a unique id
  1405. int alloc_count = 0;
  1406. Alloc<typename TypeParam::key_type> alloc(2, &alloc_count);
  1407. TypeParam ht_noarg;
  1408. TypeParam ht_onearg(100);
  1409. TypeParam ht_twoarg(100, hasher);
  1410. TypeParam ht_threearg(100, hasher, hasher); // hasher serves as key_equal too
  1411. TypeParam ht_fourarg(100, hasher, hasher, alloc);
  1412. // The allocator should have been called at most once, for the last ht.
  1413. EXPECT_GE(1, alloc_count);
  1414. int old_alloc_count = alloc_count;
  1415. const typename TypeParam::value_type input[] = {
  1416. this->UniqueObject(1),
  1417. this->UniqueObject(2),
  1418. this->UniqueObject(4),
  1419. this->UniqueObject(8)
  1420. };
  1421. const int num_inputs = sizeof(input) / sizeof(input[0]);
  1422. const typename TypeParam::value_type *begin = &input[0];
  1423. const typename TypeParam::value_type *end = begin + num_inputs;
  1424. TypeParam ht_iter_noarg(begin, end);
  1425. TypeParam ht_iter_onearg(begin, end, 100);
  1426. TypeParam ht_iter_twoarg(begin, end, 100, hasher);
  1427. TypeParam ht_iter_threearg(begin, end, 100, hasher, hasher);
  1428. TypeParam ht_iter_fourarg(begin, end, 100, hasher, hasher, alloc);
  1429. // Now the allocator should have been called more.
  1430. EXPECT_GT(alloc_count, old_alloc_count);
  1431. old_alloc_count = alloc_count;
  1432. // Let's do a lot more inserting and make sure the alloc-count goes up
  1433. for (int i = 2; i < 2000; i++)
  1434. ht_fourarg.insert(this->UniqueObject(i));
  1435. EXPECT_GT(alloc_count, old_alloc_count);
  1436. EXPECT_LT(ht_noarg.bucket_count(), 100u);
  1437. EXPECT_GE(ht_onearg.bucket_count(), 100u);
  1438. EXPECT_GE(ht_twoarg.bucket_count(), 100u);
  1439. EXPECT_GE(ht_threearg.bucket_count(), 100u);
  1440. EXPECT_GE(ht_fourarg.bucket_count(), 100u);
  1441. EXPECT_GE(ht_iter_onearg.bucket_count(), 100u);
  1442. // When we pass in a hasher -- it can serve both as the hash-function
  1443. // and the key-equal function -- its id should be 1. Where we don't
  1444. // pass it in and use the default Hasher object, the id should be 0.
  1445. EXPECT_EQ(0, ht_noarg.hash_funct().id());
  1446. EXPECT_EQ(0, ht_noarg.key_eq().id());
  1447. EXPECT_EQ(0, ht_onearg.hash_funct().id());
  1448. EXPECT_EQ(0, ht_onearg.key_eq().id());
  1449. EXPECT_EQ(1, ht_twoarg.hash_funct().id());
  1450. EXPECT_EQ(0, ht_twoarg.key_eq().id());
  1451. EXPECT_EQ(1, ht_threearg.hash_funct().id());
  1452. EXPECT_EQ(1, ht_threearg.key_eq().id());
  1453. EXPECT_EQ(0, ht_iter_noarg.hash_funct().id());
  1454. EXPECT_EQ(0, ht_iter_noarg.key_eq().id());
  1455. EXPECT_EQ(0, ht_iter_onearg.hash_funct().id());
  1456. EXPECT_EQ(0, ht_iter_onearg.key_eq().id());
  1457. EXPECT_EQ(1, ht_iter_twoarg.hash_funct().id());
  1458. EXPECT_EQ(0, ht_iter_twoarg.key_eq().id());
  1459. EXPECT_EQ(1, ht_iter_threearg.hash_funct().id());
  1460. EXPECT_EQ(1, ht_iter_threearg.key_eq().id());
  1461. // Likewise for the allocator
  1462. EXPECT_EQ(0, ht_threearg.get_allocator().id());
  1463. EXPECT_EQ(0, ht_iter_threearg.get_allocator().id());
  1464. EXPECT_EQ(2, ht_fourarg.get_allocator().id());
  1465. EXPECT_EQ(2, ht_iter_fourarg.get_allocator().id());
  1466. }
  1467. TYPED_TEST(HashtableAllTest, OperatorEquals)
  1468. {
  1469. {
  1470. TypeParam ht1, ht2;
  1471. ht1.set_deleted_key(this->UniqueKey(1));
  1472. ht2.set_deleted_key(this->UniqueKey(2));
  1473. ht1.insert(this->UniqueObject(10));
  1474. ht2.insert(this->UniqueObject(20));
  1475. EXPECT_FALSE(ht1 == ht2);
  1476. ht1 = ht2;
  1477. EXPECT_TRUE(ht1 == ht2);
  1478. }
  1479. {
  1480. TypeParam ht1, ht2;
  1481. ht1.insert(this->UniqueObject(30));
  1482. ht1 = ht2;
  1483. EXPECT_EQ(0u, ht1.size());
  1484. }
  1485. {
  1486. TypeParam ht1, ht2;
  1487. ht1.set_deleted_key(this->UniqueKey(1));
  1488. ht2.insert(this->UniqueObject(1)); // has same key as ht1.delkey
  1489. ht1 = ht2; // should reset deleted-key to 'unset'
  1490. EXPECT_EQ(1u, ht1.size());
  1491. EXPECT_EQ(1u, ht1.count(this->UniqueKey(1)));
  1492. }
  1493. }
  1494. TYPED_TEST(HashtableAllTest, Clear)
  1495. {
  1496. for (int i = 1; i < 200; i++) {
  1497. this->ht_.insert(this->UniqueObject(i));
  1498. }
  1499. this->ht_.clear();
  1500. EXPECT_EQ(0u, this->ht_.size());
  1501. // TODO(csilvers): do we want to enforce that the hashtable has or
  1502. // has not shrunk? It does for dense_* but not sparse_*.
  1503. }
  1504. TYPED_TEST(HashtableAllTest, ClearNoResize)
  1505. {
  1506. if (!this->ht_.supports_clear_no_resize())
  1507. return;
  1508. typename TypeParam::size_type empty_bucket_count = this->ht_.bucket_count();
  1509. int last_element = 1;
  1510. while (this->ht_.bucket_count() == empty_bucket_count) {
  1511. this->ht_.insert(this->UniqueObject(last_element));
  1512. ++last_element;
  1513. }
  1514. typename TypeParam::size_type last_bucket_count = this->ht_.bucket_count();
  1515. this->ht_.clear_no_resize();
  1516. EXPECT_EQ(last_bucket_count, this->ht_.bucket_count());
  1517. EXPECT_TRUE(this->ht_.empty());
  1518. // When inserting the same number of elements again, no resize
  1519. // should be necessary.
  1520. for (int i = 1; i < last_element; ++i) {
  1521. this->ht_.insert(this->UniqueObject(last_element + i));
  1522. EXPECT_EQ(last_bucket_count, this->ht_.bucket_count());
  1523. }
  1524. }
  1525. TYPED_TEST(HashtableAllTest, Swap)
  1526. {
  1527. // Let's make a second hashtable with its own hasher, key_equal, etc.
  1528. Hasher hasher(1); // 1 is a unique id
  1529. TypeParam other_ht(200, hasher, hasher);
  1530. this->ht_.set_deleted_key(this->UniqueKey(1));
  1531. other_ht.set_deleted_key(this->UniqueKey(2));
  1532. for (int i = 3; i < 2000; i++) {
  1533. this->ht_.insert(this->UniqueObject(i));
  1534. }
  1535. this->ht_.erase(this->UniqueKey(1000));
  1536. other_ht.insert(this->UniqueObject(2001));
  1537. typename TypeParam::size_type expected_buckets = other_ht.bucket_count();
  1538. this->ht_.swap(other_ht);
  1539. EXPECT_EQ(this->UniqueKey(2), this->ht_.deleted_key());
  1540. EXPECT_EQ(this->UniqueKey(1), other_ht.deleted_key());
  1541. EXPECT_EQ(1, this->ht_.hash_funct().id());
  1542. EXPECT_EQ(0, other_ht.hash_funct().id());
  1543. EXPECT_EQ(1, this->ht_.key_eq().id());
  1544. EXPECT_EQ(0, other_ht.key_eq().id());
  1545. EXPECT_EQ(expected_buckets, this->ht_.bucket_count());
  1546. EXPECT_GT(other_ht.bucket_count(), 200u);
  1547. EXPECT_EQ(1u, this->ht_.size());
  1548. EXPECT_EQ(1996u, other_ht.size()); // because we erased 1000
  1549. EXPECT_EQ(0u, this->ht_.count(this->UniqueKey(111)));
  1550. EXPECT_EQ(1u, other_ht.count(this->UniqueKey(111)));
  1551. EXPECT_EQ(1u, this->ht_.count(this->UniqueKey(2001)));
  1552. EXPECT_EQ(0u, other_ht.count(this->UniqueKey(2001)));
  1553. EXPECT_EQ(0u, this->ht_.count(this->UniqueKey(1000)));
  1554. EXPECT_EQ(0u, other_ht.count(this->UniqueKey(1000)));
  1555. // We purposefully don't swap allocs -- they're not necessarily swappable.
  1556. // Now swap back, using the free-function swap
  1557. // NOTE: MSVC seems to have trouble with this free swap, not quite
  1558. // sure why. I've given up trying to fix it though.
  1559. #ifdef _MSC_VER
  1560. other_ht.swap(this->ht_);
  1561. #else
  1562. std::swap(this->ht_, other_ht);
  1563. #endif
  1564. EXPECT_EQ(this->UniqueKey(1), this->ht_.deleted_key());
  1565. EXPECT_EQ(this->UniqueKey(2), other_ht.deleted_key());
  1566. EXPECT_EQ(0, this->ht_.hash_funct().id());
  1567. EXPECT_EQ(1, other_ht.hash_funct().id());
  1568. EXPECT_EQ(1996u, this->ht_.size());
  1569. EXPECT_EQ(1u, other_ht.size());
  1570. EXPECT_EQ(1u, this->ht_.count(this->UniqueKey(111)));
  1571. EXPECT_EQ(0u, other_ht.count(this->UniqueKey(111)));
  1572. // A user reported a crash with this code using swap to clear.
  1573. // We've since fixed the bug; this prevents a regression.
  1574. TypeParam swap_to_clear_ht;
  1575. swap_to_clear_ht.set_deleted_key(this->UniqueKey(1));
  1576. for (int i = 2; i < 10000; ++i) {
  1577. swap_to_clear_ht.insert(this->UniqueObject(i));
  1578. }
  1579. TypeParam empty_ht;
  1580. empty_ht.swap(swap_to_clear_ht);
  1581. swap_to_clear_ht.set_deleted_key(this->UniqueKey(1));
  1582. for (int i = 2; i < 10000; ++i) {
  1583. swap_to_clear_ht.insert(this->UniqueObject(i));
  1584. }
  1585. }
  1586. TYPED_TEST(HashtableAllTest, Size)
  1587. {
  1588. EXPECT_EQ(0u, this->ht_.size());
  1589. for (int i = 1; i < 1000; i++) { // go through some resizes
  1590. this->ht_.insert(this->UniqueObject(i));
  1591. EXPECT_EQ(static_cast<typename TypeParam::size_type>(i), this->ht_.size());
  1592. }
  1593. this->ht_.clear();
  1594. EXPECT_EQ(0u, this->ht_.size());
  1595. this->ht_.set_deleted_key(this->UniqueKey(1));
  1596. EXPECT_EQ(0u, this->ht_.size()); // deleted key doesn't count
  1597. for (int i = 2; i < 1000; i++) { // go through some resizes
  1598. this->ht_.insert(this->UniqueObject(i));
  1599. this->ht_.erase(this->UniqueKey(i));
  1600. EXPECT_EQ(0u, this->ht_.size());
  1601. }
  1602. }
  1603. TEST(HashtableTest, MaxSizeAndMaxBucketCount)
  1604. {
  1605. // The max size depends on the allocator. So we can't use the
  1606. // built-in allocator type; instead, we make our own types.
  1607. sparse_hash_set<int, Hasher, Hasher, Alloc<int> > ht_default;
  1608. sparse_hash_set<int, Hasher, Hasher, Alloc<int, unsigned char> > ht_char;
  1609. sparse_hash_set<int, Hasher, Hasher, Alloc<int, unsigned char, 104> > ht_104;
  1610. EXPECT_GE(ht_default.max_size(), 256u);
  1611. EXPECT_EQ(255u, ht_char.max_size());
  1612. EXPECT_EQ(104u, ht_104.max_size());
  1613. // In our implementations, MaxBucketCount == MaxSize.
  1614. EXPECT_EQ(ht_default.max_size(), ht_default.max_bucket_count());
  1615. EXPECT_EQ(ht_char.max_size(), ht_char.max_bucket_count());
  1616. EXPECT_EQ(ht_104.max_size(), ht_104.max_bucket_count());
  1617. }
  1618. TYPED_TEST(HashtableAllTest, Empty)
  1619. {
  1620. EXPECT_TRUE(this->ht_.empty());
  1621. this->ht_.insert(this->UniqueObject(1));
  1622. EXPECT_FALSE(this->ht_.empty());
  1623. this->ht_.clear();
  1624. EXPECT_TRUE(this->ht_.empty());
  1625. TypeParam empty_ht;
  1626. this->ht_.insert(this->UniqueObject(1));
  1627. this->ht_.swap(empty_ht);
  1628. EXPECT_TRUE(this->ht_.empty());
  1629. }
  1630. TYPED_TEST(HashtableAllTest, BucketCount)
  1631. {
  1632. TypeParam ht(100);
  1633. // constructor arg is number of *items* to be inserted, not the
  1634. // number of buckets, so we expect more buckets.
  1635. EXPECT_GT(ht.bucket_count(), 100u);
  1636. for (int i = 1; i < 200; i++) {
  1637. ht.insert(this->UniqueObject(i));
  1638. }
  1639. EXPECT_GT(ht.bucket_count(), 200u);
  1640. }
  1641. TYPED_TEST(HashtableAllTest, BucketAndBucketSize)
  1642. {
  1643. const typename TypeParam::size_type expected_bucknum = this->ht_.bucket(
  1644. this->UniqueKey(1));
  1645. EXPECT_EQ(0u, this->ht_.bucket_size(expected_bucknum));
  1646. this->ht_.insert(this->UniqueObject(1));
  1647. EXPECT_EQ(expected_bucknum, this->ht_.bucket(this->UniqueKey(1)));
  1648. EXPECT_EQ(1u, this->ht_.bucket_size(expected_bucknum));
  1649. // Check that a bucket we didn't insert into, has a 0 size. Since
  1650. // we have an even number of buckets, bucknum^1 is guaranteed in range.
  1651. EXPECT_EQ(0u, this->ht_.bucket_size(expected_bucknum ^ 1));
  1652. }
  1653. TYPED_TEST(HashtableAllTest, LoadFactor)
  1654. {
  1655. const typename TypeParam::size_type kSize = 16536;
  1656. // Check growing past various thresholds and then shrinking below
  1657. // them.
  1658. for (float grow_threshold = 0.2f;
  1659. grow_threshold <= 0.8f;
  1660. grow_threshold += 0.2f)
  1661. {
  1662. TypeParam ht;
  1663. ht.set_deleted_key(this->UniqueKey(1));
  1664. ht.max_load_factor(grow_threshold);
  1665. ht.min_load_factor(0.0);
  1666. EXPECT_EQ(grow_threshold, ht.max_load_factor());
  1667. EXPECT_EQ(0.0, ht.min_load_factor());
  1668. ht.resize(kSize);
  1669. size_t bucket_count = ht.bucket_count();
  1670. // Erase and insert an element to set consider_shrink = true,
  1671. // which should not cause a shrink because the threshold is 0.0.
  1672. ht.insert(this->UniqueObject(2));
  1673. ht.erase(this->UniqueKey(2));
  1674. for (int i = 2;; ++i)
  1675. {
  1676. ht.insert(this->UniqueObject(i));
  1677. if (static_cast<float>(ht.size())/bucket_count < grow_threshold) {
  1678. EXPECT_EQ(bucket_count, ht.bucket_count());
  1679. } else {
  1680. EXPECT_GT(ht.bucket_count(), bucket_count);
  1681. break;
  1682. }
  1683. }
  1684. // Now set a shrink threshold 1% below the current size and remove
  1685. // items until the size falls below that.
  1686. const float shrink_threshold = static_cast<float>(ht.size()) /
  1687. ht.bucket_count() - 0.01f;
  1688. // This time around, check the old set_resizing_parameters interface.
  1689. ht.set_resizing_parameters(shrink_threshold, 1.0);
  1690. EXPECT_EQ(1.0, ht.max_load_factor());
  1691. EXPECT_EQ(shrink_threshold, ht.min_load_factor());
  1692. bucket_count = ht.bucket_count();
  1693. for (int i = 2;; ++i)
  1694. {
  1695. ht.erase(this->UniqueKey(i));
  1696. // A resize is only triggered by an insert, so add and remove a
  1697. // value every iteration to trigger the shrink as soon as the
  1698. // threshold is passed.
  1699. ht.erase(this->UniqueKey(i+1));
  1700. ht.insert(this->UniqueObject(i+1));
  1701. if (static_cast<float>(ht.size())/bucket_count > shrink_threshold) {
  1702. EXPECT_EQ(bucket_count, ht.bucket_count());
  1703. } else {
  1704. EXPECT_LT(ht.bucket_count(), bucket_count);
  1705. break;
  1706. }
  1707. }
  1708. }
  1709. }
  1710. TYPED_TEST(HashtableAllTest, ResizeAndRehash)
  1711. {
  1712. // resize() and rehash() are synonyms. rehash() is the tr1 name.
  1713. TypeParam ht(10000);
  1714. ht.max_load_factor(0.8f); // for consistency's sake
  1715. for (int i = 1; i < 100; ++i)
  1716. ht.insert(this->UniqueObject(i));
  1717. ht.resize(0);
  1718. // Now ht should be as small as possible.
  1719. EXPECT_LT(ht.bucket_count(), 300u);
  1720. ht.rehash(9000); // use the 'rehash' version of the name.
  1721. // Bucket count should be next power of 2, after considering max_load_factor.
  1722. EXPECT_EQ(16384u, ht.bucket_count());
  1723. for (int i = 101; i < 200; ++i)
  1724. ht.insert(this->UniqueObject(i));
  1725. // Adding a few hundred buckets shouldn't have caused a resize yet.
  1726. EXPECT_EQ(ht.bucket_count(), 16384u);
  1727. }
  1728. TYPED_TEST(HashtableAllTest, FindAndCountAndEqualRange)
  1729. {
  1730. pair<typename TypeParam::iterator, typename TypeParam::iterator> eq_pair;
  1731. pair<typename TypeParam::const_iterator,
  1732. typename TypeParam::const_iterator> const_eq_pair;
  1733. EXPECT_TRUE(this->ht_.empty());
  1734. EXPECT_TRUE(this->ht_.find(this->UniqueKey(1)) == this->ht_.end());
  1735. EXPECT_EQ(0u, this->ht_.count(this->UniqueKey(1)));
  1736. eq_pair = this->ht_.equal_range(this->UniqueKey(1));
  1737. EXPECT_TRUE(eq_pair.first == eq_pair.second);
  1738. this->ht_.insert(this->UniqueObject(1));
  1739. EXPECT_FALSE(this->ht_.empty());
  1740. this->ht_.insert(this->UniqueObject(11));
  1741. this->ht_.insert(this->UniqueObject(111));
  1742. this->ht_.insert(this->UniqueObject(1111));
  1743. this->ht_.insert(this->UniqueObject(11111));
  1744. this->ht_.insert(this->UniqueObject(111111));
  1745. this->ht_.insert(this->UniqueObject(1111111));
  1746. this->ht_.insert(this->UniqueObject(11111111));
  1747. this->ht_.insert(this->UniqueObject(111111111));
  1748. EXPECT_EQ(9u, this->ht_.size());
  1749. typename TypeParam::const_iterator it = this->ht_.find(this->UniqueKey(1));
  1750. EXPECT_EQ(it.key(), this->UniqueKey(1));
  1751. // Allow testing the const version of the methods as well.
  1752. const TypeParam ht = this->ht_;
  1753. // Some successful lookups (via find, count, and equal_range).
  1754. EXPECT_TRUE(this->ht_.find(this->UniqueKey(1)) != this->ht_.end());
  1755. EXPECT_EQ(1u, this->ht_.count(this->UniqueKey(1)));
  1756. eq_pair = this->ht_.equal_range(this->UniqueKey(1));
  1757. EXPECT_TRUE(eq_pair.first != eq_pair.second);
  1758. EXPECT_EQ(eq_pair.first.key(), this->UniqueKey(1));
  1759. ++eq_pair.first;
  1760. EXPECT_TRUE(eq_pair.first == eq_pair.second);
  1761. EXPECT_TRUE(ht.find(this->UniqueKey(1)) != ht.end());
  1762. EXPECT_EQ(1u, ht.count(this->UniqueKey(1)));
  1763. const_eq_pair = ht.equal_range(this->UniqueKey(1));
  1764. EXPECT_TRUE(const_eq_pair.first != const_eq_pair.second);
  1765. EXPECT_EQ(const_eq_pair.first.key(), this->UniqueKey(1));
  1766. ++const_eq_pair.first;
  1767. EXPECT_TRUE(const_eq_pair.first == const_eq_pair.second);
  1768. EXPECT_TRUE(this->ht_.find(this->UniqueKey(11111)) != this->ht_.end());
  1769. EXPECT_EQ(1u, this->ht_.count(this->UniqueKey(11111)));
  1770. eq_pair = this->ht_.equal_range(this->UniqueKey(11111));
  1771. EXPECT_TRUE(eq_pair.first != eq_pair.second);
  1772. EXPECT_EQ(eq_pair.first.key(), this->UniqueKey(11111));
  1773. ++eq_pair.first;
  1774. EXPECT_TRUE(eq_pair.first == eq_pair.second);
  1775. EXPECT_TRUE(ht.find(this->UniqueKey(11111)) != ht.end());
  1776. EXPECT_EQ(1u, ht.count(this->UniqueKey(11111)));
  1777. const_eq_pair = ht.equal_range(this->UniqueKey(11111));
  1778. EXPECT_TRUE(const_eq_pair.first != const_eq_pair.second);
  1779. EXPECT_EQ(const_eq_pair.first.key(), this->UniqueKey(11111));
  1780. ++const_eq_pair.first;
  1781. EXPECT_TRUE(const_eq_pair.first == const_eq_pair.second);
  1782. // Some unsuccessful lookups (via find, count, and equal_range).
  1783. EXPECT_TRUE(this->ht_.find(this->UniqueKey(11112)) == this->ht_.end());
  1784. EXPECT_EQ(0u, this->ht_.count(this->UniqueKey(11112)));
  1785. eq_pair = this->ht_.equal_range(this->UniqueKey(11112));
  1786. EXPECT_TRUE(eq_pair.first == eq_pair.second);
  1787. EXPECT_TRUE(ht.find(this->UniqueKey(11112)) == ht.end());
  1788. EXPECT_EQ(0u, ht.count(this->UniqueKey(11112)));
  1789. const_eq_pair = ht.equal_range(this->UniqueKey(11112));
  1790. EXPECT_TRUE(const_eq_pair.first == const_eq_pair.second);
  1791. EXPECT_TRUE(this->ht_.find(this->UniqueKey(11110)) == this->ht_.end());
  1792. EXPECT_EQ(0u, this->ht_.count(this->UniqueKey(11110)));
  1793. eq_pair = this->ht_.equal_range(this->UniqueKey(11110));
  1794. EXPECT_TRUE(eq_pair.first == eq_pair.second);
  1795. EXPECT_TRUE(ht.find(this->UniqueKey(11110)) == ht.end());
  1796. EXPECT_EQ(0u, ht.count(this->UniqueKey(11110)));
  1797. const_eq_pair = ht.equal_range(this->UniqueKey(11110));
  1798. EXPECT_TRUE(const_eq_pair.first == const_eq_pair.second);
  1799. }
  1800. TYPED_TEST(HashtableAllTest, BracketInsert)
  1801. {
  1802. // tests operator[], for those types that support it.
  1803. if (!this->ht_.supports_brackets())
  1804. return;
  1805. // bracket_equal is equivalent to ht_[a] == b. It should insert a if
  1806. // it doesn't already exist.
  1807. EXPECT_TRUE(this->ht_.bracket_equal(this->UniqueKey(1),
  1808. this->ht_.default_data()));
  1809. EXPECT_TRUE(this->ht_.find(this->UniqueKey(1)) != this->ht_.end());
  1810. // bracket_assign is equivalent to ht_[a] = b.
  1811. this->ht_.bracket_assign(this->UniqueKey(2),
  1812. this->ht_.get_data(this->UniqueObject(4)));
  1813. EXPECT_TRUE(this->ht_.find(this->UniqueKey(2)) != this->ht_.end());
  1814. EXPECT_TRUE(this->ht_.bracket_equal(
  1815. this->UniqueKey(2), this->ht_.get_data(this->UniqueObject(4))));
  1816. this->ht_.bracket_assign(
  1817. this->UniqueKey(2), this->ht_.get_data(this->UniqueObject(6)));
  1818. EXPECT_TRUE(this->ht_.bracket_equal(
  1819. this->UniqueKey(2), this->ht_.get_data(this->UniqueObject(6))));
  1820. // bracket_equal shouldn't have modified the value.
  1821. EXPECT_TRUE(this->ht_.bracket_equal(
  1822. this->UniqueKey(2), this->ht_.get_data(this->UniqueObject(6))));
  1823. // Verify that an operator[] that doesn't cause a resize, also
  1824. // doesn't require an extra rehash.
  1825. TypeParam ht(100);
  1826. EXPECT_EQ(0, ht.hash_funct().num_hashes());
  1827. ht.bracket_assign(this->UniqueKey(2), ht.get_data(this->UniqueObject(2)));
  1828. EXPECT_EQ(1, ht.hash_funct().num_hashes());
  1829. // And overwriting, likewise, should only cause one extra hash.
  1830. ht.bracket_assign(this->UniqueKey(2), ht.get_data(this->UniqueObject(2)));
  1831. EXPECT_EQ(2, ht.hash_funct().num_hashes());
  1832. }
  1833. TYPED_TEST(HashtableAllTest, InsertValue)
  1834. {
  1835. // First, try some straightforward insertions.
  1836. EXPECT_TRUE(this->ht_.empty());
  1837. this->ht_.insert(this->UniqueObject(1));
  1838. EXPECT_FALSE(this->ht_.empty());
  1839. this->ht_.insert(this->UniqueObject(11));
  1840. this->ht_.insert(this->UniqueObject(111));
  1841. this->ht_.insert(this->UniqueObject(1111));
  1842. this->ht_.insert(this->UniqueObject(11111));
  1843. this->ht_.insert(this->UniqueObject(111111));
  1844. this->ht_.insert(this->UniqueObject(1111111));
  1845. this->ht_.insert(this->UniqueObject(11111111));
  1846. this->ht_.insert(this->UniqueObject(111111111));
  1847. EXPECT_EQ(9u, this->ht_.size());
  1848. EXPECT_EQ(1u, this->ht_.count(this->UniqueKey(1)));
  1849. EXPECT_EQ(1u, this->ht_.count(this->UniqueKey(1111)));
  1850. // Check the return type.
  1851. pair<typename TypeParam::iterator, bool> insert_it;
  1852. insert_it = this->ht_.insert(this->UniqueObject(1));
  1853. EXPECT_EQ(false, insert_it.second); // false: already present
  1854. EXPECT_TRUE(*insert_it.first == this->UniqueObject(1));
  1855. insert_it = this->ht_.insert(this->UniqueObject(2));
  1856. EXPECT_EQ(true, insert_it.second); // true: not already present
  1857. EXPECT_TRUE(*insert_it.first == this->UniqueObject(2));
  1858. }
  1859. TYPED_TEST(HashtableIntTest, InsertRange)
  1860. {
  1861. // We just test the ints here, to make the placement-new easier.
  1862. TypeParam ht_source;
  1863. ht_source.insert(this->UniqueObject(10));
  1864. ht_source.insert(this->UniqueObject(100));
  1865. ht_source.insert(this->UniqueObject(1000));
  1866. ht_source.insert(this->UniqueObject(10000));
  1867. ht_source.insert(this->UniqueObject(100000));
  1868. ht_source.insert(this->UniqueObject(1000000));
  1869. const typename TypeParam::value_type input[] = {
  1870. // This is a copy of the first element in ht_source.
  1871. *ht_source.begin(),
  1872. this->UniqueObject(2),
  1873. this->UniqueObject(4),
  1874. this->UniqueObject(8)
  1875. };
  1876. set<typename TypeParam::value_type> set_input;
  1877. set_input.insert(this->UniqueObject(1111111));
  1878. set_input.insert(this->UniqueObject(111111));
  1879. set_input.insert(this->UniqueObject(11111));
  1880. set_input.insert(this->UniqueObject(1111));
  1881. set_input.insert(this->UniqueObject(111));
  1882. set_input.insert(this->UniqueObject(11));
  1883. // Insert from ht_source, an iterator of the same type as us.
  1884. typename TypeParam::const_iterator begin = ht_source.begin();
  1885. typename TypeParam::const_iterator end = begin;
  1886. std::advance(end, 3);
  1887. this->ht_.insert(begin, end); // insert 3 elements from ht_source
  1888. EXPECT_EQ(3u, this->ht_.size());
  1889. EXPECT_TRUE(*this->ht_.begin() == this->UniqueObject(10) ||
  1890. *this->ht_.begin() == this->UniqueObject(100) ||
  1891. *this->ht_.begin() == this->UniqueObject(1000) ||
  1892. *this->ht_.begin() == this->UniqueObject(10000) ||
  1893. *this->ht_.begin() == this->UniqueObject(100000) ||
  1894. *this->ht_.begin() == this->UniqueObject(1000000));
  1895. // And insert from set_input, a separate, non-random-access iterator.
  1896. typename set<typename TypeParam::value_type>::const_iterator set_begin;
  1897. typename set<typename TypeParam::value_type>::const_iterator set_end;
  1898. set_begin = set_input.begin();
  1899. set_end = set_begin;
  1900. std::advance(set_end, 3);
  1901. this->ht_.insert(set_begin, set_end);
  1902. EXPECT_EQ(6u, this->ht_.size());
  1903. // Insert from input as well, a separate, random-access iterator.
  1904. // The first element of input overlaps with an existing element
  1905. // of ht_, so this should only up the size by 2.
  1906. this->ht_.insert(&input[0], &input[3]);
  1907. EXPECT_EQ(8u, this->ht_.size());
  1908. }
  1909. TEST(HashtableTest, InsertValueToMap)
  1910. {
  1911. // For the maps in particular, ensure that inserting doesn't change
  1912. // the value.
  1913. sparse_hash_map<int, int> shm;
  1914. pair<sparse_hash_map<int,int>::iterator, bool> shm_it;
  1915. shm[1] = 2; // test a different method of inserting
  1916. shm_it = shm.insert(pair<int, int>(1, 3));
  1917. EXPECT_EQ(false, shm_it.second);
  1918. EXPECT_EQ(1, shm_it.first->first);
  1919. EXPECT_EQ(2, shm_it.first->second);
  1920. shm_it.first->second = 20;
  1921. EXPECT_EQ(20, shm[1]);
  1922. shm_it = shm.insert(pair<int, int>(2, 4));
  1923. EXPECT_EQ(true, shm_it.second);
  1924. EXPECT_EQ(2, shm_it.first->first);
  1925. EXPECT_EQ(4, shm_it.first->second);
  1926. EXPECT_EQ(4, shm[2]);
  1927. }
  1928. TYPED_TEST(HashtableStringTest, EmptyKey)
  1929. {
  1930. // Only run the string tests, to make it easier to know what the
  1931. // empty key should be.
  1932. if (!this->ht_.supports_empty_key())
  1933. return;
  1934. EXPECT_EQ(kEmptyString, this->ht_.empty_key());
  1935. }
  1936. TYPED_TEST(HashtableAllTest, DeletedKey)
  1937. {
  1938. if (!this->ht_.supports_deleted_key())
  1939. return;
  1940. this->ht_.insert(this->UniqueObject(10));
  1941. this->ht_.insert(this->UniqueObject(20));
  1942. this->ht_.set_deleted_key(this->UniqueKey(1));
  1943. EXPECT_EQ(this->ht_.deleted_key(), this->UniqueKey(1));
  1944. EXPECT_EQ(2u, this->ht_.size());
  1945. this->ht_.erase(this->UniqueKey(20));
  1946. EXPECT_EQ(1u, this->ht_.size());
  1947. // Changing the deleted key is fine.
  1948. this->ht_.set_deleted_key(this->UniqueKey(2));
  1949. EXPECT_EQ(this->ht_.deleted_key(), this->UniqueKey(2));
  1950. EXPECT_EQ(1u, this->ht_.size());
  1951. }
  1952. TYPED_TEST(HashtableAllTest, Erase)
  1953. {
  1954. this->ht_.set_deleted_key(this->UniqueKey(1));
  1955. EXPECT_EQ(0u, this->ht_.erase(this->UniqueKey(20)));
  1956. this->ht_.insert(this->UniqueObject(10));
  1957. this->ht_.insert(this->UniqueObject(20));
  1958. EXPECT_EQ(1u, this->ht_.erase(this->UniqueKey(20)));
  1959. EXPECT_EQ(1u, this->ht_.size());
  1960. EXPECT_EQ(0u, this->ht_.erase(this->UniqueKey(20)));
  1961. EXPECT_EQ(1u, this->ht_.size());
  1962. EXPECT_EQ(0u, this->ht_.erase(this->UniqueKey(19)));
  1963. EXPECT_EQ(1u, this->ht_.size());
  1964. typename TypeParam::iterator it = this->ht_.find(this->UniqueKey(10));
  1965. EXPECT_TRUE(it != this->ht_.end());
  1966. this->ht_.erase(it);
  1967. EXPECT_EQ(0u, this->ht_.size());
  1968. for (int i = 10; i < 100; i++)
  1969. this->ht_.insert(this->UniqueObject(i));
  1970. EXPECT_EQ(90u, this->ht_.size());
  1971. this->ht_.erase(this->ht_.begin(), this->ht_.end());
  1972. EXPECT_EQ(0u, this->ht_.size());
  1973. }
  1974. TYPED_TEST(HashtableAllTest, EraseDoesNotResize)
  1975. {
  1976. this->ht_.set_deleted_key(this->UniqueKey(1));
  1977. for (int i = 10; i < 2000; i++) {
  1978. this->ht_.insert(this->UniqueObject(i));
  1979. }
  1980. const typename TypeParam::size_type old_count = this->ht_.bucket_count();
  1981. for (int i = 10; i < 1000; i++) { // erase half one at a time
  1982. EXPECT_EQ(1u, this->ht_.erase(this->UniqueKey(i)));
  1983. }
  1984. this->ht_.erase(this->ht_.begin(), this->ht_.end()); // and the rest at once
  1985. EXPECT_EQ(0u, this->ht_.size());
  1986. EXPECT_EQ(old_count, this->ht_.bucket_count());
  1987. }
  1988. TYPED_TEST(HashtableAllTest, Equals)
  1989. {
  1990. // The real test here is whether two hashtables are equal if they
  1991. // have the same items but in a different order.
  1992. TypeParam ht1;
  1993. TypeParam ht2;
  1994. EXPECT_TRUE(ht1 == ht1);
  1995. EXPECT_FALSE(ht1 != ht1);
  1996. EXPECT_TRUE(ht1 == ht2);
  1997. EXPECT_FALSE(ht1 != ht2);
  1998. ht1.set_deleted_key(this->UniqueKey(1));
  1999. // Only the contents affect equality, not things like deleted-key.
  2000. EXPECT_TRUE(ht1 == ht2);
  2001. EXPECT_FALSE(ht1 != ht2);
  2002. ht1.resize(2000);
  2003. EXPECT_TRUE(ht1 == ht2);
  2004. // The choice of allocator/etc doesn't matter either.
  2005. Hasher hasher(1);
  2006. Alloc<typename TypeParam::key_type> alloc(2, NULL);
  2007. TypeParam ht3(5, hasher, hasher, alloc);
  2008. EXPECT_TRUE(ht1 == ht3);
  2009. EXPECT_FALSE(ht1 != ht3);
  2010. ht1.insert(this->UniqueObject(2));
  2011. EXPECT_TRUE(ht1 != ht2);
  2012. EXPECT_FALSE(ht1 == ht2); // this should hold as well!
  2013. ht2.insert(this->UniqueObject(2));
  2014. EXPECT_TRUE(ht1 == ht2);
  2015. for (int i = 3; i <= 2000; i++) {
  2016. ht1.insert(this->UniqueObject(i));
  2017. }
  2018. for (int i = 2000; i >= 3; i--) {
  2019. ht2.insert(this->UniqueObject(i));
  2020. }
  2021. EXPECT_TRUE(ht1 == ht2);
  2022. }
  2023. TEST(HashtableTest, IntIO)
  2024. {
  2025. // Since the set case is just a special (easier) case than the map case, I
  2026. // just test on sparse_hash_map. This handles the easy case where we can
  2027. // use the standard reader and writer.
  2028. sparse_hash_map<int, int> ht_out;
  2029. ht_out.set_deleted_key(0);
  2030. for (int i = 1; i < 1000; i++) {
  2031. ht_out[i] = i * i;
  2032. }
  2033. ht_out.erase(563); // just to test having some erased keys when we write.
  2034. ht_out.erase(22);
  2035. string file(TmpFile("intio"));
  2036. FILE* fp = fopen(file.c_str(), "wb");
  2037. if (fp)
  2038. {
  2039. EXPECT_TRUE(fp != NULL);
  2040. EXPECT_TRUE(ht_out.write_metadata(fp));
  2041. EXPECT_TRUE(ht_out.write_nopointer_data(fp));
  2042. fclose(fp);
  2043. }
  2044. sparse_hash_map<int, int> ht_in;
  2045. fp = fopen(file.c_str(), "rb");
  2046. if (fp)
  2047. {
  2048. EXPECT_TRUE(fp != NULL);
  2049. EXPECT_TRUE(ht_in.read_metadata(fp));
  2050. EXPECT_TRUE(ht_in.read_nopointer_data(fp));
  2051. fclose(fp);
  2052. }
  2053. EXPECT_EQ(1, ht_in[1]);
  2054. EXPECT_EQ(998001, ht_in[999]);
  2055. EXPECT_EQ(100, ht_in[10]);
  2056. EXPECT_EQ(441, ht_in[21]);
  2057. EXPECT_EQ(0, ht_in[22]); // should not have been saved
  2058. EXPECT_EQ(0, ht_in[563]);
  2059. }
  2060. TEST(HashtableTest, StringIO)
  2061. {
  2062. // Since the set case is just a special (easier) case than the map case,
  2063. // I just test on sparse_hash_map. This handles the difficult case where
  2064. // we have to write our own custom reader/writer for the data.
  2065. typedef sparse_hash_map<string, string, Hasher, Hasher> SP;
  2066. SP ht_out;
  2067. ht_out.set_deleted_key(string(""));
  2068. for (int i = 32; i < 128; i++) {
  2069. // This maps 'a' to 32 a's, 'b' to 33 b's, etc.
  2070. ht_out[string(1, (char)i)] = string((size_t)i, (char)i);
  2071. }
  2072. ht_out.erase("c"); // just to test having some erased keys when we write.
  2073. ht_out.erase("y");
  2074. string file(TmpFile("stringio"));
  2075. FILE* fp = fopen(file.c_str(), "wb");
  2076. if (fp)
  2077. {
  2078. EXPECT_TRUE(fp != NULL);
  2079. EXPECT_TRUE(ht_out.write_metadata(fp));
  2080. for (SP::const_iterator it = ht_out.cbegin(); it != ht_out.cend(); ++it)
  2081. {
  2082. const string::size_type first_size = it->first.length();
  2083. fwrite(&first_size, sizeof(first_size), 1, fp); // ignore endianness issues
  2084. fwrite(it->first.c_str(), first_size, 1, fp);
  2085. const string::size_type second_size = it->second.length();
  2086. fwrite(&second_size, sizeof(second_size), 1, fp);
  2087. fwrite(it->second.c_str(), second_size, 1, fp);
  2088. }
  2089. fclose(fp);
  2090. }
  2091. sparse_hash_map<string, string, Hasher, Hasher> ht_in;
  2092. fp = fopen(file.c_str(), "rb");
  2093. if (fp)
  2094. {
  2095. EXPECT_TRUE(fp != NULL);
  2096. EXPECT_TRUE(ht_in.read_metadata(fp));
  2097. for (sparse_hash_map<string, string, Hasher, Hasher>::iterator
  2098. it = ht_in.begin(); it != ht_in.end(); ++it) {
  2099. string::size_type first_size;
  2100. EXPECT_EQ(1u, fread(&first_size, sizeof(first_size), 1, fp));
  2101. char* first = new char[first_size];
  2102. EXPECT_EQ(1u, fread(first, first_size, 1, fp));
  2103. string::size_type second_size;
  2104. EXPECT_EQ(1u, fread(&second_size, sizeof(second_size), 1, fp));
  2105. char* second = new char[second_size];
  2106. EXPECT_EQ(1u, fread(second, second_size, 1, fp));
  2107. // it points to garbage, so we have to use placement-new to initialize.
  2108. // We also have to use const-cast since it->first is const.
  2109. new(const_cast<string*>(&it->first)) string(first, first_size);
  2110. new(&it->second) string(second, second_size);
  2111. delete[] first;
  2112. delete[] second;
  2113. }
  2114. fclose(fp);
  2115. }
  2116. EXPECT_EQ(string(" "), ht_in[" "]);
  2117. EXPECT_EQ(string("+++++++++++++++++++++++++++++++++++++++++++"), ht_in["+"]);
  2118. EXPECT_EQ(string(""), ht_in["c"]); // should not have been saved
  2119. EXPECT_EQ(string(""), ht_in["y"]);
  2120. }
  2121. TYPED_TEST(HashtableAllTest, Serialization)
  2122. {
  2123. if (!this->ht_.supports_serialization()) return;
  2124. TypeParam ht_out;
  2125. ht_out.set_deleted_key(this->UniqueKey(2000));
  2126. for (int i = 1; i < 100; i++) {
  2127. ht_out.insert(this->UniqueObject(i));
  2128. }
  2129. // just to test having some erased keys when we write.
  2130. ht_out.erase(this->UniqueKey(56));
  2131. ht_out.erase(this->UniqueKey(22));
  2132. string file(TmpFile("serialization"));
  2133. FILE* fp = fopen(file.c_str(), "wb");
  2134. if (fp)
  2135. {
  2136. EXPECT_TRUE(fp != NULL);
  2137. EXPECT_TRUE(ht_out.serialize(ValueSerializer(), fp));
  2138. fclose(fp);
  2139. }
  2140. TypeParam ht_in;
  2141. fp = fopen(file.c_str(), "rb");
  2142. if (fp)
  2143. {
  2144. EXPECT_TRUE(fp != NULL);
  2145. EXPECT_TRUE(ht_in.unserialize(ValueSerializer(), fp));
  2146. fclose(fp);
  2147. }
  2148. EXPECT_EQ(this->UniqueObject(1), *ht_in.find(this->UniqueKey(1)));
  2149. EXPECT_EQ(this->UniqueObject(99), *ht_in.find(this->UniqueKey(99)));
  2150. EXPECT_FALSE(ht_in.count(this->UniqueKey(100)));
  2151. EXPECT_EQ(this->UniqueObject(21), *ht_in.find(this->UniqueKey(21)));
  2152. // should not have been saved
  2153. EXPECT_FALSE(ht_in.count(this->UniqueKey(22)));
  2154. EXPECT_FALSE(ht_in.count(this->UniqueKey(56)));
  2155. }
  2156. TYPED_TEST(HashtableIntTest, NopointerSerialization)
  2157. {
  2158. if (!this->ht_.supports_serialization()) return;
  2159. TypeParam ht_out;
  2160. ht_out.set_deleted_key(this->UniqueKey(2000));
  2161. for (int i = 1; i < 100; i++) {
  2162. ht_out.insert(this->UniqueObject(i));
  2163. }
  2164. // just to test having some erased keys when we write.
  2165. ht_out.erase(this->UniqueKey(56));
  2166. ht_out.erase(this->UniqueKey(22));
  2167. string file(TmpFile("nopointer_serialization"));
  2168. FILE* fp = fopen(file.c_str(), "wb");
  2169. if (fp)
  2170. {
  2171. EXPECT_TRUE(fp != NULL);
  2172. EXPECT_TRUE(ht_out.serialize(typename TypeParam::NopointerSerializer(), fp));
  2173. fclose(fp);
  2174. }
  2175. TypeParam ht_in;
  2176. fp = fopen(file.c_str(), "rb");
  2177. if (fp)
  2178. {
  2179. EXPECT_TRUE(fp != NULL);
  2180. EXPECT_TRUE(ht_in.unserialize(typename TypeParam::NopointerSerializer(), fp));
  2181. fclose(fp);
  2182. }
  2183. EXPECT_EQ(this->UniqueObject(1), *ht_in.find(this->UniqueKey(1)));
  2184. EXPECT_EQ(this->UniqueObject(99), *ht_in.find(this->UniqueKey(99)));
  2185. EXPECT_FALSE(ht_in.count(this->UniqueKey(100)));
  2186. EXPECT_EQ(this->UniqueObject(21), *ht_in.find(this->UniqueKey(21)));
  2187. // should not have been saved
  2188. EXPECT_FALSE(ht_in.count(this->UniqueKey(22)));
  2189. EXPECT_FALSE(ht_in.count(this->UniqueKey(56)));
  2190. }
  2191. // We don't support serializing to a string by default, but you can do
  2192. // it by writing your own custom input/output class.
  2193. class StringIO {
  2194. public:
  2195. explicit StringIO(string* s) : s_(s) {}
  2196. size_t Write(const void* buf, size_t len) {
  2197. s_->append(reinterpret_cast<const char*>(buf), len);
  2198. return len;
  2199. }
  2200. size_t Read(void* buf, size_t len) {
  2201. if (s_->length() < len)
  2202. len = s_->length();
  2203. memcpy(reinterpret_cast<char*>(buf), s_->data(), len);
  2204. s_->erase(0, len);
  2205. return len;
  2206. }
  2207. private:
  2208. StringIO& operator=(const StringIO&);
  2209. string* const s_;
  2210. };
  2211. TYPED_TEST(HashtableIntTest, SerializingToString)
  2212. {
  2213. if (!this->ht_.supports_serialization()) return;
  2214. TypeParam ht_out;
  2215. ht_out.set_deleted_key(this->UniqueKey(2000));
  2216. for (int i = 1; i < 100; i++) {
  2217. ht_out.insert(this->UniqueObject(i));
  2218. }
  2219. // just to test having some erased keys when we write.
  2220. ht_out.erase(this->UniqueKey(56));
  2221. ht_out.erase(this->UniqueKey(22));
  2222. string stringbuf;
  2223. StringIO stringio(&stringbuf);
  2224. EXPECT_TRUE(ht_out.serialize(typename TypeParam::NopointerSerializer(),
  2225. &stringio));
  2226. TypeParam ht_in;
  2227. EXPECT_TRUE(ht_in.unserialize(typename TypeParam::NopointerSerializer(),
  2228. &stringio));
  2229. EXPECT_EQ(this->UniqueObject(1), *ht_in.find(this->UniqueKey(1)));
  2230. EXPECT_EQ(this->UniqueObject(99), *ht_in.find(this->UniqueKey(99)));
  2231. EXPECT_FALSE(ht_in.count(this->UniqueKey(100)));
  2232. EXPECT_EQ(this->UniqueObject(21), *ht_in.find(this->UniqueKey(21)));
  2233. // should not have been saved
  2234. EXPECT_FALSE(ht_in.count(this->UniqueKey(22)));
  2235. EXPECT_FALSE(ht_in.count(this->UniqueKey(56)));
  2236. }
  2237. // An easier way to do the above would be to use the existing stream methods.
  2238. TYPED_TEST(HashtableIntTest, SerializingToStringStream)
  2239. {
  2240. if (!this->ht_.supports_serialization()) return;
  2241. TypeParam ht_out;
  2242. ht_out.set_deleted_key(this->UniqueKey(2000));
  2243. for (int i = 1; i < 100; i++) {
  2244. ht_out.insert(this->UniqueObject(i));
  2245. }
  2246. // just to test having some erased keys when we write.
  2247. ht_out.erase(this->UniqueKey(56));
  2248. ht_out.erase(this->UniqueKey(22));
  2249. std::stringstream string_buffer;
  2250. EXPECT_TRUE(ht_out.serialize(typename TypeParam::NopointerSerializer(),
  2251. &string_buffer));
  2252. TypeParam ht_in;
  2253. EXPECT_TRUE(ht_in.unserialize(typename TypeParam::NopointerSerializer(),
  2254. &string_buffer));
  2255. EXPECT_EQ(this->UniqueObject(1), *ht_in.find(this->UniqueKey(1)));
  2256. EXPECT_EQ(this->UniqueObject(99), *ht_in.find(this->UniqueKey(99)));
  2257. EXPECT_FALSE(ht_in.count(this->UniqueKey(100)));
  2258. EXPECT_EQ(this->UniqueObject(21), *ht_in.find(this->UniqueKey(21)));
  2259. // should not have been saved
  2260. EXPECT_FALSE(ht_in.count(this->UniqueKey(22)));
  2261. EXPECT_FALSE(ht_in.count(this->UniqueKey(56)));
  2262. }
  2263. // Verify that the metadata serialization is endianness and word size
  2264. // agnostic.
  2265. TYPED_TEST(HashtableAllTest, MetadataSerializationAndEndianness)
  2266. {
  2267. TypeParam ht_out;
  2268. string kExpectedDense("\x13W\x86""B\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0",
  2269. 24);
  2270. // GP change - switched size from 20 to formula, because the sparsegroup bitmap is 4 or 8 bytes and not 6
  2271. string kExpectedSparse("$hu1\0\0\0 \0\0\0\0\0\0\0\0\0\0\0", 12 + sizeof(group_bm_type));
  2272. if (ht_out.supports_readwrite()) {
  2273. size_t num_bytes = 0;
  2274. string file(TmpFile("metadata_serialization"));
  2275. FILE* fp = fopen(file.c_str(), "wb");
  2276. if (fp)
  2277. {
  2278. EXPECT_TRUE(fp != NULL);
  2279. EXPECT_TRUE(ht_out.write_metadata(fp));
  2280. EXPECT_TRUE(ht_out.write_nopointer_data(fp));
  2281. num_bytes = (const size_t)ftell(fp);
  2282. fclose(fp);
  2283. }
  2284. char contents[24] = {0};
  2285. fp = fopen(file.c_str(), "rb");
  2286. if (fp)
  2287. {
  2288. EXPECT_LE(num_bytes, static_cast<size_t>(24));
  2289. EXPECT_EQ(num_bytes, fread(contents, 1, num_bytes <= 24 ? num_bytes : 24, fp));
  2290. EXPECT_EQ(EOF, fgetc(fp)); // check we're *exactly* the right size
  2291. fclose(fp);
  2292. }
  2293. // TODO(csilvers): check type of ht_out instead of looking at the 1st byte.
  2294. if (contents[0] == kExpectedDense[0]) {
  2295. EXPECT_EQ(kExpectedDense, string(contents, num_bytes));
  2296. } else {
  2297. EXPECT_EQ(kExpectedSparse, string(contents, num_bytes));
  2298. }
  2299. }
  2300. // Do it again with new-style serialization. Here we can use StringIO.
  2301. if (ht_out.supports_serialization()) {
  2302. string stringbuf;
  2303. StringIO stringio(&stringbuf);
  2304. EXPECT_TRUE(ht_out.serialize(typename TypeParam::NopointerSerializer(),
  2305. &stringio));
  2306. if (stringbuf[0] == kExpectedDense[0]) {
  2307. EXPECT_EQ(kExpectedDense, stringbuf);
  2308. } else {
  2309. EXPECT_EQ(kExpectedSparse, stringbuf);
  2310. }
  2311. }
  2312. }
  2313. // ------------------------------------------------------------------------
  2314. // The above tests test the general API for correctness. These tests
  2315. // test a few corner cases that have tripped us up in the past, and
  2316. // more general, cross-API issues like memory management.
  2317. TYPED_TEST(HashtableAllTest, BracketOperatorCrashing)
  2318. {
  2319. this->ht_.set_deleted_key(this->UniqueKey(1));
  2320. for (int iters = 0; iters < 10; iters++) {
  2321. // We start at 33 because after shrinking, we'll be at 32 buckets.
  2322. for (int i = 33; i < 133; i++) {
  2323. this->ht_.bracket_assign(this->UniqueKey(i),
  2324. this->ht_.get_data(this->UniqueObject(i)));
  2325. }
  2326. this->ht_.clear_no_resize();
  2327. // This will force a shrink on the next insert, which we want to test.
  2328. this->ht_.bracket_assign(this->UniqueKey(2),
  2329. this->ht_.get_data(this->UniqueObject(2)));
  2330. this->ht_.erase(this->UniqueKey(2));
  2331. }
  2332. }
  2333. // For data types with trivial copy-constructors and destructors, we
  2334. // should use an optimized routine for data-copying, that involves
  2335. // memmove. We test this by keeping count of how many times the
  2336. // copy-constructor is called; it should be much less with the
  2337. // optimized code.
  2338. struct Memmove
  2339. {
  2340. public:
  2341. Memmove(): i(0) {}
  2342. explicit Memmove(int ival): i(ival) {}
  2343. Memmove(const Memmove& that) { this->i = that.i; num_copies++; }
  2344. int i;
  2345. static int num_copies;
  2346. };
  2347. int Memmove::num_copies = 0;
  2348. struct NoMemmove
  2349. {
  2350. public:
  2351. NoMemmove(): i(0) {}
  2352. explicit NoMemmove(int ival): i(ival) {}
  2353. NoMemmove(const NoMemmove& that) { this->i = that.i; num_copies++; }
  2354. int i;
  2355. static int num_copies;
  2356. };
  2357. int NoMemmove::num_copies = 0;
  2358. } // unnamed namespace
  2359. #if 0
  2360. // This is what tells the hashtable code it can use memmove for this class:
  2361. namespace google {
  2362. template<> struct has_trivial_copy<Memmove> : true_type { };
  2363. template<> struct has_trivial_destructor<Memmove> : true_type { };
  2364. };
  2365. #endif
  2366. namespace
  2367. {
  2368. TEST(HashtableTest, SimpleDataTypeOptimizations)
  2369. {
  2370. // Only sparsehashtable optimizes moves in this way.
  2371. sparse_hash_map<int, Memmove, Hasher, Hasher> memmove;
  2372. sparse_hash_map<int, NoMemmove, Hasher, Hasher> nomemmove;
  2373. sparse_hash_map<int, Memmove, Hasher, Hasher, Alloc<int> >
  2374. memmove_nonstandard_alloc;
  2375. Memmove::num_copies = 0;
  2376. for (int i = 10000; i > 0; i--) {
  2377. memmove[i] = Memmove(i);
  2378. }
  2379. // GP change - const int memmove_copies = Memmove::num_copies;
  2380. NoMemmove::num_copies = 0;
  2381. for (int i = 10000; i > 0; i--) {
  2382. nomemmove[i] = NoMemmove(i);
  2383. }
  2384. // GP change - const int nomemmove_copies = NoMemmove::num_copies;
  2385. Memmove::num_copies = 0;
  2386. for (int i = 10000; i > 0; i--) {
  2387. memmove_nonstandard_alloc[i] = Memmove(i);
  2388. }
  2389. // GP change - const int memmove_nonstandard_alloc_copies = Memmove::num_copies;
  2390. // GP change - commented out following two lines
  2391. //EXPECT_GT(nomemmove_copies, memmove_copies);
  2392. //EXPECT_EQ(nomemmove_copies, memmove_nonstandard_alloc_copies);
  2393. }
  2394. TYPED_TEST(HashtableAllTest, ResizeHysteresis)
  2395. {
  2396. // We want to make sure that when we create a hashtable, and then
  2397. // add and delete one element, the size of the hashtable doesn't
  2398. // change.
  2399. this->ht_.set_deleted_key(this->UniqueKey(1));
  2400. typename TypeParam::size_type old_bucket_count = this->ht_.bucket_count();
  2401. this->ht_.insert(this->UniqueObject(4));
  2402. this->ht_.erase(this->UniqueKey(4));
  2403. this->ht_.insert(this->UniqueObject(4));
  2404. this->ht_.erase(this->UniqueKey(4));
  2405. EXPECT_EQ(old_bucket_count, this->ht_.bucket_count());
  2406. // Try it again, but with a hashtable that starts very small
  2407. TypeParam ht(2);
  2408. EXPECT_LT(ht.bucket_count(), 32u); // verify we really do start small
  2409. ht.set_deleted_key(this->UniqueKey(1));
  2410. old_bucket_count = ht.bucket_count();
  2411. ht.insert(this->UniqueObject(4));
  2412. ht.erase(this->UniqueKey(4));
  2413. ht.insert(this->UniqueObject(4));
  2414. ht.erase(this->UniqueKey(4));
  2415. EXPECT_EQ(old_bucket_count, ht.bucket_count());
  2416. }
  2417. TEST(HashtableTest, ConstKey)
  2418. {
  2419. // Sometimes people write hash_map<const int, int>, even though the
  2420. // const isn't necessary. Make sure we handle this cleanly.
  2421. sparse_hash_map<const int, int, Hasher, Hasher> shm;
  2422. shm.set_deleted_key(1);
  2423. shm[10] = 20;
  2424. }
  2425. TYPED_TEST(HashtableAllTest, ResizeActuallyResizes)
  2426. {
  2427. // This tests for a problem we had where we could repeatedly "resize"
  2428. // a hashtable to the same size it was before, on every insert.
  2429. // -----------------------------------------------------------------
  2430. const typename TypeParam::size_type kSize = 1<<10; // Pick any power of 2
  2431. const float kResize = 0.8f; // anything between 0.5 and 1 is fine.
  2432. const int kThreshold = static_cast<int>(kSize * kResize - 1);
  2433. this->ht_.set_resizing_parameters(0, kResize);
  2434. this->ht_.set_deleted_key(this->UniqueKey(kThreshold + 100));
  2435. // Get right up to the resizing threshold.
  2436. for (int i = 0; i <= kThreshold; i++) {
  2437. this->ht_.insert(this->UniqueObject(i+1));
  2438. }
  2439. // The bucket count should equal kSize.
  2440. EXPECT_EQ(kSize, this->ht_.bucket_count());
  2441. // Now start doing erase+insert pairs. This should cause us to
  2442. // copy the hashtable at most once.
  2443. const int pre_copies = this->ht_.num_table_copies();
  2444. for (int i = 0; i < static_cast<int>(kSize); i++) {
  2445. this->ht_.erase(this->UniqueKey(kThreshold));
  2446. this->ht_.insert(this->UniqueObject(kThreshold));
  2447. }
  2448. EXPECT_LT(this->ht_.num_table_copies(), pre_copies + 2);
  2449. // Now create a hashtable where we go right to the threshold, then
  2450. // delete everything and do one insert. Even though our hashtable
  2451. // is now tiny, we should still have at least kSize buckets, because
  2452. // our shrink threshhold is 0.
  2453. // -----------------------------------------------------------------
  2454. TypeParam ht2;
  2455. ht2.set_deleted_key(this->UniqueKey(kThreshold + 100));
  2456. ht2.set_resizing_parameters(0, kResize);
  2457. EXPECT_LT(ht2.bucket_count(), kSize);
  2458. for (int i = 0; i <= kThreshold; i++) {
  2459. ht2.insert(this->UniqueObject(i+1));
  2460. }
  2461. EXPECT_EQ(ht2.bucket_count(), kSize);
  2462. for (int i = 0; i <= kThreshold; i++) {
  2463. ht2.erase(this->UniqueKey(i+1));
  2464. EXPECT_EQ(ht2.bucket_count(), kSize);
  2465. }
  2466. ht2.insert(this->UniqueObject(kThreshold+2));
  2467. EXPECT_GE(ht2.bucket_count(), kSize);
  2468. }
  2469. TEST(HashtableTest, CXX11)
  2470. {
  2471. #if !defined(SPP_NO_CXX11_HDR_INITIALIZER_LIST)
  2472. {
  2473. // Initializer lists
  2474. // -----------------
  2475. typedef sparse_hash_map<int, int> Smap;
  2476. Smap smap({ {1, 1}, {2, 2} });
  2477. EXPECT_EQ(smap.size(), 2);
  2478. smap = { {1, 1}, {2, 2}, {3, 4} };
  2479. EXPECT_EQ(smap.size(), 3);
  2480. smap.insert({{5, 1}, {6, 1}});
  2481. EXPECT_EQ(smap.size(), 5);
  2482. EXPECT_EQ(smap[6], 1);
  2483. EXPECT_EQ(smap.at(6), 1);
  2484. try
  2485. {
  2486. EXPECT_EQ(smap.at(999), 1);
  2487. }
  2488. catch (...)
  2489. {};
  2490. sparse_hash_set<int> sset({ 1, 3, 4, 5 });
  2491. EXPECT_EQ(sset.size(), 4);
  2492. }
  2493. #endif
  2494. }
  2495. TEST(HashtableTest, NestedHashtables)
  2496. {
  2497. // People can do better than to have a hash_map of hash_maps, but we
  2498. // should still support it. I try a few different mappings.
  2499. sparse_hash_map<string, sparse_hash_map<int, string>, Hasher, Hasher> ht1;
  2500. ht1["hi"]; // create a sub-ht with the default values
  2501. ht1["lo"][1] = "there";
  2502. sparse_hash_map<string, sparse_hash_map<int, string>, Hasher, Hasher>
  2503. ht1copy = ht1;
  2504. }
  2505. TEST(HashtableDeathTest, ResizeOverflow)
  2506. {
  2507. sparse_hash_map<int, int> ht2;
  2508. EXPECT_DEATH(ht2.resize(static_cast<size_t>(-1)), "overflows size_type");
  2509. }
  2510. TEST(HashtableDeathTest, InsertSizeTypeOverflow)
  2511. {
  2512. static const int kMax = 256;
  2513. vector<int> test_data(kMax);
  2514. for (int i = 0; i < kMax; ++i) {
  2515. test_data[(size_t)i] = i+1000;
  2516. }
  2517. sparse_hash_set<int, Hasher, Hasher, Alloc<int, uint8, 10> > shs;
  2518. // Test we are using the correct allocator
  2519. EXPECT_TRUE(shs.get_allocator().is_custom_alloc());
  2520. // Test size_type overflow in insert(it, it)
  2521. EXPECT_DEATH(shs.insert(test_data.begin(), test_data.end()), "overflows size_type");
  2522. }
  2523. TEST(HashtableDeathTest, InsertMaxSizeOverflow)
  2524. {
  2525. static const int kMax = 256;
  2526. vector<int> test_data(kMax);
  2527. for (int i = 0; i < kMax; ++i) {
  2528. test_data[(size_t)i] = i+1000;
  2529. }
  2530. sparse_hash_set<int, Hasher, Hasher, Alloc<int, uint8, 10> > shs;
  2531. // Test max_size overflow
  2532. EXPECT_DEATH(shs.insert(test_data.begin(), test_data.begin() + 11), "exceed max_size");
  2533. }
  2534. TEST(HashtableDeathTest, ResizeSizeTypeOverflow)
  2535. {
  2536. // Test min-buckets overflow, when we want to resize too close to size_type
  2537. sparse_hash_set<int, Hasher, Hasher, Alloc<int, uint8, 10> > shs;
  2538. EXPECT_DEATH(shs.resize(250), "overflows size_type");
  2539. }
  2540. TEST(HashtableDeathTest, ResizeDeltaOverflow)
  2541. {
  2542. static const int kMax = 256;
  2543. vector<int> test_data(kMax);
  2544. for (int i = 0; i < kMax; ++i) {
  2545. test_data[(size_t)i] = i+1000;
  2546. }
  2547. sparse_hash_set<int, Hasher, Hasher, Alloc<int, uint8, 255> > shs;
  2548. for (int i = 0; i < 9; i++) {
  2549. shs.insert(i);
  2550. }
  2551. EXPECT_DEATH(shs.insert(test_data.begin(), test_data.begin() + 250),
  2552. "overflows size_type");
  2553. }
  2554. // ------------------------------------------------------------------------
  2555. // This informational "test" comes last so it's easy to see.
  2556. // Also, benchmarks.
  2557. TYPED_TEST(HashtableAllTest, ClassSizes)
  2558. {
  2559. std::cout << "sizeof(" << typeid(TypeParam).name() << "): "
  2560. << sizeof(this->ht_) << "\n";
  2561. }
  2562. } // unnamed namespace
  2563. int main(int, char **)
  2564. {
  2565. // All the work is done in the static constructors. If they don't
  2566. // die, the tests have all passed.
  2567. cout << "PASS\n";
  2568. return 0;
  2569. }