Caffe2 - C++ API
A deep learning, cross platform ML framework
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 // ATen: modified from llvm::SmallVector.
15 // replaced report_bad_alloc_error with std::bad_alloc
16 // replaced isPodLike<T> with C10_IS_TRIVIALLY_COPYABLE (moved to Macros.h)
17 // replaced iterator_range constructor with inline Container&& constructor
18 // removed LLVM_NODISCARD and LLVM_ATTRIBUTE_ALWAYS_INLINE qualifiers
19 // removed LLVM_UNLIKELY
20 
21 #pragma once
22 
23 #include <c10/util/AlignOf.h>
24 #include <c10/macros/Macros.h>
25 
26 #include <algorithm>
27 #include <cassert>
28 #include <cstddef>
29 #include <cstdlib>
30 #include <cstring>
31 #include <initializer_list>
32 #include <iterator>
33 #include <memory>
34 #include <new>
35 #include <type_traits>
36 #include <utility>
37 
38 namespace c10 {
39 
40 namespace detail {
41 
42 // From llvm/Support/MathExtras.h
43 static inline uint64_t NextPowerOf2(uint64_t A) {
44  A |= (A >> 1);
45  A |= (A >> 2);
46  A |= (A >> 4);
47  A |= (A >> 8);
48  A |= (A >> 16);
49  A |= (A >> 32);
50  return A + 1;
51 }
52 
53 } // namespace detail
54 
56 class C10_API SmallVectorBase {
57  protected:
58  void *BeginX, *EndX, *CapacityX;
59 
60  protected:
61  SmallVectorBase(void* FirstEl, size_t Size)
62  : BeginX(FirstEl), EndX(FirstEl), CapacityX((char*)FirstEl + Size) {}
63 
66  void grow_pod(void* FirstEl, size_t MinSizeInBytes, size_t TSize);
67 
68  public:
70  size_t size_in_bytes() const {
71  return size_t((char*)EndX - (char*)BeginX);
72  }
73 
75  size_t capacity_in_bytes() const {
76  return size_t((char*)CapacityX - (char*)BeginX);
77  }
78 
79  bool empty() const {
80  return BeginX == EndX;
81  }
82 };
83 
87 template <typename T, typename = void>
89  private:
90  template <typename, unsigned>
91  friend struct SmallVectorStorage;
92 
93  // Allocate raw space for N elements of type T. If T has a ctor or dtor, we
94  // don't want it to be automatically run, so we need to represent the space as
95  // something else. Use an array of char of sufficient alignment.
97  U FirstEl;
98  // Space after 'FirstEl' is clobbered, do not add any instance vars after it.
99 
100  protected:
101  SmallVectorTemplateCommon(size_t Size) : SmallVectorBase(&FirstEl, Size) {}
102 
103  void grow_pod(size_t MinSizeInBytes, size_t TSize) {
104  SmallVectorBase::grow_pod(&FirstEl, MinSizeInBytes, TSize);
105  }
106 
109  bool isSmall() const {
110  return BeginX == static_cast<const void*>(&FirstEl);
111  }
112 
114  void resetToSmall() {
115  BeginX = EndX = CapacityX = &FirstEl;
116  }
117 
118  void setEnd(T* P) {
119  this->EndX = P;
120  }
121 
122  public:
123  using size_type = size_t;
124  using difference_type = ptrdiff_t;
125  using value_type = T;
126  using iterator = T*;
127  using const_iterator = const T*;
128 
129  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
130  using reverse_iterator = std::reverse_iterator<iterator>;
131 
132  using reference = T&;
133  using const_reference = const T&;
134  using pointer = T*;
135  using const_pointer = const T*;
136 
137  // forward iterator creation methods.
138  iterator begin() {
139  return (iterator)this->BeginX;
140  }
141  const_iterator begin() const {
142  return (const_iterator)this->BeginX;
143  }
144  iterator end() {
145  return (iterator)this->EndX;
146  }
147  const_iterator end() const {
148  return (const_iterator)this->EndX;
149  }
150 
151  protected:
152  iterator capacity_ptr() {
153  return (iterator)this->CapacityX;
154  }
155  const_iterator capacity_ptr() const {
156  return (const_iterator)this->CapacityX;
157  }
158 
159  public:
160  // reverse iterator creation methods.
161  reverse_iterator rbegin() {
162  return reverse_iterator(end());
163  }
164  const_reverse_iterator rbegin() const {
165  return const_reverse_iterator(end());
166  }
167  reverse_iterator rend() {
168  return reverse_iterator(begin());
169  }
170  const_reverse_iterator rend() const {
171  return const_reverse_iterator(begin());
172  }
173 
174  size_type size() const {
175  return end() - begin();
176  }
177  size_type max_size() const {
178  return size_type(-1) / sizeof(T);
179  }
180 
182  size_t capacity() const {
183  return capacity_ptr() - begin();
184  }
185 
188  return pointer(begin());
189  }
191  const_pointer data() const {
192  return const_pointer(begin());
193  }
194 
195  // SmallVector::at is NOT from LLVM.
196  reference at(size_type idx) {
197  assert(idx < size());
198  return begin()[idx];
199  }
200  const_reference at(size_type idx) const {
201  assert(idx < size());
202  return begin()[idx];
203  }
204 
205  reference operator[](size_type idx) {
206  assert(idx < size());
207  return begin()[idx];
208  }
209  const_reference operator[](size_type idx) const {
210  assert(idx < size());
211  return begin()[idx];
212  }
213 
214  reference front() {
215  assert(!empty());
216  return begin()[0];
217  }
218  const_reference front() const {
219  assert(!empty());
220  return begin()[0];
221  }
222 
223  reference back() {
224  assert(!empty());
225  return end()[-1];
226  }
227  const_reference back() const {
228  assert(!empty());
229  return end()[-1];
230  }
231 };
232 
235 template <typename T, bool isPodLike>
237  protected:
239 
240  static void destroy_range(T* S, T* E) {
241  while (S != E) {
242  --E;
243  E->~T();
244  }
245  }
246 
249  template <typename It1, typename It2>
250  static void uninitialized_move(It1 I, It1 E, It2 Dest) {
251  std::uninitialized_copy(
252  std::make_move_iterator(I), std::make_move_iterator(E), Dest);
253  }
254 
257  template <typename It1, typename It2>
258  static void uninitialized_copy(It1 I, It1 E, It2 Dest) {
259  std::uninitialized_copy(I, E, Dest);
260  }
261 
265  void grow(size_t MinSize = 0);
266 
267  public:
268  void push_back(const T& Elt) {
269  if (this->EndX >= this->CapacityX)
270  this->grow();
271  ::new ((void*)this->end()) T(Elt);
272  this->setEnd(this->end() + 1);
273  }
274 
275  void push_back(T&& Elt) {
276  if (this->EndX >= this->CapacityX)
277  this->grow();
278  ::new ((void*)this->end()) T(::std::move(Elt));
279  this->setEnd(this->end() + 1);
280  }
281 
282  void pop_back() {
283  this->setEnd(this->end() - 1);
284  this->end()->~T();
285  }
286 };
287 
288 // Define this out-of-line to dissuade the C++ compiler from inlining it.
289 template <typename T, bool isPodLike>
291  size_t CurCapacity = this->capacity();
292  size_t CurSize = this->size();
293  // Always grow, even from zero.
294  size_t NewCapacity = size_t(detail::NextPowerOf2(CurCapacity + 2));
295  if (NewCapacity < MinSize)
296  NewCapacity = MinSize;
297  T* NewElts = static_cast<T*>(malloc(NewCapacity * sizeof(T)));
298  if (NewElts == nullptr)
299  throw std::bad_alloc();
300 
301  // Move the elements over.
302  this->uninitialized_move(this->begin(), this->end(), NewElts);
303 
304  // Destroy the original elements.
305  destroy_range(this->begin(), this->end());
306 
307  // If this wasn't grown from the inline copy, deallocate the old space.
308  if (!this->isSmall())
309  free(this->begin());
310 
311  this->setEnd(NewElts + CurSize);
312  this->BeginX = NewElts;
313  this->CapacityX = this->begin() + NewCapacity;
314 }
315 
318 template <typename T>
320  protected:
322 
323  // No need to do a destroy loop for POD's.
324  static void destroy_range(T*, T*) {}
325 
328  template <typename It1, typename It2>
329  static void uninitialized_move(It1 I, It1 E, It2 Dest) {
330  // Just do a copy.
331  uninitialized_copy(I, E, Dest);
332  }
333 
336  template <typename It1, typename It2>
337  static void uninitialized_copy(It1 I, It1 E, It2 Dest) {
338  // Arbitrary iterator types; just use the basic implementation.
339  std::uninitialized_copy(I, E, Dest);
340  }
341 
344  template <typename T1, typename T2>
345  static void uninitialized_copy(
346  T1* I,
347  T1* E,
348  T2* Dest,
349  typename std::enable_if<
350  std::is_same<typename std::remove_const<T1>::type, T2>::value>::
351  type* = nullptr) {
352  // Use memcpy for PODs iterated by pointers (which includes SmallVector
353  // iterators): std::uninitialized_copy optimizes to memmove, but we can
354  // use memcpy here. Note that I and E are iterators and thus might be
355  // invalid for memcpy if they are equal.
356  if (I != E)
357  memcpy(Dest, I, (E - I) * sizeof(T));
358  }
359 
362  void grow(size_t MinSize = 0) {
363  this->grow_pod(MinSize * sizeof(T), sizeof(T));
364  }
365 
366  public:
367  void push_back(const T& Elt) {
368  if (this->EndX >= this->CapacityX)
369  this->grow();
370  memcpy(this->end(), &Elt, sizeof(T));
371  this->setEnd(this->end() + 1);
372  }
373 
374  void pop_back() {
375  this->setEnd(this->end() - 1);
376  }
377 };
378 
381 template <typename T>
383  : public SmallVectorTemplateBase<T, C10_IS_TRIVIALLY_COPYABLE(T)> {
385 
386  public:
387  using iterator = typename SuperClass::iterator;
388  using const_iterator = typename SuperClass::const_iterator;
389  using size_type = typename SuperClass::size_type;
390 
391  protected:
392  // Default ctor - Initialize to empty.
393  explicit SmallVectorImpl(unsigned N)
395  }
396 
397  public:
398  SmallVectorImpl(const SmallVectorImpl&) = delete;
399 
400  ~SmallVectorImpl() {
401  // Destroy the constructed elements in the vector.
402  this->destroy_range(this->begin(), this->end());
403 
404  // If this wasn't grown from the inline copy, deallocate the old space.
405  if (!this->isSmall())
406  free(this->begin());
407  }
408 
409  void clear() {
410  this->destroy_range(this->begin(), this->end());
411  this->EndX = this->BeginX;
412  }
413 
414  void resize(size_type N) {
415  if (N < this->size()) {
416  this->destroy_range(this->begin() + N, this->end());
417  this->setEnd(this->begin() + N);
418  } else if (N > this->size()) {
419  if (this->capacity() < N)
420  this->grow(N);
421  auto I = this->end();
422  for (auto E = this->begin() + N; I != E; ++I)
423  new (&*I) T();
424  this->setEnd(this->begin() + N);
425  }
426  }
427 
428  void resize(size_type N, const T& NV) {
429  if (N < this->size()) {
430  this->destroy_range(this->begin() + N, this->end());
431  this->setEnd(this->begin() + N);
432  } else if (N > this->size()) {
433  if (this->capacity() < N)
434  this->grow(N);
435  std::uninitialized_fill(this->end(), this->begin() + N, NV);
436  this->setEnd(this->begin() + N);
437  }
438  }
439 
440  void reserve(size_type N) {
441  if (this->capacity() < N)
442  this->grow(N);
443  }
444 
445  T pop_back_val() {
446  T Result = ::std::move(this->back());
447  this->pop_back();
448  return Result;
449  }
450 
451  void swap(SmallVectorImpl& RHS);
452 
454  template <
455  typename in_iter,
456  typename = typename std::enable_if<std::is_convertible<
457  typename std::iterator_traits<in_iter>::iterator_category,
458  std::input_iterator_tag>::value>::type>
459  void append(in_iter in_start, in_iter in_end) {
460  size_type NumInputs = std::distance(in_start, in_end);
461  // Grow allocated space if needed.
462  if (NumInputs > size_type(this->capacity_ptr() - this->end()))
463  this->grow(this->size() + NumInputs);
464 
465  // Copy the new elements over.
466  this->uninitialized_copy(in_start, in_end, this->end());
467  this->setEnd(this->end() + NumInputs);
468  }
469 
471  void append(size_type NumInputs, const T& Elt) {
472  // Grow allocated space if needed.
473  if (NumInputs > size_type(this->capacity_ptr() - this->end()))
474  this->grow(this->size() + NumInputs);
475 
476  // Copy the new elements over.
477  std::uninitialized_fill_n(this->end(), NumInputs, Elt);
478  this->setEnd(this->end() + NumInputs);
479  }
480 
481  void append(std::initializer_list<T> IL) {
482  append(IL.begin(), IL.end());
483  }
484 
485  // FIXME: Consider assigning over existing elements, rather than clearing &
486  // re-initializing them - for all assign(...) variants.
487 
488  void assign(size_type NumElts, const T& Elt) {
489  clear();
490  if (this->capacity() < NumElts)
491  this->grow(NumElts);
492  this->setEnd(this->begin() + NumElts);
493  std::uninitialized_fill(this->begin(), this->end(), Elt);
494  }
495 
496  template <
497  typename in_iter,
498  typename = typename std::enable_if<std::is_convertible<
499  typename std::iterator_traits<in_iter>::iterator_category,
500  std::input_iterator_tag>::value>::type>
501  void assign(in_iter in_start, in_iter in_end) {
502  clear();
503  append(in_start, in_end);
504  }
505 
506  void assign(std::initializer_list<T> IL) {
507  clear();
508  append(IL);
509  }
510 
511  iterator erase(const_iterator CI) {
512  // Just cast away constness because this is a non-const member function.
513  iterator I = const_cast<iterator>(CI);
514 
515  assert(I >= this->begin() && "Iterator to erase is out of bounds.");
516  assert(I < this->end() && "Erasing at past-the-end iterator.");
517 
518  iterator N = I;
519  // Shift all elts down one.
520  std::move(I + 1, this->end(), I);
521  // Drop the last elt.
522  this->pop_back();
523  return (N);
524  }
525 
526  iterator erase(const_iterator CS, const_iterator CE) {
527  // Just cast away constness because this is a non-const member function.
528  iterator S = const_cast<iterator>(CS);
529  iterator E = const_cast<iterator>(CE);
530 
531  assert(S >= this->begin() && "Range to erase is out of bounds.");
532  assert(S <= E && "Trying to erase invalid range.");
533  assert(E <= this->end() && "Trying to erase past the end.");
534 
535  iterator N = S;
536  // Shift all elts down.
537  iterator I = std::move(E, this->end(), S);
538  // Drop the last elts.
539  this->destroy_range(I, this->end());
540  this->setEnd(I);
541  return (N);
542  }
543 
544  iterator insert(iterator I, T&& Elt) {
545  if (I == this->end()) { // Important special case for empty vector.
546  this->push_back(::std::move(Elt));
547  return this->end() - 1;
548  }
549 
550  assert(I >= this->begin() && "Insertion iterator is out of bounds.");
551  assert(I <= this->end() && "Inserting past the end of the vector.");
552 
553  if (this->EndX >= this->CapacityX) {
554  size_t EltNo = I - this->begin();
555  this->grow();
556  I = this->begin() + EltNo;
557  }
558 
559  ::new ((void*)this->end()) T(::std::move(this->back()));
560  // Push everything else over.
561  std::move_backward(I, this->end() - 1, this->end());
562  this->setEnd(this->end() + 1);
563 
564  // If we just moved the element we're inserting, be sure to update
565  // the reference.
566  T* EltPtr = &Elt;
567  if (I <= EltPtr && EltPtr < this->EndX)
568  ++EltPtr;
569 
570  *I = ::std::move(*EltPtr);
571  return I;
572  }
573 
574  iterator insert(iterator I, const T& Elt) {
575  if (I == this->end()) { // Important special case for empty vector.
576  this->push_back(Elt);
577  return this->end() - 1;
578  }
579 
580  assert(I >= this->begin() && "Insertion iterator is out of bounds.");
581  assert(I <= this->end() && "Inserting past the end of the vector.");
582 
583  if (this->EndX >= this->CapacityX) {
584  size_t EltNo = I - this->begin();
585  this->grow();
586  I = this->begin() + EltNo;
587  }
588  ::new ((void*)this->end()) T(std::move(this->back()));
589  // Push everything else over.
590  std::move_backward(I, this->end() - 1, this->end());
591  this->setEnd(this->end() + 1);
592 
593  // If we just moved the element we're inserting, be sure to update
594  // the reference.
595  const T* EltPtr = &Elt;
596  if (I <= EltPtr && EltPtr < this->EndX)
597  ++EltPtr;
598 
599  *I = *EltPtr;
600  return I;
601  }
602 
603  iterator insert(iterator I, size_type NumToInsert, const T& Elt) {
604  // Convert iterator to elt# to avoid invalidating iterator when we reserve()
605  size_t InsertElt = I - this->begin();
606 
607  if (I == this->end()) { // Important special case for empty vector.
608  append(NumToInsert, Elt);
609  return this->begin() + InsertElt;
610  }
611 
612  assert(I >= this->begin() && "Insertion iterator is out of bounds.");
613  assert(I <= this->end() && "Inserting past the end of the vector.");
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(
628  std::move_iterator<iterator>(this->end() - NumToInsert),
629  std::move_iterator<iterator>(this->end()));
630 
631  // Copy the existing elements that get replaced.
632  std::move_backward(I, OldEnd - NumToInsert, OldEnd);
633 
634  std::fill_n(I, NumToInsert, Elt);
635  return I;
636  }
637 
638  // Otherwise, we're inserting more elements than exist already, and we're
639  // not inserting at the end.
640 
641  // Move over the elements that we're about to overwrite.
642  T* OldEnd = this->end();
643  this->setEnd(this->end() + NumToInsert);
644  size_t NumOverwritten = OldEnd - I;
645  this->uninitialized_move(I, OldEnd, this->end() - NumOverwritten);
646 
647  // Replace the overwritten part.
648  std::fill_n(I, NumOverwritten, Elt);
649 
650  // Insert the non-overwritten middle part.
651  std::uninitialized_fill_n(OldEnd, NumToInsert - NumOverwritten, Elt);
652  return I;
653  }
654 
655  template <
656  typename ItTy,
657  typename = typename std::enable_if<std::is_convertible<
658  typename std::iterator_traits<ItTy>::iterator_category,
659  std::input_iterator_tag>::value>::type>
660  iterator insert(iterator I, ItTy From, ItTy To) {
661  // Convert iterator to elt# to avoid invalidating iterator when we reserve()
662  size_t InsertElt = I - this->begin();
663 
664  if (I == this->end()) { // Important special case for empty vector.
665  append(From, To);
666  return this->begin() + InsertElt;
667  }
668 
669  assert(I >= this->begin() && "Insertion iterator is out of bounds.");
670  assert(I <= this->end() && "Inserting past the end of the vector.");
671 
672  size_t NumToInsert = std::distance(From, To);
673 
674  // Ensure there is enough space.
675  reserve(this->size() + NumToInsert);
676 
677  // Uninvalidate the iterator.
678  I = this->begin() + InsertElt;
679 
680  // If there are more elements between the insertion point and the end of the
681  // range than there are being inserted, we can use a simple approach to
682  // insertion. Since we already reserved space, we know that this won't
683  // reallocate the vector.
684  if (size_t(this->end() - I) >= NumToInsert) {
685  T* OldEnd = this->end();
686  append(
687  std::move_iterator<iterator>(this->end() - NumToInsert),
688  std::move_iterator<iterator>(this->end()));
689 
690  // Copy the existing elements that get replaced.
691  std::move_backward(I, OldEnd - NumToInsert, OldEnd);
692 
693  std::copy(From, To, I);
694  return I;
695  }
696 
697  // Otherwise, we're inserting more elements than exist already, and we're
698  // not inserting at the end.
699 
700  // Move over the elements that we're about to overwrite.
701  T* OldEnd = this->end();
702  this->setEnd(this->end() + NumToInsert);
703  size_t NumOverwritten = OldEnd - I;
704  this->uninitialized_move(I, OldEnd, this->end() - NumOverwritten);
705 
706  // Replace the overwritten part.
707  for (T* J = I; NumOverwritten > 0; --NumOverwritten) {
708  *J = *From;
709  ++J;
710  ++From;
711  }
712 
713  // Insert the non-overwritten middle part.
714  this->uninitialized_copy(From, To, OldEnd);
715  return I;
716  }
717 
718  void insert(iterator I, std::initializer_list<T> IL) {
719  insert(I, IL.begin(), IL.end());
720  }
721 
722  template <typename... ArgTypes>
723  void emplace_back(ArgTypes&&... Args) {
724  if (this->EndX >= this->CapacityX)
725  this->grow();
726  ::new ((void*)this->end()) T(std::forward<ArgTypes>(Args)...);
727  this->setEnd(this->end() + 1);
728  }
729 
730  SmallVectorImpl& operator=(const SmallVectorImpl& RHS);
731 
732  SmallVectorImpl& operator=(SmallVectorImpl&& RHS);
733 
734  bool operator==(const SmallVectorImpl& RHS) const {
735  if (this->size() != RHS.size())
736  return false;
737  return std::equal(this->begin(), this->end(), RHS.begin());
738  }
739  bool operator!=(const SmallVectorImpl& RHS) const {
740  return !(*this == RHS);
741  }
742 
743  bool operator<(const SmallVectorImpl& RHS) const {
744  return std::lexicographical_compare(
745  this->begin(), this->end(), RHS.begin(), RHS.end());
746  }
747 
757  void set_size(size_type N) {
758  assert(N <= this->capacity());
759  this->setEnd(this->begin() + N);
760  }
761 };
762 
763 template <typename T>
765  if (this == &RHS)
766  return;
767 
768  // We can only avoid copying elements if neither vector is small.
769  if (!this->isSmall() && !RHS.isSmall()) {
770  std::swap(this->BeginX, RHS.BeginX);
771  std::swap(this->EndX, RHS.EndX);
772  std::swap(this->CapacityX, RHS.CapacityX);
773  return;
774  }
775  if (RHS.size() > this->capacity())
776  this->grow(RHS.size());
777  if (this->size() > RHS.capacity())
778  RHS.grow(this->size());
779 
780  // Swap the shared elements.
781  size_t NumShared = this->size();
782  if (NumShared > RHS.size())
783  NumShared = RHS.size();
784  for (size_type i = 0; i != NumShared; ++i)
785  std::swap((*this)[i], RHS[i]);
786 
787  // Copy over the extra elts.
788  if (this->size() > RHS.size()) {
789  size_t EltDiff = this->size() - RHS.size();
790  this->uninitialized_copy(this->begin() + NumShared, this->end(), RHS.end());
791  RHS.setEnd(RHS.end() + EltDiff);
792  this->destroy_range(this->begin() + NumShared, this->end());
793  this->setEnd(this->begin() + NumShared);
794  } else if (RHS.size() > this->size()) {
795  size_t EltDiff = RHS.size() - this->size();
796  this->uninitialized_copy(RHS.begin() + NumShared, RHS.end(), this->end());
797  this->setEnd(this->end() + EltDiff);
798  this->destroy_range(RHS.begin() + NumShared, RHS.end());
799  RHS.setEnd(RHS.begin() + NumShared);
800  }
801 }
802 
803 template <typename T>
805  const SmallVectorImpl<T>& RHS) {
806  // Avoid self-assignment.
807  if (this == &RHS)
808  return *this;
809 
810  // If we already have sufficient space, assign the common elements, then
811  // destroy any excess.
812  size_t RHSSize = RHS.size();
813  size_t CurSize = this->size();
814  if (CurSize >= RHSSize) {
815  // Assign common elements.
816  iterator NewEnd;
817  if (RHSSize)
818  NewEnd = std::copy(RHS.begin(), RHS.begin() + RHSSize, this->begin());
819  else
820  NewEnd = this->begin();
821 
822  // Destroy excess elements.
823  this->destroy_range(NewEnd, this->end());
824 
825  // Trim.
826  this->setEnd(NewEnd);
827  return *this;
828  }
829 
830  // If we have to grow to have enough elements, destroy the current elements.
831  // This allows us to avoid copying them during the grow.
832  // FIXME: don't do this if they're efficiently moveable.
833  if (this->capacity() < RHSSize) {
834  // Destroy current elements.
835  this->destroy_range(this->begin(), this->end());
836  this->setEnd(this->begin());
837  CurSize = 0;
838  this->grow(RHSSize);
839  } else if (CurSize) {
840  // Otherwise, use assignment for the already-constructed elements.
841  std::copy(RHS.begin(), RHS.begin() + CurSize, this->begin());
842  }
843 
844  // Copy construct the new elements in place.
845  this->uninitialized_copy(
846  RHS.begin() + CurSize, RHS.end(), this->begin() + CurSize);
847 
848  // Set end.
849  this->setEnd(this->begin() + RHSSize);
850  return *this;
851 }
852 
853 template <typename T>
855  // Avoid self-assignment.
856  if (this == &RHS)
857  return *this;
858 
859  // If the RHS isn't small, clear this vector and then steal its buffer.
860  if (!RHS.isSmall()) {
861  this->destroy_range(this->begin(), this->end());
862  if (!this->isSmall())
863  free(this->begin());
864  this->BeginX = RHS.BeginX;
865  this->EndX = RHS.EndX;
866  this->CapacityX = RHS.CapacityX;
867  RHS.resetToSmall();
868  return *this;
869  }
870 
871  // If we already have sufficient space, assign the common elements, then
872  // destroy any excess.
873  size_t RHSSize = RHS.size();
874  size_t CurSize = this->size();
875  if (CurSize >= RHSSize) {
876  // Assign common elements.
877  iterator NewEnd = this->begin();
878  if (RHSSize)
879  NewEnd = std::move(RHS.begin(), RHS.end(), NewEnd);
880 
881  // Destroy excess elements and trim the bounds.
882  this->destroy_range(NewEnd, this->end());
883  this->setEnd(NewEnd);
884 
885  // Clear the RHS.
886  RHS.clear();
887 
888  return *this;
889  }
890 
891  // If we have to grow to have enough elements, destroy the current elements.
892  // This allows us to avoid copying them during the grow.
893  // FIXME: this may not actually make any sense if we can efficiently move
894  // elements.
895  if (this->capacity() < RHSSize) {
896  // Destroy current elements.
897  this->destroy_range(this->begin(), this->end());
898  this->setEnd(this->begin());
899  CurSize = 0;
900  this->grow(RHSSize);
901  } else if (CurSize) {
902  // Otherwise, use assignment for the already-constructed elements.
903  std::move(RHS.begin(), RHS.begin() + CurSize, this->begin());
904  }
905 
906  // Move-construct the new elements in place.
907  this->uninitialized_move(
908  RHS.begin() + CurSize, RHS.end(), this->begin() + CurSize);
909 
910  // Set end.
911  this->setEnd(this->begin() + RHSSize);
912 
913  RHS.clear();
914  return *this;
915 }
916 
921 template <typename T, unsigned N>
923  typename SmallVectorTemplateCommon<T>::U InlineElts[N - 1];
924 };
925 template <typename T>
926 struct SmallVectorStorage<T, 1> {};
927 template <typename T>
928 struct SmallVectorStorage<T, 0> {};
929 
938 template <typename T, unsigned N>
939 class SmallVector : public SmallVectorImpl<T> {
942 
943  public:
945 
946  explicit SmallVector(size_t Size, const T& Value = T())
947  : SmallVectorImpl<T>(N) {
948  this->assign(Size, Value);
949  }
950 
951  template <
952  typename ItTy,
953  typename = typename std::enable_if<std::is_convertible<
954  typename std::iterator_traits<ItTy>::iterator_category,
955  std::input_iterator_tag>::value>::type>
956  SmallVector(ItTy S, ItTy E) : SmallVectorImpl<T>(N) {
957  this->append(S, E);
958  }
959 
960  template <typename Container>
961  explicit SmallVector(Container&& c) : SmallVectorImpl<T>(N) {
962  this->append(c.begin(), c.end());
963  }
964 
965  SmallVector(std::initializer_list<T> IL) : SmallVectorImpl<T>(N) {
966  this->assign(IL);
967  }
968 
969  SmallVector(const SmallVector& RHS) : SmallVectorImpl<T>(N) {
970  if (!RHS.empty())
972  }
973 
974  const SmallVector& operator=(const SmallVector& RHS) {
976  return *this;
977  }
978 
979  SmallVector(SmallVector&& RHS) : SmallVectorImpl<T>(N) {
980  if (!RHS.empty())
981  SmallVectorImpl<T>::operator=(::std::move(RHS));
982  }
983 
984  template <typename Container>
985  const SmallVector& operator=(const Container& RHS) {
986  this->assign(RHS.begin(), RHS.end());
987  return *this;
988  }
989 
990  SmallVector(SmallVectorImpl<T>&& RHS) : SmallVectorImpl<T>(N) {
991  if (!RHS.empty())
992  SmallVectorImpl<T>::operator=(::std::move(RHS));
993  }
994 
995  const SmallVector& operator=(SmallVector&& RHS) {
996  SmallVectorImpl<T>::operator=(::std::move(RHS));
997  return *this;
998  }
999 
1000  const SmallVector& operator=(SmallVectorImpl<T>&& RHS) {
1001  SmallVectorImpl<T>::operator=(::std::move(RHS));
1002  return *this;
1003  }
1004 
1005  template <typename Container>
1006  const SmallVector& operator=(Container&& C) {
1007  this->assign(C.begin(), C.end());
1008  return *this;
1009  }
1010 
1011  const SmallVector& operator=(std::initializer_list<T> IL) {
1012  this->assign(IL);
1013  return *this;
1014  }
1015 };
1016 
1017 template <typename T, unsigned N>
1018 inline size_t capacity_in_bytes(const SmallVector<T, N>& X) {
1019  return X.capacity_in_bytes();
1020 }
1021 
1022 template <typename T, unsigned N>
1023 std::ostream& operator<<(std::ostream & out, const SmallVector<T, N>& list) {
1024  int i = 0;
1025  out << "[";
1026  for(auto e : list) {
1027  if (i++ > 0)
1028  out << ", ";
1029  out << e;
1030  }
1031  out << "]";
1032  return out;
1033 }
1034 
1035 } // end namespace c10
1036 
1037 namespace std {
1038 
1040 template <typename T>
1041 inline void swap(c10::SmallVectorImpl<T>& LHS, c10::SmallVectorImpl<T>& RHS) {
1042  LHS.swap(RHS);
1043 }
1044 
1046 template <typename T, unsigned N>
1047 inline void swap(c10::SmallVector<T, N>& LHS, c10::SmallVector<T, N>& RHS) {
1048  LHS.swap(RHS);
1049 }
1050 
1051 } // end namespace std
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:362
void set_size(size_type N)
Set the array size to N, which the current array must have enough capacity for.
Definition: SmallVector.h:757
Definition: static.cpp:76
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:939
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:459
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
bool isSmall() const
Return true if this is a smallvector which has not had dynamic memory allocated for it...
Definition: SmallVector.h:109
void append(size_type NumInputs, const T &Elt)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:471
This is the part of SmallVectorTemplateBase which does not depend on whether the type T is a POD...
Definition: SmallVector.h:88
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:382
size_t size_in_bytes() const
This returns size()*sizeof(T).
Definition: SmallVector.h:70
const_pointer data() const
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:191
SmallVectorTemplateBase<isPodLike = false> - This is where we put method implementations that are des...
Definition: SmallVector.h:236
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:250
size_t capacity_in_bytes() const
capacity_in_bytes - This returns capacity()*sizeof(T).
Definition: SmallVector.h:75
size_t capacity() const
Return the total number of elements in the currently allocated buffer.
Definition: SmallVector.h:182
Definition: static.cpp:52
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:345
void grow(size_t MinSize=0)
Grow the allocated memory (without initializing new elements), doubling the size of the allocated mem...
Definition: SmallVector.h:290
Definition: static.cpp:64
To register your own kernel for an operator, do in one (!) cpp file: C10_REGISTER_KERNEL(OperatorHand...
Definition: alias_info.h:7
This is all the non-templated stuff common to all SmallVectors.
Definition: SmallVector.h:56
void resetToSmall()
Put this vector in a state of being small.
Definition: SmallVector.h:114
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:337
Flush-To-Zero and Denormals-Are-Zero mode.
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:329
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:258
Storage for the SmallVector elements which aren&#39;t contained in SmallVectorTemplateCommon.
Definition: SmallVector.h:922
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:187