DUDS
Distributed Update of Data from Something
QuantityArray.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the DUDS project. It is subject to the BSD-style
3  * license terms in the LICENSE file found in the top-level directory of this
4  * distribution and at https://github.com/jjackowski/duds/blob/master/LICENSE.
5  * No part of DUDS, including this file, may be copied, modified, propagated,
6  * or distributed except according to the terms contained in the LICENSE file.
7  *
8  * Copyright (C) 2017 Jeff Jackowski
9  */
10 #ifndef QUANTITYARRAY_HPP
11 #define QUANTITYARRAY_HPP
12 
13 #include <duds/data/Quantity.hpp>
15 #include <array>
16 
17 namespace duds { namespace data {
18 
19 struct QuantityNddArray;
20 
32 template <class I>
37  I iter;
43 public:
44  // iterator traits
46  typedef typename std::iterator_traits<I>::pointer pointer;
47  typedef typename std::iterator_traits<I>::reference reference;
48  typedef typename std::iterator_traits<I>::iterator_category iterator_category;
49  typedef void difference_type;
53  QuantityIterator() = default;
59  QuantityIterator(const I &i, Unit u) : iter(i), arrayUnit(u) { }
61  ++iter;
62  return *this;
63  }
65  QuantityIterator i(iter);
66  ++iter;
67  return i;
68  }
70  --iter;
71  return *this;
72  }
74  QuantityIterator i(iter);
75  --iter;
76  return i;
77  }
78  bool operator == (const QuantityIterator &it) const {
79  return iter == it.iter;
80  }
81  bool operator != (const QuantityIterator &it) const {
82  return iter != it.iter;
83  }
89  return Quantity(*iter, arrayUnit);
90  }
96  auto value() const {
97  return *iter;
98  }
103  Unit unit() const {
104  return arrayUnit;
105  }
106 };
107 
119 template <std::size_t L>
124  typedef std::array<double, L> Array;
128  Array array;
141  iterator begin() {
142  return iterator(array.begin(), unit);
143  }
144  const_iterator begin() const {
145  return const_iterator(array.begin(), unit);
146  }
147  const_iterator cbegin() const {
148  return const_iterator(array.cbegin(), unit);
149  }
150  iterator end() {
151  return iterator(array.end(), unit);
152  }
153  const_iterator end() const {
154  return const_iterator(array.end(), unit);
155  }
156  const_iterator cend() const {
157  return const_iterator(array.cend(), unit);
158  }
165  Quantity get(std::size_t pos) const {
166  return Quantity(array.at(pos), unit);
167  }
179  void set(std::size_t pos, const Quantity &q) {
180  if (unit != q.unit) {
182  }
183  array.at(pos) = q.value;
184  }
188  QuantityArray &operator=(const QuantityArray &a) = default;
192  template <std::size_t N>
194  unit = a.unit;
195  std::size_t loop = std::min(L, N);
196  typename Array::iterator dest = array.begin();
197  typename QuantityArray<N>::Array::const_iterator src = a.array.begin();
198  for (; loop; ++dest, ++src, --loop) {
199  *dest = *src;
200  }
201  return *this;
202  }
210  QuantityArray &operator=(const QuantityNddArray &a); // defined near EOF
211 };
212 
218 struct QuantityXyz : public QuantityArray<3> {
222  double &x() {
223  return array[0];
224  }
228  const double &x() const {
229  return array[0];
230  }
234  double &y() {
235  return array[1];
236  }
240  const double &y() const {
241  return array[1];
242  }
246  double &z() {
247  return array[2];
248  }
252  const double &z() const {
253  return array[2];
254  }
258  Quantity xQ() const {
259  return get(0);
260  }
264  Quantity yQ() const {
265  return get(1);
266  }
270  Quantity zQ() const {
271  return get(2);
272  }
273 };
274 
292  Array array;
301  QuantityNddArray() = default;
305  QuantityNddArray(const QuantityNddArray &) = default;
310  array(std::move(q.array)), unit(q.unit) { }
314  QuantityNddArray &operator=(const QuantityNddArray &a) = default;
318  QuantityNddArray &operator=(QuantityNddArray &&q) = default;
325  template <std::size_t N>
327  array.copyFrom(q.array);
328  unit = q.unit;
329  return *this;
330  }
334  void clear() noexcept {
335  array.clear();
336  unit.clear();
337  }
341  bool empty() const {
342  return array.empty();
343  }
352  iterator begin() {
353  return iterator(array.begin(), unit);
354  }
355  const_iterator begin() const {
356  return const_iterator(array.begin(), unit);
357  }
358  const_iterator cbegin() const {
359  return const_iterator(array.cbegin(), unit);
360  }
361  iterator end() {
362  return iterator(array.end(), unit);
363  }
364  const_iterator end() const {
365  return const_iterator(array.end(), unit);
366  }
367  const_iterator cend() const {
368  return const_iterator(array.cend(), unit);
369  }
379  template <class Dim>
380  Quantity get(const Dim &pos) const {
381  return Quantity(array.at<Dim>(pos), unit);
382  }
389  Quantity get(const Array::DimList &pos) const {
390  return Quantity(array.at<Array::DimList>(pos), unit);
391  }
406  template <class Dim>
407  void set(const Dim &pos, const Quantity &q) {
408  if (unit != q.unit) {
410  }
411  array.at<Dim>(pos) = q.value;
412  }
425  void set(const Array::DimList &pos, const Quantity &q) {
426  if (unit != q.unit) {
428  }
429  array.at<Array::DimList>(pos) = q.value;
430  }
431 
432 private:
433  // serialization support
434  friend class boost::serialization::access;
435  template <class A>
436  void serialize(A &a, const unsigned int) {
437  a & BOOST_SERIALIZATION_NVP(array);
438  a & BOOST_SERIALIZATION_NVP(unit);
439  }
440 };
441 
442 // documented above with the QuantityArray class
443 template <std::size_t L>
445  unit = a.unit;
446  a.array.copyTo(array);
447  return *this;
448 }
449 
450 
451 } }
452 
453 #endif // #ifndef QUANTITYARRAY_HPP
Unit unit
The units of all values in the array.
Quantity operator*() const
Returns a new Quantity object.
auto value() const
Returns a reference to the value stored in the container.
A container for a value and a unit to better describe the value.
Definition: Quantity.hpp:37
Array array
The array of quantity values.
void clear()
Makes the Unit unitless.
Definition: Unit.hpp:593
QuantityNddArray(QuantityNddArray &&q) noexcept
Move constructor; the array of quantities is moved rather than copied.
const double & x() const
Returns the scalar value for the X-axis.
double & y()
Returns the scalar value for the Y-axis.
std::iterator_traits< I >::reference reference
An iterator template for QuantityArray and QuantityNddArray that provides a Quantity object when dere...
duds::general::NddArray< double > Array
The type of the array holding quantity values.
std::array< double, L > Array
The type of the array holding quantity values.
An array of quantites of dynamic size and number of dimensions.
const_iterator cend() const
double & z()
Returns the scalar value for the Z-axis.
A fixed size array of quantities all sharing the same units.
void clear() noexcept
Destroys the contents of the array.
Definition: NddArray.hpp:934
Quantity xQ() const
Returns the Quantity for the X-axis.
Indicates that two different Unit objects were used in an operation that requires identical Unit obje...
Definition: Unit.hpp:51
const_iterator cbegin() const
move_impl move(unsigned int c, unsigned int r)
Display stream manipulator that moves the display cursor to the given location.
void copyTo(std::array< T, N > &a) const
Copies the contents of this object into a std::array.
Definition: NddArray.hpp:510
QuantityIterator(const I &i, Unit u)
Construct a new iterator with the given units.
QuantityArray & operator=(const QuantityArray &a)=default
Copies one QuantityArray into another for an exact match.
bool operator==(const QuantityIterator &it) const
std::initializer_list< SizeType > DimList
A type that can specify the dimensions of the array, or a position of an element. ...
Definition: NddArray.hpp:101
QuantityIterator< typename Array::const_iterator > const_iterator
A const iterator type that will yield Quantity objects when dereferenced.
const_iterator begin() const
QuantityIterator< typename Array::iterator > iterator
An iterator type that will yield Quantity objects when dereferenced.
std::iterator_traits< I >::iterator_category iterator_category
bool empty() const
True if the array has zero dimensions.
Definition: NddArray.hpp:923
const_iterator cend() const
QuantityIterator< typename Array::iterator > iterator
An iterator type that will yield Quantity objects when dereferenced.
double & x()
Returns the scalar value for the X-axis.
T & at(const Dim &pos)
Obtain an element from the array stored at a specific position.
Definition: NddArray.hpp:679
const T * cend() const
An iterator (really just a pointer) to one past the last element of the array.
Definition: NddArray.hpp:846
void copyFrom(const AV &av)
Copies from a one dimensional container into this array.
Definition: NddArray.hpp:340
Quantity zQ() const
Returns the Quantity for the Z-axis.
Unit unit
The units of all values in the array.
Unit arrayUnit
The units used for all Quantities in the array.
QuantityArray & copy(const QuantityArray< N > &a)
Copies one QuantityArray into another; sizes do not need to match.
Represents an SI unit, either base or derived.
Definition: Unit.hpp:178
Array array
The array of quantity values.
void serialize(A &a, const unsigned int)
T * begin()
An iterator (really just a pointer) to the first element of the array.
Definition: NddArray.hpp:783
const_iterator end() const
const_iterator begin() const
QuantityIterator & operator--()
const_iterator cbegin() const
QuantityIterator< typename Array::const_iterator > const_iterator
A const iterator type that will yield Quantity objects when dereferenced.
bool operator!=(const QuantityIterator &it) const
A QuantityArray for the common usage of a three dimentional coordinate or a triple axis sample...
QuantityNddArray & operator=(const QuantityArray< N > &q)
Copies the contents of a QuantityArray into this object.
QuantityIterator & operator++()
Quantity yQ() const
Returns the Quantity for the Y-axis.
std::iterator_traits< I >::pointer pointer
const double & y() const
Returns the scalar value for the Y-axis.
const double & z() const
Returns the scalar value for the Z-axis.
Unit unit() const
Returns the units of all quantities stored in the container.
void clear() noexcept
Clears the array and units.
I iter
The iterator wrapped by this object.
T * end()
An iterator (really just a pointer) to one past the last element of the array.
Definition: NddArray.hpp:820
QuantityIterator()=default
Make an iterator to nowhere.
const_iterator end() const
#define DUDS_THROW_EXCEPTION(x)
Works like BOOST_THROW_EXCEPTION, but includes a stack trace if DUDS_ERRORS_VERBOSE is defined...
Definition: Errors.hpp:48
const T * cbegin() const
An iterator (really just a pointer) to the first element of the array.
Definition: NddArray.hpp:807
bool empty() const
True if the array is empty; units are immateral.