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