BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
LLData< T > Class Template Reference
Collaboration diagram for LLData< T >:

Public Member Functions

 LLData (size_t rank, const int *dimensions)
 
 LLData (const LLData< T > &right)
 
LLData< T > & operator= (const LLData< T > &right)
 
 ~LLData ()
 
LLData< double > meanValues () const
 
T & operator[] (size_t i)
 
const T & operator[] (size_t i) const
 
T & atCoordinate (int *coordinate)
 
const T & atCoordinate (int *coordinate) 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)
 
void setAll (const T &value)
 
void scaleAll (const T &factor)
 
size_t getTotalSize () const
 
size_t getRank () const
 
const int * getDimensions () const
 
getTotalSum () const
 

Private Member Functions

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

Private Attributes

size_t m_rank
 
int * m_dims
 
T * m_data_array
 

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 27 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 87 of file LLData.h.

87  : m_rank(0), m_dims(0), m_data_array(0)
88 {
89  allocate(rank, dimensions);
90 }
size_t m_rank
Definition: LLData.h:68
int * m_dims
Definition: LLData.h:69
T * m_data_array
Definition: LLData.h:70
void allocate(size_t rank, const int *dimensions)
Definition: LLData.h:209

References LLData< T >::allocate().

Here is the call graph for this function:

◆ LLData() [2/2]

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

Definition at line 92 of file LLData.h.

92  : m_rank(0), m_dims(0), m_data_array(0)
93 {
94  allocate(right.getRank(), right.getDimensions());
95  for (size_t i = 0; i < getTotalSize(); ++i) {
96  m_data_array[i] = right[i];
97  }
98 }
size_t getRank() const
Definition: LLData.h:56
size_t getTotalSize() const
Definition: LLData.h:198
const int * getDimensions() const
Definition: LLData.h:57

References LLData< T >::allocate(), LLData< T >::getDimensions(), LLData< T >::getRank(), LLData< T >::getTotalSize(), and LLData< T >::m_data_array.

Here is the call graph for this function:

◆ ~LLData()

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

Definition at line 100 of file LLData.h.

101 {
102  clear();
103 }
void clear()
Definition: LLData.h:225

Member Function Documentation

◆ operator=()

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

Definition at line 105 of file LLData.h.

106 {
107  if (this != &right) {
108  LLData<T> copy(right);
109  swapContents(copy);
110  }
111  return *this;
112 }
Template class to store data of any type in multi-dimensional space (low-level).
Definition: LLData.h:28
void swapContents(LLData< T > &other)
Definition: LLData.h:255

◆ meanValues()

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

◆ operator[]() [1/2]

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

Definition at line 114 of file LLData.h.

115 {
116  return m_data_array[i];
117 }

◆ operator[]() [2/2]

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

Definition at line 119 of file LLData.h.

120 {
121  return m_data_array[i];
122 }

◆ atCoordinate() [1/2]

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

Definition at line 124 of file LLData.h.

125 {
126  return m_data_array[convertCoordinate(coordinate)];
127 }
size_t convertCoordinate(int *coordinate) const
Definition: LLData.h:244

◆ atCoordinate() [2/2]

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

Definition at line 129 of file LLData.h.

130 {
131  return m_data_array[convertCoordinate(coordinate)];
132 }

◆ operator+=()

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

Definition at line 134 of file LLData.h.

135 {
136  if (!HaveSameDimensions(*this, right))
138  "Operation += on LLData requires both operands to have the same dimensions");
139  for (size_t i = 0; i < getTotalSize(); ++i) {
140  m_data_array[i] += right[i];
141  }
142  return *this;
143 }
bool HaveSameDimensions(const LLData< T > &left, const LLData< T > &right)
Definition: LLData.h:296

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 145 of file LLData.h.

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

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 156 of file LLData.h.

157 {
158  if (!HaveSameDimensions(*this, right))
160  "Operation *= on LLData requires both operands to have the same dimensions");
161  for (size_t i = 0; i < getTotalSize(); ++i) {
162  m_data_array[i] *= right[i];
163  }
164  return *this;
165 }

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 167 of file LLData.h.

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

References HaveSameDimensions().

Here is the call graph for this function:

◆ setAll()

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

Definition at line 187 of file LLData.h.

188 {
189  std::fill(m_data_array, m_data_array + getTotalSize(), value);
190 }

◆ scaleAll()

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

Definition at line 192 of file LLData.h.

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

◆ getTotalSize()

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

Definition at line 198 of file LLData.h.

199 {
200  int result = std::accumulate(m_dims, m_dims + m_rank, 1, std::multiplies<int>{});
201  return static_cast<size_t>(result);
202 }

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

◆ getRank()

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

Definition at line 56 of file LLData.h.

56 { return m_rank; }

References LLData< T >::m_rank.

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

◆ getDimensions()

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

Definition at line 57 of file LLData.h.

57 { return m_dims; }

References LLData< T >::m_dims.

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

◆ getTotalSum()

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

Definition at line 204 of file LLData.h.

205 {
206  return std::accumulate(m_data_array, m_data_array + getTotalSize(), getZeroElement());
207 }
T getZeroElement() const
Definition: LLData.h:262

◆ allocate()

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

Definition at line 209 of file LLData.h.

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

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

◆ clear()

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

Definition at line 225 of file LLData.h.

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

◆ checkDimensions()

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

Definition at line 238 of file LLData.h.

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

◆ convertCoordinate()

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

Definition at line 244 of file LLData.h.

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

◆ swapContents()

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

Definition at line 255 of file LLData.h.

256 {
257  std::swap(this->m_rank, other.m_rank);
258  std::swap(this->m_dims, other.m_dims);
259  std::swap(this->m_data_array, other.m_data_array);
260 }
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:

◆ getZeroElement() [1/3]

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

Definition at line 262 of file LLData.h.

263 {
264  T result = 0;
265  return result;
266 }

◆ getZeroElement() [2/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() [3/3]

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

Member Data Documentation

◆ m_rank

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

Definition at line 68 of file LLData.h.

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

◆ m_dims

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

Definition at line 69 of file LLData.h.

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

◆ m_data_array

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

Definition at line 70 of file LLData.h.

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


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