tesseract  3.03
GENERIC_2D_ARRAY< T > Class Template Reference

#include <matrix.h>

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

List of all members.

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)
virtual ~GENERIC_2D_ARRAY ()
void Resize (int size1, int size2, const T &empty)
void ResizeWithCopy (int size1, int size2)
void Clear ()
bool Serialize (FILE *fp) const
bool DeSerialize (bool swap, FILE *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 (int column, int row, const T &thing)
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 delete_matrix_pointers ()

Protected Member Functions

bool SerializeSize (FILE *fp) const
bool DeSerializeSize (bool swap, FILE *fp)

Protected Attributes

T * array_
empty_
int dim1_
int dim2_

Detailed Description

template<class T>
class GENERIC_2D_ARRAY< T >

Definition at line 39 of file matrix.h.


Constructor & Destructor Documentation

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

Definition at line 45 of file matrix.h.

    : empty_(empty), dim1_(dim1), dim2_(dim2), array_(array)  {
  }
template<class T>
GENERIC_2D_ARRAY< T >::GENERIC_2D_ARRAY ( int  dim1,
int  dim2,
const T &  empty 
) [inline]

Definition at line 50 of file matrix.h.

    : empty_(empty), dim1_(dim1), dim2_(dim2)  {
    array_ = new T[dim1_ * dim2_];
    for (int x = 0; x < dim1_; x++)
      for (int y = 0; y < dim2_; y++)
        this->put(x, y, empty_);
  }
template<class T>
virtual GENERIC_2D_ARRAY< T >::~GENERIC_2D_ARRAY ( ) [inline, virtual]

Definition at line 57 of file matrix.h.

{ delete[] array_; }

Member Function Documentation

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

Definition at line 94 of file matrix.h.

               {
    int total_size = num_elements();
    for (int i = 0; i < total_size; ++i)
      array_[i] = empty_;
  }
template<class T>
void GENERIC_2D_ARRAY< T >::delete_matrix_pointers ( ) [inline]

Definition at line 191 of file matrix.h.

                                {
    int size = num_elements();
    for (int i = 0; i < size; ++i) {
      T matrix_cell = array_[i];
      if (matrix_cell != empty_)
        delete matrix_cell;
    }
  }
template<class T>
bool GENERIC_2D_ARRAY< T >::DeSerialize ( bool  swap,
FILE *  fp 
) [inline]

Definition at line 113 of file matrix.h.

                                        {
    if (!DeSerializeSize(swap, fp)) return false;
    if (fread(&empty_, sizeof(empty_), 1, fp) != 1) return false;
    if (swap) ReverseN(&empty_, sizeof(empty_));
    int size = num_elements();
    if (fread(array_, sizeof(*array_), size, fp) != size) return false;
    if (swap) {
      for (int i = 0; i < size; ++i)
        ReverseN(&array_[i], sizeof(array_[i]));
    }
    return true;
  }
template<class T>
bool GENERIC_2D_ARRAY< T >::DeSerializeClasses ( bool  swap,
FILE *  fp 
) [inline]

Definition at line 141 of file matrix.h.

                                               {
    if (!DeSerializeSize(swap, fp)) return false;
    if (!empty_.DeSerialize(swap, fp)) return false;
    int size = num_elements();
    for (int i = 0; i < size; ++i) {
      if (!array_[i].DeSerialize(swap, fp)) return false;
    }
    return true;
  }
template<class T>
bool GENERIC_2D_ARRAY< T >::DeSerializeSize ( bool  swap,
FILE *  fp 
) [inline, protected]

Definition at line 211 of file matrix.h.

                                            {
    inT32 size1, size2;
    if (fread(&size1, sizeof(size1), 1, fp) != 1) return false;
    if (fread(&size2, sizeof(size2), 1, fp) != 1) return false;
    if (swap) {
      ReverseN(&size1, sizeof(size1));
      ReverseN(&size2, sizeof(size2));
    }
    Resize(size1, size2, empty_);
    return true;
  }
template<class T>
int GENERIC_2D_ARRAY< T >::dim1 ( ) const [inline]

Definition at line 152 of file matrix.h.

{ return dim1_; }
template<class T>
int GENERIC_2D_ARRAY< T >::dim2 ( ) const [inline]

Definition at line 153 of file matrix.h.

{ return dim2_; }
template<class T>
T GENERIC_2D_ARRAY< T >::get ( int  column,
int  row 
) const [inline]

Definition at line 171 of file matrix.h.

                                   {
    return array_[this->index(column, row)];
  }
template<class T>
virtual int GENERIC_2D_ARRAY< T >::index ( int  column,
int  row 
) const [inline, virtual]

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

Definition at line 161 of file matrix.h.

                                               {
    return (column * dim2_ + row);
  }
template<class T>
virtual int GENERIC_2D_ARRAY< T >::num_elements ( ) const [inline, virtual]

Definition at line 156 of file matrix.h.

{ return dim1_ * dim2_; }
template<class T>
const T& GENERIC_2D_ARRAY< T >::operator() ( int  column,
int  row 
) const [inline]

Definition at line 175 of file matrix.h.

                                                 {
    return array_[this->index(column, row)];
  }
template<class T>
T& GENERIC_2D_ARRAY< T >::operator() ( int  column,
int  row 
) [inline]

Definition at line 178 of file matrix.h.

                                     {
    return array_[this->index(column, row)];
  }
template<class T>
T* GENERIC_2D_ARRAY< T >::operator[] ( int  column) [inline]

Definition at line 183 of file matrix.h.

                            {
    return &array_[this->index(column, 0)];
  }
template<class T>
const T* GENERIC_2D_ARRAY< T >::operator[] ( int  column) const [inline]

Definition at line 186 of file matrix.h.

                                        {
    return &array_[this->index(column, 0)];
  }
template<class T>
void GENERIC_2D_ARRAY< T >::put ( int  column,
int  row,
const T &  thing 
) [inline]

Definition at line 166 of file matrix.h.

                                                {
    array_[this->index(column, row)] = thing;
  }
template<class T>
void GENERIC_2D_ARRAY< T >::Resize ( int  size1,
int  size2,
const T &  empty 
) [inline]

Definition at line 60 of file matrix.h.

                                                    {
    empty_ = empty;
    if (size1 != dim1_ || size2 != dim2_) {
      dim1_ = size1;
      dim2_ = size2;
      delete [] array_;
      array_ = new T[dim1_ * dim2_];
    }
    Clear();
  }
template<class T>
void GENERIC_2D_ARRAY< T >::ResizeWithCopy ( int  size1,
int  size2 
) [inline]

Definition at line 72 of file matrix.h.

                                            {
    if (size1 != dim1_ || size2 != dim2_) {
      T* new_array = new T[size1 * size2];
      for (int col = 0; col < size1; ++col) {
        for (int row = 0; row < size2; ++row) {
          int old_index = col * dim2() + row;
          int new_index = col * size2 + row;
          if (col < dim1_ && row < dim2_) {
            new_array[new_index] = array_[old_index];
          } else {
            new_array[new_index] = empty_;
          }
        }
      }
      delete[] array_;
      array_ = new_array;
      dim1_ = size1;
      dim2_ = size2;
    }
  }
template<class T>
bool GENERIC_2D_ARRAY< T >::Serialize ( FILE *  fp) const [inline]

Definition at line 102 of file matrix.h.

                                 {
    if (!SerializeSize(fp)) return false;
    if (fwrite(&empty_, sizeof(empty_), 1, fp) != 1) return false;
    int size = num_elements();
    if (fwrite(array_, sizeof(*array_), size, fp) != size) return false;
    return true;
  }
template<class T>
bool GENERIC_2D_ARRAY< T >::SerializeClasses ( FILE *  fp) const [inline]

Definition at line 128 of file matrix.h.

                                        {
    if (!SerializeSize(fp)) return false;
    if (!empty_.Serialize(fp)) return false;
    int size = num_elements();
    for (int i = 0; i < size; ++i) {
      if (!array_[i].Serialize(fp)) return false;
    }
    return true;
  }
template<class T>
bool GENERIC_2D_ARRAY< T >::SerializeSize ( FILE *  fp) const [inline, protected]

Definition at line 202 of file matrix.h.

                                     {
    inT32 size = dim1_;
    if (fwrite(&size, sizeof(size), 1, fp) != 1) return false;
    size = dim2_;
    if (fwrite(&size, sizeof(size), 1, fp) != 1) return false;
    return true;
  }

Member Data Documentation

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

Definition at line 223 of file matrix.h.

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

Definition at line 225 of file matrix.h.

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

Definition at line 226 of file matrix.h.

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

Definition at line 224 of file matrix.h.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines