-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.h
85 lines (67 loc) · 1.88 KB
/
matrix.h
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
# Copyright (c) 2016 The Caroline authors. All rights reserved.
# Use of this source file is governed by a MIT license that can be found in the
# LICENSE file.
# Author: Glazachev Vladimir <[email protected]>
#ifndef MATRIX_H
#define MATRIX_H
#include <vector>
#include <iostream>
/*
This file contains class to work with matrices.
This is not implemented any matrix operations. Only element access
and print functions.
*/
namespace interval {
// Matrix class.
template<class IntervalT>
class Matrix {
public:
// Constructed nrow*ncol matrix. Sets all elements to 0.
Matrix(int nrow, int ncol)
: nrow_(nrow)
, ncol_(ncol)
, data_(nrow * ncol) {
}
// Constructed nrow*ncol matrix. Sets all elements to value.
Matrix(int nrow, int ncol, IntervalT value)
: nrow_(nrow)
, ncol_(ncol)
, data_(nrow * ncol, value) {
}
// Copy constructor.
Matrix(const Matrix& other) {
nrow_ = other.nrow_;
ncol_ = other.ncol_;
data_ = other.data_;
}
Matrix& operator=(const Matrix& other) {
nrow_ = other.nrow_;
ncol_ = other.ncol_;
data_ = other.data_;
return *this;
}
IntervalT& at(int r, int c) {
return data_[r * ncol_ + c];
}
IntervalT at(int r, int c) const {
return data_[r * ncol_ + c];
}
int nrow() const { return nrow_; }
int ncol() const { return ncol_; }
private:
int nrow_;
int ncol_;
std::vector<IntervalT> data_;
};
template<typename IntervalT>
std::ostream& operator<<(std::ostream& os, const Matrix<IntervalT>& x) {
for (size_t i = 0; i < x.nrow(); ++i) {
for (size_t j = 0; j < x.ncol() - 1; ++j) {
os << x.at(i, j) << " ";
}
os << x.at(i, x.ncol() - 1) << std::endl;
}
return os;
}
} // namespace interval
#endif // MATRIX_H