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)
 
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)
 
void AdaGradScaling (const GENERIC_2D_ARRAY< T > &sqsum, int num_samples)
 
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:500
int dim1() const
Definition: matrix.h:201
int dim2() const
Definition: matrix.h:202

◆ 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:500
int dim1() const
Definition: matrix.h:201
int dim2() const
Definition: matrix.h:202

◆ 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:500

◆ 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:500

◆ ~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

◆ AdaGradScaling()

template<class T>
void GENERIC_2D_ARRAY< T >::AdaGradScaling ( const GENERIC_2D_ARRAY< T > &  sqsum,
int  num_samples 
)
inline

Definition at line 372 of file matrix.h.

372  {
373  int size = num_elements();
374  for (int i = 0; i < size; ++i) {
375  array_[i] *= sqrt(num_samples / MAX(1.0, sqsum.array_[i]));
376  }
377  }
voidpf void uLong size
Definition: ioapi.h:39
virtual int num_elements() const
Definition: matrix.h:205
#define MAX(x, y)
Definition: ndminx.h:24

◆ AssertFinite()

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

Definition at line 379 of file matrix.h.

379  {
380  int size = num_elements();
381  for (int i = 0; i < size; ++i) {
382  ASSERT_HOST(isfinite(array_[i]));
383  }
384  }
voidpf void uLong size
Definition: ioapi.h:39
virtual int num_elements() const
Definition: matrix.h:205
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ Clear()

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

Definition at line 129 of file matrix.h.

129  {
130  int total_size = num_elements();
131  for (int i = 0; i < total_size; ++i)
132  array_[i] = empty_;
133  }
virtual int num_elements() const
Definition: matrix.h:205

◆ Clip()

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

Definition at line 292 of file matrix.h.

292  {
293  int size = num_elements();
294  for (int i = 0; i < size; ++i) {
295  array_[i] = ClipToRange(array_[i], rangemin, rangemax);
296  }
297  }
voidpf void uLong size
Definition: ioapi.h:39
virtual int num_elements() const
Definition: matrix.h:205
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 447 of file matrix.h.

447  {
448  int size = num_elements();
449  for (int i = 0; i < size; ++i) {
450  T matrix_cell = array_[i];
451  if (matrix_cell != empty_)
452  delete matrix_cell;
453  }
454  }
voidpf void uLong size
Definition: ioapi.h:39
virtual int num_elements() const
Definition: matrix.h:205

◆ DeSerialize() [1/2]

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

Definition at line 155 of file matrix.h.

155  {
156  if (!DeSerializeSize(swap, fp)) return false;
157  if (fread(&empty_, sizeof(empty_), 1, fp) != 1) return false;
158  if (swap) ReverseN(&empty_, sizeof(empty_));
159  int size = num_elements();
160  if (fread(array_, sizeof(*array_), size, fp) != size) return false;
161  if (swap) {
162  for (int i = 0; i < size; ++i)
163  ReverseN(&array_[i], sizeof(array_[i]));
164  }
165  return true;
166  }
bool DeSerializeSize(bool swap, FILE *fp)
Definition: matrix.h:474
voidpf void uLong size
Definition: ioapi.h:39
virtual int num_elements() const
Definition: matrix.h:205
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:184

◆ DeSerialize() [2/2]

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

Definition at line 167 of file matrix.h.

167  {
168  if (!DeSerializeSize(fp)) return false;
169  if (fp->FReadEndian(&empty_, sizeof(empty_), 1) != 1) return false;
170  int size = num_elements();
171  if (fp->FReadEndian(array_, sizeof(*array_), size) != size) return false;
172  return true;
173  }
bool DeSerializeSize(bool swap, FILE *fp)
Definition: matrix.h:474
voidpf void uLong size
Definition: ioapi.h:39
int FReadEndian(void *buffer, int size, int count)
Definition: serialis.cpp:97
virtual int num_elements() const
Definition: matrix.h:205

◆ DeSerializeClasses()

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

Definition at line 190 of file matrix.h.

190  {
191  if (!DeSerializeSize(swap, fp)) return false;
192  if (!empty_.DeSerialize(swap, fp)) return false;
193  int size = num_elements();
194  for (int i = 0; i < size; ++i) {
195  if (!array_[i].DeSerialize(swap, fp)) return false;
196  }
197  return true;
198  }
bool DeSerializeSize(bool swap, FILE *fp)
Definition: matrix.h:474
voidpf void uLong size
Definition: ioapi.h:39
virtual int num_elements() const
Definition: matrix.h:205
bool DeSerialize(bool swap, FILE *fp)
Definition: matrix.h:155

◆ DeSerializeSize() [1/2]

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

Definition at line 474 of file matrix.h.

474  {
475  inT32 size1, size2;
476  if (fread(&size1, sizeof(size1), 1, fp) != 1) return false;
477  if (fread(&size2, sizeof(size2), 1, fp) != 1) return false;
478  if (swap) {
479  ReverseN(&size1, sizeof(size1));
480  ReverseN(&size2, sizeof(size2));
481  }
482  Resize(size1, size2, empty_);
483  return true;
484  }
int32_t inT32
Definition: host.h:38
void Resize(int size1, int size2, const T &empty)
Definition: matrix.h:98
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 485 of file matrix.h.

485  {
486  inT32 size1, size2;
487  if (fp->FReadEndian(&size1, sizeof(size1), 1) != 1) return false;
488  if (fp->FReadEndian(&size2, sizeof(size2), 1) != 1) return false;
489  Resize(size1, size2, empty_);
490  return true;
491  }
int32_t inT32
Definition: host.h:38
void Resize(int size1, int size2, const T &empty)
Definition: matrix.h:98
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 201 of file matrix.h.

201 { return dim1_; }

◆ dim2()

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

Definition at line 202 of file matrix.h.

202 { return dim2_; }

◆ get() [1/2]

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

Definition at line 223 of file matrix.h.

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

◆ get() [2/2]

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

Definition at line 226 of file matrix.h.

226  {
227  return array_[this->index(column, row)];
228  }
virtual int index(int column, int row) const
Definition: matrix.h:210

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

210  {
211  return (column * dim2_ + row);
212  }

◆ Max()

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

Definition at line 337 of file matrix.h.

337  {
338  int size = num_elements();
339  if (size <= 0) return empty_;
340  // Compute the max.
341  T max_value = array_[0];
342  for (int i = 1; i < size; ++i) {
343  const T& value = array_[i];
344  if (value > max_value) max_value = value;
345  }
346  return max_value;
347  }
voidpf void uLong size
Definition: ioapi.h:39
virtual int num_elements() const
Definition: matrix.h:205

◆ MaxAbs()

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

Definition at line 350 of file matrix.h.

350  {
351  int size = num_elements();
352  if (size <= 0) return empty_;
353  // Compute the max.
354  T max_abs = static_cast<T>(0);
355  for (int i = 0; i < size; ++i) {
356  T value = static_cast<T>(fabs(array_[i]));
357  if (value > max_abs) max_abs = value;
358  }
359  return max_abs;
360  }
voidpf void uLong size
Definition: ioapi.h:39
virtual int num_elements() const
Definition: matrix.h:205

◆ Normalize()

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

Definition at line 310 of file matrix.h.

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

◆ num_elements()

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

Definition at line 205 of file matrix.h.

205 { 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 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:210

◆ operator()() [2/2]

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

Definition at line 233 of file matrix.h.

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

◆ operator*=()

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

Definition at line 285 of file matrix.h.

285  {
286  int size = num_elements();
287  for (int i = 0; i < size; ++i) {
288  array_[i] *= factor;
289  }
290  }
voidpf void uLong size
Definition: ioapi.h:39
virtual int num_elements() const
Definition: matrix.h:205

◆ operator+=() [1/2]

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

Definition at line 246 of file matrix.h.

246  {
247  if (dim2_ == addend.dim2_) {
248  // Faster if equal size in the major dimension.
249  int size = MIN(num_elements(), addend.num_elements());
250  for (int i = 0; i < size; ++i) {
251  array_[i] += addend.array_[i];
252  }
253  } else {
254  for (int x = 0; x < dim1_; x++) {
255  for (int y = 0; y < dim2_; y++) {
256  (*this)(x, y) += addend(x, y);
257  }
258  }
259  }
260  }
voidpf void uLong size
Definition: ioapi.h:39
virtual int num_elements() const
Definition: matrix.h:205
#define MIN(x, y)
Definition: ndminx.h:28

◆ operator+=() [2/2]

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

Definition at line 278 of file matrix.h.

278  {
279  int size = num_elements();
280  for (int i = 0; i < size; ++i) {
281  array_[i] += addend;
282  }
283  }
voidpf void uLong size
Definition: ioapi.h:39
virtual int num_elements() const
Definition: matrix.h:205

◆ operator-=()

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

Definition at line 262 of file matrix.h.

262  {
263  if (dim2_ == minuend.dim2_) {
264  // Faster if equal size in the major dimension.
265  int size = MIN(num_elements(), minuend.num_elements());
266  for (int i = 0; i < size; ++i) {
267  array_[i] -= minuend.array_[i];
268  }
269  } else {
270  for (int x = 0; x < dim1_; x++) {
271  for (int y = 0; y < dim2_; y++) {
272  (*this)(x, y) -= minuend(x, y);
273  }
274  }
275  }
276  }
voidpf void uLong size
Definition: ioapi.h:39
virtual int num_elements() const
Definition: matrix.h:205
#define MIN(x, y)
Definition: ndminx.h:28

◆ 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  }
virtual int num_elements() const
Definition: matrix.h:205
int dim1() const
Definition: matrix.h:201
int dim2() const
Definition: matrix.h:202
void ResizeNoInit(int size1, int size2)
Definition: matrix.h:86

◆ operator[]() [1/2]

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

Definition at line 238 of file matrix.h.

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

◆ operator[]() [2/2]

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

Definition at line 241 of file matrix.h.

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

◆ put() [1/2]

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

Definition at line 215 of file matrix.h.

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

◆ put() [2/2]

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

Definition at line 218 of file matrix.h.

218  {
219  array_[this->index(column, row)] = thing;
220  }
virtual int index(int column, int row) const
Definition: matrix.h:210

◆ Resize()

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

Definition at line 98 of file matrix.h.

98  {
99  empty_ = empty;
100  ResizeNoInit(size1, size2);
101  Clear();
102  }
void Clear()
Definition: matrix.h:129
void ResizeNoInit(int size1, int size2)
Definition: matrix.h:86

◆ ResizeNoInit()

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

Definition at line 86 of file matrix.h.

86  {
87  int new_size = size1 * size2;
88  if (new_size > size_allocated_) {
89  delete [] array_;
90  array_ = new T[new_size];
91  size_allocated_ = new_size;
92  }
93  dim1_ = size1;
94  dim2_ = size2;
95  }
int size_allocated_
Definition: matrix.h:500

◆ ResizeWithCopy()

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

Definition at line 105 of file matrix.h.

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

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

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

◆ Serialize() [1/2]

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

Definition at line 137 of file matrix.h.

137  {
138  if (!SerializeSize(fp)) return false;
139  if (fwrite(&empty_, sizeof(empty_), 1, fp) != 1) return false;
140  int size = num_elements();
141  if (fwrite(array_, sizeof(*array_), size, fp) != size) return false;
142  return true;
143  }
bool SerializeSize(FILE *fp) const
Definition: matrix.h:458
voidpf void uLong size
Definition: ioapi.h:39
virtual int num_elements() const
Definition: matrix.h:205

◆ Serialize() [2/2]

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

Definition at line 144 of file matrix.h.

144  {
145  if (!SerializeSize(fp)) return false;
146  if (fp->FWrite(&empty_, sizeof(empty_), 1) != 1) return false;
147  int size = num_elements();
148  if (fp->FWrite(array_, sizeof(*array_), size) != size) return false;
149  return true;
150  }
bool SerializeSize(FILE *fp) const
Definition: matrix.h:458
voidpf void uLong size
Definition: ioapi.h:39
virtual int num_elements() const
Definition: matrix.h:205
int FWrite(const void *buffer, int size, int count)
Definition: serialis.cpp:148

◆ SerializeClasses()

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

Definition at line 177 of file matrix.h.

177  {
178  if (!SerializeSize(fp)) return false;
179  if (!empty_.Serialize(fp)) return false;
180  int size = num_elements();
181  for (int i = 0; i < size; ++i) {
182  if (!array_[i].Serialize(fp)) return false;
183  }
184  return true;
185  }
bool SerializeSize(FILE *fp) const
Definition: matrix.h:458
voidpf void uLong size
Definition: ioapi.h:39
virtual int num_elements() const
Definition: matrix.h:205
bool Serialize(FILE *fp) const
Definition: matrix.h:137

◆ SerializeSize() [1/2]

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

Definition at line 458 of file matrix.h.

458  {
459  inT32 size = dim1_;
460  if (fwrite(&size, sizeof(size), 1, fp) != 1) return false;
461  size = dim2_;
462  if (fwrite(&size, sizeof(size), 1, fp) != 1) return false;
463  return true;
464  }
int32_t inT32
Definition: host.h:38
voidpf void uLong size
Definition: ioapi.h:39

◆ SerializeSize() [2/2]

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

Definition at line 465 of file matrix.h.

465  {
466  inT32 size = dim1_;
467  if (fp->FWrite(&size, sizeof(size), 1) != 1) return false;
468  size = dim2_;
469  if (fp->FWrite(&size, sizeof(size), 1) != 1) return false;
470  return true;
471  }
int32_t inT32
Definition: host.h:38
voidpf void uLong size
Definition: ioapi.h:39
int FWrite(const void *buffer, int size, int count)
Definition: serialis.cpp:148

◆ SumSquares()

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

Definition at line 363 of file matrix.h.

363  {
364  int size = num_elements();
365  for (int i = 0; i < size; ++i) {
366  array_[i] += src.array_[i] * src.array_[i];
367  }
368  }
voidpf void uLong size
Definition: ioapi.h:39
virtual int num_elements() const
Definition: matrix.h:205

◆ WithinBounds()

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

Definition at line 300 of file matrix.h.

300  {
301  int size = num_elements();
302  for (int i = 0; i < size; ++i) {
303  const T& value = array_[i];
304  if (value < rangemin || rangemax < value)
305  return false;
306  }
307  return true;
308  }
voidpf void uLong size
Definition: ioapi.h:39
virtual int num_elements() const
Definition: matrix.h:205

Member Data Documentation

◆ array_

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

Definition at line 493 of file matrix.h.

◆ dim1_

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

Definition at line 495 of file matrix.h.

◆ dim2_

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

Definition at line 496 of file matrix.h.

◆ empty_

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

Definition at line 494 of file matrix.h.

◆ size_allocated_

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

Definition at line 500 of file matrix.h.


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