a struct as an multidimensional array index
Ali Çehreli
acehreli at yahoo.com
Fri Jun 10 14:38:21 UTC 2022
On 6/10/22 01:08, Chris Katko wrote:
> Is it somehow possible to use a struct as a [multidimensional] array
index:
You can define an opIndex that takes any index type if the array is
defined by you:
import std.stdio;
import std.format;
struct indexedPair {
size_t x, y;
}
struct MyArray {
bool[3][3] elements;
ref opIndex(indexedPair i) {
return elements[i.y][i.x];
}
// void toString(scope void delegate(in char[]) sink) const {
// import std.algorithm;
// sink.formattedWrite!"%-(%-(%s %)\n%)"(
// elements[].map!(row => row[].map!(column => column ? 'T' : 'f')));
// }
}
void main() {
auto arr = MyArray();
auto p = indexedPair(1, 1);
arr[p] = true;
writeln(arr);
}
I played with that toString function but for some reason it prints all
Ts. (?)
Ali
More information about the Digitalmars-d-learn
mailing list