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