Diff of /libkwave/Matrix.h [204496] .. [4b93a6] Maximize Restore

  Switch to side-by-side view

--- a/libkwave/Matrix.h
+++ b/libkwave/Matrix.h
@@ -1,9 +1,12 @@
 /***************************************************************************
-	       Matrix.h  -  template class describing a 2D grid structure
+	       Matrix.h  -  simple template class for a NxM matrix
 			     -------------------
-    begin                : Fri May 18 2001
-    copyright            : (C) 2001 by Martin Hinsch
-    email                : vidas@sourceforge.net
+    begin                : Sat May 12 2012
+    copyright            : (C) 2012 by Thomas Eschenbacher
+    email                : Thomas.Eschenbacher@gmx.de
+
+    based on Matrix.h from Martin Hinsch
+    copyright (C) 2001 by Martin Hinsch <vidas@sourceforge.net>
  ***************************************************************************/
 
 /***************************************************************************
@@ -20,181 +23,48 @@
 
 #include "config.h"
 
-template <class T> class Matrix
-{
+#include <QtGlobal>
 
-protected:
-    /** Dimensions of the matrix. */
-    unsigned mX, mY;
+namespace Kwave {
 
-    /**
-     * The raw array. It is allocated once and afterwards only changed
-     * WRT values.
-     */
-    T *cell;
+    template <class T> class Matrix
+    {
+    public:
 
-    /**
-     * The distance between two lines. This enables sub-matrizes to be
-     * created.
-     */
-    unsigned offset;
+	/**
+	 * constructor
+	 * @param cols number of columns
+	 * @param rows number of rows
+	 */
+	Matrix(unsigned int cols, unsigned int rows)
+	    :m_rows(rows), m_cols(cols), m_data(0)
+	{
+	    m_data = new T[m_rows * m_cols];
+	    Q_ASSERT(m_data);
+	}
 
-    /** Whether cell is our's. */
-    bool ownData;
+	/** destructor */
+	virtual ~Matrix()
+	{
+	    if (m_data) delete[] m_data;
+	}
 
-public:
-    /** A constructor.*/
-    Matrix(unsigned mx, unsigned my);
-    Matrix(T * data, unsigned mx, unsigned my, unsigned offset = 0);
-    Matrix(const Matrix < T > & other,
-	   unsigned sx, unsigned sy, unsigned mx, unsigned my);
+	/** Get the xth column. Enables expressions like myMatrix[x][y]. */
+	inline T * operator[] (unsigned int col) const {
+	    return m_data + (col * m_rows);
+	}
 
-    /** The destructor. */
-    virtual ~Matrix();
+    private:
 
-    unsigned x() const {
-	return mX;
-    }
+	/** number of rows */
+	unsigned int m_rows;
 
-    unsigned y() const {
-	return mY;
-    }
+	/** number of columns */
+	unsigned int m_cols;
 
-    /** Get the value of the cell at (x, y). */
-    T & at(int x, int y) const {
-	return cell[x*(mY + offset) + y];
-    }
-
-    /** Get the xth column. Enables expressions like myMatrix[x][y]. */
-    T * operator[](int x) const {
-	return cell + x*(mY + offset);
-    }
-
-    /** Set the value at (x, y).*/
-    void set(int x, int y, const T & to) {
-	at(x, y) = to;
-    }
-
-    /** Inserts a new value at (x, y) (necessary for LSC)*/
-    void insert(int x, int y, const T &to) {
-	set(x, y, to);
-    }
-
-    void fill(const T & value) {
-	for (unsigned x = 0; x < mX; x++)
-	    for (unsigned y = 0; y < mY; y++)
-		set(x, y, value);
-    }
-
-    /** Copy the value at (x1, y1) to (x2, y2). */
-    void copy(int x1, int y1, int x2, int y2) {
-	set(x2, y2, at(x1, y1));
-    }
-
-    /**
-     * Apply a function to each cell of a rectangular area of the matrix.
-     * This can be done vertically or horizontally.
-     * @param aFunction The function to apply.
-     * @param colMajor In which direction to work. If set to true
-     *               The matrix will be iterated in x direction.
-     * @param x1, y1, x2, y2 Limits of the region to apply
-     *        the function to. x2 and y2 are set to the right respective lower
-     *        border if they are smaller then x1 and y1.
-     * @return A reference to the function object after the
-     * last call.
-     */
-    template < class F >
-    const F & apply(F &aFunction, bool colMajor = true,
-		    unsigned x1 = 0, unsigned y1 = 0,
-		    unsigned x2 = 0, unsigned y2 = 0) {
-	x2 <= x1 ? (x2 = mX) : 0;
-	y2 <= y1 ? (y2 = mY) : 0;
-	unsigned x, y;
-	if (colMajor)
-	    for(x = x1; x < x2; x++)
-		for(y = y1; y < y2; y++)
-		    aFunction(at(x, y));
-	else
-	    for(y = y1; y < y2; y++)
-		for(x = x1; x < x2; x++)
-		    aFunction(at(x, y));
-
-	return aFunction;
-    }
-
-    /**
-     * Apply a function to each cell of a rectangular area of the matrix.
-     * This can be done vertically or horizontally.
-     * @param aConstFunction The function to apply.
-     * @param xFirst In which direction to work. If set to true
-     * the matrix will be iterated in x direction.
-     * @param x1, y1, x2, y2 Limits of the region to apply
-     * the function to. x2 and y2 are set to the right respective lower
-     * border if they are smaller then x1 and y1.
-     * @return A reference to the function object after the
-     * last call.
-     */
-    template < class F >
-    const F & scan(F &aConstFunction, bool xFirst = true,
-		   unsigned x1 = 0, unsigned y1 = 0,
-		   unsigned x2 = 0, unsigned y2 = 0) const {
-	x2 <= x1 ? (x2 = mX) : 0;
-	y2 <= y1 ? (y2 = mY) : 0;
-	unsigned x, y;
-	if (xFirst)
-	    for(x = x1; x < x2; x++)
-		for(y = y1; y < y2; y++)
-		    aConstFunction(at(x, y));
-	else
-	    for(y = y1; y < y2; y++)
-		for(x = x1; x < x2; x++)
-		    aConstFunction(at(x, y));
-
-	return aConstFunction;
-    }
-
-    /**
-     * Create a sub-matrix. The resulting matrix operates on the same data
-     * as the original one.
-     */
-    Matrix < T > sub(unsigned x, unsigned y, unsigned w, unsigned h) {
-	return Matrix < T > (&(at(x, y)), w, h, mY - h + offset);
-    }
-};
-
-template < class T >
-Matrix < T > ::Matrix(unsigned mx, unsigned my) {
-    mX = mx;
-    mY = my;
-    offset = 0;
-    cell = new T[mX * mY];
-    ownData = true;
-}
-
-template < class T >
-Matrix < T > ::Matrix(T * data, unsigned mx, unsigned my, unsigned offs) {
-    mX = mx;
-    mY = my;
-    offset = offs;
-    cell = data;
-    ownData = false;
-}
-
-template < class T >
-Matrix < T > ::Matrix(const Matrix < T > & other,
-		      unsigned sx, unsigned sy, unsigned mx, unsigned my) {
-    cell = &(other.at(sx, sy));
-    mX = mx;
-    mY = my;
-    offset = other.mY - mY + other.offset;
-    ownData = false;
-}
-
-template < class T >
-Matrix < T > ::~Matrix() {
-    if (!cell || !ownData) return ;
-
-    delete[] cell;
+	/** raw data */
+	T *m_data;
+    };
 }
 
 #endif /* _MATRIX_H_ */