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
|
#ifndef YAGE_MATH_MATRIX_HPP
#define YAGE_MATH_MATRIX_HPP
#include <memory>
#include <vector>
namespace yage
{
template<int Rows, int Cols, class Type> class Matrix;
namespace detail
{
template<int Rows, int Cols, class Type> class Row
{
private:
std::shared_ptr<Matrix<Rows, Cols, Type>> parent_;
int index_;
public:
Row<Rows, Cols, Type>(std::shared_ptr<Matrix<Rows, Cols, Type>> parent, int index) :
parent_(parent), index_(index)
{}
Type &operator[](int col)
{
return parent_->data_[index_*Cols+col];
}
};
} // detail
template<int Rows=4, int Cols=4, class Type=double> class Matrix
{
friend class detail::Row<Rows, Cols, Type>;
private:
std::vector<Type> data_;
public:
Matrix<Rows, Cols, Type>() : data_(Rows*Cols) {}
Matrix<Rows, Cols, Type>(int rows, int cols) : data_(rows*cols) {}
detail::Row<Rows, Cols, Type> operator[](int row)
{
return detail::Row<Rows, Cols, Type>(std::make_shared<Matrix<Rows, Cols, Type>>(*this), row);
}
};
} // yage
#endif
|