forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 3
[libc++] [P1222] std::flat_set #7
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
Quuxplusone
wants to merge
13
commits into
main
Choose a base branch
from
pr7-flat-set
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 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5602699
[libc++] [P1206] Add `container-compatible-range` and the `from_range…
Quuxplusone 20441ff
[libc++] [P1222] Implement std::sorted_equivalent and std::sorted_unique
Quuxplusone 27d9cea
[libc++] [P1222] Implement std::flat_set
Quuxplusone 5f5c36e
[libc++] [P1222] Tests for std::flat_set
Quuxplusone 31eea21
[libc++] [LWG3786] Flat maps' deduction guide needs to default Allocator
Quuxplusone 65164b3
[libc++] [LWG3884] Allocator-extended copy and move constructors for …
Quuxplusone e141deb
[libc++] [LWG3802] flat_foo allocator-extended constructors lack move…
Quuxplusone ad40cfd
[libc++] Use stable sorting in flat_set insertion
Quuxplusone 54f6892
[libc++] [LWG3804] flat_foo missing some allocator-extended deduction…
Quuxplusone dfa6d26
[libc++] [LWG3803] flat_foo constructors taking KeyContainer lack Key…
Quuxplusone f1c9ffc
[libc++] [LWG????] Permit CTAD for `std::flat_set(pmrVector, &mr)`
Quuxplusone ad50d4f
[libc++] [P1222] Implement flat_multiset
Quuxplusone c723258
[libc++] [LWG????] flat_multiset lacks transparent insert(K&&)
Quuxplusone 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
[libc++] [P1222] Tests for std::flat_set
- Loading branch information
commit 5f5c36ed1bfa76426bbeb993b8ac306087f307e1
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
libcxx/test/libcxx/containers/container.adaptors/flat.set/version.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,20 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// <flat_set> | ||
|
||
#include <flat_set> | ||
|
||
#ifndef _LIBCPP_VERSION | ||
#error _LIBCPP_VERSION not defined | ||
#endif | ||
|
||
int main(int, char**) | ||
{ | ||
return 0; | ||
} |
81 changes: 81 additions & 0 deletions
81
libcxx/test/std/containers/container.adaptors/NaiveStaticVector.h
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,81 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef SUPPORT_NAIVE_STATIC_VECTOR_H | ||
#define SUPPORT_NAIVE_STATIC_VECTOR_H | ||
|
||
#include "test_iterators.h" | ||
#include "test_macros.h" | ||
|
||
template<class T, size_t N> | ||
struct NaiveStaticVector { | ||
struct CapacityError {}; | ||
|
||
using value_type = T; | ||
using difference_type = short; | ||
using size_type = unsigned short; | ||
using iterator = random_access_iterator<T*>; | ||
using const_iterator = random_access_iterator<const T*>; | ||
|
||
explicit NaiveStaticVector() = default; | ||
template<class It> explicit NaiveStaticVector(It first, It last) { while (first != last) insert(*first++); } | ||
|
||
// Moving-from a NaiveStaticVector leaves the source vector holding moved-from objects. | ||
// This is intentional (the "Naive" in the name). | ||
// Specifically, moving-out-of a sorted+uniqued NaiveStaticVector<MoveOnly> | ||
// will leave it in a non-sorted+uniqued state. | ||
|
||
NaiveStaticVector(const NaiveStaticVector&) = default; | ||
NaiveStaticVector(NaiveStaticVector&&) = default; // deliberately don't reset size_ | ||
NaiveStaticVector& operator=(const NaiveStaticVector&) = default; | ||
NaiveStaticVector& operator=(NaiveStaticVector&&) = default; | ||
|
||
iterator begin() { return iterator(data_); } | ||
const_iterator begin() const { return const_iterator(data_); } | ||
const_iterator cbegin() const { return const_iterator(data_); } | ||
iterator end() { return begin() + size(); } | ||
const_iterator end() const { return begin() + size(); } | ||
size_type size() const { return size_; } | ||
bool empty() const { return size_ == 0; } | ||
|
||
void clear() { size_ = 0; } | ||
|
||
template<class It> | ||
iterator insert(const_iterator pos, It first, It last) { | ||
iterator result = pos - cbegin() + begin(); | ||
while (first != last) { | ||
insert(pos++, *first++); | ||
} | ||
return result; | ||
} | ||
|
||
iterator insert(const_iterator pos, T value) { | ||
if (size_ == N) { | ||
throw CapacityError(); | ||
} | ||
int i = pos - cbegin(); | ||
size_ += 1; | ||
std::move_backward(&data_[i], &data_[size_ - 1], &data_[size_]); | ||
data_[i] = std::move(value); | ||
return begin() + i; | ||
} | ||
|
||
iterator erase(const_iterator first, const_iterator last) { | ||
int i = first - cbegin(); | ||
int j = last - cbegin(); | ||
std::move(&data_[j], &data_[size_], &data_[i]); | ||
size_ -= (last - first); | ||
return begin() + i; | ||
} | ||
|
||
private: | ||
T data_[N]; | ||
size_t size_ = 0; | ||
}; | ||
|
||
#endif // SUPPORT_NAIVE_STATIC_VECTOR_H |
55 changes: 55 additions & 0 deletions
55
libcxx/test/std/containers/container.adaptors/flat.set/clear.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,55 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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_set> | ||
|
||
// class flat_set | ||
|
||
// void clear() noexcept; | ||
|
||
#include <cassert> | ||
#include <deque> | ||
#include <flat_set> | ||
#include <functional> | ||
|
||
#include "test_macros.h" | ||
#include "min_allocator.h" | ||
|
||
int main(int, char**) | ||
{ | ||
{ | ||
using M = std::flat_set<int>; | ||
M m = {1, 2, 3, 4, 5}; | ||
assert(m.size() == 5); | ||
ASSERT_NOEXCEPT(m.clear()); | ||
ASSERT_SAME_TYPE(decltype(m.clear()), void); | ||
m.clear(); | ||
assert(m.size() == 0); | ||
} | ||
{ | ||
using M = std::flat_set<int, std::greater<int>, std::deque<int, min_allocator<int>>>; | ||
M m = {1, 2, 3, 4, 5}; | ||
assert(m.size() == 5); | ||
ASSERT_NOEXCEPT(m.clear()); | ||
ASSERT_SAME_TYPE(decltype(m.clear()), void); | ||
m.clear(); | ||
assert(m.size() == 0); | ||
} | ||
{ | ||
using M = std::flat_set<bool>; | ||
M m = {true, false}; | ||
assert(m.size() == 2); | ||
ASSERT_NOEXCEPT(m.clear()); | ||
ASSERT_SAME_TYPE(decltype(m.clear()), void); | ||
m.clear(); | ||
assert(m.size() == 0); | ||
} | ||
return 0; | ||
} |
70 changes: 70 additions & 0 deletions
70
libcxx/test/std/containers/container.adaptors/flat.set/comp.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,70 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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_set> | ||
|
||
// key_compare key_comp() const; | ||
// value_compare value_comp() const; | ||
|
||
#include <cassert> | ||
#include <flat_set> | ||
#include <functional> | ||
|
||
#include "test_macros.h" | ||
|
||
int main(int, char**) { | ||
{ | ||
using M = std::flat_set<int>; | ||
using Comp = std::less<int>; // the default | ||
M m = {1, 2, 3, 4}; | ||
ASSERT_SAME_TYPE(M::key_compare, Comp); | ||
ASSERT_SAME_TYPE(M::value_compare, Comp); | ||
ASSERT_SAME_TYPE(decltype(m.key_comp()), Comp); | ||
ASSERT_SAME_TYPE(decltype(m.value_comp()), Comp); | ||
Comp kc = m.key_comp(); | ||
assert(kc(1, 2)); | ||
assert(!kc(2, 1)); | ||
Comp vc = m.value_comp(); | ||
assert(vc(1, 2)); | ||
assert(!vc(2, 1)); | ||
} | ||
{ | ||
using Comp = std::function<bool(int,int)>; | ||
using M = std::flat_set<int, Comp>; | ||
Comp comp = std::greater<int>(); | ||
M m({1, 2, 3, 4}, comp); | ||
ASSERT_SAME_TYPE(M::key_compare, Comp); | ||
ASSERT_SAME_TYPE(M::value_compare, Comp); | ||
ASSERT_SAME_TYPE(decltype(m.key_comp()), Comp); | ||
ASSERT_SAME_TYPE(decltype(m.value_comp()), Comp); | ||
Comp kc = m.key_comp(); | ||
assert(!kc(1, 2)); | ||
assert(kc(2, 1)); | ||
Comp vc = m.value_comp(); | ||
assert(!vc(1, 2)); | ||
assert(vc(2, 1)); | ||
} | ||
{ | ||
using Comp = std::less<>; | ||
using M = std::flat_set<int, Comp>; | ||
M m = {1, 2, 3, 4}; | ||
ASSERT_SAME_TYPE(M::key_compare, Comp); | ||
ASSERT_SAME_TYPE(M::value_compare, Comp); | ||
ASSERT_SAME_TYPE(decltype(m.key_comp()), Comp); | ||
ASSERT_SAME_TYPE(decltype(m.value_comp()), Comp); | ||
Comp kc = m.key_comp(); | ||
assert(kc(1, 2)); | ||
assert(!kc(2, 1)); | ||
Comp vc = m.value_comp(); | ||
assert(vc(1, 2)); | ||
assert(!vc(2, 1)); | ||
} | ||
return 0; | ||
} |
74 changes: 74 additions & 0 deletions
74
libcxx/test/std/containers/container.adaptors/flat.set/contains.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,74 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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_set> | ||
|
||
// bool contains(const key_type& x) const; | ||
|
||
#include <cassert> | ||
#include <deque> | ||
#include <flat_set> | ||
#include <functional> | ||
#include <utility> | ||
|
||
#include "test_macros.h" | ||
#include "min_allocator.h" | ||
|
||
int main(int, char**) | ||
{ | ||
{ | ||
using M = std::flat_set<int>; | ||
M m = {1, 2, 4, 5, 8}; | ||
assert(!m.contains(0)); | ||
assert( m.contains(1)); | ||
assert( m.contains(2)); | ||
assert(!m.contains(3)); | ||
assert( m.contains(4)); | ||
assert( m.contains(5)); | ||
assert(!m.contains(6)); | ||
assert(!m.contains(7)); | ||
assert( std::as_const(m).contains(8)); | ||
assert(!std::as_const(m).contains(9)); | ||
m.clear(); | ||
assert(!m.contains(1)); | ||
} | ||
{ | ||
using M = std::flat_set<int, std::greater<int>, std::deque<int, min_allocator<int>>>; | ||
M m = {1, 2, 4, 5, 8}; | ||
assert(!m.contains(0)); | ||
assert( m.contains(1)); | ||
assert( m.contains(2)); | ||
assert(!m.contains(3)); | ||
assert( m.contains(4)); | ||
assert( m.contains(5)); | ||
assert(!m.contains(6)); | ||
assert(!m.contains(7)); | ||
assert( std::as_const(m).contains(8)); | ||
assert(!std::as_const(m).contains(9)); | ||
m.clear(); | ||
assert(!m.contains(1)); | ||
} | ||
{ | ||
using M = std::flat_set<bool>; | ||
M m = {true, false}; | ||
assert( m.contains(true)); | ||
assert( m.contains(false)); | ||
m = {true}; | ||
assert( m.contains(true)); | ||
assert(!m.contains(false)); | ||
m = {false}; | ||
assert(!std::as_const(m).contains(true)); | ||
assert( std::as_const(m).contains(false)); | ||
m.clear(); | ||
assert(!m.contains(true)); | ||
assert(!m.contains(false)); | ||
} | ||
return 0; | ||
} |
53 changes: 53 additions & 0 deletions
53
libcxx/test/std/containers/container.adaptors/flat.set/contains_transparent.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,53 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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_set> | ||
|
||
// template<class K> bool contains(const K& x) const; | ||
|
||
#include <cassert> | ||
#include <flat_set> | ||
#include <string> | ||
#include <utility> | ||
|
||
#include "test_macros.h" | ||
|
||
struct StartsWith { | ||
explicit StartsWith(char ch) : lower_(1, ch), upper_(1, ch+1) {} | ||
StartsWith(const StartsWith&) = delete; | ||
void operator=(const StartsWith&) = delete; | ||
struct Less { | ||
using is_transparent = void; | ||
bool operator()(const std::string& a, const std::string& b) const { return a < b; } | ||
bool operator()(const StartsWith& a, const std::string& b) const { return a.upper_ <= b; } | ||
bool operator()(const std::string& a, const StartsWith& b) const { return a < b.lower_; } | ||
}; | ||
private: | ||
std::string lower_; | ||
std::string upper_; | ||
}; | ||
|
||
int main(int, char**) | ||
{ | ||
{ | ||
using M = std::flat_set<std::string, StartsWith::Less>; | ||
M m = { "alpha", "beta", "epsilon", "eta", "gamma" }; | ||
ASSERT_SAME_TYPE(decltype(m.contains(StartsWith('b'))), bool); | ||
ASSERT_SAME_TYPE(decltype(std::as_const(m).contains(StartsWith('b'))), bool); | ||
assert(m.contains("beta") == true); | ||
assert(m.contains("delta") == false); | ||
assert(m.contains("zeta") == false); | ||
assert(m.contains(StartsWith('b')) == true); | ||
assert(m.contains(StartsWith('d')) == false); | ||
assert(m.contains(StartsWith('e')) == true); | ||
assert(m.contains(StartsWith('z')) == false); | ||
} | ||
return 0; | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.