LLVM  7.0.0svn
SmallVector.h
Go to the documentation of this file.
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 
14 #ifndef LLVM_ADT_SMALLVECTOR_H
15 #define LLVM_ADT_SMALLVECTOR_H
16 
18 #include "llvm/Support/AlignOf.h"
19 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/MemAlloc.h"
24 #include <algorithm>
25 #include <cassert>
26 #include <cstddef>
27 #include <cstdlib>
28 #include <cstring>
29 #include <initializer_list>
30 #include <iterator>
31 #include <memory>
32 #include <new>
33 #include <type_traits>
34 #include <utility>
35 
36 namespace llvm {
37 
38 /// This is all the non-templated stuff common to all SmallVectors.
40 protected:
41  void *BeginX, *EndX, *CapacityX;
42 
43 protected:
44  SmallVectorBase(void *FirstEl, size_t Size)
45  : BeginX(FirstEl), EndX(FirstEl), CapacityX((char*)FirstEl+Size) {}
46 
47  /// This is an implementation of the grow() method which only works
48  /// on POD-like data types and is out of line to reduce code duplication.
49  void grow_pod(void *FirstEl, size_t MinSizeInBytes, size_t TSize);
50 
51 public:
52  /// This returns size()*sizeof(T).
53  size_t size_in_bytes() const {
54  return size_t((char*)EndX - (char*)BeginX);
55  }
56 
57  /// capacity_in_bytes - This returns capacity()*sizeof(T).
58  size_t capacity_in_bytes() const {
59  return size_t((char*)CapacityX - (char*)BeginX);
60  }
61 
62  LLVM_NODISCARD bool empty() const { return BeginX == EndX; }
63 };
64 
65 /// This is the part of SmallVectorTemplateBase which does not depend on whether
66 /// the type T is a POD. The extra dummy template argument is used by ArrayRef
67 /// to avoid unnecessarily requiring T to be complete.
68 template <typename T, typename = void>
70 private:
71  template <typename, unsigned> friend struct SmallVectorStorage;
72 
73  // Allocate raw space for N elements of type T. If T has a ctor or dtor, we
74  // don't want it to be automatically run, so we need to represent the space as
75  // something else. Use an array of char of sufficient alignment.
77  U FirstEl;
78  // Space after 'FirstEl' is clobbered, do not add any instance vars after it.
79 
80 protected:
81  SmallVectorTemplateCommon(size_t Size) : SmallVectorBase(&FirstEl, Size) {}
82 
83  void grow_pod(size_t MinSizeInBytes, size_t TSize) {
84  SmallVectorBase::grow_pod(&FirstEl, MinSizeInBytes, TSize);
85  }
86 
87  /// Return true if this is a smallvector which has not had dynamic
88  /// memory allocated for it.
89  bool isSmall() const {
90  return BeginX == static_cast<const void*>(&FirstEl);
91  }
92 
93  /// Put this vector in a state of being small.
94  void resetToSmall() {
95  BeginX = EndX = CapacityX = &FirstEl;
96  }
97 
98  void setEnd(T *P) { this->EndX = P; }
99 
100 public:
101  using size_type = size_t;
103  using value_type = T;
104  using iterator = T *;
105  using const_iterator = const T *;
106 
107  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
108  using reverse_iterator = std::reverse_iterator<iterator>;
109 
110  using reference = T &;
111  using const_reference = const T &;
112  using pointer = T *;
113  using const_pointer = const T *;
114 
115  // forward iterator creation methods.
117  iterator begin() { return (iterator)this->BeginX; }
119  const_iterator begin() const { return (const_iterator)this->BeginX; }
121  iterator end() { return (iterator)this->EndX; }
123  const_iterator end() const { return (const_iterator)this->EndX; }
124 
125 protected:
128 
129 public:
130  // reverse iterator creation methods.
135 
137  size_type size() const { return end()-begin(); }
138  size_type max_size() const { return size_type(-1) / sizeof(T); }
139 
140  /// Return the total number of elements in the currently allocated buffer.
141  size_t capacity() const { return capacity_ptr() - begin(); }
142 
143  /// Return a pointer to the vector's buffer, even if empty().
144  pointer data() { return pointer(begin()); }
145  /// Return a pointer to the vector's buffer, even if empty().
146  const_pointer data() const { return const_pointer(begin()); }
147 
150  assert(idx < size());
151  return begin()[idx];
152  }
155  assert(idx < size());
156  return begin()[idx];
157  }
158 
160  assert(!empty());
161  return begin()[0];
162  }
164  assert(!empty());
165  return begin()[0];
166  }
167 
169  assert(!empty());
170  return end()[-1];
171  }
173  assert(!empty());
174  return end()[-1];
175  }
176 };
177 
178 /// SmallVectorTemplateBase<isPodLike = false> - This is where we put method
179 /// implementations that are designed to work with non-POD-like T's.
180 template <typename T, bool isPodLike>
182 protected:
184 
185  static void destroy_range(T *S, T *E) {
186  while (S != E) {
187  --E;
188  E->~T();
189  }
190  }
191 
192  /// Move the range [I, E) into the uninitialized memory starting with "Dest",
193  /// constructing elements as needed.
194  template<typename It1, typename It2>
195  static void uninitialized_move(It1 I, It1 E, It2 Dest) {
196  std::uninitialized_copy(std::make_move_iterator(I),
197  std::make_move_iterator(E), Dest);
198  }
199 
200  /// Copy the range [I, E) onto the uninitialized memory starting with "Dest",
201  /// constructing elements as needed.
202  template<typename It1, typename It2>
203  static void uninitialized_copy(It1 I, It1 E, It2 Dest) {
204  std::uninitialized_copy(I, E, Dest);
205  }
206 
207  /// Grow the allocated memory (without initializing new elements), doubling
208  /// the size of the allocated memory. Guarantees space for at least one more
209  /// element, or MinSize more elements if specified.
210  void grow(size_t MinSize = 0);
211 
212 public:
213  void push_back(const T &Elt) {
214  if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
215  this->grow();
216  ::new ((void*) this->end()) T(Elt);
217  this->setEnd(this->end()+1);
218  }
219 
220  void push_back(T &&Elt) {
221  if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
222  this->grow();
223  ::new ((void*) this->end()) T(::std::move(Elt));
224  this->setEnd(this->end()+1);
225  }
226 
227  void pop_back() {
228  this->setEnd(this->end()-1);
229  this->end()->~T();
230  }
231 };
232 
233 // Define this out-of-line to dissuade the C++ compiler from inlining it.
234 template <typename T, bool isPodLike>
236  size_t CurCapacity = this->capacity();
237  size_t CurSize = this->size();
238  // Always grow, even from zero.
239  size_t NewCapacity = size_t(NextPowerOf2(CurCapacity+2));
240  if (NewCapacity < MinSize)
241  NewCapacity = MinSize;
242  T *NewElts = static_cast<T*>(llvm::safe_malloc(NewCapacity*sizeof(T)));
243 
244  // Move the elements over.
245  this->uninitialized_move(this->begin(), this->end(), NewElts);
246 
247  // Destroy the original elements.
248  destroy_range(this->begin(), this->end());
249 
250  // If this wasn't grown from the inline copy, deallocate the old space.
251  if (!this->isSmall())
252  free(this->begin());
253 
254  this->setEnd(NewElts+CurSize);
255  this->BeginX = NewElts;
256  this->CapacityX = this->begin()+NewCapacity;
257 }
258 
259 
260 /// SmallVectorTemplateBase<isPodLike = true> - This is where we put method
261 /// implementations that are designed to work with POD-like T's.
262 template <typename T>
264 protected:
266 
267  // No need to do a destroy loop for POD's.
268  static void destroy_range(T *, T *) {}
269 
270  /// Move the range [I, E) onto the uninitialized memory
271  /// starting with "Dest", constructing elements into it as needed.
272  template<typename It1, typename It2>
273  static void uninitialized_move(It1 I, It1 E, It2 Dest) {
274  // Just do a copy.
275  uninitialized_copy(I, E, Dest);
276  }
277 
278  /// Copy the range [I, E) onto the uninitialized memory
279  /// starting with "Dest", constructing elements into it as needed.
280  template<typename It1, typename It2>
281  static void uninitialized_copy(It1 I, It1 E, It2 Dest) {
282  // Arbitrary iterator types; just use the basic implementation.
283  std::uninitialized_copy(I, E, Dest);
284  }
285 
286  /// Copy the range [I, E) onto the uninitialized memory
287  /// starting with "Dest", constructing elements into it as needed.
288  template <typename T1, typename T2>
289  static void uninitialized_copy(
290  T1 *I, T1 *E, T2 *Dest,
291  typename std::enable_if<std::is_same<typename std::remove_const<T1>::type,
292  T2>::value>::type * = nullptr) {
293  // Use memcpy for PODs iterated by pointers (which includes SmallVector
294  // iterators): std::uninitialized_copy optimizes to memmove, but we can
295  // use memcpy here. Note that I and E are iterators and thus might be
296  // invalid for memcpy if they are equal.
297  if (I != E)
298  memcpy(Dest, I, (E - I) * sizeof(T));
299  }
300 
301  /// Double the size of the allocated memory, guaranteeing space for at
302  /// least one more element or MinSize if specified.
303  void grow(size_t MinSize = 0) {
304  this->grow_pod(MinSize*sizeof(T), sizeof(T));
305  }
306 
307 public:
308  void push_back(const T &Elt) {
309  if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
310  this->grow();
311  memcpy(this->end(), &Elt, sizeof(T));
312  this->setEnd(this->end()+1);
313  }
314 
315  void pop_back() {
316  this->setEnd(this->end()-1);
317  }
318 };
319 
320 /// This class consists of common code factored out of the SmallVector class to
321 /// reduce code duplication based on the SmallVector 'N' template parameter.
322 template <typename T>
323 class SmallVectorImpl : public SmallVectorTemplateBase<T, isPodLike<T>::value> {
325 
326 public:
327  using iterator = typename SuperClass::iterator;
330 
331 protected:
332  // Default ctor - Initialize to empty.
333  explicit SmallVectorImpl(unsigned N)
334  : SmallVectorTemplateBase<T, isPodLike<T>::value>(N*sizeof(T)) {
335  }
336 
337 public:
338  SmallVectorImpl(const SmallVectorImpl &) = delete;
339 
341  // Subclass has already destructed this vector's elements.
342  // If this wasn't grown from the inline copy, deallocate the old space.
343  if (!this->isSmall())
344  free(this->begin());
345  }
346 
347  void clear() {
348  this->destroy_range(this->begin(), this->end());
349  this->EndX = this->BeginX;
350  }
351 
353  if (N < this->size()) {
354  this->destroy_range(this->begin()+N, this->end());
355  this->setEnd(this->begin()+N);
356  } else if (N > this->size()) {
357  if (this->capacity() < N)
358  this->grow(N);
359  for (auto I = this->end(), E = this->begin() + N; I != E; ++I)
360  new (&*I) T();
361  this->setEnd(this->begin()+N);
362  }
363  }
364 
365  void resize(size_type N, const T &NV) {
366  if (N < this->size()) {
367  this->destroy_range(this->begin()+N, this->end());
368  this->setEnd(this->begin()+N);
369  } else if (N > this->size()) {
370  if (this->capacity() < N)
371  this->grow(N);
372  std::uninitialized_fill(this->end(), this->begin()+N, NV);
373  this->setEnd(this->begin()+N);
374  }
375  }
376 
378  if (this->capacity() < N)
379  this->grow(N);
380  }
381 
383  T Result = ::std::move(this->back());
384  this->pop_back();
385  return Result;
386  }
387 
388  void swap(SmallVectorImpl &RHS);
389 
390  /// Add the specified range to the end of the SmallVector.
391  template <typename in_iter,
392  typename = typename std::enable_if<std::is_convertible<
393  typename std::iterator_traits<in_iter>::iterator_category,
394  std::input_iterator_tag>::value>::type>
395  void append(in_iter in_start, in_iter in_end) {
396  size_type NumInputs = std::distance(in_start, in_end);
397  // Grow allocated space if needed.
398  if (NumInputs > size_type(this->capacity_ptr()-this->end()))
399  this->grow(this->size()+NumInputs);
400 
401  // Copy the new elements over.
402  this->uninitialized_copy(in_start, in_end, this->end());
403  this->setEnd(this->end() + NumInputs);
404  }
405 
406  /// Add the specified range to the end of the SmallVector.
407  void append(size_type NumInputs, const T &Elt) {
408  // Grow allocated space if needed.
409  if (NumInputs > size_type(this->capacity_ptr()-this->end()))
410  this->grow(this->size()+NumInputs);
411 
412  // Copy the new elements over.
413  std::uninitialized_fill_n(this->end(), NumInputs, Elt);
414  this->setEnd(this->end() + NumInputs);
415  }
416 
417  void append(std::initializer_list<T> IL) {
418  append(IL.begin(), IL.end());
419  }
420 
421  // FIXME: Consider assigning over existing elements, rather than clearing &
422  // re-initializing them - for all assign(...) variants.
423 
424  void assign(size_type NumElts, const T &Elt) {
425  clear();
426  if (this->capacity() < NumElts)
427  this->grow(NumElts);
428  this->setEnd(this->begin()+NumElts);
429  std::uninitialized_fill(this->begin(), this->end(), Elt);
430  }
431 
432  template <typename in_iter,
433  typename = typename std::enable_if<std::is_convertible<
434  typename std::iterator_traits<in_iter>::iterator_category,
435  std::input_iterator_tag>::value>::type>
436  void assign(in_iter in_start, in_iter in_end) {
437  clear();
438  append(in_start, in_end);
439  }
440 
441  void assign(std::initializer_list<T> IL) {
442  clear();
443  append(IL);
444  }
445 
447  // Just cast away constness because this is a non-const member function.
448  iterator I = const_cast<iterator>(CI);
449 
450  assert(I >= this->begin() && "Iterator to erase is out of bounds.");
451  assert(I < this->end() && "Erasing at past-the-end iterator.");
452 
453  iterator N = I;
454  // Shift all elts down one.
455  std::move(I+1, this->end(), I);
456  // Drop the last elt.
457  this->pop_back();
458  return(N);
459  }
460 
462  // Just cast away constness because this is a non-const member function.
463  iterator S = const_cast<iterator>(CS);
464  iterator E = const_cast<iterator>(CE);
465 
466  assert(S >= this->begin() && "Range to erase is out of bounds.");
467  assert(S <= E && "Trying to erase invalid range.");
468  assert(E <= this->end() && "Trying to erase past the end.");
469 
470  iterator N = S;
471  // Shift all elts down.
472  iterator I = std::move(E, this->end(), S);
473  // Drop the last elts.
474  this->destroy_range(I, this->end());
475  this->setEnd(I);
476  return(N);
477  }
478 
480  if (I == this->end()) { // Important special case for empty vector.
481  this->push_back(::std::move(Elt));
482  return this->end()-1;
483  }
484 
485  assert(I >= this->begin() && "Insertion iterator is out of bounds.");
486  assert(I <= this->end() && "Inserting past the end of the vector.");
487 
488  if (this->EndX >= this->CapacityX) {
489  size_t EltNo = I-this->begin();
490  this->grow();
491  I = this->begin()+EltNo;
492  }
493 
494  ::new ((void*) this->end()) T(::std::move(this->back()));
495  // Push everything else over.
496  std::move_backward(I, this->end()-1, this->end());
497  this->setEnd(this->end()+1);
498 
499  // If we just moved the element we're inserting, be sure to update
500  // the reference.
501  T *EltPtr = &Elt;
502  if (I <= EltPtr && EltPtr < this->EndX)
503  ++EltPtr;
504 
505  *I = ::std::move(*EltPtr);
506  return I;
507  }
508 
509  iterator insert(iterator I, const T &Elt) {
510  if (I == this->end()) { // Important special case for empty vector.
511  this->push_back(Elt);
512  return this->end()-1;
513  }
514 
515  assert(I >= this->begin() && "Insertion iterator is out of bounds.");
516  assert(I <= this->end() && "Inserting past the end of the vector.");
517 
518  if (this->EndX >= this->CapacityX) {
519  size_t EltNo = I-this->begin();
520  this->grow();
521  I = this->begin()+EltNo;
522  }
523  ::new ((void*) this->end()) T(std::move(this->back()));
524  // Push everything else over.
525  std::move_backward(I, this->end()-1, this->end());
526  this->setEnd(this->end()+1);
527 
528  // If we just moved the element we're inserting, be sure to update
529  // the reference.
530  const T *EltPtr = &Elt;
531  if (I <= EltPtr && EltPtr < this->EndX)
532  ++EltPtr;
533 
534  *I = *EltPtr;
535  return I;
536  }
537 
538  iterator insert(iterator I, size_type NumToInsert, const T &Elt) {
539  // Convert iterator to elt# to avoid invalidating iterator when we reserve()
540  size_t InsertElt = I - this->begin();
541 
542  if (I == this->end()) { // Important special case for empty vector.
543  append(NumToInsert, Elt);
544  return this->begin()+InsertElt;
545  }
546 
547  assert(I >= this->begin() && "Insertion iterator is out of bounds.");
548  assert(I <= this->end() && "Inserting past the end of the vector.");
549 
550  // Ensure there is enough space.
551  reserve(this->size() + NumToInsert);
552 
553  // Uninvalidate the iterator.
554  I = this->begin()+InsertElt;
555 
556  // If there are more elements between the insertion point and the end of the
557  // range than there are being inserted, we can use a simple approach to
558  // insertion. Since we already reserved space, we know that this won't
559  // reallocate the vector.
560  if (size_t(this->end()-I) >= NumToInsert) {
561  T *OldEnd = this->end();
562  append(std::move_iterator<iterator>(this->end() - NumToInsert),
563  std::move_iterator<iterator>(this->end()));
564 
565  // Copy the existing elements that get replaced.
566  std::move_backward(I, OldEnd-NumToInsert, OldEnd);
567 
568  std::fill_n(I, NumToInsert, Elt);
569  return I;
570  }
571 
572  // Otherwise, we're inserting more elements than exist already, and we're
573  // not inserting at the end.
574 
575  // Move over the elements that we're about to overwrite.
576  T *OldEnd = this->end();
577  this->setEnd(this->end() + NumToInsert);
578  size_t NumOverwritten = OldEnd-I;
579  this->uninitialized_move(I, OldEnd, this->end()-NumOverwritten);
580 
581  // Replace the overwritten part.
582  std::fill_n(I, NumOverwritten, Elt);
583 
584  // Insert the non-overwritten middle part.
585  std::uninitialized_fill_n(OldEnd, NumToInsert-NumOverwritten, Elt);
586  return I;
587  }
588 
589  template <typename ItTy,
590  typename = typename std::enable_if<std::is_convertible<
591  typename std::iterator_traits<ItTy>::iterator_category,
592  std::input_iterator_tag>::value>::type>
594  // Convert iterator to elt# to avoid invalidating iterator when we reserve()
595  size_t InsertElt = I - this->begin();
596 
597  if (I == this->end()) { // Important special case for empty vector.
598  append(From, To);
599  return this->begin()+InsertElt;
600  }
601 
602  assert(I >= this->begin() && "Insertion iterator is out of bounds.");
603  assert(I <= this->end() && "Inserting past the end of the vector.");
604 
605  size_t NumToInsert = std::distance(From, To);
606 
607  // Ensure there is enough space.
608  reserve(this->size() + NumToInsert);
609 
610  // Uninvalidate the iterator.
611  I = this->begin()+InsertElt;
612 
613  // If there are more elements between the insertion point and the end of the
614  // range than there are being inserted, we can use a simple approach to
615  // insertion. Since we already reserved space, we know that this won't
616  // reallocate the vector.
617  if (size_t(this->end()-I) >= NumToInsert) {
618  T *OldEnd = this->end();
619  append(std::move_iterator<iterator>(this->end() - NumToInsert),
620  std::move_iterator<iterator>(this->end()));
621 
622  // Copy the existing elements that get replaced.
623  std::move_backward(I, OldEnd-NumToInsert, OldEnd);
624 
625  std::copy(From, To, I);
626  return I;
627  }
628 
629  // Otherwise, we're inserting more elements than exist already, and we're
630  // not inserting at the end.
631 
632  // Move over the elements that we're about to overwrite.
633  T *OldEnd = this->end();
634  this->setEnd(this->end() + NumToInsert);
635  size_t NumOverwritten = OldEnd-I;
636  this->uninitialized_move(I, OldEnd, this->end()-NumOverwritten);
637 
638  // Replace the overwritten part.
639  for (T *J = I; NumOverwritten > 0; --NumOverwritten) {
640  *J = *From;
641  ++J; ++From;
642  }
643 
644  // Insert the non-overwritten middle part.
645  this->uninitialized_copy(From, To, OldEnd);
646  return I;
647  }
648 
649  void insert(iterator I, std::initializer_list<T> IL) {
650  insert(I, IL.begin(), IL.end());
651  }
652 
653  template <typename... ArgTypes> void emplace_back(ArgTypes &&... Args) {
654  if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
655  this->grow();
656  ::new ((void *)this->end()) T(std::forward<ArgTypes>(Args)...);
657  this->setEnd(this->end() + 1);
658  }
659 
660  SmallVectorImpl &operator=(const SmallVectorImpl &RHS);
661 
662  SmallVectorImpl &operator=(SmallVectorImpl &&RHS);
663 
664  bool operator==(const SmallVectorImpl &RHS) const {
665  if (this->size() != RHS.size()) return false;
666  return std::equal(this->begin(), this->end(), RHS.begin());
667  }
668  bool operator!=(const SmallVectorImpl &RHS) const {
669  return !(*this == RHS);
670  }
671 
672  bool operator<(const SmallVectorImpl &RHS) const {
673  return std::lexicographical_compare(this->begin(), this->end(),
674  RHS.begin(), RHS.end());
675  }
676 
677  /// Set the array size to \p N, which the current array must have enough
678  /// capacity for.
679  ///
680  /// This does not construct or destroy any elements in the vector.
681  ///
682  /// Clients can use this in conjunction with capacity() to write past the end
683  /// of the buffer when they know that more elements are available, and only
684  /// update the size later. This avoids the cost of value initializing elements
685  /// which will only be overwritten.
687  assert(N <= this->capacity());
688  this->setEnd(this->begin() + N);
689  }
690 };
691 
692 template <typename T>
694  if (this == &RHS) return;
695 
696  // We can only avoid copying elements if neither vector is small.
697  if (!this->isSmall() && !RHS.isSmall()) {
698  std::swap(this->BeginX, RHS.BeginX);
699  std::swap(this->EndX, RHS.EndX);
700  std::swap(this->CapacityX, RHS.CapacityX);
701  return;
702  }
703  if (RHS.size() > this->capacity())
704  this->grow(RHS.size());
705  if (this->size() > RHS.capacity())
706  RHS.grow(this->size());
707 
708  // Swap the shared elements.
709  size_t NumShared = this->size();
710  if (NumShared > RHS.size()) NumShared = RHS.size();
711  for (size_type i = 0; i != NumShared; ++i)
712  std::swap((*this)[i], RHS[i]);
713 
714  // Copy over the extra elts.
715  if (this->size() > RHS.size()) {
716  size_t EltDiff = this->size() - RHS.size();
717  this->uninitialized_copy(this->begin()+NumShared, this->end(), RHS.end());
718  RHS.setEnd(RHS.end()+EltDiff);
719  this->destroy_range(this->begin()+NumShared, this->end());
720  this->setEnd(this->begin()+NumShared);
721  } else if (RHS.size() > this->size()) {
722  size_t EltDiff = RHS.size() - this->size();
723  this->uninitialized_copy(RHS.begin()+NumShared, RHS.end(), this->end());
724  this->setEnd(this->end() + EltDiff);
725  this->destroy_range(RHS.begin()+NumShared, RHS.end());
726  RHS.setEnd(RHS.begin()+NumShared);
727  }
728 }
729 
730 template <typename T>
733  // Avoid self-assignment.
734  if (this == &RHS) return *this;
735 
736  // If we already have sufficient space, assign the common elements, then
737  // destroy any excess.
738  size_t RHSSize = RHS.size();
739  size_t CurSize = this->size();
740  if (CurSize >= RHSSize) {
741  // Assign common elements.
742  iterator NewEnd;
743  if (RHSSize)
744  NewEnd = std::copy(RHS.begin(), RHS.begin()+RHSSize, this->begin());
745  else
746  NewEnd = this->begin();
747 
748  // Destroy excess elements.
749  this->destroy_range(NewEnd, this->end());
750 
751  // Trim.
752  this->setEnd(NewEnd);
753  return *this;
754  }
755 
756  // If we have to grow to have enough elements, destroy the current elements.
757  // This allows us to avoid copying them during the grow.
758  // FIXME: don't do this if they're efficiently moveable.
759  if (this->capacity() < RHSSize) {
760  // Destroy current elements.
761  this->destroy_range(this->begin(), this->end());
762  this->setEnd(this->begin());
763  CurSize = 0;
764  this->grow(RHSSize);
765  } else if (CurSize) {
766  // Otherwise, use assignment for the already-constructed elements.
767  std::copy(RHS.begin(), RHS.begin()+CurSize, this->begin());
768  }
769 
770  // Copy construct the new elements in place.
771  this->uninitialized_copy(RHS.begin()+CurSize, RHS.end(),
772  this->begin()+CurSize);
773 
774  // Set end.
775  this->setEnd(this->begin()+RHSSize);
776  return *this;
777 }
778 
779 template <typename T>
781  // Avoid self-assignment.
782  if (this == &RHS) return *this;
783 
784  // If the RHS isn't small, clear this vector and then steal its buffer.
785  if (!RHS.isSmall()) {
786  this->destroy_range(this->begin(), this->end());
787  if (!this->isSmall()) free(this->begin());
788  this->BeginX = RHS.BeginX;
789  this->EndX = RHS.EndX;
790  this->CapacityX = RHS.CapacityX;
791  RHS.resetToSmall();
792  return *this;
793  }
794 
795  // If we already have sufficient space, assign the common elements, then
796  // destroy any excess.
797  size_t RHSSize = RHS.size();
798  size_t CurSize = this->size();
799  if (CurSize >= RHSSize) {
800  // Assign common elements.
801  iterator NewEnd = this->begin();
802  if (RHSSize)
803  NewEnd = std::move(RHS.begin(), RHS.end(), NewEnd);
804 
805  // Destroy excess elements and trim the bounds.
806  this->destroy_range(NewEnd, this->end());
807  this->setEnd(NewEnd);
808 
809  // Clear the RHS.
810  RHS.clear();
811 
812  return *this;
813  }
814 
815  // If we have to grow to have enough elements, destroy the current elements.
816  // This allows us to avoid copying them during the grow.
817  // FIXME: this may not actually make any sense if we can efficiently move
818  // elements.
819  if (this->capacity() < RHSSize) {
820  // Destroy current elements.
821  this->destroy_range(this->begin(), this->end());
822  this->setEnd(this->begin());
823  CurSize = 0;
824  this->grow(RHSSize);
825  } else if (CurSize) {
826  // Otherwise, use assignment for the already-constructed elements.
827  std::move(RHS.begin(), RHS.begin()+CurSize, this->begin());
828  }
829 
830  // Move-construct the new elements in place.
831  this->uninitialized_move(RHS.begin()+CurSize, RHS.end(),
832  this->begin()+CurSize);
833 
834  // Set end.
835  this->setEnd(this->begin()+RHSSize);
836 
837  RHS.clear();
838  return *this;
839 }
840 
841 /// Storage for the SmallVector elements which aren't contained in
842 /// SmallVectorTemplateCommon. There are 'N-1' elements here. The remaining '1'
843 /// element is in the base class. This is specialized for the N=1 and N=0 cases
844 /// to avoid allocating unnecessary storage.
845 template <typename T, unsigned N>
847  typename SmallVectorTemplateCommon<T>::U InlineElts[N - 1];
848 };
849 template <typename T> struct SmallVectorStorage<T, 1> {};
850 template <typename T> struct SmallVectorStorage<T, 0> {};
851 
852 /// This is a 'vector' (really, a variable-sized array), optimized
853 /// for the case when the array is small. It contains some number of elements
854 /// in-place, which allows it to avoid heap allocation when the actual number of
855 /// elements is below that threshold. This allows normal "small" cases to be
856 /// fast without losing generality for large inputs.
857 ///
858 /// Note that this does not attempt to be exception safe.
859 ///
860 template <typename T, unsigned N>
862 public:
864 
866  // Destroy the constructed elements in the vector.
867  this->destroy_range(this->begin(), this->end());
868  }
869 
870  explicit SmallVector(size_t Size, const T &Value = T())
871  : SmallVectorImpl<T>(N) {
872  this->assign(Size, Value);
873  }
874 
875  template <typename ItTy,
876  typename = typename std::enable_if<std::is_convertible<
877  typename std::iterator_traits<ItTy>::iterator_category,
878  std::input_iterator_tag>::value>::type>
880  this->append(S, E);
881  }
882 
883  template <typename RangeTy>
885  : SmallVectorImpl<T>(N) {
886  this->append(R.begin(), R.end());
887  }
888 
889  SmallVector(std::initializer_list<T> IL) : SmallVectorImpl<T>(N) {
890  this->assign(IL);
891  }
892 
894  if (!RHS.empty())
896  }
897 
898  const SmallVector &operator=(const SmallVector &RHS) {
900  return *this;
901  }
902 
904  if (!RHS.empty())
905  SmallVectorImpl<T>::operator=(::std::move(RHS));
906  }
907 
909  if (!RHS.empty())
910  SmallVectorImpl<T>::operator=(::std::move(RHS));
911  }
912 
914  SmallVectorImpl<T>::operator=(::std::move(RHS));
915  return *this;
916  }
917 
919  SmallVectorImpl<T>::operator=(::std::move(RHS));
920  return *this;
921  }
922 
923  const SmallVector &operator=(std::initializer_list<T> IL) {
924  this->assign(IL);
925  return *this;
926  }
927 };
928 
929 template <typename T, unsigned N>
930 inline size_t capacity_in_bytes(const SmallVector<T, N> &X) {
931  return X.capacity_in_bytes();
932 }
933 
934 } // end namespace llvm
935 
936 namespace std {
937 
938  /// Implement std::swap in terms of SmallVector swap.
939  template<typename T>
940  inline void
942  LHS.swap(RHS);
943  }
944 
945  /// Implement std::swap in terms of SmallVector swap.
946  template<typename T, unsigned N>
947  inline void
949  LHS.swap(RHS);
950  }
951 
952 } // end namespace std
953 
954 #endif // LLVM_ADT_SMALLVECTOR_H
void push_back(const T &Elt)
Definition: SmallVector.h:213
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:250
static void uninitialized_copy(It1 I, It1 E, It2 Dest)
Copy the range [I, E) onto the uninitialized memory starting with "Dest", constructing elements into ...
Definition: SmallVector.h:281
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
std::pair< llvm::RelocationValueRef, llvm::RelocationEntry > * iterator
Definition: SmallVector.h:104
const std::pair< llvm::RelocationValueRef, llvm::RelocationEntry > & const_reference
Definition: SmallVector.h:111
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:241
DiagnosticInfoOptimizationBase::Argument NV
bool operator!=(const SmallVectorImpl &RHS) const
Definition: SmallVector.h:668
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
LLVM_ATTRIBUTE_ALWAYS_INLINE reference operator[](size_type idx)
Definition: SmallVector.h:149
#define LLVM_UNLIKELY(EXPR)
Definition: Compiler.h:176
const_pointer data() const
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:146
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:137
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
bool isSmall() const
Return true if this is a smallvector which has not had dynamic memory allocated for it...
Definition: SmallVector.h:89
void assign(in_iter in_start, in_iter in_end)
Definition: SmallVector.h:436
SmallVector(ItTy S, ItTy E)
Definition: SmallVector.h:879
const SmallVector & operator=(SmallVector &&RHS)
Definition: SmallVector.h:913
iterator insert(iterator I, const T &Elt)
Definition: SmallVector.h:509
iterator insert(iterator I, size_type NumToInsert, const T &Elt)
Definition: SmallVector.h:538
static void uninitialized_move(It1 I, It1 E, It2 Dest)
Move the range [I, E) into the uninitialized memory starting with "Dest", constructing elements as ne...
Definition: SmallVector.h:195
void append(size_type NumInputs, const T &Elt)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:407
void reserve(size_type N)
Definition: SmallVector.h:377
const_reverse_iterator rend() const
Definition: SmallVector.h:134
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:471
Definition: BitVector.h:921
SmallVectorTemplateBase(size_t Size)
Definition: SmallVector.h:183
static void uninitialized_copy(It1 I, It1 E, It2 Dest)
Copy the range [I, E) onto the uninitialized memory starting with "Dest", constructing elements as ne...
Definition: SmallVector.h:203
#define LLVM_ATTRIBUTE_ALWAYS_INLINE
LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do so, mark a method "always...
Definition: Compiler.h:198
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
void resize(size_type N, const T &NV)
Definition: SmallVector.h:365
This file defines counterparts of C library allocation functions defined in the namespace &#39;std&#39;...
void assign(size_type NumElts, const T &Elt)
Definition: SmallVector.h:424
#define T
SmallVector(SmallVectorImpl< T > &&RHS)
Definition: SmallVector.h:908
size_t size_in_bytes() const
This returns size()*sizeof(T).
Definition: SmallVector.h:53
const SmallVector & operator=(const SmallVector &RHS)
Definition: SmallVector.h:898
const_iterator capacity_ptr() const
Definition: SmallVector.h:127
LLVM_ATTRIBUTE_ALWAYS_INLINE const_reference operator[](size_type idx) const
Definition: SmallVector.h:154
std::pair< llvm::RelocationValueRef, llvm::RelocationEntry > & reference
Definition: SmallVector.h:110
SmallVector(SmallVector &&RHS)
Definition: SmallVector.h:903
const_reference front() const
Definition: SmallVector.h:163
#define P(N)
SmallVectorTemplateBase<isPodLike = false> - This is where we put method implementations that are des...
Definition: SmallVector.h:181
size_t capacity() const
Return the total number of elements in the currently allocated buffer.
Definition: SmallVector.h:141
void swap(SmallVectorImpl &RHS)
Definition: SmallVector.h:693
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:117
void grow(size_t MinSize=0)
Grow the allocated memory (without initializing new elements), doubling the size of the allocated mem...
Definition: SmallVector.h:235
SmallVectorImpl(unsigned N)
Definition: SmallVector.h:333
const std::pair< llvm::RelocationValueRef, llvm::RelocationEntry > * const_iterator
Definition: SmallVector.h:105
const_reverse_iterator rbegin() const
Definition: SmallVector.h:132
SmallVectorBase(void *FirstEl, size_t Size)
Definition: SmallVector.h:44
void assign(std::initializer_list< T > IL)
Definition: SmallVector.h:441
uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition: MathExtras.h:640
iterator erase(const_iterator CI)
Definition: SmallVector.h:446
void swap(llvm::SmallVector< T, N > &LHS, llvm::SmallVector< T, N > &RHS)
Implement std::swap in terms of SmallVector swap.
Definition: SmallVector.h:948
SmallVector(const iterator_range< RangeTy > &R)
Definition: SmallVector.h:884
const_reference back() const
Definition: SmallVector.h:172
LLVM_ATTRIBUTE_ALWAYS_INLINE const_iterator begin() const
Definition: SmallVector.h:119
isPodLike - This is a type trait that is used to determine whether a given type can be copied around ...
Definition: ArrayRef.h:530
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_malloc(size_t Sz)
Definition: MemAlloc.h:26
const SmallVector & operator=(SmallVectorImpl< T > &&RHS)
Definition: SmallVector.h:918
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:1032
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
std::pair< llvm::RelocationValueRef, llvm::RelocationEntry > * pointer
Definition: SmallVector.h:112
void grow(size_t MinSize=0)
Double the size of the allocated memory, guaranteeing space for at least one more element or MinSize ...
Definition: SmallVector.h:303
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:382
size_t capacity_in_bytes() const
capacity_in_bytes - This returns capacity()*sizeof(T).
Definition: SmallVector.h:58
SmallVector(std::initializer_list< T > IL)
Definition: SmallVector.h:889
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:924
bool operator<(const SmallVectorImpl &RHS) const
Definition: SmallVector.h:672
A range adaptor for a pair of iterators.
void append(std::initializer_list< T > IL)
Definition: SmallVector.h:417
SmallVectorImpl & operator=(const SmallVectorImpl &RHS)
Definition: SmallVector.h:732
static void clear(coro::Shape &Shape)
Definition: Coroutines.cpp:211
iterator insert(iterator I, T &&Elt)
Definition: SmallVector.h:479
void insert(iterator I, std::initializer_list< T > IL)
Definition: SmallVector.h:649
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:395
static void destroy_range(T *S, T *E)
Definition: SmallVector.h:185
iterator erase(const_iterator CS, const_iterator CE)
Definition: SmallVector.h:461
const std::pair< llvm::RelocationValueRef, llvm::RelocationEntry > * const_pointer
Definition: SmallVector.h:113
Basic Alias true
std::pair< llvm::RelocationValueRef, llvm::RelocationEntry > value_type
Definition: SmallVector.h:103
This is the part of SmallVectorTemplateBase which does not depend on whether the type T is a POD...
Definition: SmallVector.h:69
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:121
void set_size(size_type N)
Set the array size to N, which the current array must have enough capacity for.
Definition: SmallVector.h:686
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:144
void emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:653
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:62
LLVM_ATTRIBUTE_ALWAYS_INLINE const_iterator end() const
Definition: SmallVector.h:123
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
static void uninitialized_copy(T1 *I, T1 *E, T2 *Dest, typename std::enable_if< std::is_same< typename std::remove_const< T1 >::type, T2 >::value >::type *=nullptr)
Copy the range [I, E) onto the uninitialized memory starting with "Dest", constructing elements into ...
Definition: SmallVector.h:289
This is all the non-templated stuff common to all SmallVectors.
Definition: SmallVector.h:39
SmallVectorTemplateCommon(size_t Size)
Definition: SmallVector.h:81
#define LLVM_NODISCARD
LLVM_NODISCARD - Warn if a type or return value is discarded.
Definition: Compiler.h:126
SmallVector(const SmallVector &RHS)
Definition: SmallVector.h:893
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
iterator insert(iterator I, ItTy From, ItTy To)
Definition: SmallVector.h:593
static void uninitialized_move(It1 I, It1 E, It2 Dest)
Move the range [I, E) onto the uninitialized memory starting with "Dest", constructing elements into ...
Definition: SmallVector.h:273
IteratorT begin() const
void grow_pod(size_t MinSizeInBytes, size_t TSize)
Definition: SmallVector.h:83
void grow_pod(void *FirstEl, size_t MinSizeInBytes, size_t TSize)
This is an implementation of the grow() method which only works on POD-like data types and is out of ...
Definition: SmallVector.cpp:23
Storage for the SmallVector elements which aren&#39;t contained in SmallVectorTemplateCommon.
Definition: SmallVector.h:846
bool operator==(const SmallVectorImpl &RHS) const
Definition: SmallVector.h:664
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:960
IteratorT end() const
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
#define T1
const SmallVector & operator=(std::initializer_list< T > IL)
Definition: SmallVector.h:923
void resetToSmall()
Put this vector in a state of being small.
Definition: SmallVector.h:94
SmallVector(size_t Size, const T &Value=T())
Definition: SmallVector.h:870
void resize(size_type N)
Definition: SmallVector.h:352