9 template<
class C,
unsigned D>
12 template<
class C,
unsigned D>
24 data_(data), shape_(shape), c_order_(c_order) { set_stride(); }
27 data_(g.data()), shape_(g.shape()),
28 c_order_(g.c_order()) { set_stride(); }
31 C operator()(
const Point<Int, D>& v)
const {
return (*
this)(index(v)); }
34 C& operator()(
const Point<Int, D>& v) {
return (*
this)(index(v)); }
36 C operator()(Index i)
const {
return data_[i]; }
37 C& operator()(Index i) {
return data_[i]; }
40 shape()
const {
return shape_; }
43 data()
const {
return data_; }
44 C* data() {
return data_; }
47 GridRef& operator=(C value) { Index s = size();
for (Index i = 0; i < s; ++i) data_[i] = value;
return *
this; }
48 GridRef& operator/=(C value) { Index s = size();
for (Index i = 0; i < s; ++i) data_[i] /= value;
return *
this; }
50 Vertex vertex(Index idx)
const {
Vertex v;
for (
unsigned i = 0; i < D; ++i) { v[i] = idx / stride_[i]; idx %= stride_[i]; }
return v; }
51 Index index(
const Vertex& v)
const { Index idx = 0;
for (
unsigned i = 0; i < D; ++i) { idx += ((Index) v[i]) * ((Index) stride_[i]); }
return idx; }
53 Index size()
const {
return size(shape()); }
54 void swap(
GridRef& other) { std::swap(data_, other.data_); std::swap(shape_, other.shape_); std::swap(stride_, other.stride_); std::swap(c_order_, other.c_order_); }
56 bool c_order()
const {
return c_order_; }
59 unsigned dimension() {
return D; }
63 size(
const Vertex& v) { Index res = 1;
for (
unsigned i = 0; i < D; ++i) res *= v[i];
return res; }
69 for (
unsigned i = D; i > 0; --i) { stride_[i-1] = cur; cur *= shape_[i-1]; }
71 for (
unsigned i = 0; i < D; ++i) { stride_[i] = cur; cur *= shape_[i]; }
74 void set_shape(
const Vertex& v) { shape_ = v; set_stride(); }
75 void set_data(C* data) { data_ = data; }
76 void set_c_order(
bool order) { c_order_ = order; }
86 template<
class C,
unsigned D>
91 typedef typename Parent::Value Value;
92 typedef typename Parent::Index Index;
93 typedef typename Parent::Vertex Vertex;
94 typedef Parent Reference;
104 Parent(new C[size(shape)], shape, c_order)
107 Grid(Grid&& g): Grid() { Parent::swap(g); }
109 Grid(
const Parent& g):
110 Parent(new C[size(g.shape())], g.shape(),
111 g.c_order()) { copy_data(g.data()); }
113 template<
class OtherGr
id>
114 Grid(
const OtherGrid& g):
115 Parent(new C[size(g.shape())],
117 g.c_order()) { copy_data(g.data()); }
119 ~Grid() {
delete[] Parent::data(); }
122 Grid& operator=(
const GridRef<OC, D>& other)
124 delete[] Parent::data();
125 Parent::set_c_order(other.c_order());
126 Parent::set_shape(other.shape());
127 Index s = size(shape());
128 Parent::set_data(
new C[s]);
129 copy_data(other.data());
133 Grid& operator=(Grid&& g) { Parent::swap(g);
return *
this; }
137 using Parent::operator();
138 using Parent::operator=;
143 void copy_data(
const OC* data)
145 Index s = size(shape());
146 for (Index i = 0; i < s; ++i)
147 Parent::data()[i] = data[i];