BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
LLData< T > Class Template Reference

Template class to store data of any type in multi-dimensional space (low-level). More...

Public Member Functions

 LLData (const LLData< T > &right)
 
 LLData (size_t rank, const int *dimensions)
 
 ~LLData ()
 
T & atCoordinate (int *coordinate)
 
const T & atCoordinate (int *coordinate) const
 
const int * dimensions () const
 
size_t getTotalSize () const
 
getTotalSum () const
 
LLData< double > meanValues () const
 
LLData< T > & operator*= (const LLData< T > &right)
 
LLData< T > & operator+= (const LLData< T > &right)
 
LLData< T > & operator-= (const LLData< T > &right)
 
LLData< T > & operator/= (const LLData< T > &right)
 
LLData< T > & operator= (const LLData< T > &right)
 
T & operator[] (size_t i)
 
const T & operator[] (size_t i) const
 
size_t rank () const
 
void scaleAll (const T &factor)
 
void setAll (const T &value)
 

Private Member Functions

void allocate (size_t rank, const int *dimensions)
 
bool checkDimensions (size_t rank, const int *dimensions) const
 
void clear ()
 
size_t convertCoordinate (int *coordinate) const
 
Eigen::Matrix2d getZeroElement () const
 
getZeroElement () const
 
Eigen::Matrix2d getZeroElement () const
 
void swapContents (LLData< T > &other)
 

Private Attributes

T * m_data_array
 
int * m_dims
 
size_t m_rank
 

Detailed Description

template<class T>
class LLData< T >

Template class to store data of any type in multi-dimensional space (low-level).

Definition at line 31 of file LLData.h.

Constructor & Destructor Documentation

◆ LLData() [1/2]

template<class T >
LLData< T >::LLData ( size_t  rank,
const int *  dimensions 
)
inline

Definition at line 90 of file LLData.h.

90  : m_rank(0), m_dims(0), m_data_array(0)
91 {
93 }
size_t rank() const
Definition: LLData.h:59
size_t m_rank
Definition: LLData.h:71
int * m_dims
Definition: LLData.h:72
T * m_data_array
Definition: LLData.h:73
const int * dimensions() const
Definition: LLData.h:60
void allocate(size_t rank, const int *dimensions)
Definition: LLData.h:212

References LLData< T >::allocate(), LLData< T >::dimensions(), and LLData< T >::rank().

Here is the call graph for this function:

◆ LLData() [2/2]

template<class T >
LLData< T >::LLData ( const LLData< T > &  right)

Definition at line 95 of file LLData.h.

95  : m_rank(0), m_dims(0), m_data_array(0)
96 {
97  allocate(right.rank(), right.dimensions());
98  for (size_t i = 0; i < getTotalSize(); ++i) {
99  m_data_array[i] = right[i];
100  }
101 }
size_t getTotalSize() const
Definition: LLData.h:201

References LLData< T >::allocate(), LLData< T >::dimensions(), LLData< T >::getTotalSize(), LLData< T >::m_data_array, and LLData< T >::rank().

Here is the call graph for this function:

◆ ~LLData()

template<class T >
LLData< T >::~LLData

Definition at line 103 of file LLData.h.

104 {
105  clear();
106 }
void clear()
Definition: LLData.h:228

Member Function Documentation

◆ allocate()

template<class T >
void LLData< T >::allocate ( size_t  rank,
const int *  dimensions 
)
private

Definition at line 212 of file LLData.h.

213 {
214  clear();
216  throw std::runtime_error("LLData<T>::allocate error: dimensions must be > 0");
217  }
218  m_rank = rank;
219  if (m_rank) {
220  m_dims = new int[m_rank];
221  std::copy(dimensions, dimensions + rank, m_dims);
222  m_data_array = new T[getTotalSize()];
223  } else {
224  m_data_array = new T[1];
225  }
226 }
bool checkDimensions(size_t rank, const int *dimensions) const
Definition: LLData.h:241

Referenced by LLData< T >::LLData().

◆ atCoordinate() [1/2]

template<class T >
T & LLData< T >::atCoordinate ( int *  coordinate)
inline

Definition at line 127 of file LLData.h.

128 {
129  return m_data_array[convertCoordinate(coordinate)];
130 }
size_t convertCoordinate(int *coordinate) const
Definition: LLData.h:247

◆ atCoordinate() [2/2]

template<class T >
const T & LLData< T >::atCoordinate ( int *  coordinate) const
inline

Definition at line 132 of file LLData.h.

133 {
134  return m_data_array[convertCoordinate(coordinate)];
135 }

◆ checkDimensions()

template<class T >
bool LLData< T >::checkDimensions ( size_t  rank,
const int *  dimensions 
) const
inlineprivate

Definition at line 241 of file LLData.h.

242 {
243  return std::all_of(dimensions, dimensions + rank,
244  [](const int& dim) -> bool { return dim > 0; });
245 }

◆ clear()

template<class T >
void LLData< T >::clear
private

Definition at line 228 of file LLData.h.

229 {
230  if (m_rank > 0) {
231  m_rank = 0;
232  delete[] m_data_array;
233  delete[] m_dims;
234  m_data_array = nullptr;
235  m_dims = nullptr;
236  } else {
237  delete[] m_data_array;
238  }
239 }

◆ convertCoordinate()

template<class T >
size_t LLData< T >::convertCoordinate ( int *  coordinate) const
inlineprivate

Definition at line 247 of file LLData.h.

248 {
249  size_t offset = 1;
250  size_t result = 0;
251  for (size_t i = m_rank; i > 0; --i) {
252  result += offset * coordinate[i - 1];
253  offset *= m_dims[i - 1];
254  }
255  return result;
256 }

◆ dimensions()

template<class T >
const int* LLData< T >::dimensions ( ) const
inline

Definition at line 60 of file LLData.h.

60 { return m_dims; }

References LLData< T >::m_dims.

Referenced by LLData< T >::LLData(), and HaveSameDimensions().

◆ getTotalSize()

template<class T >
size_t LLData< T >::getTotalSize
inline

Definition at line 201 of file LLData.h.

202 {
203  int result = std::accumulate(m_dims, m_dims + m_rank, 1, std::multiplies<int>{});
204  return static_cast<size_t>(result);
205 }

Referenced by LLData< T >::LLData().

◆ getTotalSum()

template<class T >
T LLData< T >::getTotalSum

Definition at line 207 of file LLData.h.

208 {
209  return std::accumulate(m_data_array, m_data_array + getTotalSize(), getZeroElement());
210 }
T getZeroElement() const
Definition: LLData.h:265

◆ getZeroElement() [1/3]

Eigen::Matrix2d LLData< Eigen::Matrix2d >::getZeroElement ( ) const
private

Definition at line 17 of file LLData.cpp.

18 {
19  Eigen::Matrix2d result = Eigen::Matrix2d::Zero();
20  return result;
21 }

◆ getZeroElement() [2/3]

template<class T >
T LLData< T >::getZeroElement
private

Definition at line 265 of file LLData.h.

266 {
267  T result = 0;
268  return result;
269 }

◆ getZeroElement() [3/3]

Eigen::Matrix2d LLData< Eigen::Matrix2d >::getZeroElement ( ) const
private

◆ meanValues()

template<class T >
LLData<double> LLData< T >::meanValues ( ) const

◆ operator*=()

template<class T >
LLData< T > & LLData< T >::operator*= ( const LLData< T > &  right)

Definition at line 159 of file LLData.h.

160 {
161  if (!HaveSameDimensions(*this, right))
162  throw std::runtime_error(
163  "Operation *= on LLData requires both operands to have the same dimensions");
164  for (size_t i = 0; i < getTotalSize(); ++i) {
165  m_data_array[i] *= right[i];
166  }
167  return *this;
168 }
bool HaveSameDimensions(const LLData< T > &left, const LLData< T > &right)
Definition: LLData.h:299

References HaveSameDimensions().

Here is the call graph for this function:

◆ operator+=()

template<class T >
LLData< T > & LLData< T >::operator+= ( const LLData< T > &  right)

Definition at line 137 of file LLData.h.

138 {
139  if (!HaveSameDimensions(*this, right))
140  throw std::runtime_error(
141  "Operation += on LLData requires both operands to have the same dimensions");
142  for (size_t i = 0; i < getTotalSize(); ++i) {
143  m_data_array[i] += right[i];
144  }
145  return *this;
146 }

References HaveSameDimensions().

Here is the call graph for this function:

◆ operator-=()

template<class T >
LLData< T > & LLData< T >::operator-= ( const LLData< T > &  right)

Definition at line 148 of file LLData.h.

149 {
150  if (!HaveSameDimensions(*this, right))
151  throw std::runtime_error(
152  "Operation -= on LLData requires both operands to have the same dimensions");
153  for (size_t i = 0; i < getTotalSize(); ++i) {
154  m_data_array[i] -= right[i];
155  }
156  return *this;
157 }

References HaveSameDimensions().

Here is the call graph for this function:

◆ operator/=()

template<class T >
LLData< T > & LLData< T >::operator/= ( const LLData< T > &  right)

Definition at line 170 of file LLData.h.

171 {
172  if (!HaveSameDimensions(*this, right))
173  throw std::runtime_error(
174  "Operation /= on LLData requires both operands to have the same dimensions");
175  for (size_t i = 0; i < getTotalSize(); ++i) {
176  double ratio;
177  if (std::abs(m_data_array[i] - right[i])
178  <= std::numeric_limits<double>::epsilon() * std::abs(right[i])) {
179  ratio = 1.0;
180  } else if (std::abs(right[i]) <= std::numeric_limits<double>::min()) {
181  ratio = double(m_data_array[i]) / std::numeric_limits<double>::min();
182  } else {
183  ratio = double(m_data_array[i] / right[i]);
184  }
185  m_data_array[i] = (T)ratio;
186  }
187  return *this;
188 }

References HaveSameDimensions().

Here is the call graph for this function:

◆ operator=()

template<class T >
LLData< T > & LLData< T >::operator= ( const LLData< T > &  right)

Definition at line 108 of file LLData.h.

109 {
110  if (this != &right) {
111  LLData<T> copy(right);
112  swapContents(copy);
113  }
114  return *this;
115 }
Template class to store data of any type in multi-dimensional space (low-level).
Definition: LLData.h:31
void swapContents(LLData< T > &other)
Definition: LLData.h:258
@ copy
full deep copying with item identifiers regenerated

◆ operator[]() [1/2]

template<class T >
T & LLData< T >::operator[] ( size_t  i)
inline

Definition at line 117 of file LLData.h.

118 {
119  return m_data_array[i];
120 }

◆ operator[]() [2/2]

template<class T >
const T & LLData< T >::operator[] ( size_t  i) const
inline

Definition at line 122 of file LLData.h.

123 {
124  return m_data_array[i];
125 }

◆ rank()

template<class T >
size_t LLData< T >::rank ( ) const
inline

Definition at line 59 of file LLData.h.

59 { return m_rank; }

References LLData< T >::m_rank.

Referenced by LLData< T >::LLData(), and HaveSameDimensions().

◆ scaleAll()

template<class T >
void LLData< T >::scaleAll ( const T &  factor)

Definition at line 195 of file LLData.h.

196 {
197  std::transform(m_data_array, m_data_array + getTotalSize(), m_data_array,
198  [&factor](const T& value) -> T { return value * factor; });
199 }

◆ setAll()

template<class T >
void LLData< T >::setAll ( const T &  value)

Definition at line 190 of file LLData.h.

191 {
192  std::fill(m_data_array, m_data_array + getTotalSize(), value);
193 }

◆ swapContents()

template<class T >
void LLData< T >::swapContents ( LLData< T > &  other)
private

Definition at line 258 of file LLData.h.

259 {
260  std::swap(this->m_rank, other.m_rank);
261  std::swap(this->m_dims, other.m_dims);
262  std::swap(this->m_data_array, other.m_data_array);
263 }
void swap(OutputDataIterator< TValue, TContainer > &left, OutputDataIterator< TValue, TContainer > &right)
make Swappable

References LLData< T >::m_data_array, LLData< T >::m_dims, LLData< T >::m_rank, and swap().

Here is the call graph for this function:

Member Data Documentation

◆ m_data_array

template<class T >
T* LLData< T >::m_data_array
private

Definition at line 73 of file LLData.h.

Referenced by LLData< T >::LLData(), and LLData< T >::swapContents().

◆ m_dims

template<class T >
int* LLData< T >::m_dims
private

Definition at line 72 of file LLData.h.

Referenced by LLData< T >::dimensions(), and LLData< T >::swapContents().

◆ m_rank

template<class T >
size_t LLData< T >::m_rank
private

Definition at line 71 of file LLData.h.

Referenced by LLData< T >::rank(), and LLData< T >::swapContents().


The documentation for this class was generated from the following file: