tesseract  4.00.00dev
GENERIC_2D_ARRAY< T > Class Template Reference

#include <matrix.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, T decay_factor)
 
void AdamUpdate (const GENERIC_2D_ARRAY< T > &sum, const GENERIC_2D_ARRAY< T > &sqsum, 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 47 of file matrix.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 53 of file matrix.h.

54  : empty_(empty), dim1_(dim1), dim2_(dim2), array_(array) {
56  }
int size_allocated_
Definition: matrix.h:507
int dim2() const
Definition: matrix.h:206
int dim1() const
Definition: matrix.h:205

◆ 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 59 of file matrix.h.

60  : empty_(empty), dim1_(dim1), dim2_(dim2) {
61  int new_size = dim1 * dim2;
62  array_ = new T[new_size];
63  size_allocated_ = new_size;
64  for (int i = 0; i < size_allocated_; ++i)
65  array_[i] = empty_;
66  }
int size_allocated_
Definition: matrix.h:507
int dim2() const
Definition: matrix.h:206
int dim1() const
Definition: matrix.h:205

◆ GENERIC_2D_ARRAY() [3/4]

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

Definition at line 68 of file matrix.h.

69  : array_(NULL), empty_(static_cast<T>(0)), dim1_(0), dim2_(0),
70  size_allocated_(0) {
71  }
int size_allocated_
Definition: matrix.h:507

◆ 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 72 of file matrix.h.

73  : array_(NULL), empty_(static_cast<T>(0)), dim1_(0), dim2_(0),
74  size_allocated_(0) {
75  *this = src;
76  }
int size_allocated_
Definition: matrix.h:507

◆ ~GENERIC_2D_ARRAY()

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

Definition at line 77 of file matrix.h.

77 { 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,
epsilon 
)
inline

Definition at line 378 of file matrix.h.

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

◆ AssertFinite()

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

Definition at line 386 of file matrix.h.

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

◆ Clear()

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

Definition at line 133 of file matrix.h.

133  {
134  int total_size = num_elements();
135  for (int i = 0; i < total_size; ++i)
136  array_[i] = empty_;
137  }
virtual int num_elements() const
Definition: matrix.h:209

◆ Clip()

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

Definition at line 296 of file matrix.h.

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

◆ delete_matrix_pointers()

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

Definition at line 454 of file matrix.h.

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

◆ DeSerialize() [1/2]

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

Definition at line 159 of file matrix.h.

159  {
160  if (!DeSerializeSize(swap, fp)) return false;
161  if (fread(&empty_, sizeof(empty_), 1, fp) != 1) return false;
162  if (swap) ReverseN(&empty_, sizeof(empty_));
163  int size = num_elements();
164  if (fread(array_, sizeof(*array_), size, fp) != size) return false;
165  if (swap) {
166  for (int i = 0; i < size; ++i)
167  ReverseN(&array_[i], sizeof(array_[i]));
168  }
169  return true;
170  }
virtual int num_elements() const
Definition: matrix.h:209
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:184
bool DeSerializeSize(bool swap, FILE *fp)
Definition: matrix.h:481

◆ DeSerialize() [2/2]

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

Definition at line 171 of file matrix.h.

171  {
172  if (!DeSerializeSize(fp)) return false;
173  if (fp->FReadEndian(&empty_, sizeof(empty_), 1) != 1) return false;
174  int size = num_elements();
175  if (fp->FReadEndian(array_, sizeof(*array_), size) != size) return false;
176  return true;
177  }
virtual int num_elements() const
Definition: matrix.h:209
int FReadEndian(void *buffer, int size, int count)
Definition: serialis.cpp:97
bool DeSerializeSize(bool swap, FILE *fp)
Definition: matrix.h:481

◆ DeSerializeClasses()

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

Definition at line 194 of file matrix.h.

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

◆ DeSerializeSize() [1/2]

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

Definition at line 481 of file matrix.h.

481  {
482  inT32 size1, size2;
483  if (fread(&size1, sizeof(size1), 1, fp) != 1) return false;
484  if (fread(&size2, sizeof(size2), 1, fp) != 1) return false;
485  if (swap) {
486  ReverseN(&size1, sizeof(size1));
487  ReverseN(&size2, sizeof(size2));
488  }
489  Resize(size1, size2, empty_);
490  return true;
491  }
void Resize(int size1, int size2, const T &empty)
Definition: matrix.h:102
int32_t inT32
Definition: host.h:38
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:184

◆ DeSerializeSize() [2/2]

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

Definition at line 492 of file matrix.h.

492  {
493  inT32 size1, size2;
494  if (fp->FReadEndian(&size1, sizeof(size1), 1) != 1) return false;
495  if (fp->FReadEndian(&size2, sizeof(size2), 1) != 1) return false;
496  Resize(size1, size2, empty_);
497  return true;
498  }
void Resize(int size1, int size2, const T &empty)
Definition: matrix.h:102
int32_t inT32
Definition: host.h:38
int FReadEndian(void *buffer, int size, int count)
Definition: serialis.cpp:97

◆ dim1()

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

Definition at line 205 of file matrix.h.

205 { return dim1_; }

◆ dim2()

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

Definition at line 206 of file matrix.h.

206 { return dim2_; }

◆ get() [1/2]

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

Definition at line 227 of file matrix.h.

227  {
228  return array_[this->index(pos.x(), pos.y())];
229  }
inT16 x() const
access function
Definition: points.h:52
virtual int index(int column, int row) const
Definition: matrix.h:214
inT16 y() const
access_function
Definition: points.h:56

◆ get() [2/2]

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

Definition at line 230 of file matrix.h.

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

◆ 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 214 of file matrix.h.

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

◆ Max()

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

Definition at line 341 of file matrix.h.

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

◆ MaxAbs()

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

Definition at line 354 of file matrix.h.

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

◆ Normalize()

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

Definition at line 314 of file matrix.h.

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

◆ num_elements()

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

Definition at line 209 of file matrix.h.

209 { 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 234 of file matrix.h.

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

◆ operator()() [2/2]

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

Definition at line 237 of file matrix.h.

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

◆ operator*=()

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

Definition at line 289 of file matrix.h.

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

◆ operator+=() [1/2]

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

Definition at line 250 of file matrix.h.

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

◆ operator+=() [2/2]

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

Definition at line 282 of file matrix.h.

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

◆ operator-=()

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

Definition at line 266 of file matrix.h.

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

◆ operator=()

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

Definition at line 79 of file matrix.h.

79  {
80  ResizeNoInit(src.dim1(), src.dim2());
81  memcpy(array_, src.array_, num_elements() * sizeof(array_[0]));
82  }
void ResizeNoInit(int size1, int size2, int pad=0)
Definition: matrix.h:88
virtual int num_elements() const
Definition: matrix.h:209
int dim2() const
Definition: matrix.h:206
int dim1() const
Definition: matrix.h:205

◆ operator[]() [1/2]

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

Definition at line 242 of file matrix.h.

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

◆ operator[]() [2/2]

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

Definition at line 245 of file matrix.h.

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

◆ put() [1/2]

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

Definition at line 219 of file matrix.h.

219  {
220  array_[this->index(pos.x(), pos.y())] = thing;
221  }
inT16 x() const
access function
Definition: points.h:52
virtual int index(int column, int row) const
Definition: matrix.h:214
inT16 y() const
access_function
Definition: points.h:56

◆ put() [2/2]

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

Definition at line 222 of file matrix.h.

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

◆ Resize()

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

Definition at line 102 of file matrix.h.

102  {
103  empty_ = empty;
104  ResizeNoInit(size1, size2);
105  Clear();
106  }
void ResizeNoInit(int size1, int size2, int pad=0)
Definition: matrix.h:88
void Clear()
Definition: matrix.h:133

◆ ResizeNoInit()

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

Definition at line 88 of file matrix.h.

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

◆ ResizeWithCopy()

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

Definition at line 109 of file matrix.h.

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

◆ 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 417 of file matrix.h.

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

◆ Serialize() [1/2]

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

Definition at line 141 of file matrix.h.

141  {
142  if (!SerializeSize(fp)) return false;
143  if (fwrite(&empty_, sizeof(empty_), 1, fp) != 1) return false;
144  int size = num_elements();
145  if (fwrite(array_, sizeof(*array_), size, fp) != size) return false;
146  return true;
147  }
bool SerializeSize(FILE *fp) const
Definition: matrix.h:465
virtual int num_elements() const
Definition: matrix.h:209

◆ Serialize() [2/2]

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

Definition at line 148 of file matrix.h.

148  {
149  if (!SerializeSize(fp)) return false;
150  if (fp->FWrite(&empty_, sizeof(empty_), 1) != 1) return false;
151  int size = num_elements();
152  if (fp->FWrite(array_, sizeof(*array_), size) != size) return false;
153  return true;
154  }
int FWrite(const void *buffer, int size, int count)
Definition: serialis.cpp:148
bool SerializeSize(FILE *fp) const
Definition: matrix.h:465
virtual int num_elements() const
Definition: matrix.h:209

◆ SerializeClasses()

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

Definition at line 181 of file matrix.h.

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

◆ SerializeSize() [1/2]

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

Definition at line 465 of file matrix.h.

465  {
466  inT32 size = dim1_;
467  if (fwrite(&size, sizeof(size), 1, fp) != 1) return false;
468  size = dim2_;
469  if (fwrite(&size, sizeof(size), 1, fp) != 1) return false;
470  return true;
471  }
int32_t inT32
Definition: host.h:38

◆ 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  inT32 size = dim1_;
474  if (fp->FWrite(&size, sizeof(size), 1) != 1) return false;
475  size = dim2_;
476  if (fp->FWrite(&size, sizeof(size), 1) != 1) return false;
477  return true;
478  }
int FWrite(const void *buffer, int size, int count)
Definition: serialis.cpp:148
int32_t inT32
Definition: host.h:38

◆ SumSquares()

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

Definition at line 367 of file matrix.h.

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

◆ WithinBounds()

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

Definition at line 304 of file matrix.h.

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

Member Data Documentation

◆ array_

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

Definition at line 500 of file matrix.h.

◆ dim1_

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

Definition at line 502 of file matrix.h.

◆ dim2_

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

Definition at line 503 of file matrix.h.

◆ empty_

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

Definition at line 501 of file matrix.h.

◆ size_allocated_

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

Definition at line 507 of file matrix.h.


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