Skip to content

Commit

Permalink
[libc++] Add container_traits (prework for std::flat_map) (llvm#109578
Browse files Browse the repository at this point in the history
)

This PR is extracted from
llvm#98643, as per code review
request
llvm#98643 (comment)
  • Loading branch information
huixie90 authored and EricWF committed Oct 22, 2024
1 parent ebebc5c commit 296731c
Show file tree
Hide file tree
Showing 12 changed files with 337 additions and 0 deletions.
1 change: 1 addition & 0 deletions libcxx/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,7 @@ set(files
__type_traits/common_type.h
__type_traits/conditional.h
__type_traits/conjunction.h
__type_traits/container_traits.h
__type_traits/copy_cv.h
__type_traits/copy_cvref.h
__type_traits/datasizeof.h
Expand Down
43 changes: 43 additions & 0 deletions libcxx/include/__type_traits/container_traits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// 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 _LIBCPP___TYPE_TRAITS_CONTAINER_TRAITS_H
#define _LIBCPP___TYPE_TRAITS_CONTAINER_TRAITS_H

#include <__config>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif

_LIBCPP_BEGIN_NAMESPACE_STD

// // __container_traits is a general purpose utility containing traits describing various containers operations.
// It currently only has one trait: `__emplacement_has_strong_exception_safety_guarantee`, but it's
// intended to be extended in the future.
//
// These traits should only be used for optimization or QoI purposes. In particular, since this is a libc++ internal
// mechanism, no user-defined containers should be expected to specialize these traits (in fact it would be illegal for
// them to do so). Hence, when using these traits to implement something, make sure that a container that fails to
// specialize these traits does not result in non-conforming code.
//
// When a trait is nonsensical for a type, this class still provides a fallback value for that trait.
// For example, `std::array` does not support `insert` or `emplace`, so
// `__emplacement_has_strong_exception_safety_guarantee` is false for such types.
template <class _Container>
struct __container_traits {
// A trait that tells whether a single element insertion/emplacement via member function
// `insert(...)` or `emplace(...)` has strong exception guarantee, that is, if the function
// exits via an exception, the original container is unaffected
static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee = false;
};

_LIBCPP_END_NAMESPACE_STD

#endif // _LIBCPP___TYPE_TRAITS_CONTAINER_TRAITS_H
13 changes: 13 additions & 0 deletions libcxx/include/deque
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ template <class T, class Allocator, class Predicate>
#include <__ranges/size.h>
#include <__split_buffer>
#include <__type_traits/conditional.h>
#include <__type_traits/container_traits.h>
#include <__type_traits/disjunction.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/is_allocator.h>
#include <__type_traits/is_convertible.h>
Expand Down Expand Up @@ -2609,6 +2611,17 @@ inline constexpr bool __format::__enable_insertable<std::deque<wchar_t>> = true;

#endif // _LIBCPP_STD_VER >= 20

template <class _Tp, class _Allocator>
struct __container_traits<deque<_Tp, _Allocator> > {
// http://eel.is/c++draft/deque.modifiers#3
// If an exception is thrown other than by the copy constructor, move constructor, assignment operator, or move
// assignment operator of T, there are no effects. If an exception is thrown while inserting a single element at
// either end, there are no effects. Otherwise, if an exception is thrown by the move constructor of a
// non-Cpp17CopyInsertable T, the effects are unspecified.
static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee =
_Or<is_nothrow_move_constructible<_Tp>, __is_cpp17_copy_insertable<_Allocator> >::value;
};

_LIBCPP_END_NAMESPACE_STD

#if _LIBCPP_STD_VER >= 17
Expand Down
12 changes: 12 additions & 0 deletions libcxx/include/forward_list
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ template <class T, class Allocator, class Predicate>
#include <__ranges/container_compatible_range.h>
#include <__ranges/from_range.h>
#include <__type_traits/conditional.h>
#include <__type_traits/container_traits.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/is_allocator.h>
#include <__type_traits/is_const.h>
Expand Down Expand Up @@ -1544,6 +1545,17 @@ erase(forward_list<_Tp, _Allocator>& __c, const _Up& __v) {
}
#endif

template <class _Tp, class _Allocator>
struct __container_traits<forward_list<_Tp, _Allocator> > {
// http://eel.is/c++draft/container.reqmts
// Unless otherwise specified (see [associative.reqmts.except], [unord.req.except], [deque.modifiers],
// [inplace.vector.modifiers], and [vector.modifiers]) all container types defined in this Clause meet the following
// additional requirements:
// - If an exception is thrown by an insert() or emplace() function while inserting a single element, that
// function has no effects.
static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee = true;
};

_LIBCPP_END_NAMESPACE_STD

#if _LIBCPP_STD_VER >= 17
Expand Down
12 changes: 12 additions & 0 deletions libcxx/include/list
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ template <class T, class Allocator, class Predicate>
#include <__ranges/container_compatible_range.h>
#include <__ranges/from_range.h>
#include <__type_traits/conditional.h>
#include <__type_traits/container_traits.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/is_allocator.h>
#include <__type_traits/is_nothrow_assignable.h>
Expand Down Expand Up @@ -1715,6 +1716,17 @@ inline constexpr bool __format::__enable_insertable<std::list<wchar_t>> = true;

#endif // _LIBCPP_STD_VER >= 20

template <class _Tp, class _Allocator>
struct __container_traits<list<_Tp, _Allocator> > {
// http://eel.is/c++draft/container.reqmts
// Unless otherwise specified (see [associative.reqmts.except], [unord.req.except], [deque.modifiers],
// [inplace.vector.modifiers], and [vector.modifiers]) all container types defined in this Clause meet the following
// additional requirements:
// - If an exception is thrown by an insert() or emplace() function while inserting a single element, that
// function has no effects.
static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee = true;
};

_LIBCPP_END_NAMESPACE_STD

#if _LIBCPP_STD_VER >= 17
Expand Down
17 changes: 17 additions & 0 deletions libcxx/include/map
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ erase_if(multimap<Key, T, Compare, Allocator>& c, Predicate pred); // C++20
#include <__ranges/container_compatible_range.h>
#include <__ranges/from_range.h>
#include <__tree>
#include <__type_traits/container_traits.h>
#include <__type_traits/is_allocator.h>
#include <__type_traits/remove_const.h>
#include <__type_traits/type_identity.h>
Expand Down Expand Up @@ -1644,6 +1645,14 @@ erase_if(map<_Key, _Tp, _Compare, _Allocator>& __c, _Predicate __pred) {
}
#endif

template <class _Key, class _Tp, class _Compare, class _Allocator>
struct __container_traits<map<_Key, _Tp, _Compare, _Allocator> > {
// http://eel.is/c++draft/associative.reqmts.except#2
// For associative containers, if an exception is thrown by any operation from within
// an insert or emplace function inserting a single element, the insertion has no effect.
static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee = true;
};

template <class _Key, class _Tp, class _Compare = less<_Key>, class _Allocator = allocator<pair<const _Key, _Tp> > >
class _LIBCPP_TEMPLATE_VIS multimap {
public:
Expand Down Expand Up @@ -2158,6 +2167,14 @@ erase_if(multimap<_Key, _Tp, _Compare, _Allocator>& __c, _Predicate __pred) {
}
#endif

template <class _Key, class _Tp, class _Compare, class _Allocator>
struct __container_traits<multimap<_Key, _Tp, _Compare, _Allocator> > {
// http://eel.is/c++draft/associative.reqmts.except#2
// For associative containers, if an exception is thrown by any operation from within
// an insert or emplace function inserting a single element, the insertion has no effect.
static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee = true;
};

_LIBCPP_END_NAMESPACE_STD

#if _LIBCPP_STD_VER >= 17
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ module std_core [system] {
}
module conditional { header "__type_traits/conditional.h" }
module conjunction { header "__type_traits/conjunction.h" }
module container_traits { header "__type_traits/container_traits.h" }
module copy_cv { header "__type_traits/copy_cv.h" }
module copy_cvref { header "__type_traits/copy_cvref.h" }
module datasizeof { header "__type_traits/datasizeof.h" }
Expand Down
17 changes: 17 additions & 0 deletions libcxx/include/set
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ erase_if(multiset<Key, Compare, Allocator>& c, Predicate pred); // C++20
#include <__ranges/container_compatible_range.h>
#include <__ranges/from_range.h>
#include <__tree>
#include <__type_traits/container_traits.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/is_allocator.h>
#include <__type_traits/is_nothrow_assignable.h>
Expand Down Expand Up @@ -1022,6 +1023,14 @@ erase_if(set<_Key, _Compare, _Allocator>& __c, _Predicate __pred) {
}
#endif

template <class _Key, class _Compare, class _Allocator>
struct __container_traits<set<_Key, _Compare, _Allocator> > {
// http://eel.is/c++draft/associative.reqmts.except#2
// For associative containers, if an exception is thrown by any operation from within
// an insert or emplace function inserting a single element, the insertion has no effect.
static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee = true;
};

template <class _Key, class _Compare = less<_Key>, class _Allocator = allocator<_Key> >
class _LIBCPP_TEMPLATE_VIS multiset {
public:
Expand Down Expand Up @@ -1481,6 +1490,14 @@ erase_if(multiset<_Key, _Compare, _Allocator>& __c, _Predicate __pred) {
}
#endif

template <class _Key, class _Compare, class _Allocator>
struct __container_traits<multiset<_Key, _Compare, _Allocator> > {
// http://eel.is/c++draft/associative.reqmts.except#2
// For associative containers, if an exception is thrown by any operation from within
// an insert or emplace function inserting a single element, the insertion has no effect.
static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee = true;
};

_LIBCPP_END_NAMESPACE_STD

#if _LIBCPP_STD_VER >= 17
Expand Down
22 changes: 22 additions & 0 deletions libcxx/include/unordered_map
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,9 @@ template <class Key, class T, class Hash, class Pred, class Alloc>
#include <__ranges/concepts.h>
#include <__ranges/container_compatible_range.h>
#include <__ranges/from_range.h>
#include <__type_traits/container_traits.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/invoke.h>
#include <__type_traits/is_allocator.h>
#include <__type_traits/is_integral.h>
#include <__type_traits/remove_const.h>
Expand Down Expand Up @@ -1830,6 +1832,16 @@ inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const unordered_map<_Key, _Tp, _Has

#endif

template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
struct __container_traits<unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc> > {
// http://eel.is/c++draft/unord.req.except#2
// For unordered associative containers, if an exception is thrown by any operation
// other than the container's hash function from within an insert or emplace function
// inserting a single element, the insertion has no effect.
static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee =
__nothrow_invokable<_Hash, const _Key&>::value;
};

template <class _Key,
class _Tp,
class _Hash = hash<_Key>,
Expand Down Expand Up @@ -2520,6 +2532,16 @@ inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const unordered_multimap<_Key, _Tp,

#endif

template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
struct __container_traits<unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc> > {
// http://eel.is/c++draft/unord.req.except#2
// For unordered associative containers, if an exception is thrown by any operation
// other than the container's hash function from within an insert or emplace function
// inserting a single element, the insertion has no effect.
static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee =
__nothrow_invokable<_Hash, const _Key&>::value;
};

_LIBCPP_END_NAMESPACE_STD

#if _LIBCPP_STD_VER >= 17
Expand Down
22 changes: 22 additions & 0 deletions libcxx/include/unordered_set
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,9 @@ template <class Value, class Hash, class Pred, class Alloc>
#include <__ranges/concepts.h>
#include <__ranges/container_compatible_range.h>
#include <__ranges/from_range.h>
#include <__type_traits/container_traits.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/invoke.h>
#include <__type_traits/is_allocator.h>
#include <__type_traits/is_integral.h>
#include <__type_traits/is_nothrow_assignable.h>
Expand Down Expand Up @@ -1183,6 +1185,16 @@ inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const unordered_set<_Value, _Hash,

#endif

template <class _Value, class _Hash, class _Pred, class _Alloc>
struct __container_traits<unordered_set<_Value, _Hash, _Pred, _Alloc> > {
// http://eel.is/c++draft/unord.req.except#2
// For unordered associative containers, if an exception is thrown by any operation
// other than the container's hash function from within an insert or emplace function
// inserting a single element, the insertion has no effect.
static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee =
__nothrow_invokable<_Hash, const _Value&>::value;
};

template <class _Value, class _Hash = hash<_Value>, class _Pred = equal_to<_Value>, class _Alloc = allocator<_Value> >
class _LIBCPP_TEMPLATE_VIS unordered_multiset {
public:
Expand Down Expand Up @@ -1793,6 +1805,16 @@ inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const unordered_multiset<_Value, _H

#endif

template <class _Value, class _Hash, class _Pred, class _Alloc>
struct __container_traits<unordered_multiset<_Value, _Hash, _Pred, _Alloc> > {
// http://eel.is/c++draft/unord.req.except#2
// For unordered associative containers, if an exception is thrown by any operation
// other than the container's hash function from within an insert or emplace function
// inserting a single element, the insertion has no effect.
static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee =
__nothrow_invokable<_Hash, const _Value&>::value;
};

_LIBCPP_END_NAMESPACE_STD

#if _LIBCPP_STD_VER >= 17
Expand Down
14 changes: 14 additions & 0 deletions libcxx/include/vector
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,8 @@ template<class T, class charT> requires is-vector-bool-reference<T> // Since C++
#include <__ranges/size.h>
#include <__split_buffer>
#include <__type_traits/conditional.h>
#include <__type_traits/container_traits.h>
#include <__type_traits/disjunction.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/is_allocator.h>
#include <__type_traits/is_constructible.h>
Expand Down Expand Up @@ -3005,6 +3007,18 @@ public:
};
#endif // _LIBCPP_STD_VER >= 23

template <class _Tp, class _Allocator>
struct __container_traits<vector<_Tp, _Allocator> > {
// http://eel.is/c++draft/vector.modifiers#2
// If an exception is thrown other than by the copy constructor, move constructor, assignment operator, or move
// assignment operator of T or by any InputIterator operation, there are no effects. If an exception is thrown while
// inserting a single element at the end and T is Cpp17CopyInsertable or is_nothrow_move_constructible_v<T> is true,
// there are no effects. Otherwise, if an exception is thrown by the move constructor of a non-Cpp17CopyInsertable T,
// the effects are unspecified.
static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee =
_Or<is_nothrow_move_constructible<_Tp>, __is_cpp17_copy_insertable<_Allocator> >::value;
};

_LIBCPP_END_NAMESPACE_STD

#if _LIBCPP_STD_VER >= 17
Expand Down
Loading

0 comments on commit 296731c

Please sign in to comment.