MCPcopy Create free account
hub / github.com/ComputationalRobotics/XM-code / HostDnTen

Class HostDnTen

XM/include/Utils/memory.h:189–273  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

187
188template <typename T>
189class HostDnTen {
190public:
191 size_s* dimensions = nullptr; // Array to store the size of each dimension
192 size_s num_dims = 0; // Number of dimensions
193 size_l total_size = 1;
194 T* vals = nullptr; // Pointer to the array of values
195 T initial_value;
196 bool is_initial_value = false;
197
198 HostDnTen() {}
199
200 HostDnTen(const size_s num_dims, const size_s* dimensions) : num_dims(num_dims){
201 this->dimensions = (size_s*)malloc(sizeof(size_s) * num_dims);
202 std::memcpy(this->dimensions, dimensions, sizeof(size_s) * num_dims); // Copy dimensions
203 total_size = 1;
204 for (size_s i = 0; i < num_dims; ++i) {
205 total_size *= dimensions[i]; // Calculate total number of elements
206 }
207 this->allocate();
208 }
209 HostDnTen(const size_s num_dims, const size_s* dimensions, T initial_value) : num_dims(num_dims) ,initial_value(initial_value){
210 this->dimensions = (size_s*)malloc(sizeof(size_s) * num_dims);
211 std::memcpy(this->dimensions, dimensions, sizeof(size_s) * num_dims); // Copy dimensions
212 is_initial_value = true;
213 total_size = 1;
214 for (size_s i = 0; i < num_dims; ++i) {
215 total_size *= dimensions[i]; // Calculate total number of elements
216 }
217 this->allocate();
218 }
219
220 HostDnTen(std::initializer_list<size_s> dims) : num_dims(dims.size()){
221 dimensions = new size_s[num_dims];
222 std::copy(dims.begin(), dims.end(), dimensions);
223 total_size = 1;
224 for (size_s i = 0; i < num_dims; ++i) {
225 total_size *= dimensions[i]; // Calculate total number of elements
226 }
227 this->allocate();
228 }
229
230 // Allocate memory for the values
231 void allocate() {
232 if (this->vals == nullptr) {
233 this->vals = (T*)malloc(sizeof(T) * total_size);
234 if (this->vals && is_initial_value) {
235 std::fill(this->vals, this->vals + total_size, initial_value);
236 }
237 }else{
238 std::cout << "Error: Vec already allocated!" << std::endl;
239 }
240 }
241
242 // Print matrix, if matrix is larger the 3-dimension then donot print
243 void print() {
244 if(num_dims == 1){
245 for (size_s j = 0; j < dimensions[0]; j++) {
246 std::printf("%0.2f ", vals[j]);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected