vector.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #pragma once
  2. #include "fwd.h"
  3. #include "reserve.h"
  4. #include <util/memory/alloc.h>
  5. #include <vector>
  6. #include <initializer_list>
  7. #ifdef _YNDX_LIBCXX_ENABLE_VECTOR_POD_RESIZE_UNINITIALIZED
  8. #include <type_traits>
  9. #endif
  10. template <class T, class A>
  11. class TVector: public std::vector<T, TReboundAllocator<A, T>> {
  12. public:
  13. using TBase = std::vector<T, TReboundAllocator<A, T>>;
  14. using TSelf = TVector<T, A>;
  15. using size_type = typename TBase::size_type;
  16. inline TVector()
  17. : TBase()
  18. {
  19. }
  20. inline TVector(const typename TBase::allocator_type& a)
  21. : TBase(a)
  22. {
  23. }
  24. inline explicit TVector(::NDetail::TReserveTag rt)
  25. : TBase()
  26. {
  27. this->reserve(rt.Capacity);
  28. }
  29. inline explicit TVector(::NDetail::TReserveTag rt, const typename TBase::allocator_type& a)
  30. : TBase(a)
  31. {
  32. this->reserve(rt.Capacity);
  33. }
  34. inline explicit TVector(size_type count)
  35. : TBase(count)
  36. {
  37. }
  38. inline explicit TVector(size_type count, const typename TBase::allocator_type& a)
  39. : TBase(count, a)
  40. {
  41. }
  42. inline TVector(size_type count, const T& val)
  43. : TBase(count, val)
  44. {
  45. }
  46. inline TVector(size_type count, const T& val, const typename TBase::allocator_type& a)
  47. : TBase(count, val, a)
  48. {
  49. }
  50. inline TVector(std::initializer_list<T> il)
  51. : TBase(il)
  52. {
  53. }
  54. inline TVector(std::initializer_list<T> il, const typename TBase::allocator_type& a)
  55. : TBase(il, a)
  56. {
  57. }
  58. inline TVector(const TSelf& src)
  59. : TBase(src)
  60. {
  61. }
  62. inline TVector(TSelf&& src) noexcept
  63. : TBase(std::move(src))
  64. {
  65. }
  66. template <class TIter>
  67. inline TVector(TIter first, TIter last)
  68. : TBase(first, last)
  69. {
  70. }
  71. inline TSelf& operator=(const TSelf& src) {
  72. TBase::operator=(src);
  73. return *this;
  74. }
  75. inline TSelf& operator=(TSelf&& src) noexcept {
  76. TBase::operator=(std::move(src));
  77. return *this;
  78. }
  79. inline TSelf& operator=(std::initializer_list<T> il) {
  80. this->assign(il.begin(), il.end());
  81. return *this;
  82. }
  83. inline explicit operator bool() const noexcept {
  84. return !this->empty();
  85. }
  86. Y_PURE_FUNCTION inline bool empty() const noexcept {
  87. return TBase::empty();
  88. }
  89. inline yssize_t ysize() const noexcept {
  90. return static_cast<yssize_t>(TBase::size());
  91. }
  92. void yresize(size_type newSize) {
  93. #if defined(_YNDX_LIBCXX_ENABLE_VECTOR_POD_RESIZE_UNINITIALIZED) && !defined(__CUDACC__)
  94. if constexpr (std::is_standard_layout_v<T> && std::is_trivial_v<T>) {
  95. TBase::resize_uninitialized(newSize);
  96. return;
  97. }
  98. #endif
  99. TBase::resize(newSize);
  100. }
  101. inline void crop(size_type size) {
  102. if (this->size() > size) {
  103. this->erase(this->begin() + size, this->end());
  104. }
  105. }
  106. };