better than union and array questions

bearophile bearophileHUGS at lycos.com
Sat Sep 19 08:55:32 PDT 2009


Saaa:

> Is there a better way to support arrays of any type?
> Currently all the code working with these Structs are templated with loads 
> of static ifs in them.

You have to ask a more precise question if you want an answer.


> Also, is it possible to add a .deepdup property to all arrays?

D devs don't read posts here, so you have to ask ask in the main newsgroup. I have asked for that more than a year ago, and I was ignored, as usual.


> Will "a[]=b.dup;" copy b twice?

When you have questions like this it's good to take a look at the produced asm.
The dup allocates a new array and then copies data on it.
The a[]=b[]; copies b on a.


> int[] array;
> array.length = 100;
> array.length = 0;
> //no other arrays pointing/slicing to this array
> This way I can be sure for the following 100 element concatenations
> the array won't be copied.
> Or isn't this implicitly part of the D spec? 

Are you talking about appends or concatenations? Concatenations produce memory allocations. But you probably mean 100 appends. Those 100 appends will not produce allocations or copies.
But generally array appends are slow anyway in D, so where you need to do them quickly it's much better to use an ArrayBuilder like the one in my dlibs, of a similar one a bit less efficient in Phobos of D2.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list