June 11, 2022
On 6/11/22 04:16, Salih Dincer wrote:

> I think D is very consistent with our feelings. That is, the order in
> memory is in the form of rows x columns.

Yet, there are no rows or columns because neither D nor C (nor C++) have multip-dimensional arrays. They all have arrays where elements are layed out in memory consecutively.

The type of the elements and what they represent is entilery up to the programmer.

> But yes, in reverse(column x
> row) when you set it up statically.

If you mean we can set up the memory in any way we want, I agree but again, since there are no mult-dimensional arrays, there cannot be the reverse of the order.

> This sample code working on pointers
> can be a proof:

If it's prooving that elemets are side-by-side, then it's by spec.

Here is an example where I have array where each element is a column:

import std.stdio;
import std.range;
import std.algorithm;

void main() {
  // I decide that this array represents
  // Three rows of two columns.
  int[][] arr;
  arr.length = 2;
  foreach (ref column; arr) {
    column.length = 3;
  }

  setFirstColumn(arr, 1);
  printArray(arr);
}

void setFirstColumn(int[][] arr, int value) {
  // The first element is my first column.
  arr[0][] = value;
}

void printArray(int[][] arr) {
  // Because stdout takes line-by-line,
  // we print a transposition.
  arr.transposed.writefln!"%-(%-(%s %)\n%)";
}

You may think that the final transposition is a trick. No, it was needed only because stdout takes line-by-line. If I used a library like ncurses, I could have printed my array exactly the way I used it.

The point is, there are no multi-dimensional arrays. Programmers use arrays as they need and sometimes the elements are arrays.

Ali

June 11, 2022
On 6/11/22 00:09, z wrote:

> I rechecked and it should be `X Y Z` for static array, but `Z Y X` for
> indexing/dynamic array creating with `new`

How so? I wrote the following program:

import std.stdio;

void main() {
  enum X = 2;
  enum Y = 3;
  enum Z = 4;

  int[X][Y][Z] s;
  int[X][Y][] d = new int[X][Y][Z];

  pragma(msg, typeof(s));
  pragma(msg, typeof(d));
}

It outputs the following for the static and the dynamic arrays:

int[2][3][4]
int[2][3][]

Consistent elements, except the static array has a compile-time known length.

Ali

June 11, 2022

On Saturday, 11 June 2022 at 15:01:05 UTC, Ali Çehreli wrote:

>

On 6/11/22 00:09, z wrote:

>

I rechecked and it should be X Y Z for static array, but Z Y X for
indexing/dynamic array creating with new

How so?

i meant with the syntax in (1), the spec's documentation appears to say they are equivalent in result with new *type*[X][Y] form.

(1) https://dlang.org/spec/expression#new_multidimensional (3. multiple argument form)

June 11, 2022
On 6/11/22 13:36, z wrote:

> i meant with the syntax in (1), the spec's documentation appears to say
> they are equivalent in result with `new *type*[X][Y]` form.
>
> (1) https://dlang.org/spec/expression#new_multidimensional (3. multiple
> argument form)

Thank you. I see now: The values in parentheses are the lengths from the outermost to the innermost:

  new int[][][](5, 20, 30)

// The equivalent of int[30][20][5]

Although, I can see how it had to be that way so that when one used less number of lengths, the syntax always works from outer to inner in that new expression:

  new int[][][](5)

// The equivalent of int[][][5]

Ali

1 2
Next ›   Last »