compact_vector-inl.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. #ifndef COMPACT_VECTOR_INL_H_
  2. #error "Direct inclusion of this file is not allowed, include compact_vector.h"
  3. // For the sake of sane code completion.
  4. #include "compact_vector.h"
  5. #endif
  6. #undef COMPACT_VECTOR_INL_H_
  7. #include <library/cpp/yt/assert/assert.h>
  8. #include <library/cpp/yt/malloc/malloc.h>
  9. #include <library/cpp/yt/misc/hash.h>
  10. #include <util/system/compiler.h>
  11. #include <algorithm>
  12. #include <bit>
  13. #include <string.h>
  14. namespace NYT {
  15. ////////////////////////////////////////////////////////////////////////////////
  16. static_assert(sizeof(uintptr_t) == 8);
  17. static_assert(std::endian::native == std::endian::little);
  18. ////////////////////////////////////////////////////////////////////////////////
  19. template <class T>
  20. struct TCompactVectorOnHeapStorage
  21. {
  22. T* End;
  23. T* Capacity;
  24. T Elements[0];
  25. };
  26. ////////////////////////////////////////////////////////////////////////////////
  27. template <class TVector, class TPtr>
  28. class TCompactVectorReallocationPtrAdjuster
  29. {
  30. public:
  31. TCompactVectorReallocationPtrAdjuster(TVector* vector, TPtr& ptr)
  32. : Vector_(vector)
  33. , Ptr_(ptr)
  34. , Index_(ptr >= Vector_->begin() && ptr <= Vector_->end()
  35. ? std::distance(Vector_->begin(), const_cast<typename TVector::iterator>(ptr))
  36. : -1)
  37. { }
  38. ~TCompactVectorReallocationPtrAdjuster()
  39. {
  40. if (Index_ >= 0) {
  41. Ptr_ = Vector_->begin() + Index_;
  42. }
  43. }
  44. private:
  45. TVector* const Vector_;
  46. TPtr& Ptr_;
  47. const ptrdiff_t Index_;
  48. };
  49. template <class TVector>
  50. class TCompactVectorReallocationPtrAdjuster<TVector, std::nullptr_t>
  51. {
  52. public:
  53. TCompactVectorReallocationPtrAdjuster(TVector* /*vector*/, std::nullptr_t /*ptr*/)
  54. { }
  55. };
  56. ////////////////////////////////////////////////////////////////////////////////
  57. template <class T, size_t N>
  58. TCompactVector<T, N>::TCompactVector() noexcept
  59. {
  60. InlineMeta_.SizePlusOne = 1;
  61. }
  62. template <class T, size_t N>
  63. TCompactVector<T, N>::TCompactVector(const TCompactVector& other)
  64. : TCompactVector()
  65. {
  66. assign(other.begin(), other.end());
  67. }
  68. template <class T, size_t N>
  69. template <size_t OtherN>
  70. TCompactVector<T, N>::TCompactVector(const TCompactVector<T, OtherN>& other)
  71. : TCompactVector()
  72. {
  73. assign(other.begin(), other.end());
  74. }
  75. template <class T, size_t N>
  76. TCompactVector<T, N>::TCompactVector(TCompactVector&& other) noexcept(std::is_nothrow_move_constructible_v<T>)
  77. : TCompactVector()
  78. {
  79. swap(other);
  80. }
  81. template <class T, size_t N>
  82. template <size_t OtherN>
  83. TCompactVector<T, N>::TCompactVector(TCompactVector<T, OtherN>&& other)
  84. : TCompactVector()
  85. {
  86. swap(other);
  87. }
  88. template <class T, size_t N>
  89. TCompactVector<T, N>::TCompactVector(size_type count)
  90. : TCompactVector()
  91. {
  92. assign(count, T());
  93. }
  94. template <class T, size_t N>
  95. TCompactVector<T, N>::TCompactVector(size_type count, const T& value)
  96. : TCompactVector()
  97. {
  98. assign(count, value);
  99. }
  100. template <class T, size_t N>
  101. template <class TIterator>
  102. TCompactVector<T, N>::TCompactVector(TIterator first, TIterator last)
  103. : TCompactVector()
  104. {
  105. assign(first, last);
  106. }
  107. template <class T, size_t N>
  108. TCompactVector<T, N>::TCompactVector(std::initializer_list<T> list)
  109. : TCompactVector()
  110. {
  111. assign(list.begin(), list.end());
  112. }
  113. template <class T, size_t N>
  114. TCompactVector<T, N>::~TCompactVector()
  115. {
  116. if (Y_LIKELY(IsInline())) {
  117. Destroy(&InlineElements_[0], &InlineElements_[InlineMeta_.SizePlusOne - 1]);
  118. } else {
  119. auto* storage = OnHeapMeta_.Storage;
  120. Destroy(storage->Elements, storage->End);
  121. ::free(storage);
  122. }
  123. }
  124. template <class T, size_t N>
  125. bool TCompactVector<T, N>::empty() const
  126. {
  127. if (Y_LIKELY(IsInline())) {
  128. return InlineMeta_.SizePlusOne == 1;
  129. } else {
  130. const auto* storage = OnHeapMeta_.Storage;
  131. return storage->Elements == storage->End;
  132. }
  133. }
  134. template <class T, size_t N>
  135. auto TCompactVector<T, N>::begin() -> iterator
  136. {
  137. return Y_LIKELY(IsInline()) ? &InlineElements_[0] : OnHeapMeta_.Storage->Elements;
  138. }
  139. template <class T, size_t N>
  140. auto TCompactVector<T, N>::begin() const -> const_iterator
  141. {
  142. return const_cast<TCompactVector*>(this)->begin();
  143. }
  144. template <class T, size_t N>
  145. auto TCompactVector<T, N>::end() -> iterator
  146. {
  147. return Y_LIKELY(IsInline()) ? &InlineElements_[InlineMeta_.SizePlusOne - 1] : OnHeapMeta_.Storage->End;
  148. }
  149. template <class T, size_t N>
  150. auto TCompactVector<T, N>::end() const -> const_iterator
  151. {
  152. return const_cast<TCompactVector*>(this)->end();
  153. }
  154. template <class T, size_t N>
  155. auto TCompactVector<T, N>::rbegin() -> reverse_iterator
  156. {
  157. return static_cast<reverse_iterator>(end());
  158. }
  159. template <class T, size_t N>
  160. auto TCompactVector<T, N>::rbegin() const -> const_reverse_iterator
  161. {
  162. return static_cast<const_reverse_iterator>(end());
  163. }
  164. template <class T, size_t N>
  165. auto TCompactVector<T, N>::rend() -> reverse_iterator
  166. {
  167. return static_cast<reverse_iterator>(begin());
  168. }
  169. template <class T, size_t N>
  170. auto TCompactVector<T, N>::rend() const -> const_reverse_iterator
  171. {
  172. return static_cast<const_reverse_iterator>(begin());
  173. }
  174. template <class T, size_t N>
  175. auto TCompactVector<T, N>::size() const -> size_type
  176. {
  177. if (Y_LIKELY(IsInline())) {
  178. return InlineMeta_.SizePlusOne - 1;
  179. } else {
  180. const auto* storage = OnHeapMeta_.Storage;
  181. return storage->End - storage->Elements;
  182. }
  183. }
  184. template <class T, size_t N>
  185. auto TCompactVector<T, N>::capacity() const -> size_type
  186. {
  187. if (Y_LIKELY(IsInline())) {
  188. return N;
  189. } else {
  190. const auto* storage = OnHeapMeta_.Storage;
  191. return storage->Capacity - storage->Elements;
  192. }
  193. }
  194. template <class T, size_t N>
  195. auto TCompactVector<T, N>::max_size() const -> size_type
  196. {
  197. return static_cast<size_type>(-1) / sizeof(T);
  198. }
  199. template <class T, size_t N>
  200. auto TCompactVector<T, N>::data() -> pointer
  201. {
  202. return static_cast<pointer>(begin());
  203. }
  204. template <class T, size_t N>
  205. auto TCompactVector<T, N>::data() const -> const_pointer
  206. {
  207. return static_cast<const_pointer>(begin());
  208. }
  209. template <class T, size_t N>
  210. auto TCompactVector<T, N>::operator[](size_type index) -> reference
  211. {
  212. YT_ASSERT(index < size());
  213. return begin()[index];
  214. }
  215. template <class T, size_t N>
  216. auto TCompactVector<T, N>::operator[](size_type index) const -> const_reference
  217. {
  218. return const_cast<TCompactVector*>(this)->operator[](index);
  219. }
  220. template <class T, size_t N>
  221. auto TCompactVector<T, N>::front() -> reference
  222. {
  223. YT_ASSERT(!empty());
  224. return begin()[0];
  225. }
  226. template <class T, size_t N>
  227. auto TCompactVector<T, N>::front() const -> const_reference
  228. {
  229. return const_cast<TCompactVector*>(this)->front();
  230. }
  231. template <class T, size_t N>
  232. auto TCompactVector<T, N>::back() -> reference
  233. {
  234. YT_ASSERT(!empty());
  235. return end()[-1];
  236. }
  237. template <class T, size_t N>
  238. auto TCompactVector<T, N>::back() const -> const_reference
  239. {
  240. return const_cast<TCompactVector*>(this)->back();
  241. }
  242. template <class T, size_t N>
  243. void TCompactVector<T, N>::push_back(const T& value)
  244. {
  245. PushBackImpl(
  246. &value,
  247. [&] (T* dst, const T* value) {
  248. ::new(dst) T(*value);
  249. });
  250. }
  251. template <class T, size_t N>
  252. void TCompactVector<T, N>::push_back(T&& value)
  253. {
  254. PushBackImpl(
  255. &value,
  256. [&] (T* dst, T* value) {
  257. ::new(dst) T(std::move(*value));
  258. });
  259. }
  260. template <class T, size_t N>
  261. template <class... TArgs>
  262. auto TCompactVector<T, N>::emplace(const_iterator pos, TArgs&&... args) -> iterator
  263. {
  264. return InsertOneImpl(
  265. pos,
  266. nullptr,
  267. [&] (auto* dst, std::nullptr_t) {
  268. ::new(dst) T(std::forward<TArgs>(args)...);
  269. },
  270. [&] (auto* dst, std::nullptr_t) {
  271. *dst = T(std::forward<TArgs>(args)...);
  272. });
  273. }
  274. template <class T, size_t N>
  275. template <class... TArgs>
  276. auto TCompactVector<T, N>::emplace_back(TArgs&&... args) -> reference
  277. {
  278. return PushBackImpl(
  279. nullptr,
  280. [&] (T* dst, std::nullptr_t) {
  281. ::new(dst) T(std::forward<TArgs>(args)...);
  282. });
  283. }
  284. template <class T, size_t N>
  285. void TCompactVector<T, N>::pop_back()
  286. {
  287. YT_ASSERT(!empty());
  288. if (Y_LIKELY(IsInline())) {
  289. InlineElements_[InlineMeta_.SizePlusOne - 2].T::~T();
  290. --InlineMeta_.SizePlusOne;
  291. } else {
  292. auto* storage = OnHeapMeta_.Storage;
  293. storage->End[-1].T::~T();
  294. --storage->End;
  295. }
  296. }
  297. template <class T, size_t N>
  298. auto TCompactVector<T, N>::erase(const_iterator pos) -> iterator
  299. {
  300. YT_ASSERT(pos >= begin());
  301. YT_ASSERT(pos < end());
  302. auto* mutablePos = const_cast<iterator>(pos);
  303. Move(mutablePos + 1, end(), mutablePos);
  304. pop_back();
  305. return mutablePos;
  306. }
  307. template <class T, size_t N>
  308. auto TCompactVector<T, N>::erase(const_iterator first, const_iterator last) -> iterator
  309. {
  310. YT_ASSERT(first >= begin());
  311. YT_ASSERT(last <= end());
  312. auto* mutableFirst = const_cast<iterator>(first);
  313. auto* mutableLast = const_cast<iterator>(last);
  314. auto count = std::distance(mutableFirst, mutableLast);
  315. if (Y_LIKELY(IsInline())) {
  316. auto* end = &InlineElements_[0] + InlineMeta_.SizePlusOne - 1;
  317. Move(mutableLast, end, mutableFirst);
  318. Destroy(end - count, end);
  319. InlineMeta_.SizePlusOne -= count;
  320. } else {
  321. auto* storage = OnHeapMeta_.Storage;
  322. auto* end = storage->End;
  323. Move(mutableLast, storage->End, mutableFirst);
  324. Destroy(end - count, end);
  325. storage->End -= count;
  326. }
  327. return mutableFirst;
  328. }
  329. template <class T, size_t N>
  330. void TCompactVector<T, N>::clear()
  331. {
  332. if (Y_LIKELY(IsInline())) {
  333. Destroy(&InlineElements_[0], &InlineElements_[InlineMeta_.SizePlusOne - 1]);
  334. InlineMeta_.SizePlusOne = 1;
  335. } else {
  336. auto* storage = OnHeapMeta_.Storage;
  337. Destroy(storage->Elements, storage->End);
  338. storage->End = storage->Elements;
  339. }
  340. }
  341. template <class T, size_t N>
  342. void TCompactVector<T, N>::resize(size_type newSize)
  343. {
  344. ResizeImpl(
  345. newSize,
  346. [] (auto* dst) {
  347. ::new(dst) T();
  348. });
  349. }
  350. template <class T, size_t N>
  351. void TCompactVector<T, N>::resize(size_type newSize, const T& value)
  352. {
  353. ResizeImpl(
  354. newSize,
  355. [&] (auto* dst) {
  356. ::new(dst) T(value);
  357. });
  358. }
  359. template <class T, size_t N>
  360. void TCompactVector<T, N>::reserve(size_t newCapacity)
  361. {
  362. if (Y_UNLIKELY(newCapacity > N)) {
  363. EnsureOnHeapCapacity(newCapacity, /*incremental*/ false);
  364. }
  365. }
  366. template <class T, size_t N>
  367. void TCompactVector<T, N>::swap(TCompactVector& other)
  368. {
  369. if (this == &other) {
  370. return;
  371. }
  372. if (!IsInline() && !other.IsInline()) {
  373. std::swap(OnHeapMeta_.Storage, other.OnHeapMeta_.Storage);
  374. return;
  375. }
  376. auto* lhs = this;
  377. auto* rhs = &other;
  378. if (lhs->size() < rhs->size()) {
  379. std::swap(lhs, rhs);
  380. }
  381. size_t rhsSize = rhs->size();
  382. size_t lhsSize = lhs->size();
  383. if (lhsSize > rhs->capacity()) {
  384. rhs->EnsureOnHeapCapacity(lhs->size(), /*incremental*/ false);
  385. }
  386. for (size_t index = 0; index < rhsSize; ++index) {
  387. std::swap((*lhs)[index], (*rhs)[index]);
  388. }
  389. UninitializedMove(lhs->begin() + rhsSize, lhs->end(), rhs->end());
  390. Destroy(lhs->begin() + rhsSize, lhs->end());
  391. rhs->SetSize(lhsSize);
  392. lhs->SetSize(rhsSize);
  393. }
  394. template <class T, size_t N>
  395. void TCompactVector<T, N>::assign(size_type count, const T& value)
  396. {
  397. clear();
  398. if (Y_UNLIKELY(count > capacity())) {
  399. EnsureOnHeapCapacity(count, /*incremental*/ false);
  400. }
  401. auto* dst = begin();
  402. std::uninitialized_fill(dst, dst + count, value);
  403. SetSize(count);
  404. }
  405. template <class T, size_t N>
  406. template <class TIterator>
  407. void TCompactVector<T, N>::assign(TIterator first, TIterator last)
  408. {
  409. clear();
  410. auto count = std::distance(first, last);
  411. if (Y_UNLIKELY(count > static_cast<ptrdiff_t>(capacity()))) {
  412. EnsureOnHeapCapacity(count, /*incremental*/ false);
  413. }
  414. std::uninitialized_copy(first, last, begin());
  415. SetSize(count);
  416. }
  417. template <class T, size_t N>
  418. void TCompactVector<T, N>::assign(std::initializer_list<T> list)
  419. {
  420. assign(list.begin(), list.end());
  421. }
  422. template <class T, size_t N>
  423. template <size_t OtherN>
  424. void TCompactVector<T, N>::assign(const TCompactVector<T, OtherN>& other)
  425. {
  426. if constexpr(N == OtherN) {
  427. if (this == &other) {
  428. return;
  429. }
  430. }
  431. auto otherSize = other.size();
  432. auto otherBegin = other.begin();
  433. if (capacity() >= otherSize) {
  434. const auto* src = other.begin();
  435. auto* dst = begin();
  436. auto thisSize = size();
  437. auto copySize = std::min(thisSize, otherSize);
  438. Copy(src, src + copySize, dst);
  439. src += copySize;
  440. dst += copySize;
  441. auto uninitializedCopySize = otherSize - copySize;
  442. UninitializedCopy(src, src + uninitializedCopySize, dst);
  443. // NB: src += uninitializedCopySize is not needed.
  444. dst += uninitializedCopySize;
  445. if (thisSize > otherSize) {
  446. Destroy(dst, end());
  447. }
  448. SetSize(otherSize);
  449. return;
  450. }
  451. clear();
  452. EnsureOnHeapCapacity(otherSize, /*incremental*/ false);
  453. YT_ASSERT(!IsInline());
  454. auto* storage = OnHeapMeta_.Storage;
  455. UninitializedCopy(otherBegin, otherBegin + otherSize, storage->Elements);
  456. storage->End = storage->Elements + otherSize;
  457. }
  458. template <class T, size_t N>
  459. template <size_t OtherN>
  460. void TCompactVector<T, N>::assign(TCompactVector<T, OtherN>&& other)
  461. {
  462. if constexpr(N == OtherN) {
  463. if (this == &other) {
  464. return;
  465. }
  466. }
  467. clear();
  468. if (!other.IsInline()) {
  469. if (Y_UNLIKELY(!IsInline())) {
  470. ::free(OnHeapMeta_.Storage);
  471. }
  472. OnHeapMeta_.Storage = other.OnHeapMeta_.Storage;
  473. other.InlineMeta_.SizePlusOne = 1;
  474. return;
  475. }
  476. auto otherSize = other.size();
  477. if (Y_UNLIKELY(otherSize > capacity())) {
  478. EnsureOnHeapCapacity(otherSize, /*incremental*/ false);
  479. }
  480. auto* otherBegin = other.begin();
  481. UninitializedMove(otherBegin, otherBegin + otherSize, begin());
  482. SetSize(otherSize);
  483. other.clear();
  484. }
  485. template <class T, size_t N>
  486. auto TCompactVector<T, N>::operator=(const TCompactVector& other) -> TCompactVector&
  487. {
  488. assign(other);
  489. return *this;
  490. }
  491. template <class T, size_t N>
  492. template <size_t OtherN>
  493. auto TCompactVector<T, N>::operator=(const TCompactVector<T, OtherN>& other) -> TCompactVector&
  494. {
  495. assign(other);
  496. return *this;
  497. }
  498. template <class T, size_t N>
  499. auto TCompactVector<T, N>::operator=(TCompactVector&& other) -> TCompactVector&
  500. {
  501. assign(std::move(other));
  502. return *this;
  503. }
  504. template <class T, size_t N>
  505. template <size_t OtherN>
  506. auto TCompactVector<T, N>::operator=(TCompactVector<T, OtherN>&& other) -> TCompactVector&
  507. {
  508. assign(std::move(other));
  509. return *this;
  510. }
  511. template <class T, size_t N>
  512. auto TCompactVector<T, N>::operator=(std::initializer_list<T> list) -> TCompactVector&
  513. {
  514. assign(list);
  515. return *this;
  516. }
  517. template <class T, size_t N>
  518. auto TCompactVector<T, N>::insert(const_iterator pos, const T& value) -> iterator
  519. {
  520. return InsertOneImpl(
  521. pos,
  522. &value,
  523. [&] (auto* dst, const auto* value) {
  524. ::new(dst) T(*value);
  525. },
  526. [&] (auto* dst, const auto* value) {
  527. *dst = *value;
  528. });
  529. }
  530. template <class T, size_t N>
  531. auto TCompactVector<T, N>::insert(const_iterator pos, T&& value) -> iterator
  532. {
  533. return InsertOneImpl(
  534. pos,
  535. &value,
  536. [&] (auto* dst, auto* value) {
  537. ::new(dst) T(std::move(*value));
  538. },
  539. [&] (auto* dst, auto* value) {
  540. *dst = std::move(*value);
  541. });
  542. }
  543. template <class T, size_t N>
  544. auto TCompactVector<T, N>::insert(const_iterator pos, size_type count, const T& value) -> iterator
  545. {
  546. return InsertManyImpl(
  547. pos,
  548. count,
  549. [&] (auto* dstFirst, auto* dstLast) {
  550. for (auto* dst = dstFirst; dst != dstLast; ++dst) {
  551. ::new(dst) T(value);
  552. }
  553. },
  554. [&] (auto* dstFirst, auto* dstLast) {
  555. for (auto* dst = dstFirst; dst != dstLast; ++dst) {
  556. *dst = value;
  557. }
  558. });
  559. }
  560. template <class T, size_t N>
  561. template <class TIterator>
  562. auto TCompactVector<T, N>::insert(const_iterator pos, TIterator first, TIterator last) -> iterator
  563. {
  564. auto current = first;
  565. return InsertManyImpl(
  566. pos,
  567. std::distance(first, last),
  568. [&] (auto* dstFirst, auto* dstLast) {
  569. for (auto* dst = dstFirst; dst != dstLast; ++dst) {
  570. ::new(dst) T(*current++);
  571. }
  572. },
  573. [&] (auto* dstFirst, auto* dstLast) {
  574. for (auto* dst = dstFirst; dst != dstLast; ++dst) {
  575. *dst = *current++;
  576. }
  577. });
  578. }
  579. template <class T, size_t N>
  580. auto TCompactVector<T, N>::insert(const_iterator pos, std::initializer_list<T> list) -> iterator
  581. {
  582. return insert(pos, list.begin(), list.end());
  583. }
  584. template <class T, size_t N>
  585. void TCompactVector<T, N>::shrink_to_small()
  586. {
  587. if (Y_LIKELY(IsInline())) {
  588. return;
  589. }
  590. auto size = this->size();
  591. if (size > N) {
  592. return;
  593. }
  594. auto* storage = OnHeapMeta_.Storage;
  595. UninitializedMove(storage->Elements, storage->End, &InlineElements_[0]);
  596. Destroy(storage->Elements, storage->End);
  597. ::free(storage);
  598. InlineMeta_.SizePlusOne = size + 1;
  599. }
  600. template <class T, size_t N>
  601. bool TCompactVector<T, N>::IsInline() const
  602. {
  603. return InlineMeta_.SizePlusOne != 0;
  604. }
  605. template <class T, size_t N>
  606. void TCompactVector<T, N>::SetSize(size_t newSize)
  607. {
  608. if (Y_LIKELY(IsInline())) {
  609. InlineMeta_.SizePlusOne = newSize + 1;
  610. } else {
  611. auto* storage = OnHeapMeta_.Storage;
  612. storage->End = storage->Elements + newSize;
  613. }
  614. }
  615. template <class T, size_t N>
  616. void TCompactVector<T, N>::EnsureOnHeapCapacity(size_t newCapacity, bool incremental)
  617. {
  618. newCapacity = std::max(newCapacity, N + 1);
  619. if (incremental) {
  620. newCapacity = std::max(newCapacity, capacity() * 2);
  621. }
  622. auto byteSize = sizeof(TOnHeapStorage) + newCapacity * sizeof(T);
  623. byteSize = nallocx(byteSize, 0);
  624. newCapacity = (byteSize - sizeof(TOnHeapStorage)) / sizeof(T);
  625. auto* newStorage = static_cast<TOnHeapStorage*>(::malloc(byteSize));
  626. YT_VERIFY((reinterpret_cast<uintptr_t>(newStorage) >> 56) == 0);
  627. newStorage->Capacity = newStorage->Elements + newCapacity;
  628. size_t size;
  629. if (IsInline()) {
  630. size = InlineMeta_.SizePlusOne - 1;
  631. UninitializedMove(&InlineElements_[0], &InlineElements_[0] + size, newStorage->Elements);
  632. Destroy(&InlineElements_[0], &InlineElements_[0] + size);
  633. } else {
  634. auto* storage = OnHeapMeta_.Storage;
  635. size = storage->End - storage->Elements;
  636. UninitializedMove(storage->Elements, storage->End, newStorage->Elements);
  637. Destroy(storage->Elements, storage->End);
  638. ::free(storage);
  639. }
  640. newStorage->End = newStorage->Elements + size;
  641. OnHeapMeta_.Storage = newStorage;
  642. }
  643. template <class T, size_t N>
  644. template <class TPtr, class F>
  645. auto TCompactVector<T, N>::PushBackImpl(TPtr valuePtr, F&& func) -> reference
  646. {
  647. auto sizePlusOne = InlineMeta_.SizePlusOne;
  648. if (Y_LIKELY(sizePlusOne != 0 && sizePlusOne != N + 1)) {
  649. auto* dst = &InlineElements_[sizePlusOne - 1];
  650. func(dst, valuePtr);
  651. ++InlineMeta_.SizePlusOne;
  652. return *dst;
  653. }
  654. auto hasSpareOnHeapCapacity = [&] {
  655. if (sizePlusOne != 0) {
  656. return false;
  657. }
  658. auto* storage = OnHeapMeta_.Storage;
  659. return storage->End < storage->Capacity;
  660. };
  661. if (Y_UNLIKELY(!hasSpareOnHeapCapacity())) {
  662. TCompactVectorReallocationPtrAdjuster<TCompactVector, TPtr> valuePtrAdjuster(this, valuePtr);
  663. EnsureOnHeapCapacity(0, /*incremental*/ true);
  664. }
  665. YT_ASSERT(!IsInline());
  666. auto* storage = OnHeapMeta_.Storage;
  667. auto* dst = storage->End++;
  668. func(dst, valuePtr);
  669. return *dst;
  670. }
  671. template <class T, size_t N>
  672. template <class F>
  673. void TCompactVector<T, N>::ResizeImpl(size_type newSize, F&& func)
  674. {
  675. auto size = this->size();
  676. if (newSize > size) {
  677. if (Y_UNLIKELY(newSize > capacity())) {
  678. EnsureOnHeapCapacity(newSize, /*incremental*/ false);
  679. }
  680. auto* first = end();
  681. auto* last = first + newSize - size;
  682. for (auto* current = first; current != last; ++current) {
  683. func(current);
  684. }
  685. } else if (newSize < size) {
  686. Destroy(begin() + newSize, end());
  687. }
  688. SetSize(newSize);
  689. }
  690. template <class T, size_t N>
  691. template <class TPtr, class UninitializedF, class InitializedF>
  692. auto TCompactVector<T, N>::InsertOneImpl(const_iterator pos, TPtr valuePtr, UninitializedF&& uninitializedFunc, InitializedF&& initializedFunc) -> iterator
  693. {
  694. YT_ASSERT(pos >= begin());
  695. YT_ASSERT(pos <= end());
  696. auto* mutablePos = const_cast<iterator>(pos);
  697. auto newSize = size() + 1;
  698. if (Y_UNLIKELY(newSize > capacity())) {
  699. TCompactVectorReallocationPtrAdjuster<TCompactVector, iterator> mutablePosAdjuster(this, mutablePos);
  700. TCompactVectorReallocationPtrAdjuster<TCompactVector, TPtr> valuePtrAdjuster(this, valuePtr);
  701. EnsureOnHeapCapacity(newSize, /*incremental*/ true);
  702. }
  703. auto* end = this->end();
  704. if constexpr(!std::is_same_v<TPtr, std::nullptr_t>) {
  705. if (valuePtr >= mutablePos && valuePtr < end) {
  706. ++valuePtr;
  707. }
  708. }
  709. auto moveCount = std::distance(mutablePos, end);
  710. if (moveCount == 0) {
  711. uninitializedFunc(end, valuePtr);
  712. } else {
  713. if constexpr(std::is_trivially_copyable_v<T>) {
  714. ::memmove(mutablePos + 1, mutablePos, moveCount * sizeof(T));
  715. } else {
  716. ::new(end) T(std::move(end[-1]));
  717. MoveBackward(mutablePos, end - 1, mutablePos + 1);
  718. }
  719. initializedFunc(mutablePos, valuePtr);
  720. }
  721. SetSize(newSize);
  722. return mutablePos;
  723. }
  724. template <class T, size_t N>
  725. template <class UninitializedF, class InitializedF>
  726. auto TCompactVector<T, N>::InsertManyImpl(const_iterator pos, size_t insertCount, UninitializedF&& uninitializedFunc, InitializedF&& initializedFunc) -> iterator
  727. {
  728. YT_ASSERT(pos >= begin());
  729. YT_ASSERT(pos <= end());
  730. auto* mutablePos = const_cast<iterator>(pos);
  731. if (insertCount == 0) {
  732. return mutablePos;
  733. }
  734. auto size = this->size();
  735. auto newSize = size + insertCount;
  736. if (Y_UNLIKELY(newSize > capacity())) {
  737. auto index = std::distance(begin(), mutablePos);
  738. EnsureOnHeapCapacity(newSize, /*incremental*/ true);
  739. mutablePos = begin() + index;
  740. }
  741. auto* end = this->end();
  742. auto moveCount = std::distance(mutablePos, end);
  743. if constexpr(std::is_trivially_copyable_v<T>) {
  744. ::memmove(mutablePos + insertCount, mutablePos, moveCount * sizeof(T));
  745. initializedFunc(mutablePos, mutablePos + insertCount);
  746. } else {
  747. if (static_cast<ptrdiff_t>(insertCount) >= moveCount) {
  748. UninitializedMove(mutablePos, end, mutablePos + insertCount);
  749. initializedFunc(mutablePos, end);
  750. uninitializedFunc(end, end + insertCount - moveCount);
  751. } else {
  752. auto overlapCount = moveCount - insertCount;
  753. UninitializedMove(mutablePos + overlapCount, end, mutablePos + overlapCount + insertCount);
  754. MoveBackward(mutablePos, mutablePos + overlapCount, mutablePos + insertCount);
  755. initializedFunc(mutablePos, mutablePos + insertCount);
  756. }
  757. }
  758. SetSize(newSize);
  759. return mutablePos;
  760. }
  761. template <class T, size_t N>
  762. void TCompactVector<T, N>::Destroy(T* first, T* last)
  763. {
  764. if constexpr(!std::is_trivially_destructible_v<T>) {
  765. for (auto* current = first; current != last; ++current) {
  766. current->T::~T();
  767. }
  768. }
  769. }
  770. template <class T, size_t N>
  771. template <class T1, class T2>
  772. void TCompactVector<T, N>::Copy(const T1* srcFirst, const T1* srcLast, T2* dst)
  773. {
  774. if constexpr(std::is_trivially_copyable_v<T1> && std::is_same_v<T1, T2>) {
  775. ::memcpy(dst, srcFirst, (srcLast - srcFirst) * sizeof(T));
  776. } else {
  777. std::copy(srcFirst, srcLast, dst);
  778. }
  779. }
  780. template <class T, size_t N>
  781. template <class T1, class T2>
  782. void TCompactVector<T, N>::UninitializedCopy(const T1* srcFirst, const T1* srcLast, T2* dst)
  783. {
  784. if constexpr(std::is_trivially_copyable_v<T1> && std::is_same_v<T1, T2>) {
  785. ::memcpy(dst, srcFirst, (srcLast - srcFirst) * sizeof(T));
  786. } else {
  787. std::uninitialized_copy(srcFirst, srcLast, dst);
  788. }
  789. }
  790. template <class T, size_t N>
  791. void TCompactVector<T, N>::Move(T* srcFirst, T* srcLast, T* dst)
  792. {
  793. if constexpr(std::is_trivially_copyable_v<T>) {
  794. ::memmove(dst, srcFirst, (srcLast - srcFirst) * sizeof(T));
  795. } else {
  796. std::move(srcFirst, srcLast, dst);
  797. }
  798. }
  799. template <class T, size_t N>
  800. void TCompactVector<T, N>::UninitializedMove(T* srcFirst, T* srcLast, T* dst)
  801. {
  802. if constexpr(std::is_trivially_copyable_v<T>) {
  803. ::memcpy(dst, srcFirst, (srcLast - srcFirst) * sizeof(T));
  804. } else {
  805. std::uninitialized_move(srcFirst, srcLast, dst);
  806. }
  807. }
  808. template <class T, size_t N>
  809. void TCompactVector<T, N>::MoveBackward(T* srcFirst, T* srcLast, T* dst)
  810. {
  811. auto* src = srcLast;
  812. dst += std::distance(srcFirst, srcLast);
  813. while (src > srcFirst) {
  814. *--dst = std::move(*--src);
  815. }
  816. }
  817. /////////////////////////////////////////////////////////////////////////////
  818. template <class T, size_t LhsN, size_t RhsN>
  819. bool operator==(const TCompactVector<T, LhsN>& lhs, const TCompactVector<T, RhsN>& rhs)
  820. {
  821. if constexpr(LhsN == RhsN) {
  822. if (&lhs == &rhs) {
  823. return true;
  824. }
  825. }
  826. if (lhs.size() != rhs.size()) {
  827. return false;
  828. }
  829. return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
  830. }
  831. template <class T, size_t LhsN, size_t RhsN>
  832. bool operator!=(const TCompactVector<T, LhsN>& lhs, const TCompactVector<T, RhsN>& rhs)
  833. {
  834. return !(lhs == rhs);
  835. }
  836. template <class T, size_t LhsN, size_t RhsN>
  837. bool operator<(const TCompactVector<T, LhsN>& lhs, const TCompactVector<T, RhsN>& rhs)
  838. {
  839. return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
  840. }
  841. ////////////////////////////////////////////////////////////////////////////////
  842. template <class T, size_t N>
  843. void swap(TCompactVector<T, N>& lhs, TCompactVector<T, N>& rhs) // NOLINT
  844. {
  845. lhs.swap(rhs);
  846. }
  847. /////////////////////////////////////////////////////////////////////////////
  848. } // namespace NYT
  849. namespace std {
  850. ////////////////////////////////////////////////////////////////////////////////
  851. template <class T, size_t N>
  852. struct hash<NYT::TCompactVector<T, N>>
  853. {
  854. size_t operator()(const NYT::TCompactVector<T, N>& container) const
  855. {
  856. size_t result = 0;
  857. for (const auto& element : container) {
  858. NYT::HashCombine(result, element);
  859. }
  860. return result;
  861. }
  862. };
  863. ////////////////////////////////////////////////////////////////////////////////
  864. } // namespace std