Is there any way to check if array is dense or not?

bearophile bearophileHUGS at lycos.com
Sun Nov 10 16:16:27 PST 2013


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


More information about the Digitalmars-d-learn mailing list