Skip to content

Commit 159aa0c

Browse files
committed
[libc++] implement std::flat_set
1 parent 2f1416b commit 159aa0c

File tree

7 files changed

+446
-0
lines changed

7 files changed

+446
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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_set>
12+
13+
// [[nodiscard]] bool empty() const noexcept;
14+
15+
#include <flat_set>
16+
17+
void f() {
18+
std::flat_set<int> c;
19+
c.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
20+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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_set>
12+
13+
// flat_set& operator=(const flat_set& s);
14+
15+
// Validate whether the container can be copy-assigned (move-assigned, swapped)
16+
// with an ADL-hijacking operator&
17+
18+
#include <flat_set>
19+
#include <utility>
20+
21+
#include "test_macros.h"
22+
#include "operator_hijacker.h"
23+
24+
void test() {
25+
std::flat_set<operator_hijacker> so;
26+
std::flat_set<operator_hijacker> s;
27+
s = so;
28+
s = std::move(so);
29+
swap(s, so);
30+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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_set>
12+
13+
// flat_set()
14+
// noexcept(
15+
// is_nothrow_default_constructible_v<container_type> &&
16+
// is_nothrow_default_constructible_v<key_compare>);
17+
18+
// This tests a conforming extension
19+
20+
#include <cassert>
21+
#include <flat_set>
22+
#include <functional>
23+
#include <vector>
24+
25+
#include "test_macros.h"
26+
#include "MoveOnly.h"
27+
#include "test_allocator.h"
28+
29+
struct ThrowingCtorComp {
30+
ThrowingCtorComp() noexcept(false) {}
31+
bool operator()(const auto&, const auto&) const { return false; }
32+
};
33+
34+
int main(int, char**) {
35+
#if defined(_LIBCPP_VERSION)
36+
{
37+
using C = std::flat_set<MoveOnly>;
38+
static_assert(std::is_nothrow_default_constructible_v<C>);
39+
C c;
40+
}
41+
{
42+
using C = std::flat_set<MoveOnly, std::less<MoveOnly>, std::vector<MoveOnly, test_allocator<MoveOnly>>>;
43+
static_assert(std::is_nothrow_default_constructible_v<C>);
44+
C c;
45+
}
46+
#endif // _LIBCPP_VERSION
47+
{
48+
using C = std::flat_set<MoveOnly, std::less<MoveOnly>, std::vector<MoveOnly, other_allocator<MoveOnly>>>;
49+
static_assert(!std::is_nothrow_default_constructible_v<C>);
50+
C c;
51+
}
52+
{
53+
using C = std::flat_set<MoveOnly, ThrowingCtorComp>;
54+
static_assert(!std::is_nothrow_default_constructible_v<C>);
55+
C c;
56+
}
57+
return 0;
58+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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_set>
12+
13+
// flat_set& operator=(flat_set&&);
14+
// Preserves the class invariant for the moved-from flat_set.
15+
16+
#include <algorithm>
17+
#include <cassert>
18+
#include <compare>
19+
#include <flat_set>
20+
#include <functional>
21+
#include <utility>
22+
#include <vector>
23+
24+
#include "../helpers.h"
25+
#include "test_macros.h"
26+
27+
struct MoveNegates {
28+
int value_ = 0;
29+
MoveNegates() = default;
30+
MoveNegates(int v) : value_(v) {}
31+
MoveNegates(MoveNegates&& rhs) : value_(rhs.value_) { rhs.value_ = -rhs.value_; }
32+
MoveNegates& operator=(MoveNegates&& rhs) {
33+
value_ = rhs.value_;
34+
rhs.value_ = -rhs.value_;
35+
return *this;
36+
}
37+
~MoveNegates() = default;
38+
auto operator<=>(const MoveNegates&) const = default;
39+
};
40+
41+
struct MoveClears {
42+
int value_ = 0;
43+
MoveClears() = default;
44+
MoveClears(int v) : value_(v) {}
45+
MoveClears(MoveClears&& rhs) : value_(rhs.value_) { rhs.value_ = 0; }
46+
MoveClears& operator=(MoveClears&& rhs) {
47+
value_ = rhs.value_;
48+
rhs.value_ = 0;
49+
return *this;
50+
}
51+
~MoveClears() = default;
52+
auto operator<=>(const MoveClears&) const = default;
53+
};
54+
55+
int main(int, char**) {
56+
{
57+
const int expected[] = {1, 2, 3, 4, 5, 6, 7, 8};
58+
using M = std::flat_set<MoveNegates, std::less<MoveNegates>>;
59+
M m = M(expected, expected + 8);
60+
M m2 = M(expected, expected + 3);
61+
62+
m2 = std::move(m);
63+
64+
assert(std::equal(m2.begin(), m2.end(), expected, expected + 8));
65+
LIBCPP_ASSERT(m.empty());
66+
assert(std::is_sorted(m.begin(), m.end(), m.key_comp())); // still sorted
67+
assert(std::adjacent_find(m.begin(), m.end(), m.key_comp()) == m.end()); // still contains no duplicates
68+
m.insert(1);
69+
m.insert(2);
70+
assert(m.contains(1));
71+
assert(m.find(2) != m.end());
72+
}
73+
{
74+
const int expected[] = {1, 2, 3, 4, 5, 6, 7, 8};
75+
using M = std::flat_set<MoveClears, std::less<MoveClears>>;
76+
M m = M(expected, expected + 8);
77+
M m2 = M(expected, expected + 3);
78+
79+
m2 = std::move(m);
80+
81+
assert(std::equal(m2.begin(), m2.end(), expected, expected + 8));
82+
LIBCPP_ASSERT(m.empty());
83+
assert(std::is_sorted(m.begin(), m.end(), m.key_comp())); // still sorted
84+
assert(std::adjacent_find(m.begin(), m.end(), m.key_comp()) == m.end()); // still contains no duplicates
85+
m.insert(1);
86+
m.insert(2);
87+
assert(m.contains(1));
88+
assert(m.find(2) != m.end());
89+
}
90+
{
91+
// moved-from object maintains invariant if one of underlying container does not clear after move
92+
using M = std::flat_set<int, std::less<>, std::vector<int>>;
93+
M m1 = M({1, 2, 3});
94+
M m2 = M({1, 2});
95+
m2 = std::move(m1);
96+
assert(m2.size() == 3);
97+
check_invariant(m1);
98+
LIBCPP_ASSERT(m1.empty());
99+
}
100+
return 0;
101+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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_set>
12+
13+
// flat_set& operator=(flat_set&& c)
14+
// noexcept(
15+
// is_nothrow_move_assignable<key_container_type>::value &&
16+
// is_nothrow_move_assignable<mapped_container_type>::value &&
17+
// is_nothrow_copy_assignable<key_compare>::value);
18+
19+
// This tests a conforming extension
20+
21+
#include <flat_set>
22+
#include <functional>
23+
#include <memory_resource>
24+
#include <type_traits>
25+
#include <vector>
26+
27+
#include "MoveOnly.h"
28+
#include "test_allocator.h"
29+
#include "test_macros.h"
30+
31+
struct MoveSensitiveComp {
32+
MoveSensitiveComp() noexcept(false) = default;
33+
MoveSensitiveComp(const MoveSensitiveComp&) noexcept(false) = default;
34+
MoveSensitiveComp(MoveSensitiveComp&& rhs) { rhs.is_moved_from_ = true; }
35+
MoveSensitiveComp& operator=(const MoveSensitiveComp&) noexcept = default;
36+
MoveSensitiveComp& operator=(MoveSensitiveComp&& rhs) {
37+
rhs.is_moved_from_ = true;
38+
return *this;
39+
}
40+
bool operator()(const auto&, const auto&) const { return false; }
41+
bool is_moved_from_ = false;
42+
};
43+
44+
struct MoveThrowsComp {
45+
MoveThrowsComp(MoveThrowsComp&&) noexcept(false);
46+
MoveThrowsComp(const MoveThrowsComp&) noexcept(true);
47+
MoveThrowsComp& operator=(MoveThrowsComp&&) noexcept(false);
48+
MoveThrowsComp& operator=(const MoveThrowsComp&) noexcept(true);
49+
bool operator()(const auto&, const auto&) const;
50+
};
51+
52+
int main(int, char**) {
53+
{
54+
using C = std::flat_set<int, int>;
55+
LIBCPP_STATIC_ASSERT(std::is_nothrow_move_assignable_v<C>);
56+
}
57+
{
58+
using C = std::flat_set<MoveOnly, std::less<MoveOnly>, std::vector<MoveOnly, test_allocator<MoveOnly>>>;
59+
static_assert(!std::is_nothrow_move_assignable_v<C>);
60+
}
61+
{
62+
using C = std::flat_set<int, std::less<int>, std::vector<int, test_allocator<int>>>;
63+
static_assert(!std::is_nothrow_move_assignable_v<C>);
64+
}
65+
{
66+
using C = std::flat_set<MoveOnly, std::less<MoveOnly>, std::vector<MoveOnly, other_allocator<MoveOnly>>>;
67+
LIBCPP_STATIC_ASSERT(std::is_nothrow_move_assignable_v<C>);
68+
}
69+
{
70+
using C = std::flat_set<int, std::less<int>, std::vector<int, other_allocator<int>>>;
71+
LIBCPP_STATIC_ASSERT(std::is_nothrow_move_assignable_v<C>);
72+
}
73+
{
74+
// Test with a comparator that throws on move-assignment.
75+
using C = std::flat_set<int, MoveThrowsComp>;
76+
LIBCPP_STATIC_ASSERT(!std::is_nothrow_move_assignable_v<C>);
77+
}
78+
{
79+
// Test with a container that throws on move-assignment.
80+
using C = std::flat_set<int, std::less<int>, std::pmr::vector<int>>;
81+
static_assert(!std::is_nothrow_move_assignable_v<C>);
82+
}
83+
84+
return 0;
85+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
// UNSUPPORTED: no-exceptions
11+
12+
// <flat_set>
13+
14+
// flat_set(flat_set&& s);
15+
// If any member function in [flat.map.defn] exits via an exception, the invariant is restored.
16+
17+
#include <algorithm>
18+
#include <cassert>
19+
#include <flat_set>
20+
#include <functional>
21+
#include <utility>
22+
#include <vector>
23+
24+
#include "../helpers.h"
25+
#include "test_macros.h"
26+
27+
static int countdown = 0;
28+
29+
struct EvilContainer : std::vector<int> {
30+
EvilContainer() = default;
31+
EvilContainer(EvilContainer&& rhs) {
32+
// Throw on move-construction.
33+
if (--countdown == 0) {
34+
rhs.insert(rhs.end(), 0);
35+
rhs.insert(rhs.end(), 0);
36+
throw 42;
37+
}
38+
}
39+
};
40+
41+
int main(int, char**) {
42+
{
43+
using M = std::flat_set<int, std::less<int>, EvilContainer>;
44+
M mo = {1, 2, 3};
45+
countdown = 1;
46+
try {
47+
M m = std::move(mo);
48+
assert(false); // not reached
49+
} catch (int x) {
50+
assert(x == 42);
51+
}
52+
// The source flat_set maintains its class invariant.
53+
check_invariant(mo);
54+
LIBCPP_ASSERT(mo.empty());
55+
}
56+
57+
return 0;
58+
}

0 commit comments

Comments
 (0)