1 | //===----------------------------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 |
10 | |
11 | // <flat_map> |
12 | |
13 | // class flat_multimap |
14 | |
15 | // template<class K> pair<iterator,iterator> equal_range(const K& x); |
16 | // template<class K> pair<const_iterator,const_iterator> equal_range(const K& x) const; |
17 | |
18 | #include <cassert> |
19 | #include <deque> |
20 | #include <flat_map> |
21 | #include <functional> |
22 | #include <string> |
23 | #include <utility> |
24 | |
25 | #include "MinSequenceContainer.h" |
26 | #include "../helpers.h" |
27 | #include "test_macros.h" |
28 | #include "min_allocator.h" |
29 | |
30 | // Constraints: The qualified-id Compare::is_transparent is valid and denotes a type. |
31 | template <class M> |
32 | concept CanEqualRange = requires(M m, Transparent<int> k) { m.equal_range(k); }; |
33 | using TransparentMap = std::flat_multimap<int, double, TransparentComparator>; |
34 | using NonTransparentMap = std::flat_multimap<int, double, NonTransparentComparator>; |
35 | static_assert(CanEqualRange<TransparentMap>); |
36 | static_assert(CanEqualRange<const TransparentMap>); |
37 | static_assert(!CanEqualRange<NonTransparentMap>); |
38 | static_assert(!CanEqualRange<const NonTransparentMap>); |
39 | |
40 | template <class KeyContainer, class ValueContainer> |
41 | void test() { |
42 | using Key = typename KeyContainer::value_type; |
43 | using Value = typename ValueContainer::value_type; |
44 | using M = std::flat_multimap<Key, Value, TransparentComparator, KeyContainer, ValueContainer>; |
45 | |
46 | using R = std::pair<typename M::iterator, typename M::iterator>; |
47 | using CR = std::pair<typename M::const_iterator, typename M::const_iterator>; |
48 | M m = {{"alpha" , 1}, |
49 | {"alpha" , 1}, |
50 | {"alpha" , 3}, |
51 | {"beta" , 2}, |
52 | {"epsilon" , 3}, |
53 | {"epsilon" , 0}, |
54 | {"eta" , 4}, |
55 | {"gamma" , 5}, |
56 | {"gamma" , 1}}; |
57 | const auto& cm = m; |
58 | ASSERT_SAME_TYPE(decltype(m.equal_range(Transparent<std::string>{"abc" })), R); |
59 | ASSERT_SAME_TYPE(decltype(std::as_const(m).equal_range(Transparent<std::string>{"b" })), CR); |
60 | |
61 | auto test_found = [&](auto&& map, const auto& expected_key, std::initializer_list<Value> expected_values) { |
62 | auto [first, last] = map.equal_range(Transparent<std::string>{expected_key}); |
63 | auto expected_range = |
64 | expected_values | std::views::transform([&](auto&& val) { return std::pair(expected_key, val); }); |
65 | assert(std::ranges::equal(std::ranges::subrange(first, last), expected_range)); |
66 | }; |
67 | |
68 | auto test_not_found = [&](auto&& map, const std::string& expected_key, long expected_offset) { |
69 | auto [first, last] = map.equal_range(Transparent<std::string>{.t: expected_key}); |
70 | assert(first == last); |
71 | assert(first - m.begin() == expected_offset); |
72 | }; |
73 | |
74 | test_found(m, "alpha" , {1, 1, 3}); |
75 | test_found(m, "beta" , {2}); |
76 | test_found(m, "epsilon" , {3, 0}); |
77 | test_found(m, "eta" , {4}); |
78 | test_found(m, "gamma" , {5, 1}); |
79 | test_found(cm, "alpha" , {1, 1, 3}); |
80 | test_found(cm, "beta" , {2}); |
81 | test_found(cm, "epsilon" , {3, 0}); |
82 | test_found(cm, "eta" , {4}); |
83 | test_found(cm, "gamma" , {5, 1}); |
84 | |
85 | test_not_found(m, "charlie" , 4); |
86 | test_not_found(m, "aaa" , 0); |
87 | test_not_found(m, "zzz" , 9); |
88 | test_not_found(cm, "charlie" , 4); |
89 | test_not_found(cm, "aaa" , 0); |
90 | test_not_found(cm, "zzz" , 9); |
91 | } |
92 | |
93 | int main(int, char**) { |
94 | test<std::vector<std::string>, std::vector<int>>(); |
95 | test<std::deque<std::string>, std::vector<int>>(); |
96 | test<MinSequenceContainer<std::string>, MinSequenceContainer<int>>(); |
97 | test<std::vector<std::string, min_allocator<std::string>>, std::vector<int, min_allocator<int>>>(); |
98 | |
99 | { |
100 | bool transparent_used = false; |
101 | TransparentComparator c(transparent_used); |
102 | std::flat_multimap<int, int, TransparentComparator> m(std::sorted_equivalent, {{1, 1}, {2, 2}, {3, 1}, {3, 3}}, c); |
103 | assert(!transparent_used); |
104 | auto p = m.equal_range(Transparent<int>{3}); |
105 | assert(p.first == m.begin() + 2); |
106 | assert(p.second == m.end()); |
107 | assert(transparent_used); |
108 | } |
109 | { |
110 | // LWG4239 std::string and C string literal |
111 | using M = std::flat_multimap<std::string, int, std::less<>>; |
112 | M m{{"alpha" , 1}, {"beta" , 2}, {"beta" , 1}, {"eta" , 3}, {"gamma" , 3}}; |
113 | auto [first, last] = m.equal_range("beta" ); |
114 | assert(first == m.begin() + 1); |
115 | assert(last == m.begin() + 3); |
116 | } |
117 | |
118 | return 0; |
119 | } |
120 | |