// RB tree implementation -*- C++ -*-
-// Copyright (C) 2001-2017 Free Software Foundation, Inc.
+// Copyright (C) 2001-2020 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
typedef _Tp* pointer;
typedef bidirectional_iterator_tag iterator_category;
- typedef ptrdiff_t difference_type;
+ typedef ptrdiff_t difference_type;
- typedef _Rb_tree_iterator<_Tp> _Self;
- typedef _Rb_tree_node_base::_Base_ptr _Base_ptr;
- typedef _Rb_tree_node<_Tp>* _Link_type;
+ typedef _Rb_tree_iterator<_Tp> _Self;
+ typedef _Rb_tree_node_base::_Base_ptr _Base_ptr;
+ typedef _Rb_tree_node<_Tp>* _Link_type;
_Rb_tree_iterator() _GLIBCXX_NOEXCEPT
: _M_node() { }
return __tmp;
}
- bool
- operator==(const _Self& __x) const _GLIBCXX_NOEXCEPT
- { return _M_node == __x._M_node; }
+ friend bool
+ operator==(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
+ { return __x._M_node == __y._M_node; }
- bool
- operator!=(const _Self& __x) const _GLIBCXX_NOEXCEPT
- { return _M_node != __x._M_node; }
+ friend bool
+ operator!=(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
+ { return __x._M_node != __y._M_node; }
_Base_ptr _M_node;
};
return __tmp;
}
- bool
- operator==(const _Self& __x) const _GLIBCXX_NOEXCEPT
- { return _M_node == __x._M_node; }
+ friend bool
+ operator==(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
+ { return __x._M_node == __y._M_node; }
- bool
- operator!=(const _Self& __x) const _GLIBCXX_NOEXCEPT
- { return _M_node != __x._M_node; }
+ friend bool
+ operator!=(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
+ { return __x._M_node != __y._M_node; }
_Base_ptr _M_node;
};
- template<typename _Val>
- inline bool
- operator==(const _Rb_tree_iterator<_Val>& __x,
- const _Rb_tree_const_iterator<_Val>& __y) _GLIBCXX_NOEXCEPT
- { return __x._M_node == __y._M_node; }
-
- template<typename _Val>
- inline bool
- operator!=(const _Rb_tree_iterator<_Val>& __x,
- const _Rb_tree_const_iterator<_Val>& __y) _GLIBCXX_NOEXCEPT
- { return __x._M_node != __y._M_node; }
-
void
_Rb_tree_insert_and_rebalance(const bool __insert_left,
_Rb_tree_node_base* __x,
_Rb_tree_rebalance_for_erase(_Rb_tree_node_base* const __z,
_Rb_tree_node_base& __header) throw ();
-#if __cplusplus > 201103L
+#if __cplusplus >= 201402L
template<typename _Cmp, typename _SfinaeType, typename = __void_t<>>
struct __has_is_transparent
{ };
struct __has_is_transparent<_Cmp, _SfinaeType,
__void_t<typename _Cmp::is_transparent>>
{ typedef void type; };
+
+ template<typename _Cmp, typename _SfinaeType>
+ using __has_is_transparent_t
+ = typename __has_is_transparent<_Cmp, _SfinaeType>::type;
#endif
#if __cplusplus > 201402L
typedef __gnu_cxx::__alloc_traits<_Node_allocator> _Alloc_traits;
-#if __cplusplus >= 201103L
- static_assert(__is_invocable<const _Compare&, const _Key&, const _Key&>{},
- "comparison object must be invocable with two arguments of key type");
-#endif
-
protected:
typedef _Rb_tree_node_base* _Base_ptr;
typedef const _Rb_tree_node_base* _Const_Base_ptr;
struct _Reuse_or_alloc_node
{
_Reuse_or_alloc_node(_Rb_tree& __t)
- : _M_root(__t._M_root()), _M_nodes(__t._M_rightmost()), _M_t(__t)
+ : _M_root(__t._M_root()), _M_nodes(__t._M_rightmost()), _M_t(__t)
{
if (_M_root)
{
struct _Alloc_node
{
_Alloc_node(_Rb_tree& __t)
- : _M_t(__t) { }
+ : _M_t(__t) { }
template<typename _Arg>
_Link_type
_M_construct_node(__tmp, __x);
return __tmp;
}
-
- void
- _M_destroy_node(_Link_type __p)
- { get_allocator().destroy(__p->_M_valptr()); }
#else
template<typename... _Args>
void
_M_construct_node(__tmp, std::forward<_Args>(__args)...);
return __tmp;
}
+#endif
void
- _M_destroy_node(_Link_type __p) noexcept
+ _M_destroy_node(_Link_type __p) _GLIBCXX_NOEXCEPT
{
+#if __cplusplus < 201103L
+ get_allocator().destroy(__p->_M_valptr());
+#else
_Alloc_traits::destroy(_M_get_Node_allocator(), __p->_M_valptr());
__p->~_Rb_tree_node<_Val>();
- }
#endif
+ }
void
_M_drop_node(_Link_type __p) _GLIBCXX_NOEXCEPT
#else
_Rb_tree_impl(_Rb_tree_impl&&) = default;
+ explicit
+ _Rb_tree_impl(_Node_allocator&& __a)
+ : _Node_allocator(std::move(__a))
+ { }
+
+ _Rb_tree_impl(_Rb_tree_impl&& __x, _Node_allocator&& __a)
+ : _Node_allocator(std::move(__a)),
+ _Base_key_compare(std::move(__x)),
+ _Rb_tree_header(std::move(__x))
+ { }
+
_Rb_tree_impl(const _Key_compare& __comp, _Node_allocator&& __a)
: _Node_allocator(std::move(__a)), _Base_key_compare(__comp)
{ }
_M_end() const _GLIBCXX_NOEXCEPT
{ return &this->_M_impl._M_header; }
- static const_reference
- _S_value(_Const_Link_type __x)
- { return *__x->_M_valptr(); }
-
static const _Key&
_S_key(_Const_Link_type __x)
- { return _KeyOfValue()(_S_value(__x)); }
+ {
+#if __cplusplus >= 201103L
+ // If we're asking for the key we're presumably using the comparison
+ // object, and so this is a good place to sanity check it.
+ static_assert(__is_invocable<_Compare&, const _Key&, const _Key&>{},
+ "comparison object must be invocable "
+ "with two arguments of key type");
+# if __cplusplus >= 201703L
+ // _GLIBCXX_RESOLVE_LIB_DEFECTS
+ // 2542. Missing const requirements for associative containers
+ if constexpr (__is_invocable<_Compare&, const _Key&, const _Key&>{})
+ static_assert(
+ is_invocable_v<const _Compare&, const _Key&, const _Key&>,
+ "comparison object must be invocable as const");
+# endif // C++17
+#endif // C++11
+
+ return _KeyOfValue()(*__x->_M_valptr());
+ }
static _Link_type
_S_left(_Base_ptr __x) _GLIBCXX_NOEXCEPT
_S_right(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
{ return static_cast<_Const_Link_type>(__x->_M_right); }
- static const_reference
- _S_value(_Const_Base_ptr __x)
- { return *static_cast<_Const_Link_type>(__x)->_M_valptr(); }
-
static const _Key&
_S_key(_Const_Base_ptr __x)
- { return _KeyOfValue()(_S_value(__x)); }
+ { return _S_key(static_cast<_Const_Link_type>(__x)); }
static _Base_ptr
_S_minimum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
#if __cplusplus >= 201103L
_Rb_tree(const allocator_type& __a)
- : _M_impl(_Compare(), _Node_allocator(__a))
+ : _M_impl(_Node_allocator(__a))
{ }
_Rb_tree(const _Rb_tree& __x, const allocator_type& __a)
: _Rb_tree(std::move(__x), _Node_allocator(__a))
{ }
- _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a);
+ private:
+ _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a, true_type)
+ noexcept(is_nothrow_default_constructible<_Compare>::value)
+ : _M_impl(std::move(__x._M_impl), std::move(__a))
+ { }
+
+ _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a, false_type)
+ : _M_impl(__x._M_impl._M_key_compare, std::move(__a))
+ {
+ if (__x._M_root() != nullptr)
+ _M_move_data(__x, false_type{});
+ }
+
+ public:
+ _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a)
+ noexcept( noexcept(
+ _Rb_tree(std::declval<_Rb_tree&&>(), std::declval<_Node_allocator&&>(),
+ std::declval<typename _Alloc_traits::is_always_equal>())) )
+ : _Rb_tree(std::move(__x), std::move(__a),
+ typename _Alloc_traits::is_always_equal{})
+ { }
#endif
~_Rb_tree() _GLIBCXX_NOEXCEPT
rend() const _GLIBCXX_NOEXCEPT
{ return const_reverse_iterator(begin()); }
- bool
+ _GLIBCXX_NODISCARD bool
empty() const _GLIBCXX_NOEXCEPT
{ return _M_impl._M_node_count == 0; }
template<typename... _Args>
iterator
_M_emplace_hint_equal(const_iterator __pos, _Args&&... __args);
+
+ template<typename _Iter>
+ using __same_value_type
+ = is_same<value_type, typename iterator_traits<_Iter>::value_type>;
+
+ template<typename _InputIterator>
+ __enable_if_t<__same_value_type<_InputIterator>::value>
+ _M_insert_range_unique(_InputIterator __first, _InputIterator __last)
+ {
+ _Alloc_node __an(*this);
+ for (; __first != __last; ++__first)
+ _M_insert_unique_(end(), *__first, __an);
+ }
+
+ template<typename _InputIterator>
+ __enable_if_t<!__same_value_type<_InputIterator>::value>
+ _M_insert_range_unique(_InputIterator __first, _InputIterator __last)
+ {
+ for (; __first != __last; ++__first)
+ _M_emplace_unique(*__first);
+ }
+
+ template<typename _InputIterator>
+ __enable_if_t<__same_value_type<_InputIterator>::value>
+ _M_insert_range_equal(_InputIterator __first, _InputIterator __last)
+ {
+ _Alloc_node __an(*this);
+ for (; __first != __last; ++__first)
+ _M_insert_equal_(end(), *__first, __an);
+ }
+
+ template<typename _InputIterator>
+ __enable_if_t<!__same_value_type<_InputIterator>::value>
+ _M_insert_range_equal(_InputIterator __first, _InputIterator __last)
+ {
+ _Alloc_node __an(*this);
+ for (; __first != __last; ++__first)
+ _M_emplace_equal(*__first);
+ }
#else
pair<iterator, bool>
_M_insert_unique(const value_type& __x);
_Alloc_node __an(*this);
return _M_insert_equal_(__pos, __x, __an);
}
-#endif
template<typename _InputIterator>
void
- _M_insert_unique(_InputIterator __first, _InputIterator __last);
+ _M_insert_range_unique(_InputIterator __first, _InputIterator __last)
+ {
+ _Alloc_node __an(*this);
+ for (; __first != __last; ++__first)
+ _M_insert_unique_(end(), *__first, __an);
+ }
template<typename _InputIterator>
void
- _M_insert_equal(_InputIterator __first, _InputIterator __last);
+ _M_insert_range_equal(_InputIterator __first, _InputIterator __last)
+ {
+ _Alloc_node __an(*this);
+ for (; __first != __last; ++__first)
+ _M_insert_equal_(end(), *__first, __an);
+ }
+#endif
private:
void
_M_erase_aux(__position);
}
#endif
+
size_type
erase(const key_type& __x);
erase(const_iterator __first, const_iterator __last)
{ _M_erase_aux(__first, __last); }
#endif
- void
- erase(const key_type* __first, const key_type* __last);
void
clear() _GLIBCXX_NOEXCEPT
pair<const_iterator, const_iterator>
equal_range(const key_type& __k) const;
-#if __cplusplus > 201103L
+#if __cplusplus >= 201402L
template<typename _Kt,
- typename _Req =
- typename __has_is_transparent<_Compare, _Kt>::type>
+ typename _Req = __has_is_transparent_t<_Compare, _Kt>>
iterator
_M_find_tr(const _Kt& __k)
{
}
template<typename _Kt,
- typename _Req =
- typename __has_is_transparent<_Compare, _Kt>::type>
+ typename _Req = __has_is_transparent_t<_Compare, _Kt>>
const_iterator
_M_find_tr(const _Kt& __k) const
{
}
template<typename _Kt,
- typename _Req =
- typename __has_is_transparent<_Compare, _Kt>::type>
+ typename _Req = __has_is_transparent_t<_Compare, _Kt>>
size_type
_M_count_tr(const _Kt& __k) const
{
}
template<typename _Kt,
- typename _Req =
- typename __has_is_transparent<_Compare, _Kt>::type>
+ typename _Req = __has_is_transparent_t<_Compare, _Kt>>
iterator
_M_lower_bound_tr(const _Kt& __k)
{
}
template<typename _Kt,
- typename _Req =
- typename __has_is_transparent<_Compare, _Kt>::type>
+ typename _Req = __has_is_transparent_t<_Compare, _Kt>>
const_iterator
_M_lower_bound_tr(const _Kt& __k) const
{
}
template<typename _Kt,
- typename _Req =
- typename __has_is_transparent<_Compare, _Kt>::type>
+ typename _Req = __has_is_transparent_t<_Compare, _Kt>>
iterator
_M_upper_bound_tr(const _Kt& __k)
{
}
template<typename _Kt,
- typename _Req =
- typename __has_is_transparent<_Compare, _Kt>::type>
+ typename _Req = __has_is_transparent_t<_Compare, _Kt>>
const_iterator
_M_upper_bound_tr(const _Kt& __k) const
{
}
template<typename _Kt,
- typename _Req =
- typename __has_is_transparent<_Compare, _Kt>::type>
+ typename _Req = __has_is_transparent_t<_Compare, _Kt>>
pair<iterator, iterator>
_M_equal_range_tr(const _Kt& __k)
{
}
template<typename _Kt,
- typename _Req =
- typename __has_is_transparent<_Compare, _Kt>::type>
+ typename _Req = __has_is_transparent_t<_Compare, _Kt>>
pair<const_iterator, const_iterator>
_M_equal_range_tr(const _Kt& __k) const
{
private:
// Move elements from container with equal allocator.
void
- _M_move_data(_Rb_tree& __x, std::true_type)
+ _M_move_data(_Rb_tree& __x, true_type)
{ _M_impl._M_move_data(__x._M_impl); }
// Move elements from container with possibly non-equal allocator,
// which might result in a copy not a move.
void
- _M_move_data(_Rb_tree&, std::false_type);
+ _M_move_data(_Rb_tree&, false_type);
// Move assignment from container with equal allocator.
void
- _M_move_assign(_Rb_tree&, std::true_type);
+ _M_move_assign(_Rb_tree&, true_type);
// Move assignment from container with possibly non-equal allocator,
// which might result in a copy not a move.
void
- _M_move_assign(_Rb_tree&, std::false_type);
+ _M_move_assign(_Rb_tree&, false_type);
#endif
#if __cplusplus > 201402L
}
}
#endif // C++17
- };
- template<typename _Key, typename _Val, typename _KeyOfValue,
- typename _Compare, typename _Alloc>
- inline bool
- operator==(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
- const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
- {
- return __x.size() == __y.size()
- && std::equal(__x.begin(), __x.end(), __y.begin());
- }
+ friend bool
+ operator==(const _Rb_tree& __x, const _Rb_tree& __y)
+ {
+ return __x.size() == __y.size()
+ && std::equal(__x.begin(), __x.end(), __y.begin());
+ }
- template<typename _Key, typename _Val, typename _KeyOfValue,
- typename _Compare, typename _Alloc>
- inline bool
- operator<(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
- const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
- {
- return std::lexicographical_compare(__x.begin(), __x.end(),
- __y.begin(), __y.end());
- }
+ friend bool
+ operator<(const _Rb_tree& __x, const _Rb_tree& __y)
+ {
+ return std::lexicographical_compare(__x.begin(), __x.end(),
+ __y.begin(), __y.end());
+ }
- template<typename _Key, typename _Val, typename _KeyOfValue,
- typename _Compare, typename _Alloc>
- inline bool
- operator!=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
- const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
- { return !(__x == __y); }
+ friend bool _GLIBCXX_DEPRECATED
+ operator!=(const _Rb_tree& __x, const _Rb_tree& __y)
+ { return !(__x == __y); }
- template<typename _Key, typename _Val, typename _KeyOfValue,
- typename _Compare, typename _Alloc>
- inline bool
- operator>(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
- const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
- { return __y < __x; }
+ friend bool _GLIBCXX_DEPRECATED
+ operator>(const _Rb_tree& __x, const _Rb_tree& __y)
+ { return __y < __x; }
- template<typename _Key, typename _Val, typename _KeyOfValue,
- typename _Compare, typename _Alloc>
- inline bool
- operator<=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
- const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
- { return !(__y < __x); }
+ friend bool _GLIBCXX_DEPRECATED
+ operator<=(const _Rb_tree& __x, const _Rb_tree& __y)
+ { return !(__y < __x); }
- template<typename _Key, typename _Val, typename _KeyOfValue,
- typename _Compare, typename _Alloc>
- inline bool
- operator>=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
- const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
- { return !(__x < __y); }
+ friend bool _GLIBCXX_DEPRECATED
+ operator>=(const _Rb_tree& __x, const _Rb_tree& __y)
+ { return !(__x < __y); }
+ };
template<typename _Key, typename _Val, typename _KeyOfValue,
typename _Compare, typename _Alloc>
{ __x.swap(__y); }
#if __cplusplus >= 201103L
- template<typename _Key, typename _Val, typename _KeyOfValue,
- typename _Compare, typename _Alloc>
- _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
- _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a)
- : _M_impl(__x._M_impl._M_key_compare, std::move(__a))
- {
- using __eq = typename _Alloc_traits::is_always_equal;
- if (__x._M_root() != nullptr)
- _M_move_data(__x, __eq());
- }
-
template<typename _Key, typename _Val, typename _KeyOfValue,
typename _Compare, typename _Alloc>
void
_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
- _M_move_data(_Rb_tree& __x, std::false_type)
+ _M_move_data(_Rb_tree& __x, false_type)
{
if (_M_get_Node_allocator() == __x._M_get_Node_allocator())
- _M_move_data(__x, std::true_type());
+ _M_move_data(__x, true_type());
else
{
_Alloc_node __an(*this);
{
clear();
if (__x._M_root() != nullptr)
- _M_move_data(__x, std::true_type());
+ _M_move_data(__x, true_type());
std::__alloc_on_move(_M_get_Node_allocator(),
__x._M_get_Node_allocator());
}
}
#endif
- template<typename _Key, typename _Val, typename _KoV,
- typename _Cmp, typename _Alloc>
- template<class _II>
- void
- _Rb_tree<_Key, _Val, _KoV, _Cmp, _Alloc>::
- _M_insert_unique(_II __first, _II __last)
- {
- _Alloc_node __an(*this);
- for (; __first != __last; ++__first)
- _M_insert_unique_(end(), *__first, __an);
- }
-
- template<typename _Key, typename _Val, typename _KoV,
- typename _Cmp, typename _Alloc>
- template<class _II>
- void
- _Rb_tree<_Key, _Val, _KoV, _Cmp, _Alloc>::
- _M_insert_equal(_II __first, _II __last)
- {
- _Alloc_node __an(*this);
- for (; __first != __last; ++__first)
- _M_insert_equal_(end(), *__first, __an);
- }
template<typename _Key, typename _Val, typename _KeyOfValue,
typename _Compare, typename _Alloc>
return __old_size - size();
}
- template<typename _Key, typename _Val, typename _KeyOfValue,
- typename _Compare, typename _Alloc>
- void
- _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
- erase(const _Key* __first, const _Key* __last)
- {
- while (__first != __last)
- erase(*__first++);
- }
-
template<typename _Key, typename _Val, typename _KeyOfValue,
typename _Compare, typename _Alloc>
typename _Rb_tree<_Key, _Val, _KeyOfValue,