SmallVector.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. //===- llvm/ADT/SmallVector.h - 'Normally small' vectors --------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the SmallVector class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_ADT_SMALLVECTOR_H
  14. #define LLVM_ADT_SMALLVECTOR_H
  15. #include "llvm/ADT/iterator_range.h"
  16. #include "llvm/Support/AlignOf.h"
  17. #include "llvm/Support/Compiler.h"
  18. #include "llvm/Support/MathExtras.h"
  19. #include "llvm/Support/type_traits.h"
  20. #include <algorithm>
  21. #include <cassert>
  22. #include <cstddef>
  23. #include <cstdlib>
  24. #include <cstring>
  25. #include <initializer_list>
  26. #include <iterator>
  27. #include <memory>
  28. namespace llvm {
  29. /// This is all the non-templated stuff common to all SmallVectors.
  30. class SmallVectorBase {
  31. protected:
  32. void *BeginX, *EndX, *CapacityX;
  33. protected:
  34. SmallVectorBase(void *FirstEl, size_t Size)
  35. : BeginX(FirstEl), EndX(FirstEl), CapacityX((char*)FirstEl+Size) {}
  36. /// This is an implementation of the grow() method which only works
  37. /// on POD-like data types and is out of line to reduce code duplication.
  38. void grow_pod(void *FirstEl, size_t MinSizeInBytes, size_t TSize);
  39. public:
  40. /// This returns size()*sizeof(T).
  41. size_t size_in_bytes() const {
  42. return size_t((char*)EndX - (char*)BeginX);
  43. }
  44. /// capacity_in_bytes - This returns capacity()*sizeof(T).
  45. size_t capacity_in_bytes() const {
  46. return size_t((char*)CapacityX - (char*)BeginX);
  47. }
  48. bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const { return BeginX == EndX; }
  49. };
  50. template <typename T, unsigned N> struct SmallVectorStorage;
  51. /// This is the part of SmallVectorTemplateBase which does not depend on whether
  52. /// the type T is a POD. The extra dummy template argument is used by ArrayRef
  53. /// to avoid unnecessarily requiring T to be complete.
  54. template <typename T, typename = void>
  55. class SmallVectorTemplateCommon : public SmallVectorBase {
  56. private:
  57. template <typename, unsigned> friend struct SmallVectorStorage;
  58. // Allocate raw space for N elements of type T. If T has a ctor or dtor, we
  59. // don't want it to be automatically run, so we need to represent the space as
  60. // something else. Use an array of char of sufficient alignment.
  61. typedef llvm::AlignedCharArrayUnion<T> U;
  62. U FirstEl;
  63. // Space after 'FirstEl' is clobbered, do not add any instance vars after it.
  64. protected:
  65. SmallVectorTemplateCommon(size_t Size) : SmallVectorBase(&FirstEl, Size) {}
  66. void grow_pod(size_t MinSizeInBytes, size_t TSize) {
  67. SmallVectorBase::grow_pod(&FirstEl, MinSizeInBytes, TSize);
  68. }
  69. /// Return true if this is a smallvector which has not had dynamic
  70. /// memory allocated for it.
  71. bool isSmall() const {
  72. return BeginX == static_cast<const void*>(&FirstEl);
  73. }
  74. /// Put this vector in a state of being small.
  75. void resetToSmall() {
  76. BeginX = EndX = CapacityX = &FirstEl;
  77. }
  78. void setEnd(T *P) { this->EndX = P; }
  79. public:
  80. typedef size_t size_type;
  81. typedef ptrdiff_t difference_type;
  82. typedef T value_type;
  83. typedef T *iterator;
  84. typedef const T *const_iterator;
  85. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  86. typedef std::reverse_iterator<iterator> reverse_iterator;
  87. typedef T &reference;
  88. typedef const T &const_reference;
  89. typedef T *pointer;
  90. typedef const T *const_pointer;
  91. // forward iterator creation methods.
  92. iterator begin() { return (iterator)this->BeginX; }
  93. const_iterator begin() const { return (const_iterator)this->BeginX; }
  94. iterator end() { return (iterator)this->EndX; }
  95. const_iterator end() const { return (const_iterator)this->EndX; }
  96. protected:
  97. iterator capacity_ptr() { return (iterator)this->CapacityX; }
  98. const_iterator capacity_ptr() const { return (const_iterator)this->CapacityX;}
  99. public:
  100. // reverse iterator creation methods.
  101. reverse_iterator rbegin() { return reverse_iterator(end()); }
  102. const_reverse_iterator rbegin() const{ return const_reverse_iterator(end()); }
  103. reverse_iterator rend() { return reverse_iterator(begin()); }
  104. const_reverse_iterator rend() const { return const_reverse_iterator(begin());}
  105. size_type size() const { return end()-begin(); }
  106. size_type max_size() const { return size_type(-1) / sizeof(T); }
  107. /// Return the total number of elements in the currently allocated buffer.
  108. size_t capacity() const { return capacity_ptr() - begin(); }
  109. /// Return a pointer to the vector's buffer, even if empty().
  110. pointer data() { return pointer(begin()); }
  111. /// Return a pointer to the vector's buffer, even if empty().
  112. const_pointer data() const { return const_pointer(begin()); }
  113. reference operator[](size_type idx) {
  114. assert(idx < size());
  115. return begin()[idx];
  116. }
  117. const_reference operator[](size_type idx) const {
  118. assert(idx < size());
  119. return begin()[idx];
  120. }
  121. reference front() {
  122. assert(!empty());
  123. return begin()[0];
  124. }
  125. const_reference front() const {
  126. assert(!empty());
  127. return begin()[0];
  128. }
  129. reference back() {
  130. assert(!empty());
  131. return end()[-1];
  132. }
  133. const_reference back() const {
  134. assert(!empty());
  135. return end()[-1];
  136. }
  137. };
  138. /// SmallVectorTemplateBase<isPodLike = false> - This is where we put method
  139. /// implementations that are designed to work with non-POD-like T's.
  140. template <typename T, bool isPodLike>
  141. class SmallVectorTemplateBase : public SmallVectorTemplateCommon<T> {
  142. protected:
  143. SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {}
  144. static void destroy_range(T *S, T *E) {
  145. while (S != E) {
  146. --E;
  147. E->~T();
  148. }
  149. }
  150. /// Use move-assignment to move the range [I, E) onto the
  151. /// objects starting with "Dest". This is just <memory>'s
  152. /// std::move, but not all stdlibs actually provide that.
  153. template<typename It1, typename It2>
  154. static It2 move(It1 I, It1 E, It2 Dest) {
  155. for (; I != E; ++I, ++Dest)
  156. *Dest = ::std::move(*I);
  157. return Dest;
  158. }
  159. /// Use move-assignment to move the range
  160. /// [I, E) onto the objects ending at "Dest", moving objects
  161. /// in reverse order. This is just <algorithm>'s
  162. /// std::move_backward, but not all stdlibs actually provide that.
  163. template<typename It1, typename It2>
  164. static It2 move_backward(It1 I, It1 E, It2 Dest) {
  165. while (I != E)
  166. *--Dest = ::std::move(*--E);
  167. return Dest;
  168. }
  169. /// Move the range [I, E) into the uninitialized memory starting with "Dest",
  170. /// constructing elements as needed.
  171. template<typename It1, typename It2>
  172. static void uninitialized_move(It1 I, It1 E, It2 Dest) {
  173. for (; I != E; ++I, ++Dest)
  174. ::new ((void*) &*Dest) T(::std::move(*I));
  175. }
  176. /// Copy the range [I, E) onto the uninitialized memory starting with "Dest",
  177. /// constructing elements as needed.
  178. template<typename It1, typename It2>
  179. static void uninitialized_copy(It1 I, It1 E, It2 Dest) {
  180. std::uninitialized_copy(I, E, Dest);
  181. }
  182. /// Grow the allocated memory (without initializing new elements), doubling
  183. /// the size of the allocated memory. Guarantees space for at least one more
  184. /// element, or MinSize more elements if specified.
  185. void grow(size_t MinSize = 0);
  186. public:
  187. void push_back(const T &Elt) {
  188. if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
  189. this->grow();
  190. ::new ((void*) this->end()) T(Elt);
  191. this->setEnd(this->end()+1);
  192. }
  193. void push_back(T &&Elt) {
  194. if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
  195. this->grow();
  196. ::new ((void*) this->end()) T(::std::move(Elt));
  197. this->setEnd(this->end()+1);
  198. }
  199. void pop_back() {
  200. this->setEnd(this->end()-1);
  201. this->end()->~T();
  202. }
  203. };
  204. // Define this out-of-line to dissuade the C++ compiler from inlining it.
  205. template <typename T, bool isPodLike>
  206. void SmallVectorTemplateBase<T, isPodLike>::grow(size_t MinSize) {
  207. size_t CurCapacity = this->capacity();
  208. size_t CurSize = this->size();
  209. // Always grow, even from zero.
  210. size_t NewCapacity = size_t(NextPowerOf2(CurCapacity+2));
  211. if (NewCapacity < MinSize)
  212. NewCapacity = MinSize;
  213. T *NewElts = (T*)new char[NewCapacity*sizeof(T)]; // HLSL Change: Use overridable operator new
  214. // Move the elements over.
  215. this->uninitialized_move(this->begin(), this->end(), NewElts);
  216. // Destroy the original elements.
  217. destroy_range(this->begin(), this->end());
  218. // If this wasn't grown from the inline copy, deallocate the old space.
  219. if (!this->isSmall())
  220. delete[] (char*)this->begin(); // HLSL Change: Use overridable operator delete
  221. this->setEnd(NewElts+CurSize);
  222. this->BeginX = NewElts;
  223. this->CapacityX = this->begin()+NewCapacity;
  224. }
  225. /// SmallVectorTemplateBase<isPodLike = true> - This is where we put method
  226. /// implementations that are designed to work with POD-like T's.
  227. template <typename T>
  228. class SmallVectorTemplateBase<T, true> : public SmallVectorTemplateCommon<T> {
  229. protected:
  230. SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {}
  231. // No need to do a destroy loop for POD's.
  232. static void destroy_range(T *, T *) {}
  233. /// Use move-assignment to move the range [I, E) onto the
  234. /// objects starting with "Dest". For PODs, this is just memcpy.
  235. template<typename It1, typename It2>
  236. static It2 move(It1 I, It1 E, It2 Dest) {
  237. return ::std::copy(I, E, Dest);
  238. }
  239. /// Use move-assignment to move the range [I, E) onto the objects ending at
  240. /// "Dest", moving objects in reverse order.
  241. template<typename It1, typename It2>
  242. static It2 move_backward(It1 I, It1 E, It2 Dest) {
  243. return ::std::copy_backward(I, E, Dest);
  244. }
  245. /// Move the range [I, E) onto the uninitialized memory
  246. /// starting with "Dest", constructing elements into it as needed.
  247. template<typename It1, typename It2>
  248. static void uninitialized_move(It1 I, It1 E, It2 Dest) {
  249. // Just do a copy.
  250. uninitialized_copy(I, E, Dest);
  251. }
  252. /// Copy the range [I, E) onto the uninitialized memory
  253. /// starting with "Dest", constructing elements into it as needed.
  254. template<typename It1, typename It2>
  255. static void uninitialized_copy(It1 I, It1 E, It2 Dest) {
  256. // Arbitrary iterator types; just use the basic implementation.
  257. std::uninitialized_copy(I, E, Dest);
  258. }
  259. /// Copy the range [I, E) onto the uninitialized memory
  260. /// starting with "Dest", constructing elements into it as needed.
  261. template <typename T1, typename T2>
  262. static void uninitialized_copy(
  263. T1 *I, T1 *E, T2 *Dest,
  264. typename std::enable_if<std::is_same<typename std::remove_const<T1>::type,
  265. T2>::value>::type * = nullptr) {
  266. // Use memcpy for PODs iterated by pointers (which includes SmallVector
  267. // iterators): std::uninitialized_copy optimizes to memmove, but we can
  268. // use memcpy here. Note that I and E are iterators and thus might be
  269. // invalid for memcpy if they are equal.
  270. if (I != E)
  271. memcpy(Dest, I, (E - I) * sizeof(T));
  272. }
  273. /// Double the size of the allocated memory, guaranteeing space for at
  274. /// least one more element or MinSize if specified.
  275. void grow(size_t MinSize = 0) {
  276. this->grow_pod(MinSize*sizeof(T), sizeof(T));
  277. }
  278. public:
  279. void push_back(const T &Elt) {
  280. if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
  281. this->grow();
  282. memcpy(this->end(), &Elt, sizeof(T));
  283. this->setEnd(this->end()+1);
  284. }
  285. void pop_back() {
  286. this->setEnd(this->end()-1);
  287. }
  288. };
  289. /// This class consists of common code factored out of the SmallVector class to
  290. /// reduce code duplication based on the SmallVector 'N' template parameter.
  291. template <typename T>
  292. class SmallVectorImpl : public SmallVectorTemplateBase<T, isPodLike<T>::value> {
  293. typedef SmallVectorTemplateBase<T, isPodLike<T>::value > SuperClass;
  294. SmallVectorImpl(const SmallVectorImpl&) = delete;
  295. public:
  296. typedef typename SuperClass::iterator iterator;
  297. typedef typename SuperClass::size_type size_type;
  298. protected:
  299. // Default ctor - Initialize to empty.
  300. explicit SmallVectorImpl(unsigned N)
  301. : SmallVectorTemplateBase<T, isPodLike<T>::value>(N*sizeof(T)) {
  302. }
  303. public:
  304. ~SmallVectorImpl() {
  305. // Destroy the constructed elements in the vector.
  306. this->destroy_range(this->begin(), this->end());
  307. // If this wasn't grown from the inline copy, deallocate the old space.
  308. if (!this->isSmall())
  309. delete[] (char*)this->begin(); // HLSL Change: Use overridable operator delete
  310. }
  311. void clear() {
  312. this->destroy_range(this->begin(), this->end());
  313. this->EndX = this->BeginX;
  314. }
  315. void resize(size_type N) {
  316. if (N < this->size()) {
  317. this->destroy_range(this->begin()+N, this->end());
  318. this->setEnd(this->begin()+N);
  319. } else if (N > this->size()) {
  320. if (this->capacity() < N)
  321. this->grow(N);
  322. for (auto I = this->end(), E = this->begin() + N; I != E; ++I)
  323. new (&*I) T();
  324. this->setEnd(this->begin()+N);
  325. }
  326. }
  327. void resize(size_type N, const T &NV) {
  328. if (N < this->size()) {
  329. this->destroy_range(this->begin()+N, this->end());
  330. this->setEnd(this->begin()+N);
  331. } else if (N > this->size()) {
  332. if (this->capacity() < N)
  333. this->grow(N);
  334. std::uninitialized_fill(this->end(), this->begin()+N, NV);
  335. this->setEnd(this->begin()+N);
  336. }
  337. }
  338. void reserve(size_type N) {
  339. if (this->capacity() < N)
  340. this->grow(N);
  341. }
  342. T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val() {
  343. T Result = ::std::move(this->back());
  344. this->pop_back();
  345. return Result;
  346. }
  347. void swap(SmallVectorImpl &RHS);
  348. /// Add the specified range to the end of the SmallVector.
  349. template<typename in_iter>
  350. void append(in_iter in_start, in_iter in_end) {
  351. size_type NumInputs = std::distance(in_start, in_end);
  352. // Grow allocated space if needed.
  353. if (NumInputs > size_type(this->capacity_ptr()-this->end()))
  354. this->grow(this->size()+NumInputs);
  355. // Copy the new elements over.
  356. this->uninitialized_copy(in_start, in_end, this->end());
  357. this->setEnd(this->end() + NumInputs);
  358. }
  359. /// Add the specified range to the end of the SmallVector.
  360. void append(size_type NumInputs, const T &Elt) {
  361. // Grow allocated space if needed.
  362. if (NumInputs > size_type(this->capacity_ptr()-this->end()))
  363. this->grow(this->size()+NumInputs);
  364. // Copy the new elements over.
  365. std::uninitialized_fill_n(this->end(), NumInputs, Elt);
  366. this->setEnd(this->end() + NumInputs);
  367. }
  368. void append(std::initializer_list<T> IL) {
  369. append(IL.begin(), IL.end());
  370. }
  371. void assign(size_type NumElts, const T &Elt) {
  372. clear();
  373. if (this->capacity() < NumElts)
  374. this->grow(NumElts);
  375. this->setEnd(this->begin()+NumElts);
  376. std::uninitialized_fill(this->begin(), this->end(), Elt);
  377. }
  378. void assign(std::initializer_list<T> IL) {
  379. clear();
  380. append(IL);
  381. }
  382. iterator erase(iterator I) {
  383. assert(I >= this->begin() && "Iterator to erase is out of bounds.");
  384. assert(I < this->end() && "Erasing at past-the-end iterator.");
  385. iterator N = I;
  386. // Shift all elts down one.
  387. this->move(I+1, this->end(), I);
  388. // Drop the last elt.
  389. this->pop_back();
  390. return(N);
  391. }
  392. iterator erase(iterator S, iterator E) {
  393. assert(S >= this->begin() && "Range to erase is out of bounds.");
  394. assert(S <= E && "Trying to erase invalid range.");
  395. assert(E <= this->end() && "Trying to erase past the end.");
  396. iterator N = S;
  397. // Shift all elts down.
  398. iterator I = this->move(E, this->end(), S);
  399. // Drop the last elts.
  400. this->destroy_range(I, this->end());
  401. this->setEnd(I);
  402. return(N);
  403. }
  404. iterator insert(iterator I, T &&Elt) {
  405. if (I == this->end()) { // Important special case for empty vector.
  406. this->push_back(::std::move(Elt));
  407. return this->end()-1;
  408. }
  409. assert(I >= this->begin() && "Insertion iterator is out of bounds.");
  410. assert(I <= this->end() && "Inserting past the end of the vector.");
  411. if (this->EndX >= this->CapacityX) {
  412. size_t EltNo = I-this->begin();
  413. this->grow();
  414. I = this->begin()+EltNo;
  415. }
  416. ::new ((void*) this->end()) T(::std::move(this->back()));
  417. // Push everything else over.
  418. this->move_backward(I, this->end()-1, this->end());
  419. this->setEnd(this->end()+1);
  420. // If we just moved the element we're inserting, be sure to update
  421. // the reference.
  422. T *EltPtr = &Elt;
  423. if (I <= EltPtr && EltPtr < this->EndX)
  424. ++EltPtr;
  425. *I = ::std::move(*EltPtr);
  426. return I;
  427. }
  428. iterator insert(iterator I, const T &Elt) {
  429. if (I == this->end()) { // Important special case for empty vector.
  430. this->push_back(Elt);
  431. return this->end()-1;
  432. }
  433. assert(I >= this->begin() && "Insertion iterator is out of bounds.");
  434. assert(I <= this->end() && "Inserting past the end of the vector.");
  435. if (this->EndX >= this->CapacityX) {
  436. size_t EltNo = I-this->begin();
  437. this->grow();
  438. I = this->begin()+EltNo;
  439. }
  440. ::new ((void*) this->end()) T(std::move(this->back()));
  441. // Push everything else over.
  442. this->move_backward(I, this->end()-1, this->end());
  443. this->setEnd(this->end()+1);
  444. // If we just moved the element we're inserting, be sure to update
  445. // the reference.
  446. const T *EltPtr = &Elt;
  447. if (I <= EltPtr && EltPtr < this->EndX)
  448. ++EltPtr;
  449. *I = *EltPtr;
  450. return I;
  451. }
  452. iterator insert(iterator I, size_type NumToInsert, const T &Elt) {
  453. // Convert iterator to elt# to avoid invalidating iterator when we reserve()
  454. size_t InsertElt = I - this->begin();
  455. if (I == this->end()) { // Important special case for empty vector.
  456. append(NumToInsert, Elt);
  457. return this->begin()+InsertElt;
  458. }
  459. assert(I >= this->begin() && "Insertion iterator is out of bounds.");
  460. assert(I <= this->end() && "Inserting past the end of the vector.");
  461. // Ensure there is enough space.
  462. reserve(this->size() + NumToInsert);
  463. // Uninvalidate the iterator.
  464. I = this->begin()+InsertElt;
  465. // If there are more elements between the insertion point and the end of the
  466. // range than there are being inserted, we can use a simple approach to
  467. // insertion. Since we already reserved space, we know that this won't
  468. // reallocate the vector.
  469. if (size_t(this->end()-I) >= NumToInsert) {
  470. T *OldEnd = this->end();
  471. append(std::move_iterator<iterator>(this->end() - NumToInsert),
  472. std::move_iterator<iterator>(this->end()));
  473. // Copy the existing elements that get replaced.
  474. this->move_backward(I, OldEnd-NumToInsert, OldEnd);
  475. std::fill_n(I, NumToInsert, Elt);
  476. return I;
  477. }
  478. // Otherwise, we're inserting more elements than exist already, and we're
  479. // not inserting at the end.
  480. // Move over the elements that we're about to overwrite.
  481. T *OldEnd = this->end();
  482. this->setEnd(this->end() + NumToInsert);
  483. size_t NumOverwritten = OldEnd-I;
  484. this->uninitialized_move(I, OldEnd, this->end()-NumOverwritten);
  485. // Replace the overwritten part.
  486. std::fill_n(I, NumOverwritten, Elt);
  487. // Insert the non-overwritten middle part.
  488. std::uninitialized_fill_n(OldEnd, NumToInsert-NumOverwritten, Elt);
  489. return I;
  490. }
  491. template<typename ItTy>
  492. iterator insert(iterator I, ItTy From, ItTy To) {
  493. // Convert iterator to elt# to avoid invalidating iterator when we reserve()
  494. size_t InsertElt = I - this->begin();
  495. if (I == this->end()) { // Important special case for empty vector.
  496. append(From, To);
  497. return this->begin()+InsertElt;
  498. }
  499. assert(I >= this->begin() && "Insertion iterator is out of bounds.");
  500. assert(I <= this->end() && "Inserting past the end of the vector.");
  501. size_t NumToInsert = std::distance(From, To);
  502. // Ensure there is enough space.
  503. reserve(this->size() + NumToInsert);
  504. // Uninvalidate the iterator.
  505. I = this->begin()+InsertElt;
  506. // If there are more elements between the insertion point and the end of the
  507. // range than there are being inserted, we can use a simple approach to
  508. // insertion. Since we already reserved space, we know that this won't
  509. // reallocate the vector.
  510. if (size_t(this->end()-I) >= NumToInsert) {
  511. T *OldEnd = this->end();
  512. append(std::move_iterator<iterator>(this->end() - NumToInsert),
  513. std::move_iterator<iterator>(this->end()));
  514. // Copy the existing elements that get replaced.
  515. this->move_backward(I, OldEnd-NumToInsert, OldEnd);
  516. std::copy(From, To, I);
  517. return I;
  518. }
  519. // Otherwise, we're inserting more elements than exist already, and we're
  520. // not inserting at the end.
  521. // Move over the elements that we're about to overwrite.
  522. T *OldEnd = this->end();
  523. this->setEnd(this->end() + NumToInsert);
  524. size_t NumOverwritten = OldEnd-I;
  525. this->uninitialized_move(I, OldEnd, this->end()-NumOverwritten);
  526. // Replace the overwritten part.
  527. for (T *J = I; NumOverwritten > 0; --NumOverwritten) {
  528. *J = *From;
  529. ++J; ++From;
  530. }
  531. // Insert the non-overwritten middle part.
  532. this->uninitialized_copy(From, To, OldEnd);
  533. return I;
  534. }
  535. void insert(iterator I, std::initializer_list<T> IL) {
  536. insert(I, IL.begin(), IL.end());
  537. }
  538. template <typename... ArgTypes> void emplace_back(ArgTypes &&... Args) {
  539. if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
  540. this->grow();
  541. ::new ((void *)this->end()) T(std::forward<ArgTypes>(Args)...);
  542. this->setEnd(this->end() + 1);
  543. }
  544. SmallVectorImpl &operator=(const SmallVectorImpl &RHS);
  545. SmallVectorImpl &operator=(SmallVectorImpl &&RHS);
  546. bool operator==(const SmallVectorImpl &RHS) const {
  547. if (this->size() != RHS.size()) return false;
  548. return std::equal(this->begin(), this->end(), RHS.begin());
  549. }
  550. bool operator!=(const SmallVectorImpl &RHS) const {
  551. return !(*this == RHS);
  552. }
  553. bool operator<(const SmallVectorImpl &RHS) const {
  554. return std::lexicographical_compare(this->begin(), this->end(),
  555. RHS.begin(), RHS.end());
  556. }
  557. /// Set the array size to \p N, which the current array must have enough
  558. /// capacity for.
  559. ///
  560. /// This does not construct or destroy any elements in the vector.
  561. ///
  562. /// Clients can use this in conjunction with capacity() to write past the end
  563. /// of the buffer when they know that more elements are available, and only
  564. /// update the size later. This avoids the cost of value initializing elements
  565. /// which will only be overwritten.
  566. void set_size(size_type N) {
  567. assert(N <= this->capacity());
  568. this->setEnd(this->begin() + N);
  569. }
  570. };
  571. template <typename T>
  572. void SmallVectorImpl<T>::swap(SmallVectorImpl<T> &RHS) {
  573. if (this == &RHS) return;
  574. // We can only avoid copying elements if neither vector is small.
  575. if (!this->isSmall() && !RHS.isSmall()) {
  576. std::swap(this->BeginX, RHS.BeginX);
  577. std::swap(this->EndX, RHS.EndX);
  578. std::swap(this->CapacityX, RHS.CapacityX);
  579. return;
  580. }
  581. if (RHS.size() > this->capacity())
  582. this->grow(RHS.size());
  583. if (this->size() > RHS.capacity())
  584. RHS.grow(this->size());
  585. // Swap the shared elements.
  586. size_t NumShared = this->size();
  587. if (NumShared > RHS.size()) NumShared = RHS.size();
  588. for (size_type i = 0; i != NumShared; ++i)
  589. std::swap((*this)[i], RHS[i]);
  590. // Copy over the extra elts.
  591. if (this->size() > RHS.size()) {
  592. size_t EltDiff = this->size() - RHS.size();
  593. this->uninitialized_copy(this->begin()+NumShared, this->end(), RHS.end());
  594. RHS.setEnd(RHS.end()+EltDiff);
  595. this->destroy_range(this->begin()+NumShared, this->end());
  596. this->setEnd(this->begin()+NumShared);
  597. } else if (RHS.size() > this->size()) {
  598. size_t EltDiff = RHS.size() - this->size();
  599. this->uninitialized_copy(RHS.begin()+NumShared, RHS.end(), this->end());
  600. this->setEnd(this->end() + EltDiff);
  601. this->destroy_range(RHS.begin()+NumShared, RHS.end());
  602. RHS.setEnd(RHS.begin()+NumShared);
  603. }
  604. }
  605. template <typename T>
  606. SmallVectorImpl<T> &SmallVectorImpl<T>::
  607. operator=(const SmallVectorImpl<T> &RHS) {
  608. // Avoid self-assignment.
  609. if (this == &RHS) return *this;
  610. // If we already have sufficient space, assign the common elements, then
  611. // destroy any excess.
  612. size_t RHSSize = RHS.size();
  613. size_t CurSize = this->size();
  614. if (CurSize >= RHSSize) {
  615. // Assign common elements.
  616. iterator NewEnd;
  617. if (RHSSize)
  618. NewEnd = std::copy(RHS.begin(), RHS.begin()+RHSSize, this->begin());
  619. else
  620. NewEnd = this->begin();
  621. // Destroy excess elements.
  622. this->destroy_range(NewEnd, this->end());
  623. // Trim.
  624. this->setEnd(NewEnd);
  625. return *this;
  626. }
  627. // If we have to grow to have enough elements, destroy the current elements.
  628. // This allows us to avoid copying them during the grow.
  629. // FIXME: don't do this if they're efficiently moveable.
  630. if (this->capacity() < RHSSize) {
  631. // Destroy current elements.
  632. this->destroy_range(this->begin(), this->end());
  633. this->setEnd(this->begin());
  634. CurSize = 0;
  635. this->grow(RHSSize);
  636. } else if (CurSize) {
  637. // Otherwise, use assignment for the already-constructed elements.
  638. std::copy(RHS.begin(), RHS.begin()+CurSize, this->begin());
  639. }
  640. // Copy construct the new elements in place.
  641. this->uninitialized_copy(RHS.begin()+CurSize, RHS.end(),
  642. this->begin()+CurSize);
  643. // Set end.
  644. this->setEnd(this->begin()+RHSSize);
  645. return *this;
  646. }
  647. template <typename T>
  648. SmallVectorImpl<T> &SmallVectorImpl<T>::operator=(SmallVectorImpl<T> &&RHS) {
  649. // Avoid self-assignment.
  650. if (this == &RHS) return *this;
  651. // If the RHS isn't small, clear this vector and then steal its buffer.
  652. if (!RHS.isSmall()) {
  653. this->destroy_range(this->begin(), this->end());
  654. if (!this->isSmall()) delete[] (char*)this->begin(); // HLSL Change: Use overridable operator delete
  655. this->BeginX = RHS.BeginX;
  656. this->EndX = RHS.EndX;
  657. this->CapacityX = RHS.CapacityX;
  658. RHS.resetToSmall();
  659. return *this;
  660. }
  661. // If we already have sufficient space, assign the common elements, then
  662. // destroy any excess.
  663. size_t RHSSize = RHS.size();
  664. size_t CurSize = this->size();
  665. if (CurSize >= RHSSize) {
  666. // Assign common elements.
  667. iterator NewEnd = this->begin();
  668. if (RHSSize)
  669. NewEnd = this->move(RHS.begin(), RHS.end(), NewEnd);
  670. // Destroy excess elements and trim the bounds.
  671. this->destroy_range(NewEnd, this->end());
  672. this->setEnd(NewEnd);
  673. // Clear the RHS.
  674. RHS.clear();
  675. return *this;
  676. }
  677. // If we have to grow to have enough elements, destroy the current elements.
  678. // This allows us to avoid copying them during the grow.
  679. // FIXME: this may not actually make any sense if we can efficiently move
  680. // elements.
  681. if (this->capacity() < RHSSize) {
  682. // Destroy current elements.
  683. this->destroy_range(this->begin(), this->end());
  684. this->setEnd(this->begin());
  685. CurSize = 0;
  686. this->grow(RHSSize);
  687. } else if (CurSize) {
  688. // Otherwise, use assignment for the already-constructed elements.
  689. this->move(RHS.begin(), RHS.begin()+CurSize, this->begin());
  690. }
  691. // Move-construct the new elements in place.
  692. this->uninitialized_move(RHS.begin()+CurSize, RHS.end(),
  693. this->begin()+CurSize);
  694. // Set end.
  695. this->setEnd(this->begin()+RHSSize);
  696. RHS.clear();
  697. return *this;
  698. }
  699. /// Storage for the SmallVector elements which aren't contained in
  700. /// SmallVectorTemplateCommon. There are 'N-1' elements here. The remaining '1'
  701. /// element is in the base class. This is specialized for the N=1 and N=0 cases
  702. /// to avoid allocating unnecessary storage.
  703. template <typename T, unsigned N>
  704. struct SmallVectorStorage {
  705. typename SmallVectorTemplateCommon<T>::U InlineElts[N - 1];
  706. };
  707. template <typename T> struct SmallVectorStorage<T, 1> {};
  708. template <typename T> struct SmallVectorStorage<T, 0> {};
  709. /// This is a 'vector' (really, a variable-sized array), optimized
  710. /// for the case when the array is small. It contains some number of elements
  711. /// in-place, which allows it to avoid heap allocation when the actual number of
  712. /// elements is below that threshold. This allows normal "small" cases to be
  713. /// fast without losing generality for large inputs.
  714. ///
  715. /// Note that this does not attempt to be exception safe.
  716. ///
  717. template <typename T, unsigned N>
  718. class SmallVector : public SmallVectorImpl<T> {
  719. /// Inline space for elements which aren't stored in the base class.
  720. SmallVectorStorage<T, N> Storage;
  721. public:
  722. SmallVector() : SmallVectorImpl<T>(N) {
  723. }
  724. explicit SmallVector(size_t Size, const T &Value = T())
  725. : SmallVectorImpl<T>(N) {
  726. this->assign(Size, Value);
  727. }
  728. template<typename ItTy>
  729. SmallVector(ItTy S, ItTy E) : SmallVectorImpl<T>(N) {
  730. this->append(S, E);
  731. }
  732. template <typename RangeTy>
  733. explicit SmallVector(const llvm::iterator_range<RangeTy> R)
  734. : SmallVectorImpl<T>(N) {
  735. this->append(R.begin(), R.end());
  736. }
  737. SmallVector(std::initializer_list<T> IL) : SmallVectorImpl<T>(N) {
  738. this->assign(IL);
  739. }
  740. SmallVector(const SmallVector &RHS) : SmallVectorImpl<T>(N) {
  741. if (!RHS.empty())
  742. SmallVectorImpl<T>::operator=(RHS);
  743. }
  744. const SmallVector &operator=(const SmallVector &RHS) {
  745. SmallVectorImpl<T>::operator=(RHS);
  746. return *this;
  747. }
  748. SmallVector(SmallVector &&RHS) : SmallVectorImpl<T>(N) {
  749. if (!RHS.empty())
  750. SmallVectorImpl<T>::operator=(::std::move(RHS));
  751. }
  752. const SmallVector &operator=(SmallVector &&RHS) {
  753. SmallVectorImpl<T>::operator=(::std::move(RHS));
  754. return *this;
  755. }
  756. SmallVector(SmallVectorImpl<T> &&RHS) : SmallVectorImpl<T>(N) {
  757. if (!RHS.empty())
  758. SmallVectorImpl<T>::operator=(::std::move(RHS));
  759. }
  760. const SmallVector &operator=(SmallVectorImpl<T> &&RHS) {
  761. SmallVectorImpl<T>::operator=(::std::move(RHS));
  762. return *this;
  763. }
  764. const SmallVector &operator=(std::initializer_list<T> IL) {
  765. this->assign(IL);
  766. return *this;
  767. }
  768. };
  769. template<typename T, unsigned N>
  770. static inline size_t capacity_in_bytes(const SmallVector<T, N> &X) {
  771. return X.capacity_in_bytes();
  772. }
  773. } // End llvm namespace
  774. namespace std {
  775. /// Implement std::swap in terms of SmallVector swap.
  776. template<typename T>
  777. inline void
  778. swap(llvm::SmallVectorImpl<T> &LHS, llvm::SmallVectorImpl<T> &RHS) {
  779. LHS.swap(RHS);
  780. }
  781. /// Implement std::swap in terms of SmallVector swap.
  782. template<typename T, unsigned N>
  783. inline void
  784. swap(llvm::SmallVector<T, N> &LHS, llvm::SmallVector<T, N> &RHS) {
  785. LHS.swap(RHS);
  786. }
  787. }
  788. #endif