On Wednesday, 1 February 2023 at 05:51:31 UTC, Paul wrote:
Thanks Salih. Much appreciated.
It's my pleasure to solve this problem...
I have struck upon an idea that would not affect (i think) the
speed of the processor requesting memory from the heap on a
page-by-page basis but limited 😀
```d
import std.stdio, std.algorithm, std.range;
enum : size_t {
ROW = 11,
COL = 4,
SUM = ROW * COL
}
void main()
{
// 4 ushort x 11 ulong:
auto arr = LimitedArray!COL(ROW);
assert(arr.cell[0].elements.length >= COL);
assert(arr.length == SUM);
alias T = typeof(arr.cell[0].elements[0]);
auto range = iota!T(T.max/2, T.max, 762).array;
assert(range.length == SUM);
range.each!((i, T x) => arr[i] = x);/*
foreach(i, x; range) {
arr[i] = x;
writeln(x);
}//*/
arr.print;
// 8 ubyte x 99 ulong:
auto minMaxTest = LimitedArray!8(99);
auto testLength = minMaxTest.length;
minMaxTest[0] = ubyte.min + 1;
minMaxTest[testLength - 1] = ubyte.max;
minMaxTest.print;
}
template LimitedArray(size_t col)
{
enum error = "Not Possible Capacity!";
enum size = (col + 1) >> 1;
static if(size >= 3)
{
alias T = ubyte; // min value, but max column
} else static if(size == 2)
{
alias T = ushort;// average array
} else static if(size == 1)
{
alias T = uint; // max value, but min column
}
enum s = ulong.sizeof/T.sizeof;
struct LimitedArray
{
invariant(uint.sizeof > size, error);
private {
union Cell {
ulong element;
T[s] elements;
}
Cell[] cell;
const size_t row;
}
this(size_t row) {
this.cell = new Cell[row];
this.row = cell.length;
}
ref T opIndex(size_t i)
in(i < length, "Range overflow!") {
auto len = cell.length;
size_t y, x = i % len;
y = i / len;
return cell[x].elements[y];
}
auto length() {
return row * col;
}
void print() {
foreach(i, c; cell) {
i.writef!"row %2s =>";
foreach(e; c.elements) e.writef!"%6s";
writeln(": ", c.element);
}
}
}
}
```
row 0 => 32767 41149 49531 57913: 16301273062966132735
row 1 => 33529 41911 50293 58675: 16515760268034671353
row 2 => 34291 42673 51055 59437: 16730247473103209971
row 3 => 35053 43435 51817 60199: 16944734678171748589
row 4 => 35815 44197 52579 60961: 17159221883240287207
row 5 => 36577 44959 53341 61723: 17373709088308825825
row 6 => 37339 45721 54103 62485: 17588196293377364443
row 7 => 38101 46483 54865 63247: 17802683498445903061
row 8 => 38863 47245 55627 64009: 18017170703514441679
row 9 => 39625 48007 56389 64771: 18231657908582980297
row 10 => 40387 48769 57151 65533: 18446145113651518915
@SDB79