Category : C++ Source Code
Archive   : BLFMATH.ZIP
Filename : SIMPMAT.H

 
Output of file : SIMPMAT.H contained in archive : BLFMATH.ZIP
/////////////////////////////////////////////////////////////////
// simpmat.h: Class definition for simple dynamic matrices.
// Copyright(c) 1993 Azarona Software. All rights reserved.
/////////////////////////////////////////////////////////////////
#ifndef H_SIMPLEMAT
#define H_SIMPLEMAT
#include "vecptr.h"

#define INLINE

// Note: You can define the macro constant VECTORPTRRETURNS
// to change the class to return vector pointers for rows
// instead of ordinary pointers. By switching back and forth
// you can see for yourself what kind of overhead vector
// pointers do or don't have over ordinary pointers.

template
class SimpleMatrix {
private:
TYPE *data;
unsigned nrows, ncols;
public:
SimpleMatrix(unsigned nr, unsigned nc);
SimpleMatrix(const SimpleMatrix &m);
~SimpleMatrix();
void Copy(const SimpleMatrix &m);
SimpleMatrix &operator=(const SimpleMatrix &m);
#ifndef VECTORPTRRETURNS
TYPE *operator[](unsigned r);
#else
VecPtr operator[](unsigned r);
#endif
VecPtr RowPtr(unsigned r=0);
VecPtr ColPtr(unsigned c=0);
VecPtr DiagPtr();
VecPtr PtrToAll();
#ifndef VECTORPTRRETURNS
const TYPE *operator[](unsigned r) const;
#else
VecPtr operator[](unsigned r) const;
#endif
VecPtr RowPtr(unsigned r=0) const;
VecPtr ColPtr(unsigned c=0) const;
VecPtr DiagPtr() const;
VecPtr PtrToAll() const;
unsigned NRows() const;
unsigned NCols() const;
unsigned NElems() const;
};

template
INLINE SimpleMatrix::~SimpleMatrix()
{
delete[] data;
}

template
INLINE SimpleMatrix &
SimpleMatrix::operator=(const SimpleMatrix &m)
{
if (this != &m) Copy(m); // Trap assignment to self
return *this;
}

// Selectors for non-const matrices

#ifndef VECTORPTRRETURNS
template
INLINE TYPE *SimpleMatrix::operator[](unsigned r)
// Returns an ordinary pointer (stride 1) to row r
{
return data + r*ncols;
}
#else
template
INLINE VecPtr SimpleMatrix::operator[](unsigned r)
// Returns an vector pointer (stride 1) to row r
{
return VecPtr(data + r*ncols);
}
#endif

template
INLINE VecPtr SimpleMatrix::RowPtr(unsigned r)
// Return a row vector pointer
{
return VecPtr(data + r*ncols, 1);
}

template
INLINE VecPtr SimpleMatrix::ColPtr(unsigned c)
// Return a column vector pointer
{
return VecPtr(data+c, nrows);
}

template
INLINE VecPtr SimpleMatrix::DiagPtr()
// Return a diagonal vector pointer. In case matrix
// isn't square, the smallest dimension is used to
// determine the diagonal length.
{
unsigned dstride = ((nrows > ncols) ? ncols : nrows) + 1;
return VecPtr(data, dstride);
}


template
INLINE VecPtr SimpleMatrix::PtrToAll()
// Return pointer to all elements of the matrix
{
return VecPtr(data, 1);
}


// Selectors for const matrices

#ifndef VECTORPTRRETURNS
template
INLINE const TYPE *SimpleMatrix::operator[](unsigned r) const
// Returns an ordinary pointer (stride 1) to row r
{
return data + r*ncols;
}
#else
template
INLINE VecPtr SimpleMatrix::operator[](unsigned r) const
// Returns an vector pointer (stride 1) to row r
{
return VecPtr(data + r*ncols);
}
#endif

template
INLINE VecPtr SimpleMatrix::RowPtr(unsigned r) const
// Return a row vector pointer
{
return VecPtr(data + r*ncols, 1);
}

template
INLINE VecPtr SimpleMatrix::ColPtr(unsigned c) const
// Return a column vector pointer
{
return VecPtr(data+c, nrows);
}

template
INLINE VecPtr SimpleMatrix::DiagPtr() const
// Return a diagonal vector pointer. In case matrix
// isn't square, the smallest dimension is used to
// determine the diagonal length.
{
unsigned dstride = ((nrows > ncols) ? ncols : nrows) + 1;
return VecPtr(data, dstride);
}


template
INLINE VecPtr SimpleMatrix::PtrToAll() const
// Return pointer to all elements of the matrix
{
return VecPtr(data, 1);
}

template
INLINE unsigned SimpleMatrix::NRows() const
{
return nrows;
}

template
INLINE unsigned SimpleMatrix::NCols() const
{
return ncols;
}

template
INLINE unsigned SimpleMatrix::NElems() const
{
return nrows * ncols;
}

#undef INLINE

// Whether or not we should include the non-line methods for
// our class templates here is implementation dependent.

#include "simpmat.mth"

#endif


  3 Responses to “Category : C++ Source Code
Archive   : BLFMATH.ZIP
Filename : SIMPMAT.H

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/