|
1 | 1 | #pragma once |
2 | 2 |
|
3 | | -#include <stdlib.h> |
4 | 3 | #include "grid.h" |
5 | 4 | #include "point_source.h" |
| 5 | +#include <stdlib.h> |
| 6 | +#include <utility> |
| 7 | +#include <vector> |
| 8 | + |
| 9 | +template <class T> class PointSourceList { |
| 10 | + public: |
| 11 | + PointSourceList() = default; |
| 12 | + ~PointSourceList(); |
| 13 | + |
| 14 | + // Copy constructor |
| 15 | + PointSourceList(const PointSourceList &other); |
| 16 | + |
| 17 | + // Copy assignment operator |
| 18 | + auto operator=(const PointSourceList &other) -> PointSourceList &; |
| 19 | + |
| 20 | + // Move constructor |
| 21 | + PointSourceList(PointSourceList &&other) noexcept; |
| 22 | + |
| 23 | + // Move assignment operator |
| 24 | + auto operator=(PointSourceList &&other) noexcept -> PointSourceList &; |
| 25 | + |
| 26 | + void Scale(T scalar); |
| 27 | + void Add(const PointSource<T> &source); |
| 28 | + void SetCoordinates(const Grid<T> &grid); |
| 29 | + void Update(T current_time); |
| 30 | + auto SumOfRates() -> T; |
6 | 31 |
|
7 | | -template<class T> |
8 | | -class PointSourceList { |
9 | | -public: |
10 | | - PointSourceList(void) : num_points_(0), x_(nullptr), y_(nullptr), |
11 | | - x_id_(nullptr), y_id_(nullptr), rate_(nullptr) {} |
12 | | - ~PointSourceList(void); |
13 | | - void Scale(const T scalar); |
14 | | - void Add(const PointSource<T>& source); |
15 | | - void SetCoordinates(const Grid<T>& grid); |
16 | | - void Update(const T current_time); |
17 | | - T SumOfRates(void); |
18 | | - |
19 | | - unsigned int& num_points(void) { return num_points_; } |
20 | | - double* x(void) const { return x_; } |
21 | | - double* y(void) const { return y_; } |
22 | | - unsigned int* x_id(void) const { return x_id_; } |
23 | | - unsigned int* y_id(void) const { return y_id_; } |
24 | | - T* rate(void) const { return rate_; } |
25 | | - |
26 | | -protected: |
27 | | - std::vector< PointSource<T> > sources_; |
28 | | - unsigned int num_points_; |
29 | | - double* x_; |
30 | | - double* y_; |
31 | | - unsigned int* x_id_; |
32 | | - unsigned int* y_id_; |
33 | | - T* rate_; |
| 32 | + auto num_points() -> unsigned int & { return num_points_; } |
| 33 | + auto x() const -> double * { return x_; } |
| 34 | + auto y() const -> double * { return y_; } |
| 35 | + auto x_id() const -> unsigned int * { return x_id_; } |
| 36 | + auto y_id() const -> unsigned int * { return y_id_; } |
| 37 | + auto rate() const -> T * { return rate_; } |
| 38 | + |
| 39 | + private: |
| 40 | + static constexpr double ROUNDING_OFFSET = 0.5; |
| 41 | + |
| 42 | + std::vector<PointSource<T>> sources_; |
| 43 | + unsigned int num_points_{0}; |
| 44 | + double *x_{nullptr}; |
| 45 | + double *y_{nullptr}; |
| 46 | + unsigned int *x_id_{nullptr}; |
| 47 | + unsigned int *y_id_{nullptr}; |
| 48 | + T *rate_{nullptr}; |
34 | 49 | }; |
35 | 50 |
|
36 | | -template<class T> |
37 | | -void PointSourceList<T>::Scale(const T scalar) { |
38 | | - for (unsigned int i = 0; i < num_points_; i++) { |
39 | | - sources_[i].Scale(scalar); |
40 | | - } |
| 51 | +// Copy constructor |
| 52 | +template <class T> |
| 53 | +PointSourceList<T>::PointSourceList(const PointSourceList &other) |
| 54 | + : sources_(other.sources_), num_points_(other.num_points_) { |
| 55 | + if (num_points_ > 0) { |
| 56 | + x_ = static_cast<double *>(malloc(num_points_ * sizeof(double))); |
| 57 | + y_ = static_cast<double *>(malloc(num_points_ * sizeof(double))); |
| 58 | + x_id_ = static_cast<unsigned int *>( |
| 59 | + malloc(num_points_ * sizeof(unsigned int))); |
| 60 | + y_id_ = static_cast<unsigned int *>( |
| 61 | + malloc(num_points_ * sizeof(unsigned int))); |
| 62 | + rate_ = static_cast<T *>(malloc(num_points_ * sizeof(T))); |
| 63 | + |
| 64 | + for (unsigned int i = 0; i < num_points_; i++) { |
| 65 | + x_[i] = other.x_[i]; |
| 66 | + y_[i] = other.y_[i]; |
| 67 | + x_id_[i] = other.x_id_[i]; |
| 68 | + y_id_[i] = other.y_id_[i]; |
| 69 | + rate_[i] = other.rate_[i]; |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// Copy assignment operator |
| 75 | +template <class T> |
| 76 | +auto PointSourceList<T>::operator=(const PointSourceList &other) |
| 77 | + -> PointSourceList & { |
| 78 | + if (this != &other) { |
| 79 | + // Clean up existing resources |
| 80 | + free(x_); |
| 81 | + free(y_); |
| 82 | + free(x_id_); |
| 83 | + free(y_id_); |
| 84 | + free(rate_); |
| 85 | + |
| 86 | + // Copy data |
| 87 | + sources_ = other.sources_; |
| 88 | + num_points_ = other.num_points_; |
| 89 | + |
| 90 | + if (num_points_ > 0) { |
| 91 | + x_ = static_cast<double *>(malloc(num_points_ * sizeof(double))); |
| 92 | + y_ = static_cast<double *>(malloc(num_points_ * sizeof(double))); |
| 93 | + x_id_ = static_cast<unsigned int *>( |
| 94 | + malloc(num_points_ * sizeof(unsigned int))); |
| 95 | + y_id_ = static_cast<unsigned int *>( |
| 96 | + malloc(num_points_ * sizeof(unsigned int))); |
| 97 | + rate_ = static_cast<T *>(malloc(num_points_ * sizeof(T))); |
| 98 | + |
| 99 | + for (unsigned int i = 0; i < num_points_; i++) { |
| 100 | + x_[i] = other.x_[i]; |
| 101 | + y_[i] = other.y_[i]; |
| 102 | + x_id_[i] = other.x_id_[i]; |
| 103 | + y_id_[i] = other.y_id_[i]; |
| 104 | + rate_[i] = other.rate_[i]; |
| 105 | + } |
| 106 | + } else { |
| 107 | + x_ = nullptr; |
| 108 | + y_ = nullptr; |
| 109 | + x_id_ = nullptr; |
| 110 | + y_id_ = nullptr; |
| 111 | + rate_ = nullptr; |
| 112 | + } |
| 113 | + } |
| 114 | + return *this; |
| 115 | +} |
| 116 | + |
| 117 | +// Move constructor |
| 118 | +template <class T> |
| 119 | +PointSourceList<T>::PointSourceList(PointSourceList &&other) noexcept |
| 120 | + : sources_(std::move(other.sources_)), num_points_(other.num_points_), |
| 121 | + x_(other.x_), y_(other.y_), x_id_(other.x_id_), y_id_(other.y_id_), |
| 122 | + rate_(other.rate_) { |
| 123 | + other.num_points_ = 0; |
| 124 | + other.x_ = nullptr; |
| 125 | + other.y_ = nullptr; |
| 126 | + other.x_id_ = nullptr; |
| 127 | + other.y_id_ = nullptr; |
| 128 | + other.rate_ = nullptr; |
41 | 129 | } |
42 | 130 |
|
| 131 | +// Move assignment operator |
43 | 132 | template <class T> |
44 | | -void PointSourceList<T>::Add(const PointSource<T>& source) { |
45 | | - sources_.push_back(source); |
46 | | - num_points_ += 1; |
47 | | - |
48 | | - x_ = (double*)realloc(x_, num_points_*sizeof(double)); |
49 | | - y_ = (double*)realloc(y_, num_points_*sizeof(double)); |
50 | | - x_id_ = (unsigned int*)realloc(x_id_, num_points_*sizeof(unsigned int)); |
51 | | - y_id_ = (unsigned int*)realloc(y_id_, num_points_*sizeof(unsigned int)); |
52 | | - rate_ = (T*)realloc(rate_, num_points_*sizeof(T)); |
53 | | - |
54 | | - // Array ID of the element we are adding. |
55 | | - unsigned int id = num_points_ - 1; |
56 | | - |
57 | | - x_[id] = source.x(); |
58 | | - y_[id] = source.y(); |
59 | | - x_id_[id] = 0; |
60 | | - y_id_[id] = 0; |
61 | | - rate_[id] = (T)0; |
| 133 | +auto PointSourceList<T>::operator=(PointSourceList &&other) noexcept |
| 134 | + -> PointSourceList & { |
| 135 | + if (this != &other) { |
| 136 | + // Clean up existing resources |
| 137 | + free(x_); |
| 138 | + free(y_); |
| 139 | + free(x_id_); |
| 140 | + free(y_id_); |
| 141 | + free(rate_); |
| 142 | + |
| 143 | + // Move data |
| 144 | + sources_ = std::move(other.sources_); |
| 145 | + num_points_ = other.num_points_; |
| 146 | + x_ = other.x_; |
| 147 | + y_ = other.y_; |
| 148 | + x_id_ = other.x_id_; |
| 149 | + y_id_ = other.y_id_; |
| 150 | + rate_ = other.rate_; |
| 151 | + |
| 152 | + // Reset other object |
| 153 | + other.num_points_ = 0; |
| 154 | + other.x_ = nullptr; |
| 155 | + other.y_ = nullptr; |
| 156 | + other.x_id_ = nullptr; |
| 157 | + other.y_id_ = nullptr; |
| 158 | + other.rate_ = nullptr; |
| 159 | + } |
| 160 | + return *this; |
| 161 | +} |
| 162 | + |
| 163 | +template <class T> void PointSourceList<T>::Scale(const T scalar) { |
| 164 | + for (unsigned int i = 0; i < num_points_; i++) { |
| 165 | + sources_[i].Scale(scalar); |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +template <class T> void PointSourceList<T>::Add(const PointSource<T> &source) { |
| 170 | + sources_.push_back(source); |
| 171 | + num_points_ += 1; |
| 172 | + |
| 173 | + x_ = static_cast<double *>(realloc(x_, num_points_ * sizeof(double))); |
| 174 | + y_ = static_cast<double *>(realloc(y_, num_points_ * sizeof(double))); |
| 175 | + x_id_ = static_cast<unsigned int *>( |
| 176 | + realloc(x_id_, num_points_ * sizeof(unsigned int))); |
| 177 | + y_id_ = static_cast<unsigned int *>( |
| 178 | + realloc(y_id_, num_points_ * sizeof(unsigned int))); |
| 179 | + rate_ = static_cast<T *>(realloc(rate_, num_points_ * sizeof(T))); |
| 180 | + |
| 181 | + // Array index of the element we are adding. |
| 182 | + const unsigned int index = num_points_ - 1; |
| 183 | + |
| 184 | + x_[index] = source.x(); |
| 185 | + y_[index] = source.y(); |
| 186 | + x_id_[index] = 0; |
| 187 | + y_id_[index] = 0; |
| 188 | + rate_[index] = T{0}; |
62 | 189 | } |
63 | 190 |
|
64 | | -template<class T> |
65 | | -void PointSourceList<T>::SetCoordinates(const Grid<T>& grid) { |
66 | | - for (unsigned int i = 0; i < num_points_; i++) { |
67 | | - unsigned int column = (unsigned int)((x_[i] - grid.x_lower_left()) / grid.cellsize() + 0.5); |
68 | | - unsigned int row = (unsigned int)((y_[i] - grid.y_lower_left()) / grid.cellsize() + 0.5); |
| 191 | +template <class T> |
| 192 | +void PointSourceList<T>::SetCoordinates(const Grid<T> &grid) { |
| 193 | + for (unsigned int i = 0; i < num_points_; i++) { |
| 194 | + const auto column = static_cast<unsigned int>( |
| 195 | + ((x_[i] - grid.x_lower_left()) / grid.cellsize()) + |
| 196 | + ROUNDING_OFFSET); |
| 197 | + const auto row = static_cast<unsigned int>( |
| 198 | + ((y_[i] - grid.y_lower_left()) / grid.cellsize()) + |
| 199 | + ROUNDING_OFFSET); |
69 | 200 |
|
70 | | - x_id_[i] = column; |
71 | | - y_id_[i] = row; |
72 | | - } |
| 201 | + x_id_[i] = column; |
| 202 | + y_id_[i] = row; |
| 203 | + } |
73 | 204 | } |
74 | 205 |
|
75 | | -template<class T> |
76 | | -void PointSourceList<T>::Update(const T current_time) { |
77 | | - for (unsigned int i = 0; i < num_points_; i++) { |
78 | | - rate_[i] = sources_[i].interpolated_rate(current_time); |
79 | | - } |
| 206 | +template <class T> void PointSourceList<T>::Update(const T current_time) { |
| 207 | + for (unsigned int i = 0; i < num_points_; i++) { |
| 208 | + rate_[i] = sources_[i].interpolated_rate(current_time); |
| 209 | + } |
80 | 210 | } |
81 | 211 |
|
82 | | -template<class T> |
83 | | -T PointSourceList<T>::SumOfRates(void) { |
84 | | - T sum = (T)0; |
85 | | - for (unsigned int i = 0; i < num_points_; i++) { |
86 | | - sum += rate_[i]; |
87 | | - } |
88 | | - return sum; |
| 212 | +template <class T> auto PointSourceList<T>::SumOfRates() -> T { |
| 213 | + T sum = T{0}; |
| 214 | + for (unsigned int i = 0; i < num_points_; i++) { |
| 215 | + sum += rate_[i]; |
| 216 | + } |
| 217 | + return sum; |
89 | 218 | } |
90 | 219 |
|
91 | | -template<class T> |
92 | | -PointSourceList<T>::~PointSourceList(void) { |
93 | | - free(x_); |
94 | | - free(y_); |
95 | | - free(x_id_); |
96 | | - free(y_id_); |
97 | | - free(rate_); |
98 | | - |
99 | | - x_ = nullptr; |
100 | | - y_ = nullptr; |
101 | | - y_id_ = nullptr; |
102 | | - x_id_ = nullptr; |
103 | | - rate_ = nullptr; |
| 220 | +template <class T> PointSourceList<T>::~PointSourceList() { |
| 221 | + free(x_); |
| 222 | + free(y_); |
| 223 | + free(x_id_); |
| 224 | + free(y_id_); |
| 225 | + free(rate_); |
| 226 | + |
| 227 | + x_ = nullptr; |
| 228 | + y_ = nullptr; |
| 229 | + y_id_ = nullptr; |
| 230 | + x_id_ = nullptr; |
| 231 | + rate_ = nullptr; |
104 | 232 | } |
0 commit comments