/*
 $Id: matrix.c,v 1.1 1996/10/10 10:01:19 roitzsch Exp $
 (C)opyright 1996 by Konrad-Zuse-Center, Berlin
 All rights reserved.
 Part of the Kaskade distribution
*/

#include "matrix.h"


template<class T> void Matrix<T>:: allocate (int rl1, int rh1, int cl1, int ch1) 
{									 
    rl=rl1;  rh=rh1;  cl=cl1;  ch=ch1;				 
    row = new T*[rh-rl+1];					 
    if (!row) { cout << "\n*** class Matrix: allocation failure\n"; cout.flush(); abort(); }
    row -= rl;							 
    row[rl] = new T[(rh-rl+1)*(ch-cl+1)];			 
    if (!row[rl]) { cout << "\n*** class Matrix: allocation failure\n" ;cout.flush(); abort();}
    row[rl] -= cl;							 
    for (int i=rl+1; i<=rh; ++i)  row[i] = row[i-1]+ch-cl+1;  	 
}		
							 
template<class T> Matrix<T>:: ~Matrix() 
{ 
    delete [] (row[rl]+cl);  
    delete (row+rl); 
}	 

template<class T> void Matrix<T>:: resize (int newrh, int newch)
{
    resize (1, newrh, 1, newch);
}

template<class T> void Matrix<T>:: resize (int newrl, int newrh, 
					   int newcl, int newch)
{
    delete (row[rl]+cl);  
    delete (row+rl);
    allocate(newrl, newrh, newcl, newch);
}							 

/*
  template<class T> Matrix<T>& Matrix<T>:: operator= (Matrix<T>& m)
  {
  int i,j; 
  for(i=rl; i<=rh; ++i) 
  for(j=cl; j<=ch; ++j) (*this)(i,j) = m(i,j);  
  return *this; 
  }    

  template<class T> Matrix<T>& Matrix<T>::operator= (T a)
  {		
  int i,j; 
  for(i=rl; i<=rh; ++i) 
  for(j=cl; j<=ch; ++j) (*this)(i,j) = a;
  return *this; 
  }
*/

template<class T> int Matrix<T>:: size()    { return (rh-rl+1)*(ch-cl+1); }
template<class T> int Matrix<T>:: memSize() { return (rh-rl+1)*(ch-cl+1)*
								sizeof(T); }

template<class T> void Matrix<T>:: checkBounds(int i, int j) const
{
    if (i<rl || i>rh || j<cl || j>ch)	
    {
	cout << "\n*** matrix index ("<<i<<","<<j<<") out of range\n"; 
	cout.flush(); abort();
    }
}
template<class T> void Matrix<T>:: checkBounds(int i) const
{
    if (i<rl || i>rh)	
    {
	cout << "\n*** matrix row index ("<<i<<") out of range\n"; 
	cout.flush(); abort();
    }
}

template<class T> void Matrix<T>:: print() const
{									 
     int i, j;								 
     for (i=rl; i<=rh; ++i) {	
		cout << "\n";						 
		for (j=cl; j<=ch; ++j)  cout << row[i][j] << " ";	 
     } 
     cout << "\n";
}	
//-------------------------------------------------------------------------

template<class T> Matrix<T>:: Matrix(Matrix<T>& /*vec*/)
{
    notImplemented("Matrix:: copy constructor! Check function arguments");
}
	
template<class T> Matrix<T>& Matrix<T>:: operator= (Matrix<T>& /*vec*/)
{
    notImplemented("Matrix:: operator=(Matrix<T>& vec)"); 
    return *this; 
    // int i; for(i=l; i<=h; ++i) v[i]= vec.v[i]; return *this; 
}		

template<class T> Matrix<T>& Matrix<T>:: operator= (T /*a*/)		
{     
    notImplemented("Matrix:: operator=(T a)"); 
    return *this; 
    //int i; for(i=l; i<=h; ++i) v[i]= a; return *this; 
}               
                                                  
template<class T> void Matrix<T>:: notImplemented(const char* s) const
{
    cout << "\n*** Class Matrix: called function " 
    					<< s << " not implemented\n";
    cout.flush(); abort();

}


syntax highlighted by Code2HTML, v. 0.9.1