00001 #ifndef MAT_MORPH_IMPLEMENTATION
00002 #define MAT_MORPH_IMPLEMENTATION
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "amorph.cpp"
00023 #include "mat_morph.h"
00024
00025 #include <basis/function.h>
00026 #include <basis/guards.h>
00027
00028 #undef static_class_name
00029 #define static_class_name() "mat_morph"
00030
00031
00032 template <class contents>
00033 mat_morph<contents>::mat_morph(int r, int c)
00034 : amorph<contents>(r*c), _rows(r), _cols(c) {}
00035
00036 template <class contents>
00037 const contents *mat_morph<contents>::get(int r, int c) const
00038 { return amorph<contents>::get(compute_index(r, c)); }
00039
00040 template <class contents>
00041 contents *mat_morph<contents>::get(int row, int column)
00042 { return amorph<contents>::operator [] (compute_index(row, column)); }
00043
00044 template <class contents>
00045 contents *mat_morph<contents>::acquire(int r, int c)
00046 { return amorph<contents>::acquire(compute_index(r, c)); }
00047
00048 template <class contents>
00049 int mat_morph<contents>::compute_index(int row, int column) const
00050 { return column + row * _cols; }
00051
00052 template <class contents>
00053 void mat_morph<contents>::reset(int rows_in, int columns_in)
00054 {
00055 amorph<contents>::clear_all();
00056 amorph<contents>::reset(rows_in * columns_in);
00057 _rows = rows_in;
00058 _cols = columns_in;
00059 }
00060
00061 template <class contents>
00062 void mat_morph<contents>::redimension(int new_rows, int new_columns)
00063 {
00064 if ( (_rows == new_rows) && (_cols == new_columns) ) return;
00065 mat_morph<contents> new_this(new_rows, new_columns);
00066 int min_rows = minimum(new_rows, rows());
00067 int min_cols = minimum(new_columns, columns());
00068 for (int r1 = 0; r1 < min_rows; r1++)
00069 for (int c1 = 0; c1 < min_cols; c1++) {
00070 new_this.put(r1, c1, amorph<contents>::acquire(compute_index(r1, c1)));
00071 }
00072 amorph<contents>::reset(new_rows * new_columns);
00073 _rows = new_rows;
00074 _cols = new_columns;
00075 for (int r2 = 0; r2 < min_rows; r2++) {
00076 for (int c2 = 0; c2 < min_cols; c2++) {
00077 amorph<contents>::put(compute_index(r2, c2), new_this.acquire(r2, c2));
00078 }
00079 }
00080 }
00081
00082 template <class contents>
00083 bool mat_morph<contents>::put(int row, int column, const contents *to_put)
00084 {
00085 if ( (row >= rows()) || (column >= columns()) )
00086 return false;
00087 amorph<contents>::put(compute_index(row, column), to_put);
00088 return true;
00089 }
00090
00091 template <class contents>
00092 bool mat_morph<contents>::zap_row(int row_to_zap)
00093 {
00094 FUNCDEF("zap_row");
00095 bounds_halt(row_to_zap, 0, rows() - 1, false);
00096 const int start = compute_index(row_to_zap, 0);
00097
00098
00099
00100
00101
00102 amorph<contents>::zap(start, start + columns() - 1);
00103 _rows--;
00104 return true;
00105 }
00106
00107 template <class contents>
00108 bool mat_morph<contents>::zap_column(int column_to_zap)
00109 {
00110 FUNCDEF("zap_column");
00111 bounds_halt(column_to_zap, 0, columns() - 1, false);
00112
00113
00114 for (int r = rows(); r >= 0; r--) {
00115 const int loc = compute_index(r, column_to_zap);
00116 amorph<contents>::zap(loc, loc);
00117 }
00118 _cols--;
00119 return true;
00120 }
00121
00122 template <class contents>
00123 bool mat_morph<contents>::insert_row(int position)
00124 {
00125 FUNCDEF("insert_row");
00126 bounds_halt(position, 0, rows(), false);
00127
00128 amorph<contents>::insert(compute_index(position, 0), columns());
00129 _rows++;
00130 return true;
00131 }
00132
00133 template <class contents>
00134 bool mat_morph<contents>::insert_column(int position)
00135 {
00136 FUNCDEF("insert_column");
00137 bounds_halt(position, 0, columns(), false);
00138
00139 for (int r = rows(); r >= 0; r--)
00140 amorph<contents>::insert(compute_index(r, position), 1);
00141 _cols++;
00142 return true;
00143 }
00144
00145 #undef static_class_name
00146
00147 #endif
00148