xxxxxxxxxx
import std.stdio;
struct KareMatris(T)
{
T[][] elements;
size_t length;
size_t rowLength()
{
auto x = length;
auto y = (x + 1) >> 1;
while (y < x)
{
x = y;
y = (x + length / x) >> 1;
}
return x;
}
this(size_t length)
{
this.length = length * length;
size_t m = T.sizeof * this.length;
ubyte[] arr = new ubyte[](m);
m /= length;
foreach (i; 0 .. length)
{
size_t n = i * m;
elements ~= cast(T[])arr[n .. n + m];
}
}
ref T opCall(Point i)
in(i.x < rowLength &&
i.y < rowLength)
{
return elements[i.x][i.y];
}
}
unittest
{
imported!"std.exception".assertThrown!Error(
{
auto errX = KareMatris!Color(1)(Point(1, 0));
auto errY = KareMatris!Color(1)(Point(0, 1));
}());
}
struct Point { size_t x, y; }
struct Color {
ubyte r, g, b;
string toString() {
import std.conv : text;
return text(r * g * b);
}
}
void main()
{
auto arr = KareMatris!Color(5);
auto p = Point(2, 4);
auto beyaz = Color(255, 255, 255);
arr(p) = beyaz;
arr.length.writeln;
arr.elements.writefln!"%-(%-(%s %)\n%)";
}