22 #include <ns3/assert.h>
23 #include <ns3/simple-ref-count.h>
92 ValArray<T>(uint16_t numRows, uint16_t numCols = 1, uint16_t numPages = 1);
98 explicit ValArray<T>(
const std::valarray<T>& values);
110 explicit ValArray<T>(
const std::vector<T>& values);
118 ValArray<T>(uint16_t numRows, uint16_t numCols,
const std::valarray<T>& values);
126 ValArray<T>(uint16_t numRows, uint16_t numCols, std::valarray<T>&& values);
139 const std::valarray<T>& values);
149 ValArray<T>(uint16_t numRows, uint16_t numCols, uint16_t numPages, std::valarray<T>&& values);
191 T&
operator()(uint16_t rowIndex, uint16_t colIndex, uint16_t pageIndex);
199 const T&
operator()(uint16_t rowIndex, uint16_t colIndex, uint16_t pageIndex)
const;
212 T&
operator()(uint16_t rowIndex, uint16_t colIndex);
221 const T&
operator()(uint16_t rowIndex, uint16_t colIndex)
const;
312 const T*
GetPagePtr(uint16_t pageIndex)
const;
349 const std::valarray<T>&
GetValues()
const;
357 T&
Elem(
size_t row,
size_t col,
size_t page);
365 const T&
Elem(
size_t row,
size_t col,
size_t page)
const;
405 return m_values.size();
412 NS_ASSERT_MSG(rowIndex < m_numRows,
"Row index out of bounds");
413 NS_ASSERT_MSG(colIndex < m_numCols,
"Column index out of bounds");
414 NS_ASSERT_MSG(pageIndex < m_numPages,
"Pages index out of bounds");
415 size_t index = (rowIndex + m_numRows * (colIndex + m_numCols * pageIndex));
416 return m_values[index];
423 NS_ASSERT_MSG(rowIndex < m_numRows,
"Row index out of bounds");
424 NS_ASSERT_MSG(colIndex < m_numCols,
"Column index out of bounds");
425 NS_ASSERT_MSG(pageIndex < m_numPages,
"Pages index out of bounds");
426 size_t index = (rowIndex + m_numRows * (colIndex + m_numCols * pageIndex));
427 return m_values[index];
434 NS_ASSERT_MSG(m_numPages == 1,
"Cannot use 2D access operator for 3D ValArray.");
435 return (*
this)(rowIndex, colIndex, 0);
442 NS_ASSERT_MSG(m_numPages == 1,
"Cannot use 2D access operator for 3D ValArray.");
443 return (*
this)(rowIndex, colIndex, 0);
451 "Invalid index to 1D ValArray. The size of the array should be set through "
453 NS_ASSERT_MSG(((m_numRows == 1 || m_numCols == 1) && (m_numPages == 1)) ||
454 (m_numRows == 1 && m_numCols == 1),
455 "Access operator allowed only for 1D ValArray.");
456 return m_values[index];
464 "Invalid index to 1D ValArray.The size of the array should be set through "
466 NS_ASSERT_MSG(((m_numRows == 1 || m_numCols == 1) && (m_numPages == 1)) ||
467 (m_numRows == 1 && m_numCols == 1),
468 "Access operator allowed only for 1D ValArray.");
469 return m_values[index];
479 m_values * std::valarray<T>(rhs, m_numRows * m_numCols * m_numPages));
486 AssertEqualDims(rhs);
494 AssertEqualDims(rhs);
502 return ValArray<T>(m_numRows, m_numCols, m_numPages, -m_values);
509 AssertEqualDims(rhs);
518 AssertEqualDims(rhs);
527 NS_ASSERT_MSG(pageIndex < m_numPages,
"Invalid page index.");
528 return &(m_values[m_numRows * m_numCols * pageIndex]);
535 NS_ASSERT_MSG(pageIndex < m_numPages,
"Invalid page index.");
536 return &(m_values[m_numRows * m_numCols * pageIndex]);
551 return (*
this)(index);
558 return (*
this)(index);
562 inline const std::valarray<T>&
572 return (*
this)(row, col,
page);
579 return (*
this)(row, col,
page);
588 : m_numRows{numRows},
597 : m_numRows{(uint16_t)values.size()},
606 : m_numRows{(uint16_t)values.size()},
609 m_values{std::move(values)}
615 : m_numRows{(uint16_t)values.size()},
620 std::copy(values.begin(), values.end(), std::begin(
m_values));
625 : m_numRows{numRows},
631 "Dimensions and the initialization array size do not match.");
636 : m_numRows{numRows},
641 "Dimensions and the initialization array size do not match.");
649 const std::valarray<T>& values)
650 : m_numRows{numRows},
652 m_numPages{numPages},
656 "Dimensions and the initialization array size do not match.");
663 std::valarray<T>&& values)
664 : m_numRows{numRows},
669 "Dimensions and the initialization array size do not match.");
677 return EqualDims(rhs) &&
678 std::equal(std::begin(m_values), std::end(m_values), std::begin(rhs.
m_values));
685 return !((*this) == rhs);
692 return EqualDims(rhs) && std::equal(std::begin(m_values),
695 [tol](T lhsValue, T rhsValue) {
696 return lhsValue == rhsValue ||
697 std::abs(lhsValue - rhsValue) <= std::abs(tol);
706 "Dimensions mismatch: "
707 "lhs (rows, cols, pages) = ("
708 << m_numRows <<
", " << m_numCols <<
", " << m_numPages
710 "rhs (rows, cols, pages) = ("
728 os <<
"Page " << p <<
":\n";
733 os <<
"\t" << a(i, j, p);
A template-based reference counting class.
ValArray is a class to efficiently store 3D array.
uint16_t m_numRows
The size of the first dimension, i.e., the number of rows of each 2D array.
void AssertEqualDims(const ValArray< T > &rhs) const
Function that asserts if the dimensions of lhs and rhs ValArray are not equal and prints a message wi...
T * GetPagePtr(uint16_t pageIndex)
Get a data pointer to a specific 2D array for use in linear algebra libraries.
ValArray< T > & operator=(ValArray< T > &&)=default
Move assignment operator.
uint16_t GetNumRows() const
ValArray< T > & operator=(const ValArray< T > &)=default
Copy assignment operator.
uint16_t GetNumCols() const
T & Elem(size_t row, size_t col, size_t page)
Alternative access operator to access a specific element.
ValArray< T > operator+(const ValArray< T > &rhs) const
operator+ definition for ValArray<T>.
const std::valarray< T > & GetValues() const
Returns underlying values.
ValArray< T > & operator+=(const ValArray< T > &rhs)
operator+= definition for ValArray<T>.
bool IsAlmostEqual(const ValArray< T > &rhs, T tol) const
Compare Valarray up to a given absolute tolerance.
bool operator!=(const ValArray< T > &rhs) const
operator!= definition for ValArray<T>.
T & operator[](size_t index)
Single-element access operator[] that can be used to access a specific element of 1D ValArray.
T & operator()(uint16_t rowIndex, uint16_t colIndex, uint16_t pageIndex)
Access operator, with bound-checking in debug profile.
bool operator==(const ValArray< T > &rhs) const
operator== definition for ValArray<T>.
uint16_t m_numPages
The size of the third dimension, i.e., the number of 2D arrays.
std::valarray< T > m_values
The data values.
ValArray< T > & operator-=(const ValArray< T > &rhs)
operator-= definition for ValArray<T>.
uint16_t GetNumPages() const
uint16_t m_numCols
The size of the second dimension, i.e., the number of columns of each 2D array.
bool EqualDims(const ValArray< T > &rhs) const
Checks whether rhs and lhs ValArray objects have the same dimensions.
ValArray< T > operator*(const T &rhs) const
Element-wise multiplication with a scalar value.
ValArray< T > operator-() const
unary operator- definition for ValArray<T>.
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
std::string page
start a separate page
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::ostream & operator<<(std::ostream &os, const Angles &a)