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 2 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,7 @@
//===----------------------------------------------------------------------===//

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

// <flat_map>

Expand All @@ -25,7 +26,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 Down Expand Up @@ -60,11 +61,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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,52 @@
#include <type_traits>
#include <vector>

#include "MinSequenceContainer.h"
#include "min_allocator.h"
#include "test_macros.h"
#include "../../../test_compare.h"
#include "test_allocator.h"

int main(int, char**) {
// explicit flat_multimap(const key_compare& comp);
template <class KeyContainer, class ValueContainer>
constexpr void test_compare() {
using Key = typename KeyContainer::value_type;
using Value = typename ValueContainer::value_type;
{
// The constructors in this subclause shall not participate in overload
// resolution unless uses_allocator_v<key_container_type, Alloc> is true
// and uses_allocator_v<mapped_container_type, Alloc> is true.
// The one-argument ctor is explicit.
using C = test_less<Key>;
static_assert(std::is_constructible_v<std::flat_multimap<Key, Value, C>, C>);
static_assert(!std::is_convertible_v<C, std::flat_multimap<Key, Value, C>>);

using C = test_less<int>;
using A1 = test_allocator<int>;
using A2 = other_allocator<int>;
using M1 = std::flat_multimap<int, int, C, std::vector<int, A1>, std::vector<int, A1>>;
using M2 = std::flat_multimap<int, int, C, std::vector<int, A1>, std::vector<int, A2>>;
using M3 = std::flat_multimap<int, int, C, std::vector<int, A2>, std::vector<int, A1>>;
static_assert(std::is_constructible_v<M1, const C&, const A1&>);
static_assert(!std::is_constructible_v<M1, const C&, const A2&>);
static_assert(!std::is_constructible_v<M2, const C&, const A2&>);
static_assert(!std::is_constructible_v<M3, const C&, const A2&>);
static_assert(std::is_constructible_v<std::flat_multimap<Key, Value>, std::less<Key>>);
static_assert(!std::is_convertible_v<std::less<Key>, std::flat_multimap<Key, Value>>);
}
{
using C = test_less<int>;
auto m = std::flat_multimap<int, char*, C>(C(3));
using C = test_less<Key>;
auto m = std::flat_multimap<Key, Value, C>(C(3));
assert(m.empty());
assert(m.begin() == m.end());
assert(m.key_comp() == C(3));
}
{
// The one-argument ctor is explicit.
using C = test_less<int>;
static_assert(std::is_constructible_v<std::flat_multimap<int, char*, C>, C>);
static_assert(!std::is_convertible_v<C, std::flat_multimap<int, char*, C>>);
}

static_assert(std::is_constructible_v<std::flat_multimap<int, char*>, std::less<int>>);
static_assert(!std::is_convertible_v<std::less<int>, std::flat_multimap<int, char*>>);
// template <class Alloc>
// flat_multimap(const key_compare& comp, const Alloc& a);
template <template <class...> class KeyContainer, template <class...> class ValueContainer>
constexpr void test_compare_alloc() {
{
// If an allocator is given, it must be usable by both containers.
using A = test_allocator<int>;
using M = std::flat_multimap<int, int, std::less<>, KeyContainer<int>, ValueContainer<int, A>>;
static_assert(std::is_constructible_v<M, std::less<>>);
static_assert(!std::is_constructible_v<M, std::less<>, std::allocator<int>>);
static_assert(!std::is_constructible_v<M, std::less<>, A>);
}
{
using C = test_less<int>;
using A1 = test_allocator<int>;
using A2 = test_allocator<short>;
auto m = std::flat_multimap<int, short, C, std::vector<int, A1>, std::vector<short, A2>>(C(4), A1(5));
auto m = std::flat_multimap<int, short, C, KeyContainer<int, A1>, ValueContainer<short, A2>>(C(4), A1(5));
assert(m.empty());
assert(m.begin() == m.end());
assert(m.key_comp() == C(4));
Expand All @@ -70,24 +74,60 @@ int main(int, char**) {
}
{
// explicit(false)
using C = test_less<int>;
using A1 = test_allocator<int>;
using A2 = test_allocator<short>;
std::flat_multimap<int, short, C, std::deque<int, A1>, std::deque<short, A2>> m = {C(4), A1(5)};
using C = test_less<int>;
using A1 = test_allocator<int>;
using A2 = test_allocator<short>;
std::flat_multimap<int, short, C, KeyContainer<int, A1>, ValueContainer<short, A2>> m = {C(4), A1(5)};
assert(m.empty());
assert(m.begin() == m.end());
assert(m.key_comp() == C(4));
assert(m.keys().get_allocator() == A1(5));
assert(m.values().get_allocator() == A2(5));
}
}

constexpr bool test() {
{
// If an allocator is given, it must be usable by both containers.
using A = test_allocator<int>;
using M = std::flat_multimap<int, int, std::less<>, std::vector<int>, std::vector<int, A>>;
static_assert(std::is_constructible_v<M, std::less<>>);
static_assert(!std::is_constructible_v<M, std::less<>, std::allocator<int>>);
static_assert(!std::is_constructible_v<M, std::less<>, A>);
// The constructors in this subclause shall not participate in overload
// resolution unless uses_allocator_v<key_container_type, Alloc> is true
// and uses_allocator_v<mapped_container_type, Alloc> is true.

using C = test_less<int>;
using A1 = test_allocator<int>;
using A2 = other_allocator<int>;
using M1 = std::flat_multimap<int, int, C, std::vector<int, A1>, std::vector<int, A1>>;
using M2 = std::flat_multimap<int, int, C, std::vector<int, A1>, std::vector<int, A2>>;
using M3 = std::flat_multimap<int, int, C, std::vector<int, A2>, std::vector<int, A1>>;
static_assert(std::is_constructible_v<M1, const C&, const A1&>);
static_assert(!std::is_constructible_v<M1, const C&, const A2&>);
static_assert(!std::is_constructible_v<M2, const C&, const A2&>);
static_assert(!std::is_constructible_v<M3, const C&, const A2&>);
}

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

test_compare_alloc<std::vector, std::vector>();

#ifndef __cpp_lib_constexpr_deque
if (!TEST_IS_CONSTANT_EVALUATED)
#endif
{
test_compare<std::deque<int>, std::vector<double>>();
test_compare_alloc<std::deque, std::deque>();
}

return true;
}

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

return 0;
}
Loading
Loading