www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is there any way to check if array is dense or not?

reply "Mariusz `shd` =?UTF-8?B?R2xpd2nFhHNraSI=?= <alienballance gmail.com> writes:
As described on 
http://wiki.dlang.org/Dense_multidimensional_arrays we can have 
dense and jagged arrays. That's nice.

Is it possible to check if an array is jagged or not by using 
some template like std.traits : isArray ?
Nov 10 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Mariusz `shd` Gliwiński:

 As described on 
 http://wiki.dlang.org/Dense_multidimensional_arrays we can have 
 dense and jagged arrays.
Built-in D arrays are always dense (unless you consider the associative arrays). That page should name them "contiguous". Sparse arrays are another thing, and they need to be implemented in libraries: http://en.wikipedia.org/wiki/Sparse_array
 Is it possible to check if an array is jagged or not by using 
 some template like std.traits : isArray ?
To tell if a dynamic array is jagged you need a run-time tests, something like this: foreach (const row; matrix) assert(row.length == matrix.front.length); If you want to tell apart a regular dynamic array from the contiguous one, you have to look at the array structure, all dimensions but the last one need to be fixed-sized arrays. There are traits to tell apart fixed sized arrays and dynamic ones, but you need a recursive template. Why do you need this information? So far I have not needed it. Bye, bearophile
Nov 10 2013
parent reply "Mariusz `shd` =?UTF-8?B?R2xpd2nFhHNraSI=?= <alienballance gmail.com> writes:
On Monday, 11 November 2013 at 00:16:31 UTC, bearophile wrote:
 Built-in D arrays are always dense (unless you consider the 
 associative arrays). That page should name them "contiguous".
From the wiki article, "when all the dimensions of the array are known at compile-time, the array is automatically implemented as dense array". My understanding is: 1) Every static array is contigous. 2) If all dimensions lengths can be "known at compile time" the array is contigous 3) If array is allocated using the method from wiki article the array is contigous In all other cases it's not. Now, i'd like to have a function to ask for any of this conditions to be met. Especially point 2) should be implemented in standard library instead of my custom code.
 If you want to tell apart a regular dynamic array from the 
 contiguous one, you have to look at the array structure, all 
 dimensions but the last one need to be fixed-sized arrays.
So it's not contigous http://dpaste.dzfl.pl/7c5d2fa8 ?
 Why do you need this information? So far I have not needed it.
There are two (not so important) reasons. 1. I'd like to use `1 memcpy(m*n)` instead of `m memcpy(n)`. 2. I'd like make a clean distinction in my code between contigous and non-contigous arrays. The goal is to use contigous arrays in every case when i don't need to resize inner arrays.
Nov 11 2013
parent "bearophile" <bearophileHUGS lycos.com> writes:
Mariusz `shd` Gliwiński:

 So it's not contigous http://dpaste.dzfl.pl/7c5d2fa8 ?
That's usually not contiguous. But even a dynamic matrix can have contiguous (untested): auto raw = new int[9]; auto m = new int[][](3); m[0] = raw[0 .. 3]; m[1] = raw[3 .. 6]; m[2] = raw[6 .. 9];
 1. I'd like to use `1 memcpy(m*n)` instead of `m memcpy(n)`.
In idiomatic D code it's much better to avoid unsafe C functions (untested), this only works withe the contiguous matrix: data[] = m[0].ptr[0 .. m.length * m[0].length];
 The goal is to use contigous arrays in every case when i don't 
 need to resize inner arrays.
If you use a dynamic array of fixed-sized arrays this is often not possible, because often you know the sizes only at run-time. So in this case you need slices, as I have shown above. Bye, bearophile
Nov 11 2013