@@ -99,7 +99,7 @@ class FastSmallVector
99
99
FastSmallVector& operator =(FastSmallVector&& other)
100
100
{
101
101
size_ = other.size_ ;
102
- if (size_ <= N ) {
102
+ if (other. usingSmallBuf () ) {
103
103
smallBuf_ = std::move (other.smallBuf_ );
104
104
dataPtr_ = smallBuf_.data ();
105
105
}
@@ -119,7 +119,7 @@ class FastSmallVector
119
119
{
120
120
size_ = other.size_ ;
121
121
122
- if (size_ <= N ) {
122
+ if (other. usingSmallBuf () ) {
123
123
smallBuf_ = other.smallBuf_ ;
124
124
dataPtr_ = smallBuf_.data ();
125
125
}
@@ -176,20 +176,22 @@ class FastSmallVector
176
176
{ return dataPtr_ + size_; }
177
177
178
178
size_type capacity ()
179
- { return size_ <= N ? N : data_.capacity (); }
179
+ { return this -> usingSmallBuf () ? N : data_.capacity (); }
180
180
181
181
void push_back (const ValueType& value)
182
182
{
183
- if (size_ < N) {
184
- // Data is contained in smallBuf_
185
- smallBuf_[size_++] = value;
186
- } else if (size_ == N) {
187
- // Must switch from using smallBuf_ to using data_
188
- data_.reserve (N + 1 );
189
- data_.assign (smallBuf_.begin (), smallBuf_.end ());
190
- data_.push_back (value);
191
- ++size_;
192
- dataPtr_ = data_.data ();
183
+ if (this ->usingSmallBuf ()) {
184
+ if (size_ < N) {
185
+ // Data is contained in smallBuf_
186
+ smallBuf_[size_++] = value;
187
+ } else if (size_ == N) {
188
+ // Must switch from using smallBuf_ to using data_
189
+ data_.reserve (N + 1 );
190
+ data_.assign (smallBuf_.begin (), smallBuf_.end ());
191
+ data_.push_back (value);
192
+ ++size_;
193
+ dataPtr_ = data_.data ();
194
+ }
193
195
} else {
194
196
// Data is contained in data_
195
197
data_.push_back (value);
@@ -198,17 +200,23 @@ class FastSmallVector
198
200
}
199
201
}
200
202
201
- void expandSize (size_t numElem)
203
+ void resize (size_t numElem)
202
204
{
203
- assert (numElem >= size_);
204
- if (size_ <= N) {
205
+ if (numElem == size_) return ; // nothing to do
206
+
207
+ if (this ->usingSmallBuf ()) {
205
208
if (numElem > N) {
206
- data_.resize (numElem, 0 );
207
- data_. assign (smallBuf_.begin (), smallBuf_.begin () + size_);
209
+ data_.resize (numElem);
210
+ std::copy (smallBuf_.begin (), smallBuf_.begin () + size_, data_. begin () );
208
211
dataPtr_ = data_.data ();
212
+ } else if (numElem < size_) {
213
+ // when shrinking, remove the values after numElem so that the space
214
+ // is ready to use in the potentional future resize
215
+ std::fill (smallBuf_.begin () + numElem, smallBuf_.begin () + size_, ValueType{});
209
216
}
210
217
} else {
211
- data_.resize (numElem, 0 );
218
+ // when shriking to numElem < N, we do not switch back to use smallBuf_
219
+ data_.resize (numElem);
212
220
}
213
221
size_ = numElem;
214
222
}
@@ -225,6 +233,11 @@ class FastSmallVector
225
233
dataPtr_ = smallBuf_.data ();
226
234
}
227
235
236
+ bool usingSmallBuf () const
237
+ {
238
+ return dataPtr_ == smallBuf_.data ();
239
+ }
240
+
228
241
std::array<ValueType, N> smallBuf_{};
229
242
std::vector<ValueType> data_;
230
243
size_type size_;
0 commit comments