SparseMatrix< T > Class Template Reference

#include <SparseMatrix.h>

List of all members.


Detailed Description

template<class T>
class SparseMatrix< T >

Sparse 2-D Matrix class. Currently only element storage/access is supported.

Definition at line 82 of file SparseMatrix.h.

Public Member Functions

 SparseMatrix (void)
 SparseMatrix (const int xdim, const int ydim)
 ~SparseMatrix (void)
const int Columns () const
const int Rows () const
const int XDim () const
const int YDim () const
int const NNZ (void) const
void SetValue (const int x, const int y, const T value)
GetValue (const int x, const int y)
operator() (const int i, const int j)

Protected Member Functions

bool AddCellXStart (matrixCell< T > *c)
bool AddCellYStart (matrixCell< T > *c)
GetCellXStart (const int x, const int y)
GetCellYStart (const int x, const int y)
bool DeleteCell (const int x, const int y)

Protected Attributes

matrixCellStart< T > * xDimStarts
matrixCellStart< T > * yDimStarts
int ndims
int * dims
int count


Constructor & Destructor Documentation

template<class T>
SparseMatrix< T >::SparseMatrix void   ) 
 

Definition at line 63 of file SparseMatrix.inl.

References Utility::CheckPointer(), SparseMatrix< T >::dims, SparseMatrix< T >::xDimStarts, and SparseMatrix< T >::yDimStarts.

00064     : ndims(2), count(0)
00065 {
00066     xDimStarts = 0;
00067     yDimStarts = 0;
00068 
00069     dims = new int[ndims];
00070     Utility::CheckPointer(dims);
00071     for(int j=0;j<ndims;j++)
00072     {
00073         dims[j] = 0;
00074     }
00075     
00076 }

template<class T>
SparseMatrix< T >::SparseMatrix const int  xdim,
const int  ydim
 

Definition at line 82 of file SparseMatrix.inl.

References Utility::CheckPointer(), SparseMatrix< T >::count, SparseMatrix< T >::dims, SparseMatrix< T >::ndims, Utility::RunTimeError(), SparseMatrix< T >::xDimStarts, and SparseMatrix< T >::yDimStarts.

00083 {
00084     if(xdim < 1 || ydim < 1)
00085     {
00086         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00087         Utility::RunTimeError("All array dimensions should be larger than 1!");
00088     }
00089 
00090     ndims = 2;
00091     count = 0;
00092     dims = new int[ndims];
00093     Utility::CheckPointer(dims);
00094     dims[0] = xdim;
00095     dims[1] = ydim;
00096 
00097     xDimStarts = new matrixCellStart<T>[xdim];
00098     Utility::CheckPointer(xDimStarts);
00099     for(int i=0; i<xdim; i++)
00100     {
00101         xDimStarts[i].listCount = 0;
00102         xDimStarts[i].first = new matrixCell<T>;
00103         xDimStarts[i].first->xNext = 0;
00104         xDimStarts[i].first->yNext = 0;
00105     }
00106 
00107     yDimStarts = new matrixCellStart<T>[ydim];
00108     Utility::CheckPointer(yDimStarts);
00109     for(int i=0; i<ydim; i++)
00110     {
00111         yDimStarts[i].listCount = 0;
00112         yDimStarts[i].first = new matrixCell<T>;
00113         yDimStarts[i].first->xNext = 0;
00114         yDimStarts[i].first->yNext = 0;
00115     }
00116     
00117     
00118 }

template<class T>
SparseMatrix< T >::~SparseMatrix void   ) 
 

Definition at line 126 of file SparseMatrix.inl.

References SparseMatrix< T >::XDim(), SparseMatrix< T >::xDimStarts, SparseMatrix< T >::YDim(), SparseMatrix< T >::yDimStarts, and matrixCell< T >::yNext.

00127 {
00128 
00129     
00130 
00131     for(int i=0; i<XDim(); i++)
00132     {
00133         matrixCell<T> *temp = xDimStarts[i].first;
00134         while(temp != 0)
00135         {
00136             matrixCell<T> *c = temp->yNext;
00137             delete temp;
00138             temp = c;
00139         }
00140     }
00141     delete [] xDimStarts;
00142     
00143     for(int i=0; i<YDim(); i++)
00144     {
00145         delete yDimStarts[i].first;
00146     }
00147     delete [] yDimStarts;
00148     delete [] dims;
00149 
00150     
00151 }


Member Function Documentation

template<class T>
bool SparseMatrix< T >::AddCellXStart matrixCell< T > *  c  )  [protected]
 

Definition at line 331 of file SparseMatrix.inl.

References matrixCell< T >::value, matrixCell< T >::x, SparseMatrix< T >::xDimStarts, matrixCell< T >::y, and matrixCell< T >::yNext.

Referenced by SparseMatrix< T >::SetValue().

00332 {
00333     matrixCell<T> *temp = xDimStarts[c->x].first;
00334     while(temp->yNext != 0)
00335     {
00336         if(temp->yNext->y == c->y)
00337         {
00338             temp->yNext->value = c->value;
00339             delete c;
00340             return true;
00341         }
00342         else if(temp->yNext->y > c->y)
00343         {
00344             c->yNext = temp->yNext;
00345             temp->yNext = c;
00346             return false;
00347         }
00348         
00349         temp = temp->yNext;
00350     }
00351     
00352     c->yNext = 0;
00353     temp->yNext = c;
00354     return false;
00355 }

template<class T>
bool SparseMatrix< T >::AddCellYStart matrixCell< T > *  c  )  [protected]
 

Definition at line 302 of file SparseMatrix.inl.

References matrixCell< T >::value, matrixCell< T >::x, matrixCell< T >::xNext, matrixCell< T >::y, and SparseMatrix< T >::yDimStarts.

Referenced by SparseMatrix< T >::SetValue().

00303 {
00304     matrixCell<T> *temp = yDimStarts[c->y].first;
00305     while(temp->xNext != 0)
00306     {
00307         if(temp->xNext->x == c->x)
00308         {
00309             temp->xNext->value = c->value;
00310             delete c;
00311             return true;
00312         }
00313         else if(temp->xNext->x > c->x)
00314         {
00315             c->xNext = temp->xNext;
00316             temp->xNext = c;
00317             return false;
00318         }
00319         
00320         temp = temp->xNext;
00321     }
00322     
00323     c->xNext = 0;
00324     temp->xNext = c;
00325     return false;
00326 }

template<class T>
const int SparseMatrix< T >::Columns  )  const [inline]
 

Definition at line 158 of file SparseMatrix.inl.

References SparseMatrix< T >::dims.

00159 {
00160     return dims[0];
00161 }

template<class T>
bool SparseMatrix< T >::DeleteCell const int  x,
const int  y
[protected]
 

Definition at line 402 of file SparseMatrix.inl.

References matrixCell< T >::x, SparseMatrix< T >::xDimStarts, matrixCell< T >::xNext, matrixCell< T >::y, SparseMatrix< T >::yDimStarts, and matrixCell< T >::yNext.

Referenced by SparseMatrix< T >::SetValue().

00403 {
00404     matrixCell<T> *c = 0;
00405     bool deleted = false;
00406     
00407     matrixCell<T> *temp = yDimStarts[y].first;
00408     while(temp->xNext != 0)
00409     {
00410         if(temp->xNext->x == x)
00411         {
00412             c = temp->xNext;
00413             temp->xNext = c->xNext;
00414             deleted = true;
00415             break;
00416         }
00417         else if(temp->xNext->x > c->x)
00418         {
00419             return false;
00420         }
00421 
00422         temp = temp->xNext;
00423     }
00424 
00425     if(deleted == false)
00426     {
00427         return false;
00428     }
00429 
00430     temp = xDimStarts[x].first;
00431     while(temp->yNext != 0)
00432     {
00433         if(temp->yNext->y == y)
00434         {
00435             c = temp->yNext;
00436             temp->yNext = c->yNext;
00437             deleted = true;
00438             break;
00439         }
00440         else if(temp->yNext->y > c->y)
00441         {
00442             return false;
00443         }
00444         
00445         temp = temp->yNext;
00446     }
00447     if(deleted == false)
00448     {
00449         return false;
00450     }
00451 
00452     delete c;
00453     return true;
00454 }

template<class T>
T SparseMatrix< T >::GetCellXStart const int  x,
const int  y
[protected]
 

Definition at line 380 of file SparseMatrix.inl.

References matrixCell< T >::value, SparseMatrix< T >::xDimStarts, matrixCell< T >::y, and matrixCell< T >::yNext.

Referenced by SparseMatrix< T >::GetValue().

00381 {
00382     matrixCell<T> *temp = xDimStarts[x].first;
00383     while(temp->yNext != 0)
00384     {
00385         if(temp->yNext->y == y)
00386         {
00387             return temp->yNext->value;
00388         }
00389         else if(temp->yNext->y > y)
00390         {
00391             return 0;
00392         }
00393 
00394         temp = temp->yNext;
00395     }
00396     
00397     return 0;
00398 }

template<class T>
T SparseMatrix< T >::GetCellYStart const int  x,
const int  y
[protected]
 

Definition at line 359 of file SparseMatrix.inl.

References matrixCell< T >::value, matrixCell< T >::x, matrixCell< T >::xNext, and SparseMatrix< T >::yDimStarts.

Referenced by SparseMatrix< T >::GetValue().

00360 {
00361     matrixCell<T> *temp = yDimStarts[y].first;
00362     while(temp->xNext != 0)
00363     {
00364         if(temp->xNext->x == x)
00365         {
00366             return temp->xNext->value;
00367         }
00368         else if(temp->xNext->x > x)
00369         {
00370             return 0;
00371         }
00372         
00373         temp = temp->xNext;
00374     }
00375     
00376     return 0;
00377 }

template<class T>
T SparseMatrix< T >::GetValue const int  x,
const int  y
[inline]
 

Definition at line 250 of file SparseMatrix.inl.

References SparseMatrix< T >::dims, SparseMatrix< T >::GetCellXStart(), SparseMatrix< T >::GetCellYStart(), Utility::RunTimeError(), SparseMatrix< T >::xDimStarts, and SparseMatrix< T >::yDimStarts.

Referenced by SparseMatrix< T >::operator()().

00251 {
00252     if(x<0 || x>=dims[0] || y<0 || y>=dims[1])
00253     {
00254         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00255         Utility::RunTimeError("Index outside bounds!");
00256     }
00257 
00258     if( xDimStarts[x].listCount > yDimStarts[y].listCount )  // less elements for row scan
00259     {
00260         return GetCellYStart(x, y);
00261     }
00262     else // less elements for column scan
00263     {
00264         return GetCellXStart(x, y);
00265     }
00266 }

template<class T>
const int SparseMatrix< T >::NNZ void   )  const [inline]
 

Definition at line 190 of file SparseMatrix.inl.

00191 {
00192     return count;
00193 }

template<class T>
T SparseMatrix< T >::operator() const int  i,
const int  j
[inline]
 

Definition at line 275 of file SparseMatrix.inl.

References SparseMatrix< T >::GetValue().

00276 {
00277     return GetValue(i, j);
00278 }

template<class T>
const int SparseMatrix< T >::Rows  )  const [inline]
 

Definition at line 164 of file SparseMatrix.inl.

References SparseMatrix< T >::dims.

00165 {
00166     return dims[1];
00167 }

template<class T>
void SparseMatrix< T >::SetValue const int  x,
const int  y,
const T  value
[inline]
 

Definition at line 197 of file SparseMatrix.inl.

References SparseMatrix< T >::AddCellXStart(), SparseMatrix< T >::AddCellYStart(), SparseMatrix< T >::count, SparseMatrix< T >::DeleteCell(), SparseMatrix< T >::dims, Utility::RunTimeError(), matrixCell< T >::value, matrixCell< T >::x, SparseMatrix< T >::xDimStarts, matrixCell< T >::xNext, matrixCell< T >::y, SparseMatrix< T >::yDimStarts, and matrixCell< T >::yNext.

00198 {
00199     if(x<0 || x>=dims[0] || y<0 || y>=dims[1])
00200     {
00201         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00202         Utility::RunTimeError("Index outside bounds!");
00203     }
00204 
00205     if(value == 0)
00206     {
00207         bool deleted = DeleteCell(x, y);
00208         if(deleted)
00209         {
00210             xDimStarts[x].listCount--;
00211             yDimStarts[y].listCount--;
00212             count--;
00213         }
00214         return;
00215     }
00216 
00217 
00218     matrixCell<T> *c = new matrixCell<T>;
00219     c->value = value;
00220     c->x = x;
00221     c->y = y;
00222     c->xNext = 0;
00223     c->yNext = 0;
00224 
00225     if( xDimStarts[x].listCount > yDimStarts[y].listCount )  // less elements for row scan
00226     {
00227         bool exists = AddCellYStart(c);
00228         if(!exists)
00229         {
00230             AddCellXStart(c);
00231             xDimStarts[x].listCount++;
00232             yDimStarts[y].listCount++;
00233             count++;
00234         }
00235     }
00236     else // less elements for column scan
00237     {
00238         bool exists = AddCellXStart(c);
00239         if(!exists)
00240         {
00241             AddCellYStart(c);
00242             xDimStarts[x].listCount++;
00243             yDimStarts[y].listCount++;
00244             count++;
00245         }
00246     }
00247 }

template<class T>
const int SparseMatrix< T >::XDim  )  const [inline]
 

Definition at line 171 of file SparseMatrix.inl.

References SparseMatrix< T >::dims.

Referenced by SparseMatrix< T >::~SparseMatrix().

00172 {
00173     return dims[0];
00174 }

template<class T>
const int SparseMatrix< T >::YDim  )  const [inline]
 

Definition at line 177 of file SparseMatrix.inl.

References SparseMatrix< T >::dims, SparseMatrix< T >::ndims, and Utility::RunTimeError().

Referenced by SparseMatrix< T >::~SparseMatrix().

00178 {
00179     if(ndims < 2)
00180     {
00181         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00182         Utility::RunTimeError("YDim(): Number of dimensions is less than 2!");
00183     }
00184     return dims[1];
00185 }


Member Data Documentation

template<class T>
int SparseMatrix< T >::count [protected]
 

Definition at line 89 of file SparseMatrix.h.

Referenced by SparseMatrix< T >::SetValue(), and SparseMatrix< T >::SparseMatrix().

template<class T>
int* SparseMatrix< T >::dims [protected]
 

Definition at line 88 of file SparseMatrix.h.

Referenced by SparseMatrix< T >::Columns(), SparseMatrix< T >::GetValue(), SparseMatrix< T >::Rows(), SparseMatrix< T >::SetValue(), SparseMatrix< T >::SparseMatrix(), SparseMatrix< T >::XDim(), and SparseMatrix< T >::YDim().

template<class T>
int SparseMatrix< T >::ndims [protected]
 

Definition at line 87 of file SparseMatrix.h.

Referenced by SparseMatrix< T >::SparseMatrix(), and SparseMatrix< T >::YDim().

template<class T>
matrixCellStart<T>* SparseMatrix< T >::xDimStarts [protected]
 

Definition at line 85 of file SparseMatrix.h.

Referenced by SparseMatrix< T >::AddCellXStart(), SparseMatrix< T >::DeleteCell(), SparseMatrix< T >::GetCellXStart(), SparseMatrix< T >::GetValue(), SparseMatrix< T >::SetValue(), SparseMatrix< T >::SparseMatrix(), and SparseMatrix< T >::~SparseMatrix().

template<class T>
matrixCellStart<T>* SparseMatrix< T >::yDimStarts [protected]
 

Definition at line 86 of file SparseMatrix.h.

Referenced by SparseMatrix< T >::AddCellYStart(), SparseMatrix< T >::DeleteCell(), SparseMatrix< T >::GetCellYStart(), SparseMatrix< T >::GetValue(), SparseMatrix< T >::SetValue(), SparseMatrix< T >::SparseMatrix(), and SparseMatrix< T >::~SparseMatrix().


The documentation for this class was generated from the following files:
CIMPL 0.1 Code Reference. Copyright © 2004, Baris Sumengen. All rights reserved.