File: Matrix.h

package info (click to toggle)
moagg 0.18-6
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 1,924 kB
  • ctags: 4,059
  • sloc: cpp: 23,814; sh: 2,652; makefile: 283
file content (111 lines) | stat: -rw-r--r-- 2,327 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#ifndef MATRIX_H
#define MATRIX_H

#include <cassert>

#include "Tools.h"

//----------------------------------------------------------------------------
template <class T>
class Matrix
{
  public:
    //------------------------------------------------------------------------
    Matrix()
    {
        m_xSize = 0;
        m_ySize = 0;
        m_matrix = NULL;
    }

    Matrix(const Matrix &other)
    {
        m_matrix = NULL;
        operator=(other);
    }

    ~Matrix()
    {
        ZAP_ARRAY(m_matrix);
    }

    //------------------------------------------------------------------------
    void resize(unsigned x, unsigned y)
    {
        ZAP_ARRAY(m_matrix);

        m_xSize = x;
        m_ySize = y;
        m_matrix = new T[m_xSize * m_ySize];
    }

    void resize(unsigned x, unsigned y, T value)
    {
        resize(x, y);
        setAll(value);
    }

    //------------------------------------------------------------------------
    unsigned getXSize() const
    {
        return m_xSize;
    }

    unsigned getYSize() const
    {
        return m_ySize;
    }

    //------------------------------------------------------------------------
    T get(unsigned x, unsigned y) const
    {
        assert(x < m_xSize);
        assert(y < m_ySize);
        return m_matrix[y*m_xSize + x];
    }

    void set(unsigned x, unsigned y, T value)
    {
        assert(x < m_xSize);
        assert(y < m_ySize);
        m_matrix[y*m_xSize + x] = value;
    }

    void setAll(T value)
    {
        unsigned yOffset = 0;
        for (unsigned y=0; y<m_ySize; y++)
        {
            for (unsigned x=0; x<m_xSize; x++)
            {
                m_matrix[yOffset + x] = value;
            }
            yOffset += m_xSize;
        }
    }

    //------------------------------------------------------------------------
    Matrix &operator=(const Matrix &other)
    {
        resize(other.m_xSize, other.m_ySize);
        for (unsigned y=0; y<m_ySize; y++)
        {
            for (unsigned x=0; x<m_xSize; x++)
            {
                set(x, y, other.get(x, y));
            }
        }

        return *this;
    }

  protected:

    //------------------------------------------------------------------------
    unsigned m_xSize;
    unsigned m_ySize;

    T* m_matrix;
};

#endif //MATRIX_H