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
|
template <typename T, size_t N>
class ArrayAccessor{
T *array;
size_t *dimensions;
size_t offset;
ArrayAccessor<T, N - 1> next;
public:
ArrayAccessor(T *array, size_t offset, size_t *dimensions):
array(array),
offset(offset),
dimensions(dimensions){
}
ArrayAccessor<T, N - 1> &operator[](size_t offset){
this->next = ArrayAccessor<T, N - 1>(
this->array + this->offset * *this->dimensions,
offset,
this->dimensions + 1
);
return this->next;
}
};
template <typename T>
class ArrayAccessor<T, 1>{
T *array;
size_t offset;
public:
ArrayAccessor(T *array, size_t offset, size_t *dimensions):
array(array),
offset(offset)
{}
T &operator[](size_t offset){
return this->array[this->offset * *this->dimensions];
}
};
template <typename T>
class Matrix3{
T *array;
size_t width, height, depth;
size_t dimensions[3];
public:
Matrix3(size_t width, size_t height, size_t depth){
//...
this->dimensions[0] = 1;
this->dimensions[1] = width;
this->dimensions[2] = width * height;
//...
}
ArrayAccessor<T, 3> operator[](size_t offset){
return ArrayAccessor<T, 3>(this->array, offset, this->dimensions);
}
};
|