On Tuesday, 31 January 2023 at 01:04:41 UTC, Paul wrote:
Can I perform a similar assignment to the column? This, myArr[][0] = 5, doesn't work.

Of course, this question has a short answer and a long answer. So the issue is more about column-major. I am someone who likes to talk with codes. In fact, everything is side by side in memory. This example (something like array) covers the issue:

```d
import std.stdio;

void main()
{
  int[COL][ROW] sample = [ [ 5, 5, 5 ],
                           [ 0, 0, 0 ],
                           [ 0, 0, 0 ],
                         ];

  auto arrayish = Arrayish!int(ROW, COL);
  assert(arrayish.length == SUM);

  // copy array...
  foreach(r; 0..ROW)
  {
    foreach(c; 0..COL)
    {
      arrayish[r, c] = sample[r][c];
    }
  }

  arrayish.print();

  foreach(n; 0..COL)
  {
    //arrayish.columnMajor(n).writeln;/*
    arrayish[n].writeln;//*/
  }

  // clear and set...
  arrayish.elements[] = 0;
  foreach(r; 0..ROW) arrayish[r] = 5;
  arrayish.print();
}

struct Arrayish(T) {
  private {
    T[] elements;
    const size_t row, col;
  }

  this(size_t row, size_t col) {
    this.elements = new T[row * col];
    this.row = row;
    this.col = col;
  }

  ref T opIndex(size_t row = 0, size_t col = 0) {
    return elements[row * this.col + col];
  }

  ref T columnMajor(size_t row = 0, size_t col = 0) {
    return elements[col * this.row + row];
  }

  auto length() {
    return row * col;
  }

  void print() {
    foreach(r; 0..row) {
      foreach(c; 0..col)
        this[r, c].write;
      writeln;
    }
  }
} /* Prints:

555
000
000
5
0
0
500
500
500

*/

```
SDB@79

Reply via email to