mat_morph.cpp

Go to the documentation of this file.
00001 #ifndef MAT_MORPH_IMPLEMENTATION
00002 #define MAT_MORPH_IMPLEMENTATION
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : mat_morph                                                         *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 2003-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 // notes:
00019 //   thanks to aaron buchanan for the insert_row and insert_column methods
00020 // and for assorted clean-ups.
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   // used in bounds_halt macro.
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   // this is only safe because the indices are stored in row-major order (which
00098   // i hope means the right thing).  in any case, the order is like so:
00099   //   1 2 3 4
00100   //   5 6 7 8
00101   // thus we can whack a whole row contiguously.
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   // this starts at the end, which keeps the indexes meaningful.  otherwise
00113   // the destruction interferes with finding the elements.
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   // see comment in zap_row for reasoning about the below.
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   // similarly to zap_column, we must iterate in reverse.
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 

Generated on Fri Nov 28 04:29:12 2008 for HOOPLE Libraries by  doxygen 1.5.1