scifir-units 2.0.0
scifir-units is a library of units of measurement, angles, coordinates, fields, and related data, all lightweight, that help in the development of scientific software and scientific machines
Loading...
Searching...
No Matches
size_nd.hpp
Go to the documentation of this file.
1#ifndef SCIFIR_UNITS_SPECIAL_UNITS_SIZE_ND_HPP_INCLUDED
2#define SCIFIR_UNITS_SPECIAL_UNITS_SIZE_ND_HPP_INCLUDED
3
4#include "../derived_units/space_units.hpp"
5#include "../util/types.hpp"
6
7#include "boost/algorithm/string.hpp"
8
9#include <string>
10#include <vector>
11
12using namespace std;
13
14namespace scifir
15{
16 template<typename T = length>
17 class size_nd
18 {
19 public:
21 {}
22
24 {}
25
26 size_nd(size_nd<T>&& x) : widths(std::move(x.widths))
27 {}
28
29 template<typename U>
30 explicit size_nd(const vector<U>& new_widths) : widths()
31 {
32 for(const U& new_width : new_widths)
33 {
34 widths.push_back(T(new_width));
35 }
36 }
37
39 {
40 for (const string& new_width : new_widths)
41 {
42 widths.push_back(T(new_width));
43 }
44 }
45
46 explicit size_nd(const string& init_size_nd) : size_nd()
47 {
49 }
50
52 {
53 widths = x.widths;
54 return *this;
55 }
56
58 {
59 widths = std::move(x.widths);
60 return *this;
61 }
62
64 {
66 return *this;
67 }
68
69 bool is_nd(int i) const
70 {
71 return widths.size() == i;
72 }
73
74 int get_nd() const
75 {
76 return widths.size();
77 }
78
79 template<typename U>
81 {
82 if (get_nd() == x.get_nd())
83 {
85 for (unsigned int i = 0; i < widths.size(); i++)
86 {
87 new_widths.push_back(widths[i]);
88 new_widths[i] += T(x.widths[i]);
89 }
90 return size_nd<T>(new_widths);
91 }
92 else
93 {
94 return size_nd<T>();
95 }
96 }
97
98 template<typename U>
100 {
101 if (get_nd() == x.get_nd())
102 {
104 for (unsigned int i = 0; i < widths.size(); i++)
105 {
106 new_widths.push_back(widths[i]);
107 new_widths[i] -= T(x.widths[i]);
108 }
109 return size_nd<T>(new_widths);
110 }
111 else
112 {
113 return size_nd<T>();
114 }
115 }
116
117 template<typename U>
118 void operator +=(const size_nd<U>& x)
119 {
120 if (get_nd() == x.get_nd())
121 {
122 for (unsigned int i = 0; i < widths.size(); i++)
123 {
124 widths[i] += T(x.widths[i]);
125 }
126 }
127 }
128
129 template<typename U>
130 void operator -=(const size_nd<U>& x)
131 {
132 if (get_nd() == x.get_nd())
133 {
134 for (unsigned int i = 0; i < widths.size(); i++)
135 {
136 widths[i] -= T(x.widths[i]);
137 }
138 }
139 }
140
142 {
143 vector<dimension> new_dimensions = create_dimensions(widths[0].get_dimensions()[0].get_symbol() + std::to_string(get_nd()));
144 float new_value = 1;
145 for (unsigned int i = 0; i < widths.size(); i++)
146 {
147 new_value *= widths[i].get_value();
148 }
150 }
151
152 string display() const
153 {
154 if (widths.size() > 0)
155 {
157 output << widths[0];
158 if (widths.size() > 1)
159 {
160 for (unsigned int i = 1; i < widths.size(); i++)
161 {
162 output << " * " << widths[i];
163 }
164 }
165 return output.str();
166 }
167 else
168 {
169 return "[empty]";
170 }
171 }
172
174
175 private:
177 {
178 widths.clear();
180 boost::split(new_widths,init_size_nd,boost::is_any_of("*"));
181 for (string& new_width : new_widths)
182 {
183 boost::trim(new_width);
184 widths.push_back(T(new_width));
185 }
186 }
187 };
188
189 template<>
191 {
192 public:
194 {}
195
197 {}
198
199 size_nd(size_nd<float>&& x) : widths(std::move(x.widths))
200 {}
201
204
206 {
207 for (const string& new_width : new_widths)
208 {
209 widths.push_back(stof(new_width));
210 }
211 }
212
213 explicit size_nd(const string& init_size_nd) : size_nd()
214 {
216 }
217
219 {
220 widths = x.widths;
221 return *this;
222 }
223
225 {
226 widths = std::move(x.widths);
227 return *this;
228 }
229
231 {
233 return *this;
234 }
235
236 bool is_nd(unsigned int i) const
237 {
238 return widths.size() == i;
239 }
240
241 int get_nd() const
242 {
243 return int(widths.size());
244 }
245
247 {
248 if (get_nd() == x.get_nd())
249 {
251 for (unsigned int i = 0; i < new_widths.size(); i++)
252 {
253 new_widths[i] += x.widths[i];
254 }
256 }
257 else
258 {
259 return size_nd<float>();
260 }
261 }
262
264 {
265 if (get_nd() == x.get_nd())
266 {
268 for (unsigned int i = 0; i < new_widths.size(); i++)
269 {
270 new_widths[i] -= x.widths[i];
271 }
273 }
274 else
275 {
276 return size_nd<float>();
277 }
278 }
279
281 {
282 if (get_nd() == x.get_nd())
283 {
284 for (unsigned int i = 0; i < widths.size(); i++)
285 {
286 widths[i] += x.widths[i];
287 }
288 }
289 }
290
292 {
293 if (get_nd() == x.get_nd())
294 {
295 for (unsigned int i = 0; i < widths.size(); i++)
296 {
297 widths[i] -= x.widths[i];
298 }
299 }
300 }
301
302 float get_volume_nd() const
303 {
304 float new_value = 1;
305 for (unsigned int i = 0; i < widths.size(); i++)
306 {
307 new_value *= widths[i];
308 }
309 return new_value;
310 }
311
312 string display() const
313 {
314 if (widths.size() > 0)
315 {
317 output << display_float(widths[0],2);
318 if (widths.size() > 1)
319 {
320 for (unsigned int i = 1; i < widths.size(); i++)
321 {
322 output << " * " << display_float(widths[i],2);
323 }
324 }
325 return output.str();
326 }
327 else
328 {
329 return "[empty]";
330 }
331 }
332
334
335 private:
337 {
338 widths.clear();
340 boost::split(new_widths,init_size_nd,boost::is_any_of("*"));
341 for (string& new_width : new_widths)
342 {
343 boost::trim(new_width);
344 widths.push_back(stof(new_width));
345 }
346 }
347 };
348
349 template<typename T>
350 string to_string(const size_nd<T>& x)
351 {
352 return x.display();
353 }
354
355 string to_string(const size_nd<float>& x);
356}
357
358template<typename T>
360{
361 if (x.get_nd() == y.get_nd())
362 {
363 for (int i = 0; i < x.widths.size(); i++)
364 {
365 if (x.widths[i] != y.widths[i])
366 {
367 return false;
368 }
369 }
370 return true;
371 }
372 else
373 {
374 return false;
375 }
376}
377
378template<typename T>
380{
381 return !(x == y);
382}
383
384template<typename T>
385bool operator ==(const scifir::size_nd<T>& x, const string& init_size_nd)
386{
387 scifir::size_nd<T> y(init_size_nd);
388 return (x == y);
389}
390
391template<typename T>
392bool operator !=(const scifir::size_nd<T>& x, const string& init_size_nd)
393{
394 return !(x == init_size_nd);
395}
396
397template<typename T>
398bool operator ==(const string& init_size_nd, const scifir::size_nd<T>& x)
399{
400 scifir::size_nd<T> y(init_size_nd);
401 return (x == y);
402}
403
404template<typename T>
405bool operator !=(const string& init_size_nd, const scifir::size_nd<T>& x)
406{
407 return !(init_size_nd == x);
408}
409
410template<typename T>
411void operator +=(string& x, const scifir::size_nd<T>& y)
412{
413 x += to_string(y);
414}
415
416template<typename T>
417string operator +(const string& x, const scifir::size_nd<T>& y)
418{
419 return x + to_string(y);
420}
421
422template<typename T>
423string operator +(const scifir::size_nd<T>& x, const string& y)
424{
425 return to_string(x) + y;
426}
427
428template<typename T>
429ostream& operator <<(ostream& os, const scifir::size_nd<T>& x)
430{
431 return os << to_string(x);
432}
433
434template<typename T>
435istream& operator >>(istream& is, scifir::size_nd<T>& x)
436{
437 char a[256];
438 is.getline(a, 256);
439 string b(a);
440 x = scifir::size_nd<T>(b);
441 return is;
442}
443
444#endif // SCIFIR_UNITS_SPECIAL_UNITS_SIZE_ND_HPP_INCLUDED
445
Class that allows to create scalar units, which are composed of a value (as a float) and dimensions....
size_nd(const vector< float > &new_widths)
Definition size_nd.hpp:202
size_nd(const vector< string > &new_widths)
Definition size_nd.hpp:205
size_nd(const string &init_size_nd)
Definition size_nd.hpp:213
vector< float > widths
Definition size_nd.hpp:333
void initialize_from_string(const string &init_size_nd)
Definition size_nd.hpp:336
bool is_nd(unsigned int i) const
Definition size_nd.hpp:236
string display() const
Definition size_nd.hpp:312
size_nd(size_nd< float > &&x)
Definition size_nd.hpp:199
float get_volume_nd() const
Definition size_nd.hpp:302
size_nd(const size_nd< float > &x)
Definition size_nd.hpp:196
size_nd(size_nd< T > &&x)
Definition size_nd.hpp:26
void operator-=(const size_nd< U > &x)
Definition size_nd.hpp:130
int get_nd() const
Definition size_nd.hpp:74
size_nd(const string &init_size_nd)
Definition size_nd.hpp:46
size_nd(const vector< string > &new_widths)
Definition size_nd.hpp:38
bool is_nd(int i) const
Definition size_nd.hpp:69
size_nd< T > operator-(const size_nd< U > &x) const
Definition size_nd.hpp:99
size_nd(const size_nd< T > &x)
Definition size_nd.hpp:23
size_nd< T > operator+(const size_nd< U > &x) const
Definition size_nd.hpp:80
string display() const
Definition size_nd.hpp:152
void operator+=(const size_nd< U > &x)
Definition size_nd.hpp:118
size_nd< T > & operator=(const size_nd< T > &x)
Definition size_nd.hpp:51
vector< T > widths
Definition size_nd.hpp:173
size_nd(const vector< U > &new_widths)
Definition size_nd.hpp:30
scalar_unit get_volume_nd() const
Definition size_nd.hpp:141
void initialize_from_string(const string &init_size_nd)
Definition size_nd.hpp:176
The namespace scifir contains all scifir-units, excepting the string literals, which are outside.
Definition address.cpp:6
string to_string(const aid &x)
Creates a string representation of aid, it's for aid equivalent to the display() function of aid.
Definition aid.cpp:582
string display_float(const float &value, int number_of_decimals)
Definition types.cpp:36
vector< dimension > create_dimensions(string init_dimensions)
Creates the dimensions from an initialization string of dimensions.
void operator+=(string &x, const scifir::size_nd< T > &y)
Definition size_nd.hpp:411
ostream & operator<<(ostream &os, const scifir::size_nd< T > &x)
Definition size_nd.hpp:429
istream & operator>>(istream &is, scifir::size_nd< T > &x)
Definition size_nd.hpp:435
string operator+(const string &x, const scifir::size_nd< T > &y)
Definition size_nd.hpp:417
bool operator==(const scifir::size_nd< T > &x, const scifir::size_nd< T > &y)
Definition size_nd.hpp:359
bool operator!=(const scifir::size_nd< T > &x, const scifir::size_nd< T > &y)
Definition size_nd.hpp:379