All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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)
 
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.

46  : empty_(empty), dim1_(dim1), dim2_(dim2), array_(array) {
47  }
int dim2() const
Definition: matrix.h:153
int dim1() const
Definition: matrix.h:152
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.

51  : empty_(empty), dim1_(dim1), dim2_(dim2) {
52  array_ = new T[dim1_ * dim2_];
53  for (int x = 0; x < dim1_; x++)
54  for (int y = 0; y < dim2_; y++)
55  this->put(x, y, empty_);
56  }
int dim2() const
Definition: matrix.h:153
int dim1() const
Definition: matrix.h:152
void put(int column, int row, const T &thing)
Definition: matrix.h:166
template<class T>
virtual GENERIC_2D_ARRAY< T >::~GENERIC_2D_ARRAY ( )
inlinevirtual

Definition at line 57 of file matrix.h.

57 { delete[] array_; }

Member Function Documentation

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

Definition at line 94 of file matrix.h.

94  {
95  int total_size = num_elements();
96  for (int i = 0; i < total_size; ++i)
97  array_[i] = empty_;
98  }
virtual int num_elements() const
Definition: matrix.h:156
template<class T>
void GENERIC_2D_ARRAY< T >::delete_matrix_pointers ( )
inline

Definition at line 191 of file matrix.h.

191  {
192  int size = num_elements();
193  for (int i = 0; i < size; ++i) {
194  T matrix_cell = array_[i];
195  if (matrix_cell != empty_)
196  delete matrix_cell;
197  }
198  }
virtual int num_elements() const
Definition: matrix.h:156
template<class T>
bool GENERIC_2D_ARRAY< T >::DeSerialize ( bool  swap,
FILE *  fp 
)
inline

Definition at line 113 of file matrix.h.

113  {
114  if (!DeSerializeSize(swap, fp)) return false;
115  if (fread(&empty_, sizeof(empty_), 1, fp) != 1) return false;
116  if (swap) ReverseN(&empty_, sizeof(empty_));
117  int size = num_elements();
118  if (fread(array_, sizeof(*array_), size, fp) != size) return false;
119  if (swap) {
120  for (int i = 0; i < size; ++i)
121  ReverseN(&array_[i], sizeof(array_[i]));
122  }
123  return true;
124  }
virtual int num_elements() const
Definition: matrix.h:156
bool DeSerializeSize(bool swap, FILE *fp)
Definition: matrix.h:211
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:177
template<class T>
bool GENERIC_2D_ARRAY< T >::DeSerializeClasses ( bool  swap,
FILE *  fp 
)
inline

Definition at line 141 of file matrix.h.

141  {
142  if (!DeSerializeSize(swap, fp)) return false;
143  if (!empty_.DeSerialize(swap, fp)) return false;
144  int size = num_elements();
145  for (int i = 0; i < size; ++i) {
146  if (!array_[i].DeSerialize(swap, fp)) return false;
147  }
148  return true;
149  }
virtual int num_elements() const
Definition: matrix.h:156
bool DeSerializeSize(bool swap, FILE *fp)
Definition: matrix.h:211
bool DeSerialize(bool swap, FILE *fp)
Definition: matrix.h:113
template<class T>
bool GENERIC_2D_ARRAY< T >::DeSerializeSize ( bool  swap,
FILE *  fp 
)
inlineprotected

Definition at line 211 of file matrix.h.

211  {
212  inT32 size1, size2;
213  if (fread(&size1, sizeof(size1), 1, fp) != 1) return false;
214  if (fread(&size2, sizeof(size2), 1, fp) != 1) return false;
215  if (swap) {
216  ReverseN(&size1, sizeof(size1));
217  ReverseN(&size2, sizeof(size2));
218  }
219  Resize(size1, size2, empty_);
220  return true;
221  }
void Resize(int size1, int size2, const T &empty)
Definition: matrix.h:60
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:177
int inT32
Definition: host.h:102
template<class T>
int GENERIC_2D_ARRAY< T >::dim1 ( ) const
inline

Definition at line 152 of file matrix.h.

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

Definition at line 153 of file matrix.h.

153 { 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.

171  {
172  return array_[this->index(column, row)];
173  }
virtual int index(int column, int row) const
Definition: matrix.h:161
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 161 of file matrix.h.

161  {
162  return (column * dim2_ + row);
163  }
template<class T>
virtual int GENERIC_2D_ARRAY< T >::num_elements ( ) const
inlinevirtual

Definition at line 156 of file matrix.h.

156 { 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.

175  {
176  return array_[this->index(column, row)];
177  }
virtual int index(int column, int row) const
Definition: matrix.h:161
template<class T>
T& GENERIC_2D_ARRAY< T >::operator() ( int  column,
int  row 
)
inline

Definition at line 178 of file matrix.h.

178  {
179  return array_[this->index(column, row)];
180  }
virtual int index(int column, int row) const
Definition: matrix.h:161
template<class T>
T* GENERIC_2D_ARRAY< T >::operator[] ( int  column)
inline

Definition at line 183 of file matrix.h.

183  {
184  return &array_[this->index(column, 0)];
185  }
virtual int index(int column, int row) const
Definition: matrix.h:161
template<class T>
const T* GENERIC_2D_ARRAY< T >::operator[] ( int  column) const
inline

Definition at line 186 of file matrix.h.

186  {
187  return &array_[this->index(column, 0)];
188  }
virtual int index(int column, int row) const
Definition: matrix.h:161
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.

166  {
167  array_[this->index(column, row)] = thing;
168  }
virtual int index(int column, int row) const
Definition: matrix.h:161
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.

60  {
61  empty_ = empty;
62  if (size1 != dim1_ || size2 != dim2_) {
63  dim1_ = size1;
64  dim2_ = size2;
65  delete [] array_;
66  array_ = new T[dim1_ * dim2_];
67  }
68  Clear();
69  }
void Clear()
Definition: matrix.h:94
template<class T>
void GENERIC_2D_ARRAY< T >::ResizeWithCopy ( int  size1,
int  size2 
)
inline

Definition at line 72 of file matrix.h.

72  {
73  if (size1 != dim1_ || size2 != dim2_) {
74  T* new_array = new T[size1 * size2];
75  for (int col = 0; col < size1; ++col) {
76  for (int row = 0; row < size2; ++row) {
77  int old_index = col * dim2() + row;
78  int new_index = col * size2 + row;
79  if (col < dim1_ && row < dim2_) {
80  new_array[new_index] = array_[old_index];
81  } else {
82  new_array[new_index] = empty_;
83  }
84  }
85  }
86  delete[] array_;
87  array_ = new_array;
88  dim1_ = size1;
89  dim2_ = size2;
90  }
91  }
int dim2() const
Definition: matrix.h:153
template<class T>
bool GENERIC_2D_ARRAY< T >::Serialize ( FILE *  fp) const
inline

Definition at line 102 of file matrix.h.

102  {
103  if (!SerializeSize(fp)) return false;
104  if (fwrite(&empty_, sizeof(empty_), 1, fp) != 1) return false;
105  int size = num_elements();
106  if (fwrite(array_, sizeof(*array_), size, fp) != size) return false;
107  return true;
108  }
virtual int num_elements() const
Definition: matrix.h:156
bool SerializeSize(FILE *fp) const
Definition: matrix.h:202
template<class T>
bool GENERIC_2D_ARRAY< T >::SerializeClasses ( FILE *  fp) const
inline

Definition at line 128 of file matrix.h.

128  {
129  if (!SerializeSize(fp)) return false;
130  if (!empty_.Serialize(fp)) return false;
131  int size = num_elements();
132  for (int i = 0; i < size; ++i) {
133  if (!array_[i].Serialize(fp)) return false;
134  }
135  return true;
136  }
virtual int num_elements() const
Definition: matrix.h:156
bool SerializeSize(FILE *fp) const
Definition: matrix.h:202
bool Serialize(FILE *fp) const
Definition: matrix.h:102
template<class T>
bool GENERIC_2D_ARRAY< T >::SerializeSize ( FILE *  fp) const
inlineprotected

Definition at line 202 of file matrix.h.

202  {
203  inT32 size = dim1_;
204  if (fwrite(&size, sizeof(size), 1, fp) != 1) return false;
205  size = dim2_;
206  if (fwrite(&size, sizeof(size), 1, fp) != 1) return false;
207  return true;
208  }
int inT32
Definition: host.h:102

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: