39 explicit Alloc1Dbase(
int sz) : size_(sz) { data = sz ?
new T[sz]{} :
nullptr; }
44 typedef T *const_iterator;
45 T &operator[](
int i) {
46 if (i < 0 || i >= size_)
throw std::out_of_range(
"Alloc1D");
49 const T &operator[](
int i)
const {
50 if (i < 0 || i >= size_)
throw std::out_of_range(
"Alloc1D");
54 return std::equal(data, data + size_, t.data, t.data + t.size_);
56 bool operator!=(
const Alloc1Dbase<T> &t)
const {
return !(*
this == t); }
58 int size()
const {
return size_; }
59 void clear() { std::fill(data, data + size_, T()); }
60 T *begin() {
return data; }
61 T *end() {
return data + size_; }
85 rowref(U *r,
int c) : row(r), ncols(c) {}
89 typedef const U *const_iterator;
90 U &operator[](
int i)
const {
91 if (i < 0 || i >= ncols)
throw std::out_of_range(
"Alloc2D");
94 U *begin()
const {
return row; }
95 U *end()
const {
return row + ncols; }
106 data = sz ?
new T[sz]{} :
nullptr;
113 rowref<T> operator[](
int i) {
114 if (i < 0 || i >= nrows)
throw std::out_of_range(
"Alloc2D");
115 return {data + i * ncols, ncols};
117 rowref<const T> operator[](
int i)
const {
118 if (i < 0 || i >= nrows)
throw std::out_of_range(
"Alloc2D");
119 return {data + i * ncols, ncols};
121 T &at(
int i,
int j) {
122 if (i < 0 || i >= nrows || j < 0 || j >= ncols)
throw std::out_of_range(
"Alloc2D");
123 return data[i * ncols + j];
125 const T &at(
int i,
int j)
const {
126 if (i < 0 || i >= nrows || j < 0 || j >= ncols)
throw std::out_of_range(
"Alloc2D");
127 return data[i * ncols + j];
129 T &operator[](std::pair<int, int> i) {
130 if (i.first < 0 || i.first >= nrows || i.second < 0 || i.second >= ncols)
131 throw std::out_of_range(
"Alloc2D");
132 return data[i.first * ncols + i.second];
134 const T &operator[](std::pair<int, int> i)
const {
135 if (i.first < 0 || i.first >= nrows || i.second < 0 || i.second >= ncols)
136 throw std::out_of_range(
"Alloc2D");
137 return data[i.first * ncols + i.second];
140 int sz = nrows * ncols;
141 if (nrows != t.nrows || ncols != t.ncols)
return false;
142 return std::equal(data, data + sz, t.data);
144 bool operator!=(
const Alloc2Dbase<T> &t)
const {
return !(*
this == t); }
146 int rows()
const {
return nrows; }
147 int cols()
const {
return ncols; }
148 void clear() { std::fill(data, data + nrows * ncols, T()); }
160 int nmats, nrows, ncols;
170 if (i < 0 || i >= nrows)
throw std::out_of_range(
"Alloc3D");
171 return {matrix + i * ncols, ncols};
173 U &operator[](std::pair<int, int> i)
const {
174 if (i.first < 0 || i.first >= nrows || i.second < 0 || i.second >= ncols)
175 throw std::out_of_range(
"Alloc3D");
176 return matrix[i.first * ncols + i.second];
185 Alloc3Dbase(
int m,
int r,
int c) : nmats(m), nrows(r), ncols(c) {
186 size_t sz = m * r * c;
187 data = sz ?
new T[sz]{} :
nullptr;
190 : nmats(a.nmats), nrows(a.nrows), ncols(a.ncols), data(a.data) {
195 matref<T> operator[](
int i) {
196 if (i < 0 || i >= nmats)
throw std::out_of_range(
"Alloc3D");
197 return {data + i * nrows * ncols, nrows, ncols};
199 matref<const T> operator[](
int i)
const {
200 if (i < 0 || i >= nmats)
throw std::out_of_range(
"Alloc3D");
201 return {data + i * nrows * ncols, nrows, ncols};
203 T &at(
int i,
int j,
int k) {
204 if (i < 0 || i >= nmats || j < 0 || j >= nrows || k < 0 || k >= ncols)
205 throw std::out_of_range(
"Alloc3D");
206 return data[i * nrows * ncols + j * ncols + k];
208 const T &at(
int i,
int j,
int k)
const {
209 if (i < 0 || i >= nmats || j < 0 || j >= nrows || k < 0 || k >= ncols)
210 throw std::out_of_range(
"Alloc3D");
211 return data[i * nrows * ncols + j * ncols + k];
213 T &operator[](std::tuple<int, int, int> i) {
214 if (std::get<0>(i) < 0 || std::get<0>(i) >= nmats || std::get<1>(i) < 0 ||
215 std::get<1>(i) >= nrows || std::get<2>(i) < 0 || std::get<2>(i) >= ncols)
216 throw std::out_of_range(
"Alloc3D");
217 return data[std::get<0>(i) * nrows * ncols + std::get<1>(i) * ncols + std::get<2>(i)];
219 const T &operator[](std::tuple<int, int, int> i)
const {
220 if (std::get<0>(i) < 0 || std::get<0>(i) >= nmats || std::get<1>(i) < 0 ||
221 std::get<1>(i) >= nrows || std::get<2>(i) < 0 || std::get<2>(i) >= ncols)
222 throw std::out_of_range(
"Alloc3D");
223 return data[std::get<0>(i) * nrows * ncols + std::get<1>(i) * ncols + std::get<2>(i)];
226 int sz = nmats * nrows * ncols;
227 if (nmats != t.nmats || nrows != t.nrows || ncols != t.ncols)
return false;
228 return std::equal(data, data + sz, t.data);
230 bool operator!=(
const Alloc3Dbase<T> &t)
const {
return !(*
this == t); }
232 int matrixes()
const {
return nmats; }
233 int rows()
const {
return nrows; }
234 int cols()
const {
return ncols; }
235 void clear() { std::fill(data, data + nmats * nrows * ncols, T()); }