-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[libc++] Implement P3567R2 flat_meow fixes #162022
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
huixie90
wants to merge
3
commits into
llvm:main
Choose a base branch
from
huixie90:hxie/P3567
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
...ainers/container.adaptors/flat.map/flat.map.modifiers/insert_range_sorted_unique.pass.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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<container-compatible-range<value_type> R> | ||
// void insert_range(sorted_unique, R&& rg); | ||
|
||
#include <algorithm> | ||
#include <deque> | ||
#include <flat_map> | ||
#include <functional> | ||
#include <ranges> | ||
#include <vector> | ||
|
||
#include "MinSequenceContainer.h" | ||
#include "../helpers.h" | ||
#include "MoveOnly.h" | ||
#include "test_macros.h" | ||
#include "test_iterators.h" | ||
#include "min_allocator.h" | ||
|
||
// test constraint container-compatible-range | ||
template <class M, class R> | ||
concept CanInsertRangeSortedUnique = requires(M m, R&& r) { m.insert_range(std::sorted_unique, std::forward<R>(r)); }; | ||
|
||
using Map = std::flat_map<int, double>; | ||
|
||
static_assert(CanInsertRangeSortedUnique<Map, std::ranges::subrange<std::pair<int, double>*>>); | ||
static_assert(CanInsertRangeSortedUnique<Map, std::ranges::subrange<std::pair<short, double>*>>); | ||
static_assert(!CanInsertRangeSortedUnique<Map, std::ranges::subrange<int*>>); | ||
static_assert(!CanInsertRangeSortedUnique<Map, std::ranges::subrange<double*>>); | ||
|
||
template <class KeyContainer, class ValueContainer> | ||
constexpr void test() { | ||
using Key = typename KeyContainer::value_type; | ||
using Value = typename ValueContainer::value_type; | ||
|
||
{ | ||
using P = std::pair<int, int>; | ||
using M = std::flat_map<Key, Value, std::less<Key>, KeyContainer, ValueContainer>; | ||
using It = forward_iterator<const P*>; | ||
M m = {{10, 1}, {8, 2}, {5, 3}, {2, 4}, {1, 5}}; | ||
P ar[] = {{1, 2}, {3, 1}, {4, 3}, {5, 5}, {9, 6}}; | ||
std::ranges::subrange r = {It(ar), It(ar + 5)}; | ||
static_assert(std::ranges::common_range<decltype(r)>); | ||
m.insert_range(std::sorted_unique, r); | ||
assert((m == M{{1, 5}, {2, 4}, {3, 1}, {4, 3}, {5, 3}, {8, 2}, {9, 6}, {10, 1}})); | ||
} | ||
{ | ||
using P = std::pair<int, int>; | ||
using M = std::flat_map<Key, Value, std::greater<>, KeyContainer, ValueContainer>; | ||
using It = cpp20_input_iterator<const P*>; | ||
M m = {{8, 1}, {5, 2}, {3, 3}, {2, 4}}; | ||
P ar[] = {{9, 6}, {5, 5}, {4, 3}, {3, 1}, {1, 2}}; | ||
std::ranges::subrange r = {It(ar), sentinel_wrapper<It>(It(ar + 5))}; | ||
static_assert(!std::ranges::common_range<decltype(r)>); | ||
m.insert_range(std::sorted_unique, r); | ||
assert((m == M{{1, 2}, {2, 4}, {3, 3}, {4, 3}, {5, 2}, {8, 1}, {9, 6}})); | ||
} | ||
{ | ||
// was empty | ||
using P = std::pair<int, int>; | ||
using M = std::flat_map<Key, Value, std::less<Key>, KeyContainer, ValueContainer>; | ||
M m; | ||
P ar[] = {{1, 2}, {3, 1}, {4, 3}, {5, 5}, {9, 6}}; | ||
m.insert_range(std::sorted_unique, ar); | ||
assert(std::ranges::equal(m, ar)); | ||
} | ||
} | ||
|
||
constexpr bool test() { | ||
test<std::vector<int>, std::vector<int>>(); | ||
#ifndef __cpp_lib_constexpr_deque | ||
if (!TEST_IS_CONSTANT_EVALUATED) | ||
#endif | ||
{ | ||
test<std::deque<int>, std::vector<int>>(); | ||
} | ||
test<MinSequenceContainer<int>, MinSequenceContainer<int>>(); | ||
test<std::vector<int, min_allocator<int>>, std::vector<int, min_allocator<int>>>(); | ||
{ | ||
// Items are forwarded correctly from the input range | ||
std::pair<MoveOnly, MoveOnly> a[] = {{1, 1}, {3, 3}, {4, 4}, {5, 5}}; | ||
std::flat_map<MoveOnly, MoveOnly> m; | ||
m.insert_range(std::sorted_unique, a | std::views::as_rvalue); | ||
std::pair<MoveOnly, MoveOnly> expected[] = {{1, 1}, {3, 3}, {4, 4}, {5, 5}}; | ||
assert(std::ranges::equal(m, expected)); | ||
} | ||
{ | ||
// The element type of the range doesn't need to be std::pair | ||
std::pair<int, int> pa[] = {{1, 1}, {3, 3}, {4, 4}, {5, 5}}; | ||
std::vector<std::reference_wrapper<std::pair<int, int>>> a(pa, pa + 4); | ||
std::flat_map<int, int> m; | ||
m.insert_range(std::sorted_unique, a); | ||
std::pair<int, int> expected[] = {{1, 1}, {3, 3}, {4, 4}, {5, 5}}; | ||
assert(std::ranges::equal(m, expected)); | ||
} | ||
if (!TEST_IS_CONSTANT_EVALUATED) { | ||
auto insert_func = [](auto& m, const auto& newValues) { m.insert_range(std::sorted_unique, newValues); }; | ||
test_insert_range_exception_guarantee(insert_func); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
int main(int, char**) { | ||
test(); | ||
#if TEST_STD_VER >= 26 | ||
static_assert(test()); | ||
#endif | ||
|
||
return 0; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -7,8 +7,6 @@ | |||||
//===----------------------------------------------------------------------===// | ||||||
|
||||||
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 | ||||||
// `check_assertion.h` requires Unix headers and regex support. | ||||||
// REQUIRES: has-unix-headers | ||||||
// UNSUPPORTED: no-localization | ||||||
// UNSUPPORTED: no-exceptions | ||||||
|
||||||
|
@@ -17,7 +15,7 @@ | |||||
// void swap(flat_map& y) noexcept; | ||||||
// friend void swap(flat_map& x, flat_map& y) noexcept | ||||||
|
||||||
// Test that std::terminate is called if any exception is thrown during swap | ||||||
// Test that the variants are hold if any exception is thrown during swap | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
#include <flat_map> | ||||||
#include <cassert> | ||||||
|
@@ -27,7 +25,6 @@ | |||||
|
||||||
#include "test_macros.h" | ||||||
#include "../helpers.h" | ||||||
#include "check_assertion.h" | ||||||
|
||||||
template <class F> | ||||||
void test_swap_exception_guarantee([[maybe_unused]] F&& swap_function) { | ||||||
|
@@ -42,8 +39,15 @@ void test_swap_exception_guarantee([[maybe_unused]] F&& swap_function) { | |||||
m1.emplace(2, 2); | ||||||
m2.emplace(3, 3); | ||||||
m2.emplace(4, 4); | ||||||
// swap is noexcept | ||||||
EXPECT_STD_TERMINATE([&] { swap_function(m1, m2); }); | ||||||
try { | ||||||
swap_function(m1, m2); | ||||||
assert(false); | ||||||
} catch (int) { | ||||||
check_invariant(m1); | ||||||
check_invariant(m2); | ||||||
LIBCPP_ASSERT(m1.size() == 0); | ||||||
LIBCPP_ASSERT(m2.size() == 0); | ||||||
} | ||||||
} | ||||||
|
||||||
{ | ||||||
|
@@ -58,8 +62,15 @@ void test_swap_exception_guarantee([[maybe_unused]] F&& swap_function) { | |||||
m2.emplace(3, 3); | ||||||
m2.emplace(4, 4); | ||||||
|
||||||
// swap is noexcept | ||||||
EXPECT_STD_TERMINATE([&] { swap_function(m1, m2); }); | ||||||
try { | ||||||
swap_function(m1, m2); | ||||||
assert(false); | ||||||
} catch (int) { | ||||||
check_invariant(m1); | ||||||
check_invariant(m2); | ||||||
LIBCPP_ASSERT(m1.size() == 0); | ||||||
LIBCPP_ASSERT(m2.size() == 0); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should mark the paper as implemented, once it is voted.