-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix-private.hpp
50 lines (43 loc) · 1 KB
/
matrix-private.hpp
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
#include "matrix.hpp"
#include <iostream>
template <typename T>
Matrix<T>::Matrix(size_t numRows, size_t numCols): numRows_{numRows}, numCols_{numCols}
{
size_t numEntries = numRows * numCols;
data_ = new T[numEntries];
}
template <typename T>
Matrix<T>::~Matrix()
{
delete[] data_;
}
template <typename T>
T& Matrix<T>::operator() (size_t row, size_t col) const {
size_t idx = row * numCols_ + col;
return data_[idx];
}
template <typename T>
size_t Matrix<T>::numRows() const {
return numRows_;
}
template <typename T>
size_t Matrix<T>::numCols() const {
return numCols_;
}
template <typename T>
void Matrix<T>::print(std::ostream& os) const {
for (size_t r = 0; r < numRows_; ++r)
{
for (size_t c = 0; c < numCols_; ++c)
{
size_t idx = r * numCols_ + c;
os << data_[idx]<< " ";
}
os << std::endl;
}
}
template <typename T>
std::ostream& operator<<(std::ostream& os, const Matrix<T>& m){
m.print(os);
return os;
}