Deterministic Gaussian Sampling
Loading...
Searching...
No Matches
gsl_utils_weight_helper.h
1#ifndef GSL_UTILS_WEIGHT_HELPER_H
2#define GSL_UTILS_WEIGHT_HELPER_H
3
4#include <type_traits>
5#include <stdexcept>
6
7#include "gsl_vector_matrix_types.h"
8
9template <typename T>
11 static_assert(std::is_same<T, float>::value ||
12 std::is_same<T, double>::value,
13 "Only float and double supported");
14
15 public:
16 using GSLVectorType =
17 typename GSLTemplateTypeAlias<T>::VectorType;
18
19 GSLWeightHelper(const GSLVectorType* v, size_t size) {
20 if (size == 0)
21 throw std::runtime_error("Weight vector size must be > 0");
22
23 if (!v) {
25 _freeMemory = true;
26
27 const T weight = static_cast<T>(1.0) / static_cast<T>(size);
28 for (size_t i = 0; i < size; ++i)
29 _ownedPtr->data[i] = weight;
30
31 _ptr = _ownedPtr;
32 } else {
33 _ptr = v;
34 }
35 }
36
38 if (_freeMemory && _ownedPtr) {
40 }
41 }
42
43 const GSLVectorType* get() const { return _ptr; }
44
45 operator const GSLVectorType*() const { return _ptr; }
46
47 private:
48 const GSLVectorType* _ptr = nullptr;
49 GSLVectorType* _ownedPtr = nullptr;
50 bool _freeMemory = false;
51};
52
53#endif // GSL_UTILS_WEIGHT_HELPER_H
Definition gsl_vector_matrix_types.h:13
Definition gsl_utils_weight_helper.h:10
Definition dirac_to_dirac_approx_short.h:9