Path Tracer
SmallVector.h
1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
4 
5 Copyright (c) 2006-2020, assimp team
6 
7 All rights reserved.
8 
9 Redistribution and use of this software in source and binary forms,
10 with or without modification, are permitted provided that the
11 following conditions are met:
12 
13 * Redistributions of source code must retain the above
14  copyright notice, this list of conditions and the
15  following disclaimer.
16 
17 * Redistributions in binary form must reproduce the above
18  copyright notice, this list of conditions and the
19  following disclaimer in the documentation and/or other
20  materials provided with the distribution.
21 
22 * Neither the name of the assimp team, nor the names of its
23  contributors may be used to endorse or promote products
24  derived from this software without specific prior
25  written permission of the assimp team.
26 
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 
39 ----------------------------------------------------------------------
40 */
41 
45 #pragma once
46 #ifndef AI_SMALLVECTOR_H_INC
47 #define AI_SMALLVECTOR_H_INC
48 
49 #ifdef __GNUC__
50 # pragma GCC system_header
51 #endif
52 
53 namespace Assimp {
54 
55 // --------------------------------------------------------------------------------------------
61 // --------------------------------------------------------------------------------------------
62 template<typename T, unsigned int Capacity>
63 class SmallVector {
64 public:
67  mStorage(mInplaceStorage),
68  mSize(0),
69  mCapacity(Capacity) {
70  // empty
71  }
72 
75  if (mStorage != mInplaceStorage) {
76  delete [] mStorage;
77  }
78  }
79 
82  void push_back(const T& item) {
83  if (mSize < mCapacity) {
84  mStorage[mSize++] = item;
85  return;
86  }
87 
88  push_back_and_grow(item);
89  }
90 
93  void resize(size_t newSize) {
94  if (newSize > mCapacity) {
95  grow(newSize);
96  }
97  mSize = newSize;
98  }
99 
102  size_t size() const {
103  return mSize;
104  }
105 
108  T* begin() {
109  return mStorage;
110  }
111 
114  T* end() {
115  return &mStorage[mSize];
116  }
117 
120  T* begin() const {
121  return mStorage;
122  }
123 
126  T* end() const {
127  return &mStorage[mSize];
128  }
129 
130  SmallVector(const SmallVector &) = delete;
131  SmallVector(SmallVector &&) = delete;
132  SmallVector &operator = (const SmallVector &) = delete;
133  SmallVector &operator = (SmallVector &&) = delete;
134 
135 private:
136  void grow( size_t newCapacity) {
137  T* oldStorage = mStorage;
138  T* newStorage = new T[newCapacity];
139 
140  std::memcpy(newStorage, oldStorage, mSize * sizeof(T));
141 
142  mStorage = newStorage;
143  mCapacity = newCapacity;
144 
145  if (oldStorage != mInplaceStorage) {
146  delete [] oldStorage;
147  }
148  }
149 
150  void push_back_and_grow(const T& item) {
151  grow(mCapacity + Capacity);
152 
153  mStorage[mSize++] = item;
154  }
155 
156  T* mStorage;
157  size_t mSize;
158  size_t mCapacity;
159  T mInplaceStorage[Capacity];
160 };
161 
162 } // end namespace Assimp
163 
164 #endif // !! AI_SMALLVECTOR_H_INC
Assimp::SmallVector::end
T * end() const
Returns a const pointer to the end.
Definition: SmallVector.h:126
Assimp::SmallVector::push_back
void push_back(const T &item)
Will push a new item. The capacity will grow in case of a too small capacity.
Definition: SmallVector.h:82
Assimp::SmallVector::end
T * end()
Returns a pointer to the end.
Definition: SmallVector.h:114
Assimp::SmallVector::resize
void resize(size_t newSize)
Will resize the vector.
Definition: SmallVector.h:93
Assimp::SmallVector
Small vector with inplace storage.
Definition: SmallVector.h:63
Assimp::SmallVector::SmallVector
SmallVector()
The default class constructor.
Definition: SmallVector.h:66
Assimp::SmallVector::begin
T * begin() const
Returns a const pointer to the first item.
Definition: SmallVector.h:120
Assimp::SmallVector::begin
T * begin()
Returns a pointer to the first item.
Definition: SmallVector.h:108
Assimp::SmallVector::~SmallVector
~SmallVector()
The class destructor.
Definition: SmallVector.h:74
Assimp::SmallVector::size
size_t size() const
Returns the current size of the vector.
Definition: SmallVector.h:102
Assimp
Definition: ai_assert.h:50