summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/matrix.h
blob: 311602cfdc313b6ff1b5941dd10e0cb505656160 (plain) (blame)
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
/*
 * File:     matrix.h
 * Summary:  Two-dimensional array class.
 */

#ifndef MATRIX_H
#define MATRIX_H

template <typename Z>
class Matrix {
public:
    Matrix(int width, int height, const Z &initial);
    Matrix(int width, int height);
    ~Matrix();

    void init(const Z &initial);
    Z &operator () (int x, int y)
    {
        return data[x + y * mwidth];
    }
    const Z &operator () (int x, int y) const
    {
        return data[x + y * mwidth];
    }

    int width() const { return mwidth; }
    int height() const { return mheight; }

private:
    Z *data;
    int mwidth, mheight, size;
};

template <typename Z>
Matrix<Z>::Matrix(int _width, int _height, const Z &initial)
    : data(NULL), mwidth(_width), mheight(_height), size(_width * _height)
{
    data = new Z [ size ];
    init(initial);
}

template <typename Z>
Matrix<Z>::Matrix(int _width, int _height)
    : data(NULL), mwidth(_width), mheight(_height), size(_width * _height)
{
    data = new Z [ size ];
}

template <typename Z>
Matrix<Z>::~Matrix()
{
    delete [] data;
}

template <typename Z>
void Matrix<Z>::init(const Z &initial)
{
    for (int i = 0; i < size; ++i)
        data[i] = initial;
}

#endif    // MATRIX_H