Skip to content

Commit 70c0f1e

Browse files
committed
addressing comments regarding the FastSmallVector::expandSize
it is renamed to a more general resize and the function is revised accordingly.
1 parent 28a301b commit 70c0f1e

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

opm/material/common/FastSmallVector.hpp

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class FastSmallVector
9999
FastSmallVector& operator=(FastSmallVector&& other)
100100
{
101101
size_ = other.size_;
102-
if (size_ <= N) {
102+
if (other.usingSmallBuf()) {
103103
smallBuf_ = std::move(other.smallBuf_);
104104
dataPtr_ = smallBuf_.data();
105105
}
@@ -119,7 +119,7 @@ class FastSmallVector
119119
{
120120
size_ = other.size_;
121121

122-
if (size_ <= N) {
122+
if (other.usingSmallBuf()) {
123123
smallBuf_ = other.smallBuf_;
124124
dataPtr_ = smallBuf_.data();
125125
}
@@ -176,20 +176,22 @@ class FastSmallVector
176176
{ return dataPtr_ + size_; }
177177

178178
size_type capacity()
179-
{ return size_ <= N ? N : data_.capacity(); }
179+
{ return this->usingSmallBuf() ? N : data_.capacity(); }
180180

181181
void push_back(const ValueType& value)
182182
{
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+
}
193195
} else {
194196
// Data is contained in data_
195197
data_.push_back(value);
@@ -198,17 +200,23 @@ class FastSmallVector
198200
}
199201
}
200202

201-
void expandSize(size_t numElem)
203+
void resize(size_t numElem)
202204
{
203-
assert(numElem >= size_);
204-
if (size_ <= N) {
205+
if (numElem == size_) return; // nothing to do
206+
207+
if (this->usingSmallBuf()) {
205208
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());
208211
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{});
209216
}
210217
} else {
211-
data_.resize(numElem, 0);
218+
// when shriking to numElem < N, we do not switch back to use smallBuf_
219+
data_.resize(numElem);
212220
}
213221
size_ = numElem;
214222
}
@@ -225,6 +233,11 @@ class FastSmallVector
225233
dataPtr_ = smallBuf_.data();
226234
}
227235

236+
bool usingSmallBuf() const
237+
{
238+
return dataPtr_ == smallBuf_.data();
239+
}
240+
228241
std::array<ValueType, N> smallBuf_{};
229242
std::vector<ValueType> data_;
230243
size_type size_;

opm/material/densead/DynamicEvaluation.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ class Evaluation<ValueT, DynamicSize, staticSize>
691691
void appendDerivativesToConstant(size_t numDer) {
692692
assert(size() == 0); // we only append derivatives to a constant
693693
if (numDer > 0) {
694-
data_.expandSize(1 + numDer);
694+
data_.resize(1 + numDer);
695695
}
696696
}
697697
};

0 commit comments

Comments
 (0)