File: try_emplace_transparent.pass.cpp

package info (click to toggle)
llvm-toolchain-20 1%3A20.1.6-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,111,304 kB
  • sloc: cpp: 7,438,677; ansic: 1,393,822; asm: 1,012,926; python: 241,650; f90: 86,635; objc: 75,479; lisp: 42,144; pascal: 17,286; sh: 10,027; ml: 5,082; perl: 4,730; awk: 3,523; makefile: 3,349; javascript: 2,251; xml: 892; fortran: 672
file content (182 lines) | stat: -rw-r--r-- 7,972 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20

// <flat_map>

// template<class K, class... Args>
//   pair<iterator, bool> try_emplace(K&& k, Args&&... args);
// template<class K, class... Args>
//   iterator try_emplace(const_iterator hint, K&& k, Args&&... args);

#include <flat_map>
#include <cassert>
#include <functional>
#include <deque>
#include <vector>

#include "MinSequenceContainer.h"
#include "test_macros.h"
#include "../helpers.h"
#include "min_allocator.h"
#include "../../../Emplaceable.h"

// Constraints:
// The qualified-id Compare::is_transparent is valid and denotes a type.
// is_constructible_v<key_type, K> is true.
// is_constructible_v<mapped_type, Args...> is true.
// For the first overload, is_convertible_v<K&&, const_iterator> and is_convertible_v<K&&, iterator> are both false
template <class M, class... Args>
concept CanTryEmplace = requires(M m, Args&&... args) { m.try_emplace(std::forward<Args>(args)...); };

using TransparentMap    = std::flat_map<int, Emplaceable, TransparentComparator>;
using NonTransparentMap = std::flat_map<int, Emplaceable, NonTransparentComparator>;

using TransparentMapIter      = typename TransparentMap::iterator;
using TransparentMapConstIter = typename TransparentMap::const_iterator;

static_assert(!CanTryEmplace<TransparentMap>);
static_assert(!CanTryEmplace<NonTransparentMap>);

static_assert(CanTryEmplace<TransparentMap, ConvertibleTransparent<int>>);
static_assert(CanTryEmplace<TransparentMap, ConvertibleTransparent<int>, Emplaceable>);
static_assert(CanTryEmplace<TransparentMap, ConvertibleTransparent<int>, int, double>);
static_assert(!CanTryEmplace<TransparentMap, ConvertibleTransparent<int>, const Emplaceable&>);
static_assert(!CanTryEmplace<TransparentMap, ConvertibleTransparent<int>, int>);
static_assert(!CanTryEmplace<TransparentMap, NonConvertibleTransparent<int>, Emplaceable>);
static_assert(!CanTryEmplace<NonTransparentMap, NonConvertibleTransparent<int>, Emplaceable>);
static_assert(!CanTryEmplace<TransparentMap, ConvertibleTransparent<int>, int>);
static_assert(!CanTryEmplace<TransparentMap, TransparentMapIter, Emplaceable>);
static_assert(!CanTryEmplace<TransparentMap, TransparentMapConstIter, Emplaceable>);

static_assert(CanTryEmplace<TransparentMap, TransparentMapConstIter, ConvertibleTransparent<int>>);
static_assert(CanTryEmplace<TransparentMap, TransparentMapConstIter, ConvertibleTransparent<int>, Emplaceable>);
static_assert(CanTryEmplace<TransparentMap, TransparentMapConstIter, ConvertibleTransparent<int>, int, double>);
static_assert(!CanTryEmplace<TransparentMap, TransparentMapConstIter, ConvertibleTransparent<int>, const Emplaceable&>);
static_assert(!CanTryEmplace<TransparentMap, TransparentMapConstIter, ConvertibleTransparent<int>, int>);
static_assert(!CanTryEmplace<TransparentMap, TransparentMapConstIter, NonConvertibleTransparent<int>, Emplaceable>);
static_assert(!CanTryEmplace<NonTransparentMap, TransparentMapConstIter, NonConvertibleTransparent<int>, Emplaceable>);
static_assert(!CanTryEmplace<TransparentMap, TransparentMapConstIter, ConvertibleTransparent<int>, int>);

template <class KeyContainer, class ValueContainer>
void test() {
  using Key   = typename KeyContainer::value_type;
  using Value = typename ValueContainer::value_type;
  using M     = std::flat_map<Key, Value, TransparentComparator, KeyContainer, ValueContainer>;

  { // pair<iterator, bool> try_emplace(K&& k, Args&&... args);
    using R = std::pair<typename M::iterator, bool>;
    M m;
    for (int i = 0; i < 20; i += 2)
      m.emplace(i, Moveable(i, (double)i));

    assert(m.size() == 10);

    Moveable mv1(3, 3.0);
    for (int i = 0; i < 20; i += 2) {
      std::same_as<R> decltype(auto) r = m.try_emplace(ConvertibleTransparent<int>{i}, std::move(mv1));
      assert(m.size() == 10);
      assert(!r.second);           // was not inserted
      assert(!mv1.moved());        // was not moved from
      assert(r.first->first == i); // key
    }

    std::same_as<R> decltype(auto) r2 = m.try_emplace(ConvertibleTransparent<int>{-1}, std::move(mv1));
    assert(m.size() == 11);
    assert(r2.second);                   // was inserted
    assert(mv1.moved());                 // was moved from
    assert(r2.first->first == -1);       // key
    assert(r2.first->second.get() == 3); // value

    Moveable mv2(5, 3.0);
    std::same_as<R> decltype(auto) r3 = m.try_emplace(ConvertibleTransparent<int>{5}, std::move(mv2));
    assert(m.size() == 12);
    assert(r3.second);                   // was inserted
    assert(mv2.moved());                 // was moved from
    assert(r3.first->first == 5);        // key
    assert(r3.first->second.get() == 5); // value

    Moveable mv3(-1, 3.0);
    std::same_as<R> decltype(auto) r4 = m.try_emplace(ConvertibleTransparent<int>{117}, std::move(mv2));
    assert(m.size() == 13);
    assert(r4.second);                    // was inserted
    assert(mv2.moved());                  // was moved from
    assert(r4.first->first == 117);       // key
    assert(r4.first->second.get() == -1); // value
  }

  { // iterator try_emplace(const_iterator hint, K&& k, Args&&... args);
    using R = typename M::iterator;
    M m;
    for (int i = 0; i < 20; i += 2)
      m.try_emplace(i, Moveable(i, (double)i));
    assert(m.size() == 10);
    typename M::const_iterator it = m.find(2);

    Moveable mv1(3, 3.0);
    for (int i = 0; i < 20; i += 2) {
      std::same_as<R> decltype(auto) r1 = m.try_emplace(it, ConvertibleTransparent<int>{i}, std::move(mv1));
      assert(m.size() == 10);
      assert(!mv1.moved());          // was not moved from
      assert(r1->first == i);        // key
      assert(r1->second.get() == i); // value
    }

    std::same_as<R> decltype(auto) r2 = m.try_emplace(it, ConvertibleTransparent<int>{3}, std::move(mv1));
    assert(m.size() == 11);
    assert(mv1.moved());           // was moved from
    assert(r2->first == 3);        // key
    assert(r2->second.get() == 3); // value
  }
}

int main(int, char**) {
  test<std::vector<int>, std::vector<Moveable>>();
  test<std::deque<int>, std::vector<Moveable>>();
  test<MinSequenceContainer<int>, MinSequenceContainer<Moveable>>();
  test<std::vector<int, min_allocator<int>>, std::vector<Moveable, min_allocator<Moveable>>>();

  {
    bool transparent_used = false;
    TransparentComparator c(transparent_used);
    std::flat_map<int, int, TransparentComparator> m(std::sorted_unique, {{1, 1}, {2, 2}, {3, 3}}, c);
    assert(!transparent_used);
    auto p = m.try_emplace(ConvertibleTransparent<int>{3}, 3);
    assert(!p.second);
    assert(transparent_used);
  }
  {
    bool transparent_used = false;
    TransparentComparator c(transparent_used);
    std::flat_map<int, int, TransparentComparator> m(std::sorted_unique, {{1, 1}, {2, 2}, {3, 3}}, c);
    assert(!transparent_used);
    auto p = m.try_emplace(m.begin(), ConvertibleTransparent<int>{3}, 3);
    assert(p->second == 3);
    assert(transparent_used);
  }
  {
    auto try_emplace = [](auto& m, auto key_arg, auto value_arg) {
      using M   = std::decay_t<decltype(m)>;
      using Key = typename M::key_type;
      m.try_emplace(ConvertibleTransparent<Key>{key_arg}, value_arg);
    };
    test_emplace_exception_guarantee(try_emplace);
  }

  {
    auto try_emplace_iter = [](auto& m, auto key_arg, auto value_arg) {
      using M   = std::decay_t<decltype(m)>;
      using Key = typename M::key_type;
      m.try_emplace(m.begin(), ConvertibleTransparent<Key>{key_arg}, value_arg);
    };
    test_emplace_exception_guarantee(try_emplace_iter);
  }

  return 0;
}