Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members  

DynamicSparseRowMatrix.h

00001 /****************************************************************************
00002  *                                                                          *
00003  *  Program: FDDlib                                                         *
00004  *  Version: 1.1                                                            *
00005  *                                                                          *
00006  *  Copyright (C) 2002 - 2004 by Eric Miller and Dana Brooks                *
00007  *  All rights reserved.                                                    *
00008  *                                                                          *
00009  *  This software is Version 1.1 of the fddlib tomography toolbox.          *
00010  *  It is not to be redistributed or used for any commercial purpose        *
00011  *  without the prior written consent of the authors and Northeastern       *
00012  *  University.                                                             *
00013  *                                                                          *
00014  *  This software is provided as is, and any express or implied warranties, *
00015  *  including but not limited to the implied warranty of merchantability    *
00016  *  and the implied warranty of fitness for a particular purpose, are dis-  *
00017  *  claimed.  In no event shall the authors or Northeastern University be   *
00018  *  liable for any direct, indirect, incidental, special, exemplary, or     *
00019  *  consequential damages (including but not limited to procurement of      * 
00020  *  substitute goods or services; loss of use, data, or profits; or busi-   *
00021  *  ness interruption) however caused and on any theory of liability,       *
00022  *  whether in contract, strict liability, or tort (including negligence or *
00023  *  otherwise) arising in any way from the use of this software, even if    *
00024  *  advised of the possibility of such damage.                              *
00025  *                                                                          *
00026  *  Portions of this code benefit from ideas from Kyle Guilbert, Greg       *
00027  *  Boverman, Derek Uluski, David Kaeli, and Jennifer Black                 *
00028  *                                                                          *
00029  ****************************************************************************/
00030 
00031 #ifndef _DynamicSparseRowMatrix_H_
00032 #define _DynamicSparseRowMatrix_H_
00033 
00034 #include <stdlib.h>
00035 #include "SparseRowMatrix.h"
00036 #include "error.h"
00037 
00038 namespace FDDlib {
00039 
00049 template <class T>
00050 class DynamicSparseRowMatrix : public SparseRowMatrix<T>
00051 {
00052 private:
00054   SparseRowEntry<T>* setHelper(int row, int col);
00055 
00056 public:
00058   DynamicSparseRowMatrix();
00059 
00061   DynamicSparseRowMatrix(int maxrows, int max_per_row);
00062 
00064   DynamicSparseRowMatrix(const DynamicSparseRowMatrix<T>& ESR);
00065 
00067   int set(int row, int col, T val);
00068 
00078   int setEntriesPerRow(int newmax);
00079 
00080 };
00081 
00082 
00083 template<class T>
00084 DynamicSparseRowMatrix<T>::DynamicSparseRowMatrix() :
00085   SparseRowMatrix<T>()
00086 {}
00087 
00088 template<class T>
00089 DynamicSparseRowMatrix<T>::DynamicSparseRowMatrix(int maxrows,int max_per_row):
00090   SparseRowMatrix<T>(maxrows, max_per_row)
00091 {}
00092 
00093 template<class T>
00094 DynamicSparseRowMatrix<T>::DynamicSparseRowMatrix(const
00095   DynamicSparseRowMatrix<T>& ESR) : SparseRowMatrix<T>(ESR)
00096 {}
00097 
00098 template<class T>
00099 inline int DynamicSparseRowMatrix<T>::setEntriesPerRow(int newmax)
00100 {
00101   int i, j;
00102   const int oldmax = EntriesPerRow_;
00103   const int newlen = MaxRows_*newmax;
00104   const int arrlendiff = (newlen)-(MaxRows_*oldmax);
00105   int offset = 0;
00106   void* newaddr;
00107 
00108   if (newmax <= oldmax)
00109     return ERR_ARGS;
00110 
00111   // reallocate space
00112   newaddr = realloc(Entries_, sizeof(SparseRowEntry<T>)*newlen);
00113 
00114   // if realloc fails
00115   if (newaddr == NULL) return ERR_DATA;
00116   else Entries_ = (struct SparseRowEntry<T>*)newaddr;
00117 
00118   // copy all the entries into their new positions,
00119   // starting from the rightmost row chunk.
00120   for (i = (MaxRows_*oldmax)-1; i >= oldmax; i -= oldmax) {
00121     for (j = i; j > (i-oldmax); j--) {
00122       Entries_[j + (arrlendiff-(newmax-oldmax)) - offset] = Entries_[j];
00123     }
00124     // increase the offset. this makes sure the next row entries
00125     // are moved over the correct number of positions.
00126     offset += (newmax-oldmax);
00127   }
00128 
00129   EntriesPerRow_ = newmax;
00130   return SUCCESS;
00131 }
00132 
00133 template<class T>
00134 inline SparseRowEntry<T>* DynamicSparseRowMatrix<T>::setHelper(int row, int col)
00135 {
00136   int i;
00137   SparseRowEntry<T>* ep;
00138 
00139   ep = getRowEntry(row);
00140   for (i = 0; i < NumCols_[row] && ep->column != col; i++)
00141   {
00142     // if (ep->column == col)   
00143       // break;
00144     ep++;
00145   }
00146   // Removed if-break from body of loop and added && ep->column != col
00147   // to eliminate use of break; statement.  1/15/04 Jennifer Black
00148 
00149   return ep;
00150 }
00151 
00152 template<class T>
00153 inline int DynamicSparseRowMatrix<T>::set(int row, int col, T val)
00154 {
00155   int       i;
00156   SparseRowEntry<T>* ep;
00157   SparseRowEntry<T>* r;
00158 
00159   ep = setHelper(row, col);
00160   r = getRowEntry(row);
00161 
00162   // if we have reached the maximum entries per row
00163   if (ep - r >= EntriesPerRow_) {
00164     if (setEntriesPerRow(EntriesPerRow_+1)) return ERR_DATA;
00165 
00166     // We now have enough space to put in the new entry.
00167     // Since the entries are all rearranged, we need to
00168     // get the row entry and insertion point again.
00169     ep = setHelper(row, col);
00170     r = getRowEntry(row);
00171 
00172   }
00173 
00174   // increment column count if necessary
00175   if (ep - r == NumCols_[row])
00176     (NumCols_[row])++;
00177 
00178   ep->val = val;
00179   ep->column = col;
00180 
00181   return SUCCESS;
00182 
00183 }
00184 
00185 } // end namespace
00186 
00187 #endif

Generated on Mon Aug 30 15:41:06 2004 for FDDLib by doxygen1.2.18