Skip to content

[libc++] constexpr flat_multimap #148417

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

Merged
merged 4 commits into from
Jul 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
355 changes: 210 additions & 145 deletions libcxx/include/__flat_map/flat_multimap.h

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "min_allocator.h"

template <class KeyContainer, class ValueContainer>
void test() {
constexpr void test() {
using Key = typename KeyContainer::value_type;
using Value = typename ValueContainer::value_type;
using M = std::flat_multimap<Key, Value, std::less<int>, KeyContainer, ValueContainer>;
Expand All @@ -41,11 +41,23 @@ void test() {
assert(m.empty());
}

int main(int, char**) {
constexpr bool test() {
test<std::vector<int>, std::vector<double>>();
test<std::deque<int>, std::vector<double>>();
#ifndef __cpp_lib_constexpr_deque
if (!TEST_IS_CONSTANT_EVALUATED)
#endif
test<std::deque<int>, std::vector<double>>();
test<MinSequenceContainer<int>, MinSequenceContainer<double>>();
test<std::vector<int, min_allocator<int>>, std::vector<double, min_allocator<double>>>();

return true;
}

int main(int, char**) {
test();
#if TEST_STD_VER >= 26
static_assert(test());
#endif

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "test_allocator.h"
#include "test_macros.h"

int main(int, char**) {
constexpr bool test() {
{
using A1 = limited_allocator<int, 10>;
using A2 = limited_allocator<int, 20>;
Expand Down Expand Up @@ -74,5 +74,15 @@ int main(int, char**) {
assert(c.max_size() <= max_dist);
assert(c.max_size() <= alloc_max_size(std::allocator<char>()));
}

return true;
}

int main(int, char**) {
test();
#if TEST_STD_VER >= 26
static_assert(test());
#endif

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
// ADDITIONAL_COMPILE_FLAGS(has-fconstexpr-steps): -fconstexpr-steps=200000000
// ADDITIONAL_COMPILE_FLAGS(has-fconstexpr-ops-limit): -fconstexpr-ops-limit=800000000

// <flat_map>

Expand All @@ -25,7 +27,7 @@
#include "min_allocator.h"

template <class KeyContainer, class ValueContainer>
void test() {
constexpr void test() {
using M = std::flat_multimap<int, char, std::less<int>, KeyContainer, ValueContainer>;
{
const M m = {{1, 'a'}, {1, 'b'}, {4, 'd'}, {5, 'e'}, {5, 'h'}};
Expand All @@ -47,7 +49,7 @@ void test() {
}
{
M m;
std::size_t s = 1000;
std::size_t s = 500;
for (auto i = 0u; i < s; ++i) {
m.emplace(i, 'a');
}
Expand All @@ -60,11 +62,22 @@ void test() {
}
}

int main(int, char**) {
constexpr bool test() {
test<std::vector<int>, std::vector<char>>();
test<std::deque<int>, std::vector<char>>();
#ifndef __cpp_lib_constexpr_deque
if (!TEST_IS_CONSTANT_EVALUATED)
#endif
test<std::deque<int>, std::vector<char>>();
test<MinSequenceContainer<int>, MinSequenceContainer<char>>();
test<std::vector<int, min_allocator<int>>, std::vector<char, min_allocator<char>>>();
return true;
}

int main(int, char**) {
test();
#if TEST_STD_VER >= 26
static_assert(test());
#endif

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@

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

// <flat_map>
// <flat_multimap>

// template<class Allocator>
// explicit flat_multimap(const Allocator& a);

#include <cassert>
#include <deque>
#include <flat_map>
#include <functional>
#include <vector>
Expand All @@ -22,7 +23,23 @@
#include "test_allocator.h"
#include "../../../test_compare.h"

int main(int, char**) {
template <template <class...> class KeyContainer, template <class...> class ValueContainer>
constexpr void test() {
using A = test_allocator<short>;
using M =
std::flat_multimap<int,
long,
std::less<int>,
KeyContainer<int, test_allocator<int>>,
ValueContainer<long, test_allocator<long>>>;
M m(A(0, 5));
assert(m.empty());
assert(m.begin() == m.end());
assert(m.keys().get_allocator().get_id() == 5);
assert(m.values().get_allocator().get_id() == 5);
}

constexpr bool test() {
{
// The constructors in this subclause shall not participate in overload
// resolution unless uses_allocator_v<key_container_type, Alloc> is true
Expand Down Expand Up @@ -53,20 +70,23 @@ int main(int, char**) {
static_assert(std::is_constructible_v<M, test_allocator<int>>);
static_assert(!std::is_convertible_v<test_allocator<int>, M>);
}

test<std::vector, std::vector>();
#ifndef __cpp_lib_constexpr_deque
if (!TEST_IS_CONSTANT_EVALUATED)
#endif
{
using A = test_allocator<short>;
using M =
std::flat_multimap<int,
long,
std::less<int>,
std::vector<int, test_allocator<int>>,
std::vector<long, test_allocator<long>>>;
M m(A(0, 5));
assert(m.empty());
assert(m.begin() == m.end());
assert(m.keys().get_allocator().get_id() == 5);
assert(m.values().get_allocator().get_id() == 5);
test<std::deque, std::deque>();
}

return true;
}

int main(int, char**) {
test();
#if TEST_STD_VER >= 26
static_assert(test());
#endif

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,32 @@
#include "test_macros.h"
#include "min_allocator.h"
#include "test_allocator.h"
#include "../helpers.h"

template <class KeyContainer, class ValueContainer>
void test() {
constexpr void test() {
using Key = typename KeyContainer::value_type;
using Value = typename ValueContainer::value_type;
using M = std::flat_multimap<Key, Value, std::less<Key>, KeyContainer, ValueContainer>;
{
M m = {{8, 8}, {10, 10}};
assert(m.size() == 2);
m = {{3, 0}, {1, 0}, {2, 0}, {2, 1}, {3, 1}, {4, 0}, {3, 2}, {5, 0}, {6, 0}, {5, 1}};
std::pair<int, int> expected[] = {{1, 0}, {2, 0}, {2, 1}, {3, 0}, {3, 1}, {3, 2}, {4, 0}, {5, 0}, {5, 1}, {6, 0}};
assert(std::ranges::equal(m, expected));
m = {{3, 0}, {1, 0}, {2, 0}, {2, 1}, {3, 1}, {4, 0}, {3, 2}, {5, 0}, {6, 0}, {5, 1}};
assert(std::ranges::equal(m.keys(), std::vector{{1, 2, 2, 3, 3, 3, 4, 5, 5, 6}}));
check_possible_values(
m.values(),
std::vector<std::vector<Value>>{
{0},
{0, 1},
{0, 1},
{0, 1, 2},
{0, 1, 2},
{0, 1, 2},
{0},
{0, 1},
{0, 1},
{0},
});
}
{
M m = {{10, 1}, {8, 1}};
Expand All @@ -46,13 +60,28 @@ void test() {
}
}

int main(int, char**) {
constexpr bool test() {
test<std::vector<int>, std::vector<int>>();
test<std::vector<int>, std::vector<double>>();
test<std::deque<int>, std::vector<double>>();
#ifndef __cpp_lib_constexpr_deque
if (!TEST_IS_CONSTANT_EVALUATED)
#endif
{
test<std::deque<int>, std::vector<double>>();
}

test<MinSequenceContainer<int>, MinSequenceContainer<double>>();
test<std::vector<int, min_allocator<int>>, std::vector<double, min_allocator<double>>>();
test<std::vector<int, min_allocator<int>>, std::vector<int, min_allocator<int>>>();

return true;
}

int main(int, char**) {
test();
#if TEST_STD_VER >= 26
static_assert(test());
#endif

return 0;
}
Loading
Loading