25#ifndef __BITPIT_STENCIL_WEIGHT_TPP__
26#define __BITPIT_STENCIL_WEIGHT_TPP__
36template<
typename weight_t>
38 : m_capacity(capacity)
49template<
typename weight_t>
52 return m_storage.size();
63template<
typename weight_t>
79template<
typename weight_t>
84 m_storage.shrink_to_fit();
95template<
typename weight_t>
99 throw std::runtime_error(
"Unable to retrieve a weight from the pool: the pool is empty.");
102 weight_t weight = std::move(m_storage.back());
103 m_storage.pop_back();
113template<
typename weight_t>
116 if (m_capacity == size()) {
120 m_storage.emplace_back(std::move(weight));
128template<
typename weight_t>
131 std::size_t nStorableWeights = std::min(m_capacity - size(), weights->size());
132 if (nStorableWeights == 0) {
136 m_storage.insert(m_storage.end(),
137 std::make_move_iterator(weights->begin()),
138 std::make_move_iterator(weights->begin() + nStorableWeights));
149template<
typename weight_t,
typename value_t>
153 return (std::abs(weight - zero) <= tolerance);
164template<
typename weight_t,
typename value_t>
165template<
typename W,
typename V, std::
size_t D,
typename std::enable_if<std::is_same<std::array<V, D>, W>::value>::type *>
168 for (std::size_t k = 0; k < D; ++k) {
169 if (std::abs(weight[k] - zero[k]) > tolerance) {
185template<
typename weight_t,
typename value_t>
186template<
typename W,
typename V,
typename std::enable_if<std::is_same<std::vector<V>, W>::value>::type *>
189 const int nItems = weight.size();
190 for (
int k = 0; k < nItems; ++k) {
191 if (std::abs(weight[k] - zero[k]) > tolerance) {
208template<
typename weight_t,
typename value_t>
212 *target += factor * weight;
223template<
typename weight_t,
typename value_t>
224template<
typename W,
typename V, std::
size_t D,
typename std::enable_if<std::is_same<std::array<V, D>, W>::value>::type *>
227 for (std::size_t i = 0; i < D; ++i) {
228 (*target)[i] += factor * weight[i];
244template<
typename weight_t,
typename value_t>
245template<
typename W,
typename V,
typename std::enable_if<std::is_same<std::vector<V>, W>::value>::type *>
248 std::size_t weightSize = weight.size();
249 std::size_t targetSize = target->size();
250 std::size_t commonSize = std::min(weightSize, targetSize);
252 for (std::size_t i = 0; i < commonSize; ++i) {
253 (*target)[i] += factor * weight[i];
256 if (weightSize > targetSize) {
257 target->insert(target->end(), weight.cbegin() + commonSize, weight.cend());
260 auto targetBegin = target->begin();
261 auto targetEnd = target->end();
262 for (
auto itr = targetBegin + commonSize; itr != targetEnd; ++itr) {
275template<
typename weight_t,
typename value_t>
288template<
typename weight_t,
typename value_t>
289template<
typename W,
typename V, std::
size_t D,
typename std::enable_if<std::is_same<std::array<V, D>, W>::value>::type *>
292 std::copy_n(weight.data(), D, target->data());
303template<
typename weight_t,
typename value_t>
304template<
typename W,
typename V,
typename std::enable_if<std::is_same<std::vector<V>, W>::value>::type *>
307 std::size_t weightSize = weight.size();
308 std::size_t targetSize = target->size();
309 std::size_t commonSize = std::min(weightSize, targetSize);
311 std::copy_n(weight.data(), commonSize, target->data());
313 if (weightSize < targetSize) {
314 target->resize(weightSize);
315 }
else if (weightSize > targetSize) {
316 target->insert(target->end(), weight.begin() + commonSize, weight.end());
326template<
typename weight_t,
typename value_t>
330 *target = std::move(weight);
339template<
typename weight_t,
typename value_t>
352template<
typename weight_t,
typename value_t>
367template<
typename weight_t,
typename value_t>
368template<
typename W,
typename V, std::
size_t D,
typename std::enable_if<std::is_same<std::array<V, D>, W>::value>::type *>
380template<
typename weight_t,
typename value_t>
381template<
typename W,
typename V, std::
size_t D,
typename std::enable_if<std::is_same<std::array<V, D>, W>::value>::type *>
384 return weight[index];
393template<
typename weight_t,
typename value_t>
394template<
typename W,
typename V,
typename std::enable_if<std::is_same<std::vector<V>, W>::value>::type *>
406template<
typename weight_t,
typename value_t>
407template<
typename W,
typename V,
typename std::enable_if<std::is_same<std::vector<V>, W>::value>::type *>
410 return weight[index];
bool isNegligible(const W &weight, const weight_t &zero, double tolerance=1e-12) const
void copy(const W &weight, W *target) const
value_t & at(const W &weight, std::size_t index)
void move(W &&weight, W *target) const
void sum(const W &weight, double factor, W *target) const
void store(weight_t &&weight)
DiscreteStencilWeightPool(std::size_t capacity=128)
std::size_t capacity() const
#define BITPIT_UNUSED(variable)