SmallVector.h 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  1. //===- llvm/ADT/SmallVector.h - 'Normally small' vectors --------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines the SmallVector class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. // ATen: modified from llvm::SmallVector.
  13. // used std::is_trivially_{copy,move}_constructible
  14. // replaced iterator_range constructor with inline Container&& constructor
  15. // replaced LLVM_NODISCARD, LLVM_LIKELY, and LLVM_UNLIKELY with c10 equivalents
  16. // removed LLVM_GSL_OWNER
  17. // added SmallVector::at
  18. // added operator<< for std::ostream
  19. // added C10_API to export SmallVectorBase
  20. #pragma once
  21. #include <c10/macros/Macros.h>
  22. #include <c10/util/AlignOf.h>
  23. #include <algorithm>
  24. #include <cassert>
  25. #include <cstddef>
  26. #include <cstdlib>
  27. #include <cstring>
  28. #include <functional>
  29. #include <initializer_list>
  30. #include <iterator>
  31. #include <limits>
  32. #include <memory>
  33. #include <ostream>
  34. #include <type_traits>
  35. #include <utility>
  36. namespace c10 {
  37. /// This is all the stuff common to all SmallVectors.
  38. ///
  39. /// The template parameter specifies the type which should be used to hold the
  40. /// Size and Capacity of the SmallVector, so it can be adjusted.
  41. /// Using 32 bit size is desirable to shrink the size of the SmallVector.
  42. /// Using 64 bit size is desirable for cases like SmallVector<char>, where a
  43. /// 32 bit size would limit the vector to ~4GB. SmallVectors are used for
  44. /// buffering bitcode output - which can exceed 4GB.
  45. template <class Size_T>
  46. class C10_API SmallVectorBase {
  47. protected:
  48. void* BeginX;
  49. Size_T Size = 0, Capacity;
  50. /// The maximum value of the Size_T used.
  51. static constexpr size_t SizeTypeMax() {
  52. return std::numeric_limits<Size_T>::max();
  53. }
  54. SmallVectorBase(void* FirstEl, size_t TotalCapacity)
  55. : BeginX(FirstEl), Capacity(TotalCapacity) {}
  56. /// This is a helper for \a grow() that's out of line to reduce code
  57. /// duplication. This function will report a fatal error if it can't grow at
  58. /// least to \p MinSize.
  59. void* mallocForGrow(size_t MinSize, size_t TSize, size_t& NewCapacity);
  60. /// This is an implementation of the grow() method which only works
  61. /// on POD-like data types and is out of line to reduce code duplication.
  62. /// This function will report a fatal error if it cannot increase capacity.
  63. void grow_pod(const void* FirstEl, size_t MinSize, size_t TSize);
  64. public:
  65. SmallVectorBase() = delete;
  66. size_t size() const {
  67. return Size;
  68. }
  69. size_t capacity() const {
  70. return Capacity;
  71. }
  72. C10_NODISCARD bool empty() const {
  73. return !Size;
  74. }
  75. /// Set the array size to \p N, which the current array must have enough
  76. /// capacity for.
  77. ///
  78. /// This does not construct or destroy any elements in the vector.
  79. ///
  80. /// Clients can use this in conjunction with capacity() to write past the end
  81. /// of the buffer when they know that more elements are available, and only
  82. /// update the size later. This avoids the cost of value initializing elements
  83. /// which will only be overwritten.
  84. void set_size(size_t N) {
  85. assert(N <= capacity());
  86. Size = N;
  87. }
  88. };
  89. template <class T>
  90. using SmallVectorSizeType =
  91. std::conditional_t<sizeof(T) < 4 && sizeof(void*) >= 8, uint64_t, uint32_t>;
  92. /// Figure out the offset of the first element.
  93. template <class T, typename = void>
  94. struct SmallVectorAlignmentAndSize {
  95. // NOLINTNEXTLINE(*c-arrays*)
  96. alignas(SmallVectorBase<SmallVectorSizeType<T>>) char Base[sizeof(
  97. SmallVectorBase<SmallVectorSizeType<T>>)];
  98. // NOLINTNEXTLINE(*c-arrays*)
  99. alignas(T) char FirstEl[sizeof(T)];
  100. };
  101. /// This is the part of SmallVectorTemplateBase which does not depend on whether
  102. /// the type T is a POD. The extra dummy template argument is used by ArrayRef
  103. /// to avoid unnecessarily requiring T to be complete.
  104. template <typename T, typename = void>
  105. class SmallVectorTemplateCommon
  106. : public SmallVectorBase<SmallVectorSizeType<T>> {
  107. using Base = SmallVectorBase<SmallVectorSizeType<T>>;
  108. /// Find the address of the first element. For this pointer math to be valid
  109. /// with small-size of 0 for T with lots of alignment, it's important that
  110. /// SmallVectorStorage is properly-aligned even for small-size of 0.
  111. void* getFirstEl() const {
  112. return const_cast<void*>(reinterpret_cast<const void*>(
  113. reinterpret_cast<const char*>(this) +
  114. offsetof(SmallVectorAlignmentAndSize<T>, FirstEl)));
  115. }
  116. // Space after 'FirstEl' is clobbered, do not add any instance vars after it.
  117. protected:
  118. SmallVectorTemplateCommon(size_t Size) : Base(getFirstEl(), Size) {}
  119. void grow_pod(size_t MinSize, size_t TSize) {
  120. Base::grow_pod(getFirstEl(), MinSize, TSize);
  121. }
  122. /// Return true if this is a smallvector which has not had dynamic
  123. /// memory allocated for it.
  124. bool isSmall() const {
  125. return this->BeginX == getFirstEl();
  126. }
  127. /// Put this vector in a state of being small.
  128. void resetToSmall() {
  129. this->BeginX = getFirstEl();
  130. this->Size = this->Capacity = 0; // FIXME: Setting Capacity to 0 is suspect.
  131. }
  132. /// Return true if V is an internal reference to the given range.
  133. bool isReferenceToRange(const void* V, const void* First, const void* Last)
  134. const {
  135. // Use std::less to avoid UB.
  136. std::less<> LessThan;
  137. return !LessThan(V, First) && LessThan(V, Last);
  138. }
  139. /// Return true if V is an internal reference to this vector.
  140. bool isReferenceToStorage(const void* V) const {
  141. return isReferenceToRange(V, this->begin(), this->end());
  142. }
  143. /// Return true if First and Last form a valid (possibly empty) range in this
  144. /// vector's storage.
  145. bool isRangeInStorage(const void* First, const void* Last) const {
  146. // Use std::less to avoid UB.
  147. std::less<> LessThan;
  148. return !LessThan(First, this->begin()) && !LessThan(Last, First) &&
  149. !LessThan(this->end(), Last);
  150. }
  151. /// Return true unless Elt will be invalidated by resizing the vector to
  152. /// NewSize.
  153. bool isSafeToReferenceAfterResize(const void* Elt, size_t NewSize) {
  154. // Past the end.
  155. if (C10_LIKELY(!isReferenceToStorage(Elt)))
  156. return true;
  157. // Return false if Elt will be destroyed by shrinking.
  158. if (NewSize <= this->size())
  159. return Elt < this->begin() + NewSize;
  160. // Return false if we need to grow.
  161. return NewSize <= this->capacity();
  162. }
  163. /// Check whether Elt will be invalidated by resizing the vector to NewSize.
  164. void assertSafeToReferenceAfterResize(const void* Elt, size_t NewSize) {
  165. (void)Elt; // Suppress unused variable warning
  166. (void)NewSize; // Suppress unused variable warning
  167. assert(
  168. isSafeToReferenceAfterResize(Elt, NewSize) &&
  169. "Attempting to reference an element of the vector in an operation "
  170. "that invalidates it");
  171. }
  172. /// Check whether Elt will be invalidated by increasing the size of the
  173. /// vector by N.
  174. void assertSafeToAdd(const void* Elt, size_t N = 1) {
  175. this->assertSafeToReferenceAfterResize(Elt, this->size() + N);
  176. }
  177. /// Check whether any part of the range will be invalidated by clearing.
  178. void assertSafeToReferenceAfterClear(const T* From, const T* To) {
  179. if (From == To)
  180. return;
  181. this->assertSafeToReferenceAfterResize(From, 0);
  182. this->assertSafeToReferenceAfterResize(To - 1, 0);
  183. }
  184. template <
  185. class ItTy,
  186. std::enable_if_t<!std::is_same_v<std::remove_const_t<ItTy>, T*>, bool> =
  187. false>
  188. void assertSafeToReferenceAfterClear(ItTy, ItTy) {}
  189. /// Check whether any part of the range will be invalidated by growing.
  190. void assertSafeToAddRange(const T* From, const T* To) {
  191. if (From == To)
  192. return;
  193. this->assertSafeToAdd(From, To - From);
  194. this->assertSafeToAdd(To - 1, To - From);
  195. }
  196. template <
  197. class ItTy,
  198. std::enable_if_t<!std::is_same_v<std::remove_const_t<ItTy>, T*>, bool> =
  199. false>
  200. void assertSafeToAddRange(ItTy, ItTy) {}
  201. /// Reserve enough space to add one element, and return the updated element
  202. /// pointer in case it was a reference to the storage.
  203. template <class U>
  204. static const T* reserveForParamAndGetAddressImpl(
  205. U* This,
  206. const T& Elt,
  207. size_t N) {
  208. size_t NewSize = This->size() + N;
  209. if (C10_LIKELY(NewSize <= This->capacity()))
  210. return &Elt;
  211. bool ReferencesStorage = false;
  212. int64_t Index = -1;
  213. if constexpr (!U::TakesParamByValue) {
  214. if (C10_UNLIKELY(This->isReferenceToStorage(&Elt))) {
  215. ReferencesStorage = true;
  216. Index = &Elt - This->begin();
  217. }
  218. }
  219. This->grow(NewSize);
  220. return ReferencesStorage ? This->begin() + Index : &Elt;
  221. }
  222. public:
  223. using size_type = size_t;
  224. using difference_type = ptrdiff_t;
  225. using value_type = T;
  226. using iterator = T*;
  227. using const_iterator = const T*;
  228. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  229. using reverse_iterator = std::reverse_iterator<iterator>;
  230. using reference = T&;
  231. using const_reference = const T&;
  232. using pointer = T*;
  233. using const_pointer = const T*;
  234. using Base::capacity;
  235. using Base::empty;
  236. using Base::size;
  237. // forward iterator creation methods.
  238. iterator begin() {
  239. return (iterator)this->BeginX;
  240. }
  241. const_iterator begin() const {
  242. return (const_iterator)this->BeginX;
  243. }
  244. iterator end() {
  245. return begin() + size();
  246. }
  247. const_iterator end() const {
  248. return begin() + size();
  249. }
  250. // reverse iterator creation methods.
  251. reverse_iterator rbegin() {
  252. return reverse_iterator(end());
  253. }
  254. const_reverse_iterator rbegin() const {
  255. return const_reverse_iterator(end());
  256. }
  257. reverse_iterator rend() {
  258. return reverse_iterator(begin());
  259. }
  260. const_reverse_iterator rend() const {
  261. return const_reverse_iterator(begin());
  262. }
  263. size_type size_in_bytes() const {
  264. return size() * sizeof(T);
  265. }
  266. constexpr size_type max_size() const {
  267. return std::min(this->SizeTypeMax(), size_type(-1) / sizeof(T));
  268. }
  269. size_t capacity_in_bytes() const {
  270. return capacity() * sizeof(T);
  271. }
  272. /// Return a pointer to the vector's buffer, even if empty().
  273. pointer data() {
  274. return pointer(begin());
  275. }
  276. /// Return a pointer to the vector's buffer, even if empty().
  277. const_pointer data() const {
  278. return const_pointer(begin());
  279. }
  280. // SmallVector::at is NOT from LLVM.
  281. reference at(size_type idx) {
  282. assert(idx < size());
  283. return begin()[idx];
  284. }
  285. const_reference at(size_type idx) const {
  286. assert(idx < size());
  287. return begin()[idx];
  288. }
  289. reference operator[](size_type idx) {
  290. assert(idx < size());
  291. return begin()[idx];
  292. }
  293. const_reference operator[](size_type idx) const {
  294. assert(idx < size());
  295. return begin()[idx];
  296. }
  297. reference front() {
  298. assert(!empty());
  299. return begin()[0];
  300. }
  301. const_reference front() const {
  302. assert(!empty());
  303. return begin()[0];
  304. }
  305. reference back() {
  306. assert(!empty());
  307. return end()[-1];
  308. }
  309. const_reference back() const {
  310. assert(!empty());
  311. return end()[-1];
  312. }
  313. };
  314. /// SmallVectorTemplateBase<TriviallyCopyable = false> - This is where we put
  315. /// method implementations that are designed to work with non-trivial T's.
  316. ///
  317. /// We approximate is_trivially_copyable with trivial move/copy construction and
  318. /// trivial destruction. While the standard doesn't specify that you're allowed
  319. /// copy these types with memcpy, there is no way for the type to observe this.
  320. /// This catches the important case of std::pair<POD, POD>, which is not
  321. /// trivially assignable.
  322. ///
  323. /// XXX: if build fails here fall back to C10_IS_TRIVIALLY_COPYABLE and make a
  324. /// note
  325. template <
  326. typename T,
  327. bool = (std::is_trivially_copy_constructible_v<T>)&&(
  328. std::is_trivially_move_constructible_v<
  329. T>)&&std::is_trivially_destructible_v<T>>
  330. class SmallVectorTemplateBase : public SmallVectorTemplateCommon<T> {
  331. friend class SmallVectorTemplateCommon<T>;
  332. protected:
  333. static constexpr bool TakesParamByValue = false;
  334. using ValueParamT = const T&;
  335. SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {}
  336. static void destroy_range(T* S, T* E) {
  337. while (S != E) {
  338. --E;
  339. E->~T();
  340. }
  341. }
  342. /// Move the range [I, E) into the uninitialized memory starting with "Dest",
  343. /// constructing elements as needed.
  344. template <typename It1, typename It2>
  345. static void uninitialized_move(It1 I, It1 E, It2 Dest) {
  346. std::uninitialized_copy(
  347. std::make_move_iterator(I), std::make_move_iterator(E), Dest);
  348. }
  349. /// Copy the range [I, E) onto the uninitialized memory starting with "Dest",
  350. /// constructing elements as needed.
  351. template <typename It1, typename It2>
  352. static void uninitialized_copy(It1 I, It1 E, It2 Dest) {
  353. std::uninitialized_copy(I, E, Dest);
  354. }
  355. /// Grow the allocated memory (without initializing new elements), doubling
  356. /// the size of the allocated memory. Guarantees space for at least one more
  357. /// element, or MinSize more elements if specified.
  358. void grow(size_t MinSize = 0);
  359. /// Create a new allocation big enough for \p MinSize and pass back its size
  360. /// in \p NewCapacity. This is the first section of \a grow().
  361. T* mallocForGrow(size_t MinSize, size_t& NewCapacity) {
  362. return static_cast<T*>(
  363. SmallVectorBase<SmallVectorSizeType<T>>::mallocForGrow(
  364. MinSize, sizeof(T), NewCapacity));
  365. }
  366. /// Move existing elements over to the new allocation \p NewElts, the middle
  367. /// section of \a grow().
  368. void moveElementsForGrow(T* NewElts);
  369. /// Transfer ownership of the allocation, finishing up \a grow().
  370. void takeAllocationForGrow(T* NewElts, size_t NewCapacity);
  371. /// Reserve enough space to add one element, and return the updated element
  372. /// pointer in case it was a reference to the storage.
  373. const T* reserveForParamAndGetAddress(const T& Elt, size_t N = 1) {
  374. return this->reserveForParamAndGetAddressImpl(this, Elt, N);
  375. }
  376. /// Reserve enough space to add one element, and return the updated element
  377. /// pointer in case it was a reference to the storage.
  378. T* reserveForParamAndGetAddress(T& Elt, size_t N = 1) {
  379. return const_cast<T*>(this->reserveForParamAndGetAddressImpl(this, Elt, N));
  380. }
  381. static T&& forward_value_param(T&& V) {
  382. return std::move(V);
  383. }
  384. static const T& forward_value_param(const T& V) {
  385. return V;
  386. }
  387. void growAndAssign(size_t NumElts, const T& Elt) {
  388. // Grow manually in case Elt is an internal reference.
  389. size_t NewCapacity = 0;
  390. T* NewElts = mallocForGrow(NumElts, NewCapacity);
  391. std::uninitialized_fill_n(NewElts, NumElts, Elt);
  392. this->destroy_range(this->begin(), this->end());
  393. takeAllocationForGrow(NewElts, NewCapacity);
  394. this->set_size(NumElts);
  395. }
  396. template <typename... ArgTypes>
  397. T& growAndEmplaceBack(ArgTypes&&... Args) {
  398. // Grow manually in case one of Args is an internal reference.
  399. size_t NewCapacity = 0;
  400. T* NewElts = mallocForGrow(0, NewCapacity);
  401. ::new ((void*)(NewElts + this->size())) T(std::forward<ArgTypes>(Args)...);
  402. moveElementsForGrow(NewElts);
  403. takeAllocationForGrow(NewElts, NewCapacity);
  404. this->set_size(this->size() + 1);
  405. return this->back();
  406. }
  407. public:
  408. void push_back(const T& Elt) {
  409. const T* EltPtr = reserveForParamAndGetAddress(Elt);
  410. ::new ((void*)this->end()) T(*EltPtr);
  411. this->set_size(this->size() + 1);
  412. }
  413. // NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved)
  414. void push_back(T&& Elt) {
  415. T* EltPtr = reserveForParamAndGetAddress(Elt);
  416. ::new ((void*)this->end()) T(::std::move(*EltPtr));
  417. this->set_size(this->size() + 1);
  418. }
  419. void pop_back() {
  420. this->set_size(this->size() - 1);
  421. this->end()->~T();
  422. }
  423. };
  424. // Define this out-of-line to dissuade the C++ compiler from inlining it.
  425. template <typename T, bool TriviallyCopyable>
  426. void SmallVectorTemplateBase<T, TriviallyCopyable>::grow(size_t MinSize) {
  427. size_t NewCapacity = 0;
  428. T* NewElts = mallocForGrow(MinSize, NewCapacity);
  429. moveElementsForGrow(NewElts);
  430. takeAllocationForGrow(NewElts, NewCapacity);
  431. }
  432. // Define this out-of-line to dissuade the C++ compiler from inlining it.
  433. template <typename T, bool TriviallyCopyable>
  434. void SmallVectorTemplateBase<T, TriviallyCopyable>::moveElementsForGrow(
  435. T* NewElts) {
  436. // Move the elements over.
  437. this->uninitialized_move(this->begin(), this->end(), NewElts);
  438. // Destroy the original elements.
  439. destroy_range(this->begin(), this->end());
  440. }
  441. // Define this out-of-line to dissuade the C++ compiler from inlining it.
  442. template <typename T, bool TriviallyCopyable>
  443. void SmallVectorTemplateBase<T, TriviallyCopyable>::takeAllocationForGrow(
  444. T* NewElts,
  445. size_t NewCapacity) {
  446. // If this wasn't grown from the inline copy, deallocate the old space.
  447. if (!this->isSmall())
  448. free(this->begin());
  449. this->BeginX = NewElts;
  450. this->Capacity = NewCapacity;
  451. }
  452. /// SmallVectorTemplateBase<TriviallyCopyable = true> - This is where we put
  453. /// method implementations that are designed to work with trivially copyable
  454. /// T's. This allows using memcpy in place of copy/move construction and
  455. /// skipping destruction.
  456. template <typename T>
  457. class SmallVectorTemplateBase<T, true> : public SmallVectorTemplateCommon<T> {
  458. friend class SmallVectorTemplateCommon<T>;
  459. protected:
  460. /// True if it's cheap enough to take parameters by value. Doing so avoids
  461. /// overhead related to mitigations for reference invalidation.
  462. static constexpr bool TakesParamByValue = sizeof(T) <= 2 * sizeof(void*);
  463. /// Either const T& or T, depending on whether it's cheap enough to take
  464. /// parameters by value.
  465. using ValueParamT = std::conditional_t<TakesParamByValue, T, const T&>;
  466. SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {}
  467. // No need to do a destroy loop for POD's.
  468. static void destroy_range(T*, T*) {}
  469. /// Move the range [I, E) onto the uninitialized memory
  470. /// starting with "Dest", constructing elements into it as needed.
  471. template <typename It1, typename It2>
  472. static void uninitialized_move(It1 I, It1 E, It2 Dest) {
  473. // Just do a copy.
  474. uninitialized_copy(I, E, Dest);
  475. }
  476. /// Copy the range [I, E) onto the uninitialized memory
  477. /// starting with "Dest", constructing elements into it as needed.
  478. template <typename It1, typename It2>
  479. static void uninitialized_copy(It1 I, It1 E, It2 Dest) {
  480. // Arbitrary iterator types; just use the basic implementation.
  481. std::uninitialized_copy(I, E, Dest);
  482. }
  483. /// Copy the range [I, E) onto the uninitialized memory
  484. /// starting with "Dest", constructing elements into it as needed.
  485. template <typename T1, typename T2>
  486. static void uninitialized_copy(
  487. T1* I,
  488. T1* E,
  489. T2* Dest,
  490. std::enable_if_t<std::is_same_v<std::remove_const_t<T1>, T2>>* =
  491. nullptr) {
  492. // Use memcpy for PODs iterated by pointers (which includes SmallVector
  493. // iterators): std::uninitialized_copy optimizes to memmove, but we can
  494. // use memcpy here. Note that I and E are iterators and thus might be
  495. // invalid for memcpy if they are equal.
  496. if (I != E)
  497. memcpy(reinterpret_cast<void*>(Dest), I, (E - I) * sizeof(T));
  498. }
  499. /// Double the size of the allocated memory, guaranteeing space for at
  500. /// least one more element or MinSize if specified.
  501. void grow(size_t MinSize = 0) {
  502. this->grow_pod(MinSize, sizeof(T));
  503. }
  504. /// Reserve enough space to add one element, and return the updated element
  505. /// pointer in case it was a reference to the storage.
  506. const T* reserveForParamAndGetAddress(const T& Elt, size_t N = 1) {
  507. return this->reserveForParamAndGetAddressImpl(this, Elt, N);
  508. }
  509. /// Reserve enough space to add one element, and return the updated element
  510. /// pointer in case it was a reference to the storage.
  511. T* reserveForParamAndGetAddress(T& Elt, size_t N = 1) {
  512. return const_cast<T*>(this->reserveForParamAndGetAddressImpl(this, Elt, N));
  513. }
  514. /// Copy \p V or return a reference, depending on \a ValueParamT.
  515. static ValueParamT forward_value_param(ValueParamT V) {
  516. return V;
  517. }
  518. void growAndAssign(size_t NumElts, T Elt) {
  519. // Elt has been copied in case it's an internal reference, side-stepping
  520. // reference invalidation problems without losing the realloc optimization.
  521. this->set_size(0);
  522. this->grow(NumElts);
  523. std::uninitialized_fill_n(this->begin(), NumElts, Elt);
  524. this->set_size(NumElts);
  525. }
  526. template <typename... ArgTypes>
  527. T& growAndEmplaceBack(ArgTypes&&... Args) {
  528. // Use push_back with a copy in case Args has an internal reference,
  529. // side-stepping reference invalidation problems without losing the realloc
  530. // optimization.
  531. push_back(T(std::forward<ArgTypes>(Args)...));
  532. return this->back();
  533. }
  534. public:
  535. void push_back(ValueParamT Elt) {
  536. const T* EltPtr = reserveForParamAndGetAddress(Elt);
  537. memcpy(reinterpret_cast<void*>(this->end()), EltPtr, sizeof(T));
  538. this->set_size(this->size() + 1);
  539. }
  540. void pop_back() {
  541. this->set_size(this->size() - 1);
  542. }
  543. };
  544. /// This class consists of common code factored out of the SmallVector class to
  545. /// reduce code duplication based on the SmallVector 'N' template parameter.
  546. template <typename T>
  547. class SmallVectorImpl : public SmallVectorTemplateBase<T> {
  548. using SuperClass = SmallVectorTemplateBase<T>;
  549. public:
  550. using iterator = typename SuperClass::iterator;
  551. using const_iterator = typename SuperClass::const_iterator;
  552. using reference = typename SuperClass::reference;
  553. using size_type = typename SuperClass::size_type;
  554. protected:
  555. using SmallVectorTemplateBase<T>::TakesParamByValue;
  556. using ValueParamT = typename SuperClass::ValueParamT;
  557. // Default ctor - Initialize to empty.
  558. explicit SmallVectorImpl(unsigned N) : SmallVectorTemplateBase<T>(N) {}
  559. public:
  560. SmallVectorImpl(const SmallVectorImpl&) = delete;
  561. ~SmallVectorImpl() {
  562. // Subclass has already destructed this vector's elements.
  563. // If this wasn't grown from the inline copy, deallocate the old space.
  564. if (!this->isSmall())
  565. free(this->begin());
  566. }
  567. void clear() {
  568. this->destroy_range(this->begin(), this->end());
  569. this->Size = 0;
  570. }
  571. private:
  572. template <bool ForOverwrite>
  573. void resizeImpl(size_type N) {
  574. if (N < this->size()) {
  575. this->pop_back_n(this->size() - N);
  576. } else if (N > this->size()) {
  577. this->reserve(N);
  578. for (auto I = this->end(), E = this->begin() + N; I != E; ++I)
  579. if (ForOverwrite)
  580. new (&*I) T;
  581. else
  582. new (&*I) T();
  583. this->set_size(N);
  584. }
  585. }
  586. public:
  587. void resize(size_type N) {
  588. resizeImpl<false>(N);
  589. }
  590. /// Like resize, but \ref T is POD, the new values won't be initialized.
  591. void resize_for_overwrite(size_type N) {
  592. resizeImpl<true>(N);
  593. }
  594. void resize(size_type N, ValueParamT NV) {
  595. if (N == this->size())
  596. return;
  597. if (N < this->size()) {
  598. this->pop_back_n(this->size() - N);
  599. return;
  600. }
  601. // N > this->size(). Defer to append.
  602. this->append(N - this->size(), NV);
  603. }
  604. void reserve(size_type N) {
  605. if (this->capacity() < N)
  606. this->grow(N);
  607. }
  608. void pop_back_n(size_type NumItems) {
  609. assert(this->size() >= NumItems);
  610. this->destroy_range(this->end() - NumItems, this->end());
  611. this->set_size(this->size() - NumItems);
  612. }
  613. C10_NODISCARD T pop_back_val() {
  614. T Result = ::std::move(this->back());
  615. this->pop_back();
  616. return Result;
  617. }
  618. void swap(SmallVectorImpl& RHS) noexcept;
  619. /// Add the specified range to the end of the SmallVector.
  620. template <
  621. typename in_iter,
  622. typename = std::enable_if_t<std::is_convertible_v<
  623. typename std::iterator_traits<in_iter>::iterator_category,
  624. std::input_iterator_tag>>>
  625. void append(in_iter in_start, in_iter in_end) {
  626. this->assertSafeToAddRange(in_start, in_end);
  627. size_type NumInputs = std::distance(in_start, in_end);
  628. this->reserve(this->size() + NumInputs);
  629. this->uninitialized_copy(in_start, in_end, this->end());
  630. this->set_size(this->size() + NumInputs);
  631. }
  632. /// Append \p NumInputs copies of \p Elt to the end.
  633. void append(size_type NumInputs, ValueParamT Elt) {
  634. const T* EltPtr = this->reserveForParamAndGetAddress(Elt, NumInputs);
  635. std::uninitialized_fill_n(this->end(), NumInputs, *EltPtr);
  636. this->set_size(this->size() + NumInputs);
  637. }
  638. void append(std::initializer_list<T> IL) {
  639. append(IL.begin(), IL.end());
  640. }
  641. void append(const SmallVectorImpl& RHS) {
  642. append(RHS.begin(), RHS.end());
  643. }
  644. void assign(size_type NumElts, ValueParamT Elt) {
  645. // Note that Elt could be an internal reference.
  646. if (NumElts > this->capacity()) {
  647. this->growAndAssign(NumElts, Elt);
  648. return;
  649. }
  650. // Assign over existing elements.
  651. std::fill_n(this->begin(), std::min(NumElts, this->size()), Elt);
  652. if (NumElts > this->size())
  653. std::uninitialized_fill_n(this->end(), NumElts - this->size(), Elt);
  654. else if (NumElts < this->size())
  655. this->destroy_range(this->begin() + NumElts, this->end());
  656. this->set_size(NumElts);
  657. }
  658. // FIXME: Consider assigning over existing elements, rather than clearing &
  659. // re-initializing them - for all assign(...) variants.
  660. template <
  661. typename in_iter,
  662. typename = std::enable_if_t<std::is_convertible_v<
  663. typename std::iterator_traits<in_iter>::iterator_category,
  664. std::input_iterator_tag>>>
  665. void assign(in_iter in_start, in_iter in_end) {
  666. this->assertSafeToReferenceAfterClear(in_start, in_end);
  667. clear();
  668. append(in_start, in_end);
  669. }
  670. void assign(std::initializer_list<T> IL) {
  671. clear();
  672. append(IL);
  673. }
  674. void assign(const SmallVectorImpl& RHS) {
  675. assign(RHS.begin(), RHS.end());
  676. }
  677. iterator erase(iterator I) {
  678. assert(
  679. this->isReferenceToStorage(I) && "Iterator to erase is out of bounds.");
  680. iterator N = I;
  681. // Shift all elts down one.
  682. std::move(I + 1, this->end(), I);
  683. // Drop the last elt.
  684. this->pop_back();
  685. return (N);
  686. }
  687. iterator erase(iterator S, iterator E) {
  688. assert(this->isRangeInStorage(S, E) && "Range to erase is out of bounds.");
  689. iterator N = S;
  690. // Shift all elts down.
  691. iterator I = std::move(E, this->end(), S);
  692. // Drop the last elts.
  693. this->destroy_range(I, this->end());
  694. this->set_size(I - this->begin());
  695. return (N);
  696. }
  697. private:
  698. template <class ArgType>
  699. iterator insert_one_impl(iterator I, ArgType&& Elt) {
  700. // Callers ensure that ArgType is derived from T.
  701. static_assert(
  702. std::is_same<std::remove_const_t<std::remove_reference_t<ArgType>>, T>::
  703. value,
  704. "ArgType must be derived from T!");
  705. if (I == this->end()) { // Important special case for empty vector.
  706. this->push_back(::std::forward<ArgType>(Elt));
  707. return this->end() - 1;
  708. }
  709. assert(
  710. this->isReferenceToStorage(I) &&
  711. "Insertion iterator is out of bounds.");
  712. // Grow if necessary.
  713. size_t Index = I - this->begin();
  714. std::remove_reference_t<ArgType>* EltPtr =
  715. this->reserveForParamAndGetAddress(Elt);
  716. I = this->begin() + Index;
  717. ::new ((void*)this->end()) T(::std::move(this->back()));
  718. // Push everything else over.
  719. std::move_backward(I, this->end() - 1, this->end());
  720. this->set_size(this->size() + 1);
  721. // If we just moved the element we're inserting, be sure to update
  722. // the reference (never happens if TakesParamByValue).
  723. static_assert(
  724. !TakesParamByValue || std::is_same<ArgType, T>::value,
  725. "ArgType must be 'T' when taking by value!");
  726. if (!TakesParamByValue && this->isReferenceToRange(EltPtr, I, this->end()))
  727. ++EltPtr;
  728. *I = ::std::forward<ArgType>(*EltPtr);
  729. return I;
  730. }
  731. public:
  732. iterator insert(iterator I, T&& Elt) {
  733. return insert_one_impl(I, this->forward_value_param(std::move(Elt)));
  734. }
  735. iterator insert(iterator I, const T& Elt) {
  736. return insert_one_impl(I, this->forward_value_param(Elt));
  737. }
  738. iterator insert(iterator I, size_type NumToInsert, ValueParamT Elt) {
  739. // Convert iterator to elt# to avoid invalidating iterator when we reserve()
  740. size_t InsertElt = I - this->begin();
  741. if (I == this->end()) { // Important special case for empty vector.
  742. append(NumToInsert, Elt);
  743. return this->begin() + InsertElt;
  744. }
  745. assert(
  746. this->isReferenceToStorage(I) &&
  747. "Insertion iterator is out of bounds.");
  748. // Ensure there is enough space, and get the (maybe updated) address of
  749. // Elt.
  750. const T* EltPtr = this->reserveForParamAndGetAddress(Elt, NumToInsert);
  751. // Uninvalidate the iterator.
  752. I = this->begin() + InsertElt;
  753. // If there are more elements between the insertion point and the end of the
  754. // range than there are being inserted, we can use a simple approach to
  755. // insertion. Since we already reserved space, we know that this won't
  756. // reallocate the vector.
  757. if (size_t(this->end() - I) >= NumToInsert) {
  758. T* OldEnd = this->end();
  759. append(
  760. std::move_iterator<iterator>(this->end() - NumToInsert),
  761. std::move_iterator<iterator>(this->end()));
  762. // Copy the existing elements that get replaced.
  763. std::move_backward(I, OldEnd - NumToInsert, OldEnd);
  764. // If we just moved the element we're inserting, be sure to update
  765. // the reference (never happens if TakesParamByValue).
  766. if (!TakesParamByValue && I <= EltPtr && EltPtr < this->end())
  767. EltPtr += NumToInsert;
  768. std::fill_n(I, NumToInsert, *EltPtr);
  769. return I;
  770. }
  771. // Otherwise, we're inserting more elements than exist already, and we're
  772. // not inserting at the end.
  773. // Move over the elements that we're about to overwrite.
  774. T* OldEnd = this->end();
  775. this->set_size(this->size() + NumToInsert);
  776. size_t NumOverwritten = OldEnd - I;
  777. this->uninitialized_move(I, OldEnd, this->end() - NumOverwritten);
  778. // If we just moved the element we're inserting, be sure to update
  779. // the reference (never happens if TakesParamByValue).
  780. if (!TakesParamByValue && I <= EltPtr && EltPtr < this->end())
  781. EltPtr += NumToInsert;
  782. // Replace the overwritten part.
  783. std::fill_n(I, NumOverwritten, *EltPtr);
  784. // Insert the non-overwritten middle part.
  785. std::uninitialized_fill_n(OldEnd, NumToInsert - NumOverwritten, *EltPtr);
  786. return I;
  787. }
  788. template <
  789. typename ItTy,
  790. typename = std::enable_if_t<std::is_convertible_v<
  791. typename std::iterator_traits<ItTy>::iterator_category,
  792. std::input_iterator_tag>>>
  793. iterator insert(iterator I, ItTy From, ItTy To) {
  794. // Convert iterator to elt# to avoid invalidating iterator when we reserve()
  795. size_t InsertElt = I - this->begin();
  796. if (I == this->end()) { // Important special case for empty vector.
  797. append(From, To);
  798. return this->begin() + InsertElt;
  799. }
  800. assert(
  801. this->isReferenceToStorage(I) &&
  802. "Insertion iterator is out of bounds.");
  803. // Check that the reserve that follows doesn't invalidate the iterators.
  804. this->assertSafeToAddRange(From, To);
  805. size_t NumToInsert = std::distance(From, To);
  806. // Ensure there is enough space.
  807. reserve(this->size() + NumToInsert);
  808. // Uninvalidate the iterator.
  809. I = this->begin() + InsertElt;
  810. // If there are more elements between the insertion point and the end of the
  811. // range than there are being inserted, we can use a simple approach to
  812. // insertion. Since we already reserved space, we know that this won't
  813. // reallocate the vector.
  814. if (size_t(this->end() - I) >= NumToInsert) {
  815. T* OldEnd = this->end();
  816. append(
  817. std::move_iterator<iterator>(this->end() - NumToInsert),
  818. std::move_iterator<iterator>(this->end()));
  819. // Copy the existing elements that get replaced.
  820. std::move_backward(I, OldEnd - NumToInsert, OldEnd);
  821. std::copy(From, To, I);
  822. return I;
  823. }
  824. // Otherwise, we're inserting more elements than exist already, and we're
  825. // not inserting at the end.
  826. // Move over the elements that we're about to overwrite.
  827. T* OldEnd = this->end();
  828. this->set_size(this->size() + NumToInsert);
  829. size_t NumOverwritten = OldEnd - I;
  830. this->uninitialized_move(I, OldEnd, this->end() - NumOverwritten);
  831. // Replace the overwritten part.
  832. for (T* J = I; NumOverwritten > 0; --NumOverwritten) {
  833. *J = *From;
  834. ++J;
  835. ++From;
  836. }
  837. // Insert the non-overwritten middle part.
  838. this->uninitialized_copy(From, To, OldEnd);
  839. return I;
  840. }
  841. void insert(iterator I, std::initializer_list<T> IL) {
  842. insert(I, IL.begin(), IL.end());
  843. }
  844. template <typename... ArgTypes>
  845. reference emplace_back(ArgTypes&&... Args) {
  846. if (C10_UNLIKELY(this->size() >= this->capacity()))
  847. return this->growAndEmplaceBack(std::forward<ArgTypes>(Args)...);
  848. ::new ((void*)this->end()) T(std::forward<ArgTypes>(Args)...);
  849. this->set_size(this->size() + 1);
  850. return this->back();
  851. }
  852. SmallVectorImpl& operator=(const SmallVectorImpl& RHS);
  853. SmallVectorImpl& operator=(SmallVectorImpl&& RHS) noexcept(
  854. std::is_nothrow_move_constructible_v<T> &&
  855. std::is_nothrow_destructible_v<T>);
  856. bool operator==(const SmallVectorImpl& RHS) const {
  857. if (this->size() != RHS.size())
  858. return false;
  859. return std::equal(this->begin(), this->end(), RHS.begin());
  860. }
  861. bool operator!=(const SmallVectorImpl& RHS) const {
  862. return !(*this == RHS);
  863. }
  864. bool operator<(const SmallVectorImpl& RHS) const {
  865. return std::lexicographical_compare(
  866. this->begin(), this->end(), RHS.begin(), RHS.end());
  867. }
  868. };
  869. template <typename T>
  870. void SmallVectorImpl<T>::swap(SmallVectorImpl<T>& RHS) noexcept {
  871. if (this == &RHS)
  872. return;
  873. // We can only avoid copying elements if neither vector is small.
  874. if (!this->isSmall() && !RHS.isSmall()) {
  875. std::swap(this->BeginX, RHS.BeginX);
  876. std::swap(this->Size, RHS.Size);
  877. std::swap(this->Capacity, RHS.Capacity);
  878. return;
  879. }
  880. this->reserve(RHS.size());
  881. RHS.reserve(this->size());
  882. // Swap the shared elements.
  883. size_t NumShared = this->size();
  884. if (NumShared > RHS.size())
  885. NumShared = RHS.size();
  886. for (size_type i = 0; i != NumShared; ++i)
  887. std::swap((*this)[i], RHS[i]);
  888. // Copy over the extra elts.
  889. if (this->size() > RHS.size()) {
  890. size_t EltDiff = this->size() - RHS.size();
  891. this->uninitialized_copy(this->begin() + NumShared, this->end(), RHS.end());
  892. RHS.set_size(RHS.size() + EltDiff);
  893. this->destroy_range(this->begin() + NumShared, this->end());
  894. this->set_size(NumShared);
  895. } else if (RHS.size() > this->size()) {
  896. size_t EltDiff = RHS.size() - this->size();
  897. this->uninitialized_copy(RHS.begin() + NumShared, RHS.end(), this->end());
  898. this->set_size(this->size() + EltDiff);
  899. this->destroy_range(RHS.begin() + NumShared, RHS.end());
  900. RHS.set_size(NumShared);
  901. }
  902. }
  903. template <typename T>
  904. SmallVectorImpl<T>& SmallVectorImpl<T>::operator=(
  905. const SmallVectorImpl<T>& RHS) {
  906. // Avoid self-assignment.
  907. if (this == &RHS)
  908. return *this;
  909. // If we already have sufficient space, assign the common elements, then
  910. // destroy any excess.
  911. size_t RHSSize = RHS.size();
  912. size_t CurSize = this->size();
  913. if (CurSize >= RHSSize) {
  914. // Assign common elements.
  915. iterator NewEnd;
  916. if (RHSSize)
  917. NewEnd = std::copy(RHS.begin(), RHS.begin() + RHSSize, this->begin());
  918. else
  919. NewEnd = this->begin();
  920. // Destroy excess elements.
  921. this->destroy_range(NewEnd, this->end());
  922. // Trim.
  923. this->set_size(RHSSize);
  924. return *this;
  925. }
  926. // If we have to grow to have enough elements, destroy the current elements.
  927. // This allows us to avoid copying them during the grow.
  928. // FIXME: don't do this if they're efficiently moveable.
  929. if (this->capacity() < RHSSize) {
  930. // Destroy current elements.
  931. this->clear();
  932. CurSize = 0;
  933. this->grow(RHSSize);
  934. } else if (CurSize) {
  935. // Otherwise, use assignment for the already-constructed elements.
  936. std::copy(RHS.begin(), RHS.begin() + CurSize, this->begin());
  937. }
  938. // Copy construct the new elements in place.
  939. this->uninitialized_copy(
  940. RHS.begin() + CurSize, RHS.end(), this->begin() + CurSize);
  941. // Set end.
  942. this->set_size(RHSSize);
  943. return *this;
  944. }
  945. template <typename T>
  946. SmallVectorImpl<T>& SmallVectorImpl<T>::
  947. operator=(SmallVectorImpl<T>&& RHS) noexcept(
  948. std::is_nothrow_move_constructible_v<T> &&
  949. std::is_nothrow_destructible_v<T>) {
  950. // Avoid self-assignment.
  951. if (this == &RHS)
  952. return *this;
  953. // If the RHS isn't small, clear this vector and then steal its buffer.
  954. if (!RHS.isSmall()) {
  955. this->destroy_range(this->begin(), this->end());
  956. if (!this->isSmall())
  957. free(this->begin());
  958. this->BeginX = RHS.BeginX;
  959. this->Size = RHS.Size;
  960. this->Capacity = RHS.Capacity;
  961. RHS.resetToSmall();
  962. return *this;
  963. }
  964. // If we already have sufficient space, assign the common elements, then
  965. // destroy any excess.
  966. size_t RHSSize = RHS.size();
  967. size_t CurSize = this->size();
  968. if (CurSize >= RHSSize) {
  969. // Assign common elements.
  970. iterator NewEnd = this->begin();
  971. if (RHSSize)
  972. NewEnd = std::move(RHS.begin(), RHS.end(), NewEnd);
  973. // Destroy excess elements and trim the bounds.
  974. this->destroy_range(NewEnd, this->end());
  975. this->set_size(RHSSize);
  976. // Clear the RHS.
  977. RHS.clear();
  978. return *this;
  979. }
  980. // If we have to grow to have enough elements, destroy the current elements.
  981. // This allows us to avoid copying them during the grow.
  982. // FIXME: this may not actually make any sense if we can efficiently move
  983. // elements.
  984. if (this->capacity() < RHSSize) {
  985. // Destroy current elements.
  986. this->clear();
  987. CurSize = 0;
  988. this->grow(RHSSize);
  989. } else if (CurSize) {
  990. // Otherwise, use assignment for the already-constructed elements.
  991. std::move(RHS.begin(), RHS.begin() + CurSize, this->begin());
  992. }
  993. // Move-construct the new elements in place.
  994. this->uninitialized_move(
  995. RHS.begin() + CurSize, RHS.end(), this->begin() + CurSize);
  996. // Set end.
  997. this->set_size(RHSSize);
  998. RHS.clear();
  999. return *this;
  1000. }
  1001. /// Storage for the SmallVector elements. This is specialized for the N=0 case
  1002. /// to avoid allocating unnecessary storage.
  1003. template <typename T, unsigned N>
  1004. struct SmallVectorStorage {
  1005. alignas(T) char InlineElts[N * sizeof(T)];
  1006. };
  1007. /// We need the storage to be properly aligned even for small-size of 0 so that
  1008. /// the pointer math in \a SmallVectorTemplateCommon::getFirstEl() is
  1009. /// well-defined.
  1010. template <typename T>
  1011. struct alignas(T) SmallVectorStorage<T, 0> {};
  1012. /// Forward declaration of SmallVector so that
  1013. /// calculateSmallVectorDefaultInlinedElements can reference
  1014. /// `sizeof(SmallVector<T, 0>)`.
  1015. template <typename T, unsigned N>
  1016. class /* LLVM_GSL_OWNER */ SmallVector;
  1017. /// Helper class for calculating the default number of inline elements for
  1018. /// `SmallVector<T>`.
  1019. ///
  1020. /// This should be migrated to a constexpr function when our minimum
  1021. /// compiler support is enough for multi-statement constexpr functions.
  1022. template <typename T>
  1023. struct CalculateSmallVectorDefaultInlinedElements {
  1024. // Parameter controlling the default number of inlined elements
  1025. // for `SmallVector<T>`.
  1026. //
  1027. // The default number of inlined elements ensures that
  1028. // 1. There is at least one inlined element.
  1029. // 2. `sizeof(SmallVector<T>) <= kPreferredSmallVectorSizeof` unless
  1030. // it contradicts 1.
  1031. static constexpr size_t kPreferredSmallVectorSizeof = 64;
  1032. // static_assert that sizeof(T) is not "too big".
  1033. //
  1034. // Because our policy guarantees at least one inlined element, it is possible
  1035. // for an arbitrarily large inlined element to allocate an arbitrarily large
  1036. // amount of inline storage. We generally consider it an antipattern for a
  1037. // SmallVector to allocate an excessive amount of inline storage, so we want
  1038. // to call attention to these cases and make sure that users are making an
  1039. // intentional decision if they request a lot of inline storage.
  1040. //
  1041. // We want this assertion to trigger in pathological cases, but otherwise
  1042. // not be too easy to hit. To accomplish that, the cutoff is actually somewhat
  1043. // larger than kPreferredSmallVectorSizeof (otherwise,
  1044. // `SmallVector<SmallVector<T>>` would be one easy way to trip it, and that
  1045. // pattern seems useful in practice).
  1046. //
  1047. // One wrinkle is that this assertion is in theory non-portable, since
  1048. // sizeof(T) is in general platform-dependent. However, we don't expect this
  1049. // to be much of an issue, because most LLVM development happens on 64-bit
  1050. // hosts, and therefore sizeof(T) is expected to *decrease* when compiled for
  1051. // 32-bit hosts, dodging the issue. The reverse situation, where development
  1052. // happens on a 32-bit host and then fails due to sizeof(T) *increasing* on a
  1053. // 64-bit host, is expected to be very rare.
  1054. static_assert(
  1055. sizeof(T) <= 256,
  1056. "You are trying to use a default number of inlined elements for "
  1057. "`SmallVector<T>` but `sizeof(T)` is really big! Please use an "
  1058. "explicit number of inlined elements with `SmallVector<T, N>` to make "
  1059. "sure you really want that much inline storage.");
  1060. // Discount the size of the header itself when calculating the maximum inline
  1061. // bytes.
  1062. static constexpr size_t PreferredInlineBytes =
  1063. kPreferredSmallVectorSizeof - sizeof(SmallVector<T, 0>);
  1064. static constexpr size_t NumElementsThatFit = PreferredInlineBytes / sizeof(T);
  1065. static constexpr size_t value =
  1066. NumElementsThatFit == 0 ? 1 : NumElementsThatFit;
  1067. };
  1068. /// This is a 'vector' (really, a variable-sized array), optimized
  1069. /// for the case when the array is small. It contains some number of elements
  1070. /// in-place, which allows it to avoid heap allocation when the actual number of
  1071. /// elements is below that threshold. This allows normal "small" cases to be
  1072. /// fast without losing generality for large inputs.
  1073. ///
  1074. /// \note
  1075. /// In the absence of a well-motivated choice for the number of inlined
  1076. /// elements \p N, it is recommended to use \c SmallVector<T> (that is,
  1077. /// omitting the \p N). This will choose a default number of inlined elements
  1078. /// reasonable for allocation on the stack (for example, trying to keep \c
  1079. /// sizeof(SmallVector<T>) around 64 bytes).
  1080. ///
  1081. /// \warning This does not attempt to be exception safe.
  1082. ///
  1083. /// \see https://llvm.org/docs/ProgrammersManual.html#llvm-adt-smallvector-h
  1084. template <
  1085. typename T,
  1086. unsigned N = CalculateSmallVectorDefaultInlinedElements<T>::value>
  1087. class /* LLVM_GSL_OWNER */ SmallVector : public SmallVectorImpl<T>,
  1088. SmallVectorStorage<T, N> {
  1089. public:
  1090. SmallVector() : SmallVectorImpl<T>(N) {}
  1091. ~SmallVector() {
  1092. // Destroy the constructed elements in the vector.
  1093. this->destroy_range(this->begin(), this->end());
  1094. }
  1095. explicit SmallVector(size_t Size, const T& Value = T())
  1096. : SmallVectorImpl<T>(N) {
  1097. this->assign(Size, Value);
  1098. }
  1099. template <
  1100. typename ItTy,
  1101. typename = std::enable_if_t<std::is_convertible_v<
  1102. typename std::iterator_traits<ItTy>::iterator_category,
  1103. std::input_iterator_tag>>>
  1104. SmallVector(ItTy S, ItTy E) : SmallVectorImpl<T>(N) {
  1105. this->append(S, E);
  1106. }
  1107. // note: The enable_if restricts Container to types that have a .begin() and
  1108. // .end() that return valid input iterators.
  1109. template <
  1110. typename Container,
  1111. std::enable_if_t<
  1112. std::is_convertible_v<
  1113. typename std::iterator_traits<
  1114. decltype(std::declval<Container>()
  1115. .begin())>::iterator_category,
  1116. std::input_iterator_tag> &&
  1117. std::is_convertible_v<
  1118. typename std::iterator_traits<
  1119. decltype(std::declval<Container>()
  1120. .end())>::iterator_category,
  1121. std::input_iterator_tag>,
  1122. int> = 0>
  1123. explicit SmallVector(Container&& c) : SmallVectorImpl<T>(N) {
  1124. this->append(c.begin(), c.end());
  1125. }
  1126. SmallVector(std::initializer_list<T> IL) : SmallVectorImpl<T>(N) {
  1127. this->assign(IL);
  1128. }
  1129. SmallVector(const SmallVector& RHS) : SmallVectorImpl<T>(N) {
  1130. if (!RHS.empty())
  1131. SmallVectorImpl<T>::operator=(RHS);
  1132. }
  1133. SmallVector& operator=(const SmallVector& RHS) {
  1134. SmallVectorImpl<T>::operator=(RHS);
  1135. return *this;
  1136. }
  1137. SmallVector(SmallVector&& RHS) noexcept(
  1138. std::is_nothrow_move_assignable_v<SmallVectorImpl<T>>)
  1139. : SmallVectorImpl<T>(N) {
  1140. if (!RHS.empty())
  1141. SmallVectorImpl<T>::operator=(::std::move(RHS));
  1142. }
  1143. // note: The enable_if restricts Container to types that have a .begin() and
  1144. // .end() that return valid input iterators.
  1145. template <
  1146. typename Container,
  1147. std::enable_if_t<
  1148. std::is_convertible_v<
  1149. typename std::iterator_traits<
  1150. decltype(std::declval<Container>()
  1151. .begin())>::iterator_category,
  1152. std::input_iterator_tag> &&
  1153. std::is_convertible_v<
  1154. typename std::iterator_traits<
  1155. decltype(std::declval<Container>()
  1156. .end())>::iterator_category,
  1157. std::input_iterator_tag>,
  1158. int> = 0>
  1159. SmallVector& operator=(const Container& RHS) {
  1160. this->assign(RHS.begin(), RHS.end());
  1161. return *this;
  1162. }
  1163. SmallVector(SmallVectorImpl<T>&& RHS) noexcept(
  1164. std::is_nothrow_move_assignable_v<SmallVectorImpl<T>>)
  1165. : SmallVectorImpl<T>(N) {
  1166. if (!RHS.empty())
  1167. SmallVectorImpl<T>::operator=(::std::move(RHS));
  1168. }
  1169. SmallVector& operator=(SmallVector&& RHS) noexcept(
  1170. std::is_nothrow_move_assignable_v<SmallVectorImpl<T>>) {
  1171. SmallVectorImpl<T>::operator=(::std::move(RHS));
  1172. return *this;
  1173. }
  1174. SmallVector& operator=(SmallVectorImpl<T>&& RHS) noexcept(
  1175. std::is_nothrow_move_constructible_v<SmallVectorImpl<T>>) {
  1176. SmallVectorImpl<T>::operator=(::std::move(RHS));
  1177. return *this;
  1178. }
  1179. // note: The enable_if restricts Container to types that have a .begin() and
  1180. // .end() that return valid input iterators.
  1181. template <
  1182. typename Container,
  1183. std::enable_if_t<
  1184. std::is_convertible_v<
  1185. typename std::iterator_traits<
  1186. decltype(std::declval<Container>()
  1187. .begin())>::iterator_category,
  1188. std::input_iterator_tag> &&
  1189. std::is_convertible_v<
  1190. typename std::iterator_traits<
  1191. decltype(std::declval<Container>()
  1192. .end())>::iterator_category,
  1193. std::input_iterator_tag>,
  1194. int> = 0>
  1195. // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
  1196. SmallVector& operator=(Container&& C) {
  1197. this->assign(C.begin(), C.end());
  1198. return *this;
  1199. }
  1200. SmallVector& operator=(std::initializer_list<T> IL) {
  1201. this->assign(IL);
  1202. return *this;
  1203. }
  1204. };
  1205. template <typename T, unsigned N>
  1206. inline size_t capacity_in_bytes(const SmallVector<T, N>& X) {
  1207. return X.capacity_in_bytes();
  1208. }
  1209. template <typename T, unsigned N>
  1210. std::ostream& operator<<(std::ostream& out, const SmallVector<T, N>& list) {
  1211. int i = 0;
  1212. out << "[";
  1213. for (auto e : list) {
  1214. if (i++ > 0)
  1215. out << ", ";
  1216. out << e;
  1217. }
  1218. out << "]";
  1219. return out;
  1220. }
  1221. template <typename RangeType>
  1222. using ValueTypeFromRangeType = std::remove_const_t<
  1223. std::remove_reference_t<decltype(*std::begin(std::declval<RangeType&>()))>>;
  1224. /// Given a range of type R, iterate the entire range and return a
  1225. /// SmallVector with elements of the vector. This is useful, for example,
  1226. /// when you want to iterate a range and then sort the results.
  1227. template <unsigned Size, typename R>
  1228. // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
  1229. SmallVector<ValueTypeFromRangeType<R>, Size> to_vector(R&& Range) {
  1230. return {std::begin(Range), std::end(Range)};
  1231. }
  1232. template <typename R>
  1233. SmallVector<
  1234. ValueTypeFromRangeType<R>,
  1235. CalculateSmallVectorDefaultInlinedElements<
  1236. ValueTypeFromRangeType<R>>::value>
  1237. // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
  1238. to_vector(R&& Range) {
  1239. return {std::begin(Range), std::end(Range)};
  1240. }
  1241. } // end namespace c10
  1242. namespace std {
  1243. /// Implement std::swap in terms of SmallVector swap.
  1244. template <typename T>
  1245. inline void swap(
  1246. c10::SmallVectorImpl<T>& LHS,
  1247. c10::SmallVectorImpl<T>& RHS) noexcept {
  1248. LHS.swap(RHS);
  1249. }
  1250. /// Implement std::swap in terms of SmallVector swap.
  1251. template <typename T, unsigned N>
  1252. inline void swap(
  1253. c10::SmallVector<T, N>& LHS,
  1254. c10::SmallVector<T, N>& RHS) noexcept {
  1255. LHS.swap(RHS);
  1256. }
  1257. } // end namespace std