kwave  18.07.70
Matrix.h
Go to the documentation of this file.
1 /***************************************************************************
2  Matrix.h - simple template class for a NxM matrix
3  -------------------
4  begin : Sat May 12 2012
5  copyright : (C) 2012 by Thomas Eschenbacher
6  email : Thomas.Eschenbacher@gmx.de
7 
8  based on Matrix.h from Martin Hinsch
9  copyright (C) 2001 by Martin Hinsch <vidas@sourceforge.net>
10  ***************************************************************************/
11 
12 /***************************************************************************
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * *
19  ***************************************************************************/
20 
21 #ifndef MATRIX_H
22 #define MATRIX_H
23 
24 #include "config.h"
25 
26 #include <string.h>
27 
28 #include <QtGlobal>
29 
30 namespace Kwave
31 {
32 
33  template <class T> class Matrix
34  {
35  public:
36 
42  Matrix(unsigned int cols, unsigned int rows)
43  :m_rows(rows), m_cols(cols), m_data(Q_NULLPTR)
44  {
45  m_data = new T[m_rows * m_cols];
46  Q_ASSERT(m_data);
47  }
48 
50  Matrix(const Matrix &other)
51  :m_rows(other.m_rows), m_cols(other.m_cols), m_data(Q_NULLPTR)
52  {
53  m_data = new T[m_rows * m_cols];
54  Q_ASSERT(m_data);
55  memcpy(m_data, other.m_data, m_rows * m_cols * sizeof(T));
56  }
57 
59  virtual ~Matrix()
60  {
61  if (m_data) delete[] m_data;
62  }
63 
65  inline T * operator[] (unsigned int col) const {
66  return m_data + (col * m_rows);
67  }
68 
69  private:
70 
72  unsigned int m_rows;
73 
75  unsigned int m_cols;
76 
78  T *m_data;
79  };
80 }
81 
82 #endif /* MATRIX_H */
83 
84 //***************************************************************************
85 //***************************************************************************
Definition: App.h:33
Matrix(const Matrix &other)
Definition: Matrix.h:50
T * m_data
Definition: Matrix.h:78
virtual ~Matrix()
Definition: Matrix.h:59
unsigned int m_cols
Definition: Matrix.h:75
Matrix(unsigned int cols, unsigned int rows)
Definition: Matrix.h:42
unsigned int m_rows
Definition: Matrix.h:72
T * operator[](unsigned int col) const
Definition: Matrix.h:65