Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // <unordered_map> |
| 11 | |
| 12 | // template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>, |
| 13 | // class Alloc = allocator<pair<const Key, T>>> |
| 14 | // class unordered_multimap |
| 15 | |
| 16 | // void rehash(size_type n); |
| 17 | |
| 18 | #include <unordered_map> |
| 19 | #include <string> |
| 20 | #include <cassert> |
Howard Hinnant | f836d53 | 2011-12-02 21:23:14 +0000 | [diff] [blame] | 21 | #include <cfloat> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 22 | |
Marshall Clow | 061d0cc | 2013-11-26 20:58:02 +0000 | [diff] [blame] | 23 | #include "min_allocator.h" |
Howard Hinnant | 7a6b7ce | 2013-06-22 15:21:29 +0000 | [diff] [blame] | 24 | |
| 25 | template <class C> |
| 26 | void test(const C& c) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 27 | { |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 28 | assert(c.size() == 6); |
Howard Hinnant | 7a6b7ce | 2013-06-22 15:21:29 +0000 | [diff] [blame] | 29 | typedef std::pair<typename C::const_iterator, typename C::const_iterator> Eq; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 30 | Eq eq = c.equal_range(1); |
| 31 | assert(std::distance(eq.first, eq.second) == 2); |
Howard Hinnant | 7a6b7ce | 2013-06-22 15:21:29 +0000 | [diff] [blame] | 32 | typename C::const_iterator i = eq.first; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 33 | assert(i->first == 1); |
| 34 | assert(i->second == "one"); |
| 35 | ++i; |
| 36 | assert(i->first == 1); |
| 37 | assert(i->second == "four"); |
| 38 | eq = c.equal_range(2); |
| 39 | assert(std::distance(eq.first, eq.second) == 2); |
| 40 | i = eq.first; |
| 41 | assert(i->first == 2); |
| 42 | assert(i->second == "two"); |
| 43 | ++i; |
| 44 | assert(i->first == 2); |
| 45 | assert(i->second == "four"); |
| 46 | |
| 47 | eq = c.equal_range(3); |
| 48 | assert(std::distance(eq.first, eq.second) == 1); |
| 49 | i = eq.first; |
| 50 | assert(i->first == 3); |
| 51 | assert(i->second == "three"); |
| 52 | eq = c.equal_range(4); |
| 53 | assert(std::distance(eq.first, eq.second) == 1); |
| 54 | i = eq.first; |
| 55 | assert(i->first == 4); |
| 56 | assert(i->second == "four"); |
| 57 | assert(std::distance(c.begin(), c.end()) == c.size()); |
| 58 | assert(std::distance(c.cbegin(), c.cend()) == c.size()); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 59 | assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | int main() |
| 63 | { |
| 64 | { |
| 65 | typedef std::unordered_multimap<int, std::string> C; |
| 66 | typedef std::pair<int, std::string> P; |
| 67 | P a[] = |
| 68 | { |
| 69 | P(1, "one"), |
| 70 | P(2, "two"), |
| 71 | P(3, "three"), |
| 72 | P(4, "four"), |
| 73 | P(1, "four"), |
| 74 | P(2, "four"), |
| 75 | }; |
| 76 | C c(a, a + sizeof(a)/sizeof(a[0])); |
| 77 | test(c); |
| 78 | assert(c.bucket_count() >= 7); |
| 79 | c.rehash(3); |
| 80 | assert(c.bucket_count() == 7); |
| 81 | test(c); |
| 82 | c.max_load_factor(2); |
| 83 | c.rehash(3); |
| 84 | assert(c.bucket_count() == 3); |
| 85 | test(c); |
| 86 | c.rehash(31); |
| 87 | assert(c.bucket_count() == 31); |
| 88 | test(c); |
| 89 | } |
Howard Hinnant | 7a6b7ce | 2013-06-22 15:21:29 +0000 | [diff] [blame] | 90 | #if __cplusplus >= 201103L |
| 91 | { |
| 92 | typedef std::unordered_multimap<int, std::string, std::hash<int>, std::equal_to<int>, |
| 93 | min_allocator<std::pair<const int, std::string>>> C; |
| 94 | typedef std::pair<int, std::string> P; |
| 95 | P a[] = |
| 96 | { |
| 97 | P(1, "one"), |
| 98 | P(2, "two"), |
| 99 | P(3, "three"), |
| 100 | P(4, "four"), |
| 101 | P(1, "four"), |
| 102 | P(2, "four"), |
| 103 | }; |
| 104 | C c(a, a + sizeof(a)/sizeof(a[0])); |
| 105 | test(c); |
| 106 | assert(c.bucket_count() >= 7); |
| 107 | c.rehash(3); |
| 108 | assert(c.bucket_count() == 7); |
| 109 | test(c); |
| 110 | c.max_load_factor(2); |
| 111 | c.rehash(3); |
| 112 | assert(c.bucket_count() == 3); |
| 113 | test(c); |
| 114 | c.rehash(31); |
| 115 | assert(c.bucket_count() == 31); |
| 116 | test(c); |
| 117 | } |
| 118 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 119 | } |