-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray2.h
132 lines (104 loc) · 3.15 KB
/
array2.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#pragma once
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <memory>
#include <stdexcept>
template <typename T> class array2 {
public:
array2() = default;
array2(size_t rows, size_t cols, T default_value = T())
: rows_(rows), cols_(cols) {
if ((data_ = (T *)malloc(rows_ * cols_ * sizeof(T))) == nullptr) {
throw std::runtime_error("Cannot allocate memory");
}
std::uninitialized_fill_n(data_, rows * cols, default_value);
}
array2(const array2<T> &rhs) : rows_(rhs.rows_), cols_(rhs.cols_) {
size_t bytes = rows_ * cols_ * sizeof(T);
if ((data_ = (T *)malloc(bytes)) == nullptr) {
throw std::runtime_error("Cannot allocate memory");
}
memcpy(data_, rhs.data_, bytes);
}
array2(array2<T> &&rhs) noexcept
: data_{rhs.data_}, rows_{rhs.rows_}, cols_{rhs.cols_} {
rhs.data_ = nullptr;
rhs.rows_ = rhs.cols_ = 0;
}
~array2() { free(data_); }
array2 &operator=(const array2 &rhs) {
if (this != &rhs) {
size_t bytes = rhs.rows_ * rhs.cols_ * sizeof(T);
if (size() < rhs.size()) {
T *new_data = (T *)realloc(data_, bytes);
if (new_data == nullptr) {
throw std::runtime_error("Cannot reallocate memory");
} else if (data_ != new_data) {
free(data_);
data_ = new_data;
}
}
memcpy(data_, rhs.data_, bytes);
}
return *this;
}
array2 &operator=(array2 &&rhs) noexcept {
if (this != &rhs) {
free(data_);
data_ = rhs.data_;
rows_ = rhs.rows_;
cols_ = rhs.cols_;
rhs.data_ = nullptr;
rhs.rows_ = rhs.cols_ = 0;
}
return *this;
}
bool operator==(const array2 &rhs) noexcept {
if (rows_ != rhs.rows_ || cols_ != rhs.cols_) {
return false;
}
return memcmp(data_, rhs.data_, rows_ * cols_ * sizeof(T)) == 0;
}
void resize(size_t rows, size_t cols, T val = T()) {
T *new_data = (T *)realloc(data_, rows * cols * sizeof(T));
if (new_data == nullptr) {
throw std::runtime_error("Cannot reallocate memory");
} else if (new_data != data_) {
free(data_);
data_ = new_data;
}
rows_ = rows;
cols_ = cols;
std::uninitialized_fill_n(data_, rows_ * cols_, val);
}
void clear() {
free(data_);
data_ = nullptr;
rows_ = cols_ = 0;
}
T operator()(size_t row, size_t col) const noexcept {
assert(row < rows_ && col < cols_);
return data_[row * cols_ + col];
}
T &operator()(size_t row, size_t col) noexcept {
assert(row < rows_ && col < cols_);
return data_[row * cols_ + col];
}
T operator[](size_t idx) const noexcept {
assert(idx < size());
return data_[idx];
}
T &operator[](size_t idx) noexcept {
assert(idx < size());
return data_[idx];
}
size_t rows() const noexcept { return rows_; }
size_t cols() const noexcept { return cols_; }
size_t size() const noexcept { return rows_ * cols_; }
T *data() const noexcept { return data_; }
private:
T *data_{nullptr};
size_t rows_{0};
size_t cols_{0};
};