mini-llvm 0.1.0
Loading...
Searching...
No Matches
Matrix.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2
3#pragma once
4
5#include <algorithm>
6#include <cassert>
7#include <cstddef>
8#include <vector>
9
10namespace mini_llvm {
11
12template <typename T>
13class Matrix {
14public:
15 constexpr Matrix()
16 : rows_(0), cols_(0), data_() {}
17
18 constexpr Matrix(size_t rows, size_t cols)
19 : rows_(rows), cols_(cols), data_(rows * cols) {}
20
21 constexpr Matrix(size_t rows, size_t cols, const T &value)
22 : rows_(rows), cols_(cols), data_(rows * cols, value) {}
23
24 constexpr Matrix(const Matrix &) = default;
25 constexpr Matrix(Matrix &&) noexcept = default;
26
27 constexpr Matrix &operator=(const Matrix &) = default;
28 constexpr Matrix &operator=(Matrix &&) noexcept = default;
29
30 constexpr size_t rows() const noexcept {
31 return rows_;
32 }
33
34 constexpr size_t cols() const noexcept {
35 return cols_;
36 }
37
38 constexpr size_t size() const noexcept {
39 return rows_ * cols_;
40 }
41
42 constexpr T *data() noexcept {
43 return data_.data();
44 }
45
46 constexpr const T *data() const noexcept {
47 return data_.data();
48 }
49
50 constexpr T &operator[](size_t i, size_t j) noexcept {
51 assert(i < rows_ && j < cols_);
52 return data_[i * cols_ + j];
53 }
54
55 constexpr const T &operator[](size_t i, size_t j) const noexcept {
56 assert(i < rows_ && j < cols_);
57 return data_[i * cols_ + j];
58 }
59
60 constexpr void fill(const T &value) {
61 std::fill(data_.begin(), data_.end(), value);
62 }
63
64 constexpr void resize(size_t rows, size_t cols) {
65 data_.resize(rows * cols);
66 rows_ = rows;
67 cols_ = cols;
68 }
69
70 constexpr void resize(size_t rows, size_t cols, const T &value) {
71 data_.resize(rows * cols, value);
72 rows_ = rows;
73 cols_ = cols;
74 }
75
76private:
77 size_t rows_, cols_;
78 std::vector<T> data_;
79};
80
81} // namespace mini_llvm
constexpr void fill(const T &value)
Definition Matrix.h:60
constexpr Matrix(size_t rows, size_t cols, const T &value)
Definition Matrix.h:21
constexpr void resize(size_t rows, size_t cols, const T &value)
Definition Matrix.h:70
constexpr T & operator[](size_t i, size_t j) noexcept
Definition Matrix.h:50
constexpr const T & operator[](size_t i, size_t j) const noexcept
Definition Matrix.h:55
constexpr Matrix(const Matrix &)=default
constexpr T * data() noexcept
Definition Matrix.h:42
constexpr size_t cols() const noexcept
Definition Matrix.h:34
constexpr Matrix(size_t rows, size_t cols)
Definition Matrix.h:18
constexpr const T * data() const noexcept
Definition Matrix.h:46
constexpr Matrix(Matrix &&) noexcept=default
constexpr void resize(size_t rows, size_t cols)
Definition Matrix.h:64
constexpr Matrix()
Definition Matrix.h:15
constexpr size_t size() const noexcept
Definition Matrix.h:38
constexpr size_t rows() const noexcept
Definition Matrix.h:30
Definition GraphColoringAllocator.h:13