tesseract  4.0.0-1-g2a2b
GENERIC_2D_ARRAY< T > Class Template Reference

#include <intsimdmatrix.h>

Inheritance diagram for GENERIC_2D_ARRAY< T >:
BandTriMatrix< T >

Public Member Functions

 GENERIC_2D_ARRAY (int dim1, int dim2, const T &empty, T *array)
 
 GENERIC_2D_ARRAY (int dim1, int dim2, const T &empty)
 
 GENERIC_2D_ARRAY ()
 
 GENERIC_2D_ARRAY (const GENERIC_2D_ARRAY< T > &src)
 
virtual ~GENERIC_2D_ARRAY ()
 
void operator= (const GENERIC_2D_ARRAY< T > &src)
 
void ResizeNoInit (int size1, int size2, int pad=0)
 
void Resize (int size1, int size2, const T &empty)
 
void ResizeWithCopy (int size1, int size2)
 
void Clear ()
 
bool Serialize (FILE *fp) const
 
bool Serialize (tesseract::TFile *fp) const
 
bool DeSerialize (bool swap, FILE *fp)
 
bool DeSerialize (tesseract::TFile *fp)
 
bool SerializeClasses (FILE *fp) const
 
bool DeSerializeClasses (bool swap, FILE *fp)
 
int dim1 () const
 
int dim2 () const
 
virtual int num_elements () const
 
virtual int index (int column, int row) const
 
void put (ICOORD pos, const T &thing)
 
void put (int column, int row, const T &thing)
 
get (ICOORD pos) const
 
get (int column, int row) const
 
const T & operator() (int column, int row) const
 
T & operator() (int column, int row)
 
T * operator[] (int column)
 
const T * operator[] (int column) const
 
void operator+= (const GENERIC_2D_ARRAY< T > &addend)
 
void operator-= (const GENERIC_2D_ARRAY< T > &minuend)
 
void operator+= (const T &addend)
 
void operator*= (const T &factor)
 
void Clip (const T &rangemin, const T &rangemax)
 
bool WithinBounds (const T &rangemin, const T &rangemax) const
 
double Normalize ()
 
Max () const
 
MaxAbs () const
 
void SumSquares (const GENERIC_2D_ARRAY< T > &src, const T &decay_factor)
 
void AdamUpdate (const GENERIC_2D_ARRAY< T > &sum, const GENERIC_2D_ARRAY< T > &sqsum, const T &epsilon)
 
void AssertFinite () const
 
void RotatingTranspose (const int *dims, int num_dims, int src_dim, int dest_dim, GENERIC_2D_ARRAY< T > *result) const
 
void delete_matrix_pointers ()
 

Protected Member Functions

bool SerializeSize (FILE *fp) const
 
bool SerializeSize (tesseract::TFile *fp) const
 
bool DeSerializeSize (bool swap, FILE *fp)
 
bool DeSerializeSize (tesseract::TFile *fp)
 

Protected Attributes

T * array_
 
empty_
 
int dim1_
 
int dim2_
 
int size_allocated_
 

Detailed Description

template<class T>
class GENERIC_2D_ARRAY< T >

Definition at line 25 of file intsimdmatrix.h.

Constructor & Destructor Documentation

◆ GENERIC_2D_ARRAY() [1/4]

template<class T>
GENERIC_2D_ARRAY< T >::GENERIC_2D_ARRAY ( int  dim1,
int  dim2,
const T &  empty,
T *  array 
)
inline

Definition at line 56 of file matrix.h.

57  : empty_(empty), dim1_(dim1), dim2_(dim2), array_(array) {
59  }
int size_allocated_
Definition: matrix.h:512
int dim1() const
Definition: matrix.h:206
int dim2() const
Definition: matrix.h:207

◆ GENERIC_2D_ARRAY() [2/4]

template<class T>
GENERIC_2D_ARRAY< T >::GENERIC_2D_ARRAY ( int  dim1,
int  dim2,
const T &  empty 
)
inline

Definition at line 62 of file matrix.h.

63  : empty_(empty), dim1_(dim1), dim2_(dim2) {
64  int new_size = dim1 * dim2;
65  array_ = new T[new_size];
66  size_allocated_ = new_size;
67  for (int i = 0; i < size_allocated_; ++i)
68  array_[i] = empty_;
69  }
int size_allocated_
Definition: matrix.h:512
int dim1() const
Definition: matrix.h:206
int dim2() const
Definition: matrix.h:207

◆ GENERIC_2D_ARRAY() [3/4]

template<class T>
GENERIC_2D_ARRAY< T >::GENERIC_2D_ARRAY ( )
inline

Definition at line 71 of file matrix.h.

72  : array_(nullptr), empty_(static_cast<T>(0)), dim1_(0), dim2_(0),
73  size_allocated_(0) {
74  }
int size_allocated_
Definition: matrix.h:512

◆ GENERIC_2D_ARRAY() [4/4]

template<class T>
GENERIC_2D_ARRAY< T >::GENERIC_2D_ARRAY ( const GENERIC_2D_ARRAY< T > &  src)
inline

Definition at line 75 of file matrix.h.

76  : array_(nullptr), empty_(static_cast<T>(0)), dim1_(0), dim2_(0),
77  size_allocated_(0) {
78  *this = src;
79  }
int size_allocated_
Definition: matrix.h:512

◆ ~GENERIC_2D_ARRAY()

template<class T>
virtual GENERIC_2D_ARRAY< T >::~GENERIC_2D_ARRAY ( )
inlinevirtual

Definition at line 80 of file matrix.h.

80 { delete[] array_; }

Member Function Documentation

◆ AdamUpdate()

template<class T>
void GENERIC_2D_ARRAY< T >::AdamUpdate ( const GENERIC_2D_ARRAY< T > &  sum,
const GENERIC_2D_ARRAY< T > &  sqsum,
const T &  epsilon 
)
inline

Definition at line 379 of file matrix.h.

380  {
381  int size = num_elements();
382  for (int i = 0; i < size; ++i) {
383  array_[i] += sum.array_[i] / (sqrt(sqsum.array_[i]) + epsilon);
384  }
385  }
virtual int num_elements() const
Definition: matrix.h:210

◆ AssertFinite()

template<class T>
void GENERIC_2D_ARRAY< T >::AssertFinite ( ) const
inline

Definition at line 387 of file matrix.h.

387  {
388  int size = num_elements();
389  for (int i = 0; i < size; ++i) {
390  ASSERT_HOST(isfinite(array_[i]));
391  }
392  }
virtual int num_elements() const
Definition: matrix.h:210
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ Clear()

template<class T>
void GENERIC_2D_ARRAY< T >::Clear ( )
inline

Definition at line 136 of file matrix.h.

136  {
137  int total_size = num_elements();
138  for (int i = 0; i < total_size; ++i)
139  array_[i] = empty_;
140  }
virtual int num_elements() const
Definition: matrix.h:210

◆ Clip()

template<class T>
void GENERIC_2D_ARRAY< T >::Clip ( const T &  rangemin,
const T &  rangemax 
)
inline

Definition at line 297 of file matrix.h.

297  {
298  int size = num_elements();
299  for (int i = 0; i < size; ++i) {
300  array_[i] = ClipToRange(array_[i], rangemin, rangemax);
301  }
302  }
virtual int num_elements() const
Definition: matrix.h:210
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:111

◆ delete_matrix_pointers()

template<class T>
void GENERIC_2D_ARRAY< T >::delete_matrix_pointers ( )
inline

Definition at line 455 of file matrix.h.

455  {
456  int size = num_elements();
457  for (int i = 0; i < size; ++i) {
458  T matrix_cell = array_[i];
459  if (matrix_cell != empty_)
460  delete matrix_cell;
461  }
462  }
virtual int num_elements() const
Definition: matrix.h:210

◆ DeSerialize() [1/2]

template<class T>
bool GENERIC_2D_ARRAY< T >::DeSerialize ( bool  swap,
FILE *  fp 
)
inline

Definition at line 161 of file matrix.h.

161  {
162  if (!DeSerializeSize(swap, fp)) return false;
163  if (!tesseract::DeSerialize(fp, &empty_)) return false;
164  if (swap) ReverseN(&empty_, sizeof(empty_));
165  int size = num_elements();
166  if (!tesseract::DeSerialize(fp, &array_[0], size)) return false;
167  if (swap) {
168  for (int i = 0; i < size; ++i)
169  ReverseN(&array_[i], sizeof(array_[i]));
170  }
171  return true;
172  }
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:178
virtual int num_elements() const
Definition: matrix.h:210
bool DeSerializeSize(bool swap, FILE *fp)
Definition: matrix.h:480
bool DeSerialize(FILE *fp, char *data, size_t n)
Definition: serialis.cpp:27

◆ DeSerialize() [2/2]

template<class T>
bool GENERIC_2D_ARRAY< T >::DeSerialize ( tesseract::TFile fp)
inline

Definition at line 174 of file matrix.h.

174  {
175  return DeSerializeSize(fp) &&
176  fp->DeSerialize(&empty_) &&
177  fp->DeSerialize(&array_[0], num_elements());
178  }
bool DeSerialize(char *data, size_t count=1)
Definition: serialis.cpp:103
virtual int num_elements() const
Definition: matrix.h:210
bool DeSerializeSize(bool swap, FILE *fp)
Definition: matrix.h:480

◆ DeSerializeClasses()

template<class T>
bool GENERIC_2D_ARRAY< T >::DeSerializeClasses ( bool  swap,
FILE *  fp 
)
inline

Definition at line 195 of file matrix.h.

195  {
196  if (!DeSerializeSize(swap, fp)) return false;
197  if (!empty_.DeSerialize(swap, fp)) return false;
198  int size = num_elements();
199  for (int i = 0; i < size; ++i) {
200  if (!array_[i].DeSerialize(swap, fp)) return false;
201  }
202  return true;
203  }
virtual int num_elements() const
Definition: matrix.h:210
bool DeSerializeSize(bool swap, FILE *fp)
Definition: matrix.h:480
bool DeSerialize(bool swap, FILE *fp)
Definition: matrix.h:161

◆ DeSerializeSize() [1/2]

template<class T>
bool GENERIC_2D_ARRAY< T >::DeSerializeSize ( bool  swap,
FILE *  fp 
)
inlineprotected

Definition at line 480 of file matrix.h.

480  {
481  uint32_t size1, size2;
482  if (!tesseract::DeSerialize(fp, &size1)) return false;
483  if (!tesseract::DeSerialize(fp, &size2)) return false;
484  if (swap) {
485  ReverseN(&size1, sizeof(size1));
486  ReverseN(&size2, sizeof(size2));
487  }
488  // Arbitrarily limit the number of elements to protect against bad data.
489  if (size1 > UINT16_MAX) return false;
490  if (size2 > UINT16_MAX) return false;
491  Resize(size1, size2, empty_);
492  return true;
493  }
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:178
void Resize(int size1, int size2, const T &empty)
Definition: matrix.h:105
bool DeSerialize(FILE *fp, char *data, size_t n)
Definition: serialis.cpp:27

◆ DeSerializeSize() [2/2]

template<class T>
bool GENERIC_2D_ARRAY< T >::DeSerializeSize ( tesseract::TFile fp)
inlineprotected

Definition at line 494 of file matrix.h.

494  {
495  int32_t size1, size2;
496  if (!fp->DeSerialize(&size1)) return false;
497  if (!fp->DeSerialize(&size2)) return false;
498  // Arbitrarily limit the number of elements to protect against bad data.
499  if (size1 > UINT16_MAX) return false;
500  if (size2 > UINT16_MAX) return false;
501  Resize(size1, size2, empty_);
502  return true;
503  }
bool DeSerialize(char *data, size_t count=1)
Definition: serialis.cpp:103
void Resize(int size1, int size2, const T &empty)
Definition: matrix.h:105

◆ dim1()

template<class T>
int GENERIC_2D_ARRAY< T >::dim1 ( ) const
inline

Definition at line 206 of file matrix.h.

206 { return dim1_; }

◆ dim2()

template<class T>
int GENERIC_2D_ARRAY< T >::dim2 ( ) const
inline

Definition at line 207 of file matrix.h.

207 { return dim2_; }

◆ get() [1/2]

template<class T>
T GENERIC_2D_ARRAY< T >::get ( ICOORD  pos) const
inline

Definition at line 228 of file matrix.h.

228  {
229  return array_[this->index(pos.x(), pos.y())];
230  }
int16_t y() const
access_function
Definition: points.h:57
virtual int index(int column, int row) const
Definition: matrix.h:215
int16_t x() const
access function
Definition: points.h:53

◆ get() [2/2]

template<class T>
T GENERIC_2D_ARRAY< T >::get ( int  column,
int  row 
) const
inline

Definition at line 231 of file matrix.h.

231  {
232  return array_[this->index(column, row)];
233  }
virtual int index(int column, int row) const
Definition: matrix.h:215

◆ index()

template<class T>
virtual int GENERIC_2D_ARRAY< T >::index ( int  column,
int  row 
) const
inlinevirtual

Reimplemented in BandTriMatrix< T >, and BandTriMatrix< BLOB_CHOICE_LIST *>.

Definition at line 215 of file matrix.h.

215  {
216  return (column * dim2_ + row);
217  }

◆ Max()

template<class T>
T GENERIC_2D_ARRAY< T >::Max ( ) const
inline

Definition at line 342 of file matrix.h.

342  {
343  int size = num_elements();
344  if (size <= 0) return empty_;
345  // Compute the max.
346  T max_value = array_[0];
347  for (int i = 1; i < size; ++i) {
348  const T& value = array_[i];
349  if (value > max_value) max_value = value;
350  }
351  return max_value;
352  }
virtual int num_elements() const
Definition: matrix.h:210

◆ MaxAbs()

template<class T>
T GENERIC_2D_ARRAY< T >::MaxAbs ( ) const
inline

Definition at line 355 of file matrix.h.

355  {
356  int size = num_elements();
357  if (size <= 0) return empty_;
358  // Compute the max.
359  T max_abs = static_cast<T>(0);
360  for (int i = 0; i < size; ++i) {
361  T value = static_cast<T>(fabs(array_[i]));
362  if (value > max_abs) max_abs = value;
363  }
364  return max_abs;
365  }
virtual int num_elements() const
Definition: matrix.h:210

◆ Normalize()

template<class T>
double GENERIC_2D_ARRAY< T >::Normalize ( )
inline

Definition at line 315 of file matrix.h.

315  {
316  int size = num_elements();
317  if (size <= 0) return 0.0;
318  // Compute the mean.
319  double mean = 0.0;
320  for (int i = 0; i < size; ++i) {
321  mean += array_[i];
322  }
323  mean /= size;
324  // Subtract the mean and compute the standard deviation.
325  double sd = 0.0;
326  for (int i = 0; i < size; ++i) {
327  double normed = array_[i] - mean;
328  array_[i] = normed;
329  sd += normed * normed;
330  }
331  sd = sqrt(sd / size);
332  if (sd > 0.0) {
333  // Divide by the sd.
334  for (int i = 0; i < size; ++i) {
335  array_[i] /= sd;
336  }
337  }
338  return sd;
339  }
virtual int num_elements() const
Definition: matrix.h:210

◆ num_elements()

template<class T>
virtual int GENERIC_2D_ARRAY< T >::num_elements ( ) const
inlinevirtual

Definition at line 210 of file matrix.h.

210 { return dim1_ * dim2_; }

◆ operator()() [1/2]

template<class T>
const T& GENERIC_2D_ARRAY< T >::operator() ( int  column,
int  row 
) const
inline

Definition at line 235 of file matrix.h.

235  {
236  return array_[this->index(column, row)];
237  }
virtual int index(int column, int row) const
Definition: matrix.h:215

◆ operator()() [2/2]

template<class T>
T& GENERIC_2D_ARRAY< T >::operator() ( int  column,
int  row 
)
inline

Definition at line 238 of file matrix.h.

238  {
239  return array_[this->index(column, row)];
240  }
virtual int index(int column, int row) const
Definition: matrix.h:215

◆ operator*=()

template<class T>
void GENERIC_2D_ARRAY< T >::operator*= ( const T &  factor)
inline

Definition at line 290 of file matrix.h.

290  {
291  int size = num_elements();
292  for (int i = 0; i < size; ++i) {
293  array_[i] *= factor;
294  }
295  }
virtual int num_elements() const
Definition: matrix.h:210

◆ operator+=() [1/2]

template<class T>
void GENERIC_2D_ARRAY< T >::operator+= ( const GENERIC_2D_ARRAY< T > &  addend)
inline

Definition at line 251 of file matrix.h.

251  {
252  if (dim2_ == addend.dim2_) {
253  // Faster if equal size in the major dimension.
254  int size = std::min(num_elements(), addend.num_elements());
255  for (int i = 0; i < size; ++i) {
256  array_[i] += addend.array_[i];
257  }
258  } else {
259  for (int x = 0; x < dim1_; x++) {
260  for (int y = 0; y < dim2_; y++) {
261  (*this)(x, y) += addend(x, y);
262  }
263  }
264  }
265  }
virtual int num_elements() const
Definition: matrix.h:210

◆ operator+=() [2/2]

template<class T>
void GENERIC_2D_ARRAY< T >::operator+= ( const T &  addend)
inline

Definition at line 283 of file matrix.h.

283  {
284  int size = num_elements();
285  for (int i = 0; i < size; ++i) {
286  array_[i] += addend;
287  }
288  }
virtual int num_elements() const
Definition: matrix.h:210

◆ operator-=()

template<class T>
void GENERIC_2D_ARRAY< T >::operator-= ( const GENERIC_2D_ARRAY< T > &  minuend)
inline

Definition at line 267 of file matrix.h.

267  {
268  if (dim2_ == minuend.dim2_) {
269  // Faster if equal size in the major dimension.
270  int size = std::min(num_elements(), minuend.num_elements());
271  for (int i = 0; i < size; ++i) {
272  array_[i] -= minuend.array_[i];
273  }
274  } else {
275  for (int x = 0; x < dim1_; x++) {
276  for (int y = 0; y < dim2_; y++) {
277  (*this)(x, y) -= minuend(x, y);
278  }
279  }
280  }
281  }
virtual int num_elements() const
Definition: matrix.h:210

◆ operator=()

template<class T>
void GENERIC_2D_ARRAY< T >::operator= ( const GENERIC_2D_ARRAY< T > &  src)
inline

Definition at line 82 of file matrix.h.

82  {
83  ResizeNoInit(src.dim1(), src.dim2());
84  memcpy(array_, src.array_, num_elements() * sizeof(array_[0]));
85  }
void ResizeNoInit(int size1, int size2, int pad=0)
Definition: matrix.h:91
virtual int num_elements() const
Definition: matrix.h:210
int dim1() const
Definition: matrix.h:206
int dim2() const
Definition: matrix.h:207

◆ operator[]() [1/2]

template<class T>
T* GENERIC_2D_ARRAY< T >::operator[] ( int  column)
inline

Definition at line 243 of file matrix.h.

243  {
244  return &array_[this->index(column, 0)];
245  }
virtual int index(int column, int row) const
Definition: matrix.h:215

◆ operator[]() [2/2]

template<class T>
const T* GENERIC_2D_ARRAY< T >::operator[] ( int  column) const
inline

Definition at line 246 of file matrix.h.

246  {
247  return &array_[this->index(column, 0)];
248  }
virtual int index(int column, int row) const
Definition: matrix.h:215

◆ put() [1/2]

template<class T>
void GENERIC_2D_ARRAY< T >::put ( ICOORD  pos,
const T &  thing 
)
inline

Definition at line 220 of file matrix.h.

220  {
221  array_[this->index(pos.x(), pos.y())] = thing;
222  }
int16_t y() const
access_function
Definition: points.h:57
virtual int index(int column, int row) const
Definition: matrix.h:215
int16_t x() const
access function
Definition: points.h:53

◆ put() [2/2]

template<class T>
void GENERIC_2D_ARRAY< T >::put ( int  column,
int  row,
const T &  thing 
)
inline

Definition at line 223 of file matrix.h.

223  {
224  array_[this->index(column, row)] = thing;
225  }
virtual int index(int column, int row) const
Definition: matrix.h:215

◆ Resize()

template<class T>
void GENERIC_2D_ARRAY< T >::Resize ( int  size1,
int  size2,
const T &  empty 
)
inline

Definition at line 105 of file matrix.h.

105  {
106  empty_ = empty;
107  ResizeNoInit(size1, size2);
108  Clear();
109  }
void ResizeNoInit(int size1, int size2, int pad=0)
Definition: matrix.h:91
void Clear()
Definition: matrix.h:136

◆ ResizeNoInit()

template<class T>
void GENERIC_2D_ARRAY< T >::ResizeNoInit ( int  size1,
int  size2,
int  pad = 0 
)
inline

Definition at line 91 of file matrix.h.

91  {
92  int new_size = size1 * size2 + pad;
93  if (new_size > size_allocated_) {
94  delete [] array_;
95  array_ = new T[new_size];
96  size_allocated_ = new_size;
97  }
98  dim1_ = size1;
99  dim2_ = size2;
100  // Fill the padding data so it isn't uninitialized.
101  for (int i = size1 * size2; i < new_size; ++i) array_[i] = empty_;
102  }
int size_allocated_
Definition: matrix.h:512

◆ ResizeWithCopy()

template<class T>
void GENERIC_2D_ARRAY< T >::ResizeWithCopy ( int  size1,
int  size2 
)
inline

Definition at line 112 of file matrix.h.

112  {
113  if (size1 != dim1_ || size2 != dim2_) {
114  int new_size = size1 * size2;
115  T* new_array = new T[new_size];
116  for (int col = 0; col < size1; ++col) {
117  for (int row = 0; row < size2; ++row) {
118  int old_index = col * dim2() + row;
119  int new_index = col * size2 + row;
120  if (col < dim1_ && row < dim2_) {
121  new_array[new_index] = array_[old_index];
122  } else {
123  new_array[new_index] = empty_;
124  }
125  }
126  }
127  delete[] array_;
128  array_ = new_array;
129  dim1_ = size1;
130  dim2_ = size2;
131  size_allocated_ = new_size;
132  }
133  }
int size_allocated_
Definition: matrix.h:512
int dim2() const
Definition: matrix.h:207

◆ RotatingTranspose()

template<class T>
void GENERIC_2D_ARRAY< T >::RotatingTranspose ( const int *  dims,
int  num_dims,
int  src_dim,
int  dest_dim,
GENERIC_2D_ARRAY< T > *  result 
) const
inline

Definition at line 418 of file matrix.h.

419  {
420  int max_d = std::max(src_dim, dest_dim);
421  int min_d = std::min(src_dim, dest_dim);
422  // In a tensor of shape [d0, d1... min_d, ... max_d, ... dn-2, dn-1], the
423  // ends outside of min_d and max_d are unaffected, with [max_d +1, dn-1]
424  // being contiguous blocks of data that will move together, and
425  // [d0, min_d -1] being replicas of the transpose operation.
426  // num_replicas represents the large dimensions unchanged by the operation.
427  // move_size represents the small dimensions unchanged by the operation.
428  // src_step represents the stride in the src between each adjacent group
429  // in the destination.
430  int num_replicas = 1, move_size = 1, src_step = 1;
431  for (int d = 0; d < min_d; ++d) num_replicas *= dims[d];
432  for (int d = max_d + 1; d < num_dims; ++d) move_size *= dims[d];
433  for (int d = src_dim + 1; d < num_dims; ++d) src_step *= dims[d];
434  if (src_dim > dest_dim) src_step *= dims[src_dim];
435  // wrap_size is the size of a single replica, being the amount that is
436  // handled num_replicas times.
437  int wrap_size = move_size;
438  for (int d = min_d; d <= max_d; ++d) wrap_size *= dims[d];
439  result->ResizeNoInit(dim1_, dim2_);
440  result->empty_ = empty_;
441  const T* src = array_;
442  T* dest = result->array_;
443  for (int replica = 0; replica < num_replicas; ++replica) {
444  for (int start = 0; start < src_step; start += move_size) {
445  for (int pos = start; pos < wrap_size; pos += src_step) {
446  memcpy(dest, src + pos, sizeof(*dest) * move_size);
447  dest += move_size;
448  }
449  }
450  src += wrap_size;
451  }
452  }
void ResizeNoInit(int size1, int size2, int pad=0)
Definition: matrix.h:91

◆ Serialize() [1/2]

template<class T>
bool GENERIC_2D_ARRAY< T >::Serialize ( FILE *  fp) const
inline

Definition at line 144 of file matrix.h.

144  {
145  if (!SerializeSize(fp)) return false;
146  if (!tesseract::Serialize(fp, &empty_)) return false;
147  int size = num_elements();
148  return tesseract::Serialize(fp, &array_[0], size);
149  }
bool Serialize(FILE *fp, const char *data, size_t n)
Definition: serialis.cpp:59
virtual int num_elements() const
Definition: matrix.h:210
bool SerializeSize(FILE *fp) const
Definition: matrix.h:466

◆ Serialize() [2/2]

template<class T>
bool GENERIC_2D_ARRAY< T >::Serialize ( tesseract::TFile fp) const
inline

Definition at line 151 of file matrix.h.

151  {
152  if (!SerializeSize(fp)) return false;
153  if (!fp->Serialize(&empty_)) return false;
154  int size = num_elements();
155  return fp->Serialize(&array_[0], size);
156  }
virtual int num_elements() const
Definition: matrix.h:210
bool Serialize(const char *data, size_t count=1)
Definition: serialis.cpp:147
bool SerializeSize(FILE *fp) const
Definition: matrix.h:466

◆ SerializeClasses()

template<class T>
bool GENERIC_2D_ARRAY< T >::SerializeClasses ( FILE *  fp) const
inline

Definition at line 182 of file matrix.h.

182  {
183  if (!SerializeSize(fp)) return false;
184  if (!empty_.Serialize(fp)) return false;
185  int size = num_elements();
186  for (int i = 0; i < size; ++i) {
187  if (!array_[i].Serialize(fp)) return false;
188  }
189  return true;
190  }
virtual int num_elements() const
Definition: matrix.h:210
bool Serialize(FILE *fp) const
Definition: matrix.h:144
bool SerializeSize(FILE *fp) const
Definition: matrix.h:466

◆ SerializeSize() [1/2]

template<class T>
bool GENERIC_2D_ARRAY< T >::SerializeSize ( FILE *  fp) const
inlineprotected

Definition at line 466 of file matrix.h.

466  {
467  uint32_t size = dim1_;
468  if (!tesseract::Serialize(fp, &size)) return false;
469  size = dim2_;
470  return tesseract::Serialize(fp, &size);
471  }
bool Serialize(FILE *fp, const char *data, size_t n)
Definition: serialis.cpp:59

◆ SerializeSize() [2/2]

template<class T>
bool GENERIC_2D_ARRAY< T >::SerializeSize ( tesseract::TFile fp) const
inlineprotected

Definition at line 472 of file matrix.h.

472  {
473  uint32_t size = dim1_;
474  if (!fp->Serialize(&size)) return false;
475  size = dim2_;
476  return fp->Serialize(&size);
477  }
bool Serialize(const char *data, size_t count=1)
Definition: serialis.cpp:147

◆ SumSquares()

template<class T>
void GENERIC_2D_ARRAY< T >::SumSquares ( const GENERIC_2D_ARRAY< T > &  src,
const T &  decay_factor 
)
inline

Definition at line 368 of file matrix.h.

368  {
369  T update_factor = 1.0 - decay_factor;
370  int size = num_elements();
371  for (int i = 0; i < size; ++i) {
372  array_[i] = array_[i] * decay_factor +
373  update_factor * src.array_[i] * src.array_[i];
374  }
375  }
virtual int num_elements() const
Definition: matrix.h:210

◆ WithinBounds()

template<class T>
bool GENERIC_2D_ARRAY< T >::WithinBounds ( const T &  rangemin,
const T &  rangemax 
) const
inline

Definition at line 305 of file matrix.h.

305  {
306  int size = num_elements();
307  for (int i = 0; i < size; ++i) {
308  const T& value = array_[i];
309  if (value < rangemin || rangemax < value)
310  return false;
311  }
312  return true;
313  }
virtual int num_elements() const
Definition: matrix.h:210

Member Data Documentation

◆ array_

template<class T>
T* GENERIC_2D_ARRAY< T >::array_
protected

Definition at line 505 of file matrix.h.

◆ dim1_

template<class T>
int GENERIC_2D_ARRAY< T >::dim1_
protected

Definition at line 507 of file matrix.h.

◆ dim2_

template<class T>
int GENERIC_2D_ARRAY< T >::dim2_
protected

Definition at line 508 of file matrix.h.

◆ empty_

template<class T>
T GENERIC_2D_ARRAY< T >::empty_
protected

Definition at line 506 of file matrix.h.

◆ size_allocated_

template<class T>
int GENERIC_2D_ARRAY< T >::size_allocated_
protected

Definition at line 512 of file matrix.h.


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