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