16 Alloc1Dbase() =
delete;
17 Alloc1Dbase(
const Alloc1Dbase &) =
delete;
18 Alloc1Dbase &operator=(
const Alloc1Dbase &) =
delete;
19 Alloc1Dbase &operator=(Alloc1Dbase &&) =
delete;
22 explicit Alloc1Dbase(
int sz) : size_(sz) { data = sz ?
new T[sz]{} :
nullptr; }
23 Alloc1Dbase(Alloc1Dbase &&a) noexcept : size_(a.size_), data(a.data) { a.data = 0; }
24 virtual ~Alloc1Dbase() {
delete[] data; }
27 typedef T *const_iterator;
28 T &operator[](
int i) {
29 if (i < 0 || i >= size_)
throw std::out_of_range(
"Alloc1D");
32 const T &operator[](
int i)
const {
33 if (i < 0 || i >= size_)
throw std::out_of_range(
"Alloc1D");
36 bool operator==(
const Alloc1Dbase<T> &t)
const {
37 return std::equal(data, data + size_, t.data, t.data + t.size_);
39 bool operator!=(
const Alloc1Dbase<T> &t)
const {
return !(*
this == t); }
41 int size()
const {
return size_; }
42 void clear() { std::fill(data, data + size_, T()); }
43 T *begin() {
return data; }
44 T *end() {
return data + size_; }
47template <
class T,
int S>
48class Alloc1D :
public Alloc1Dbase<T> {
50 Alloc1D() : Alloc1Dbase<T>(S) {}
51 Alloc1Dbase<T> &base() {
return *
this; }
52 bool operator!=(
const Alloc1D<T, S> &t)
const {
return Alloc1Dbase<T>::operator!=(t); }
66 friend class Alloc2Dbase;
68 rowref(U *r,
int c) : row(r), ncols(c) {}
73 U &operator[](
int i)
const {
74 if (i < 0 || i >= ncols)
throw std::out_of_range(
"Alloc2D");
77 U *begin()
const {
return row; }
78 U *end()
const {
return row + ncols; }
80 Alloc2Dbase() =
delete;
81 Alloc2Dbase(
const Alloc2Dbase &) =
delete;
82 Alloc2Dbase &operator=(
const Alloc2Dbase &) =
delete;
83 Alloc2Dbase &operator=(Alloc2Dbase &&) =
delete;
87 Alloc2Dbase(
int r,
int c) : nrows(r), ncols(c) {
89 data = sz ?
new T[sz]{} :
nullptr;
91 Alloc2Dbase(Alloc2Dbase &&a) noexcept : nrows(a.nrows), ncols(a.ncols), data(a.data) {
94 virtual ~Alloc2Dbase() {
delete[] data; }
96 rowref<T> operator[](
int i) {
97 if (i < 0 || i >= nrows)
throw std::out_of_range(
"Alloc2D");
98 return {data + i * ncols, ncols};
100 rowref<const T> operator[](
int i)
const {
101 if (i < 0 || i >= nrows)
throw std::out_of_range(
"Alloc2D");
102 return {data + i * ncols, ncols};
104 T &at(
int i,
int j) {
105 if (i < 0 || i >= nrows || j < 0 || j >= ncols)
throw std::out_of_range(
"Alloc2D");
106 return data[i * ncols + j];
108 const T &at(
int i,
int j)
const {
109 if (i < 0 || i >= nrows || j < 0 || j >= ncols)
throw std::out_of_range(
"Alloc2D");
110 return data[i * ncols + j];
112 T &operator[](std::pair<int, int> i) {
113 if (i.first < 0 || i.first >= nrows || i.second < 0 || i.second >= ncols)
114 throw std::out_of_range(
"Alloc2D");
115 return data[i.first * ncols + i.second];
117 const T &operator[](std::pair<int, int> i)
const {
118 if (i.first < 0 || i.first >= nrows || i.second < 0 || i.second >= ncols)
119 throw std::out_of_range(
"Alloc2D");
120 return data[i.first * ncols + i.second];
122 bool operator==(
const Alloc2Dbase<T> &t)
const {
123 int sz = nrows * ncols;
124 if (nrows != t.nrows || ncols != t.ncols)
return false;
125 return std::equal(data, data + sz, t.data);
127 bool operator!=(
const Alloc2Dbase<T> &t)
const {
return !(*
this == t); }
129 int rows()
const {
return nrows; }
130 int cols()
const {
return ncols; }
131 void clear() { std::fill(data, data + nrows * ncols, T()); }
134template <
class T,
int R,
int C>
135class Alloc2D :
public Alloc2Dbase<T> {
137 Alloc2D() : Alloc2Dbase<T>(R, C) {}
138 Alloc2Dbase<T> &base() {
return *
this; }
143 int nmats, nrows, ncols;
153 if (i < 0 || i >= nrows)
throw std::out_of_range(
"Alloc3D");
154 return {matrix + i * ncols, ncols};
156 U &operator[](std::pair<int, int> i)
const {
157 if (i.first < 0 || i.first >= nrows || i.second < 0 || i.second >= ncols)
158 throw std::out_of_range(
"Alloc3D");
159 return matrix[i.first * ncols + i.second];
162 Alloc3Dbase() =
delete;
163 Alloc3Dbase(
const Alloc3Dbase &) =
delete;
164 Alloc3Dbase &operator=(
const Alloc3Dbase &) =
delete;
165 Alloc3Dbase &operator=(Alloc3Dbase &&) =
delete;
168 Alloc3Dbase(
int m,
int r,
int c) : nmats(m), nrows(r), ncols(c) {
169 size_t sz = m * r * c;
170 data = sz ?
new T[sz]{} :
nullptr;
172 Alloc3Dbase(Alloc3Dbase &&a) noexcept
173 : nmats(a.nmats), nrows(a.nrows), ncols(a.ncols), data(a.data) {
176 virtual ~Alloc3Dbase() {
delete[] data; }
178 matref<T> operator[](
int i) {
179 if (i < 0 || i >= nmats)
throw std::out_of_range(
"Alloc3D");
180 return {data + i * nrows * ncols, nrows, ncols};
182 matref<const T> operator[](
int i)
const {
183 if (i < 0 || i >= nmats)
throw std::out_of_range(
"Alloc3D");
184 return {data + i * nrows * ncols, nrows, ncols};
186 T &at(
int i,
int j,
int k) {
187 if (i < 0 || i >= nmats || j < 0 || j >= nrows || k < 0 || k >= ncols)
188 throw std::out_of_range(
"Alloc3D");
189 return data[i * nrows * ncols + j * ncols + k];
191 const T &at(
int i,
int j,
int k)
const {
192 if (i < 0 || i >= nmats || j < 0 || j >= nrows || k < 0 || k >= ncols)
193 throw std::out_of_range(
"Alloc3D");
194 return data[i * nrows * ncols + j * ncols + k];
196 T &operator[](std::tuple<int, int, int> i) {
197 if (std::get<0>(i) < 0 || std::get<0>(i) >= nmats || std::get<1>(i) < 0 ||
198 std::get<1>(i) >= nrows || std::get<2>(i) < 0 || std::get<2>(i) >= ncols)
199 throw std::out_of_range(
"Alloc3D");
200 return data[std::get<0>(i) * nrows * ncols + std::get<1>(i) * ncols + std::get<2>(i)];
202 const T &operator[](std::tuple<int, int, int> i)
const {
203 if (std::get<0>(i) < 0 || std::get<0>(i) >= nmats || std::get<1>(i) < 0 ||
204 std::get<1>(i) >= nrows || std::get<2>(i) < 0 || std::get<2>(i) >= ncols)
205 throw std::out_of_range(
"Alloc3D");
206 return data[std::get<0>(i) * nrows * ncols + std::get<1>(i) * ncols + std::get<2>(i)];
208 bool operator==(
const Alloc3Dbase<T> &t)
const {
209 int sz = nmats * nrows * ncols;
210 if (nmats != t.nmats || nrows != t.nrows || ncols != t.ncols)
return false;
211 return std::equal(data, data + sz, t.data);
213 bool operator!=(
const Alloc3Dbase<T> &t)
const {
return !(*
this == t); }
215 int matrixes()
const {
return nmats; }
216 int rows()
const {
return nrows; }
217 int cols()
const {
return ncols; }
218 void clear() { std::fill(data, data + nmats * nrows * ncols, T()); }
221template <
class T,
int B,
int R,
int C>
222class Alloc3D :
public Alloc3Dbase<T> {
224 Alloc3D() : Alloc3Dbase<T>(B, R, C) {}
225 Alloc3Dbase<T> &base() {
return *
this; }