47-vector.patch 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. diff --git a/include/vector b/include/vector
  2. index 4ec6b60..559b7fd 100644
  3. --- a/include/vector
  4. +++ b/include/vector
  5. @@ -343,6 +343,7 @@ template<class T, class charT> requires is-vector-bool-reference<T> // Since C++
  6. #include <__type_traits/is_allocator.h>
  7. #include <__type_traits/is_constructible.h>
  8. #include <__type_traits/is_nothrow_move_assignable.h>
  9. +#include <__type_traits/is_trivially_destructible.h>
  10. #include <__type_traits/noexcept_move_assign_container.h>
  11. #include <__type_traits/type_identity.h>
  12. #include <__utility/exception_guard.h>
  13. @@ -716,7 +717,7 @@ public:
  14. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __position);
  15. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __first, const_iterator __last);
  16. - _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  17. + _LIBCPP_REINITIALIZES_OBJECT _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  18. void clear() _NOEXCEPT
  19. {
  20. size_type __old_size = size();
  21. @@ -724,6 +725,15 @@ public:
  22. __annotate_shrink(__old_size);
  23. }
  24. +#if _YNDX_LIBCXX_ENABLE_VECTOR_POD_RESIZE_UNINITIALIZED
  25. + // This function works only for POD types, otherwise memory leak may occur.
  26. +#if _LIBCPP_STD_VER >= 20
  27. + void resize_uninitialized(size_type __sz) requires is_trivially_destructible_v<_Tp>;
  28. +#else
  29. + void resize_uninitialized(size_type __sz);
  30. +#endif
  31. +#endif
  32. +
  33. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void resize(size_type __sz);
  34. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void resize(size_type __sz, const_reference __x);
  35. @@ -809,7 +819,7 @@ private:
  36. template <class _InputIterator, class _Sentinel>
  37. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  38. void __construct_at_end(_InputIterator __first, _Sentinel __last, size_type __n);
  39. -
  40. + void __append_uninitialized(size_type __n);
  41. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __append(size_type __n);
  42. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __append(size_type __n, const_reference __x);
  43. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  44. @@ -1145,6 +1155,23 @@ vector<_Tp, _Allocator>::__construct_at_end(_InputIterator __first, _Sentinel __
  45. __tx.__pos_ = std::__uninitialized_allocator_copy(__alloc(), __first, __last, __tx.__pos_);
  46. }
  47. +template <class _Tp, class _Allocator>
  48. +void
  49. +vector<_Tp, _Allocator>::__append_uninitialized(size_type __n)
  50. +{
  51. + if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n) {
  52. + __annotate_increase(__n);
  53. + this->__end_ += __n;
  54. + }
  55. + else
  56. + {
  57. + allocator_type& __a = this->__alloc();
  58. + __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
  59. + __v.__uninitialized_at_end(__n);
  60. + __swap_out_circular_buffer(__v);
  61. + }
  62. +}
  63. +
  64. // Default constructs __n objects starting at __end_
  65. // throws if construction throws
  66. // Postcondition: size() == size() + __n
  67. @@ -2003,6 +2030,26 @@ vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
  68. this->__destruct_at_end(this->__begin_ + __sz);
  69. }
  70. +#if _YNDX_LIBCXX_ENABLE_VECTOR_POD_RESIZE_UNINITIALIZED
  71. +
  72. +template <class _Tp, class _Allocator>
  73. +void
  74. +vector<_Tp, _Allocator>::resize_uninitialized(size_type __sz)
  75. +#if _LIBCPP_STD_VER >= 20
  76. + requires is_trivially_destructible_v<_Tp>
  77. +#endif
  78. +{
  79. + size_type __cs = size();
  80. + if (__cs < __sz)
  81. + this->__append_uninitialized(__sz - __cs);
  82. + else if (__cs > __sz) {
  83. + this->__end_ = this->__begin_ + __sz;
  84. + __annotate_shrink(__cs);
  85. + }
  86. +}
  87. +
  88. +#endif
  89. +
  90. template <class _Tp, class _Allocator>
  91. _LIBCPP_CONSTEXPR_SINCE_CXX20
  92. void
  93. @@ -2047,6 +2094,7 @@ vector<_Tp, _Allocator>::__invariants() const
  94. return true;
  95. }
  96. +#if _YNDX_LIBCXX_ENABLE_VECTOR_BOOL_COMPRESSION == 1
  97. // vector<bool>
  98. template <class _Allocator> class vector<bool, _Allocator>;
  99. @@ -2360,7 +2408,7 @@ public:
  100. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator erase(const_iterator __position);
  101. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator erase(const_iterator __first, const_iterator __last);
  102. - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  103. + _LIBCPP_REINITIALIZES_OBJECT _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  104. void clear() _NOEXCEPT {__size_ = 0;}
  105. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(vector&)
  106. @@ -3294,6 +3342,32 @@ struct _LIBCPP_TEMPLATE_VIS hash<vector<bool, _Allocator> >
  107. size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
  108. {return __vec.__hash_code();}
  109. };
  110. +#else // _YNDX_LIBCXX_ENABLE_VECTOR_BOOL_COMPRESSION
  111. +// Hash function implementation for uncompressed std::vector<bool> which returns the same result.
  112. +template <class _Allocator>
  113. +struct _LIBCPP_TEMPLATE_VIS hash<vector<bool, _Allocator> >
  114. + : public unary_function<vector<bool, _Allocator>, size_t>
  115. +{
  116. + _LIBCPP_INLINE_VISIBILITY
  117. + size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
  118. + {
  119. + size_t __h = 0;
  120. + size_t __idx = 0;
  121. + size_t __n = __vec.size();
  122. + constexpr size_t __bits_per_word = sizeof(typename allocator_traits<_Allocator>::size_type) * CHAR_BIT;
  123. + static_assert(sizeof(typename allocator_traits<_Allocator>::size_type) <= sizeof(size_t), "size_type constraint violated");
  124. + for (;__idx + __bits_per_word <= __n;) {
  125. + for (size_t __bit = 0; __bit < __bits_per_word; __bit++, __idx++) {
  126. + __h ^= static_cast<size_t>(__vec[__idx]) << __bit;
  127. + }
  128. + }
  129. + for (size_t __bit = 0; __idx < __n; __bit++, __idx++) {
  130. + __h ^= static_cast<size_t>(__vec[__idx]) << __bit;
  131. + }
  132. + return __h;
  133. + }
  134. +};
  135. +#endif // _YNDX_LIBCXX_ENABLE_VECTOR_BOOL_COMPRESSION
  136. template <class _Tp, class _Allocator>
  137. _LIBCPP_CONSTEXPR_SINCE_CXX20