www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - better than union and array questions

reply "Saaa" <empty needmail.com> writes:
(D1 Phobos)
I use the struct below like: Struct[][char[]] _struct;

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.

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

One last question:
Will "a[]=b.dup;" copy b twice?

----

struct Struct
{
enum TYPE{ UNKNOWN, BOOL, BYTE,INT, FLOAT};
// I excluded boolean and byte to shorten this message
TYPE type = TYPE.UNKNOWN;
union
{
 float floatMin;
 int intMin;
}
union
{
 float floatMax;
 int intMax;
}
union
{
 int[][] intArray;
 float[][] floatArray;
}
}
Sep 18 2009
next sibling parent "Saaa" <empty needmail.com> writes:
 (D1 Phobos)
 I use the struct below like: Struct[][char[]] _struct;

 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.

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

 One last question:
 Will "a[]=b.dup;" copy b twice?
One more :) 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?
Sep 18 2009
prev sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
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
Sep 19 2009
next sibling parent Saaa <no bo.dy> writes:
bearophile Wrote:

 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.
Maybe a more general one :) I use a tagged union scheme to encapsulate different types(arrays) as one type. Accessing the arrays thus always need a type-check, plus I need multiple get/setArray functions (or one template with loads of static ifs). Is there maybe a general scheme which results in the same type support without the all the separate type handling hassle?
 
 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.
Two might not be a front, at least they make a line.
 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.
By 'copies b on a' you mean only the length and pointer, right. How do you produce asm? Not that I can read it but it would be a nice way to start learning it a bit.
 
 
 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.
Dlibs license doesn't like my commercial project :) (also, deprecated) ByeBye and thanks, Saaa
Sep 19 2009
prev sibling parent reply Don <nospam nospam.com> writes:
bearophile wrote:
 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.
You always seem to be so negative! In one recent DMD release, half the changes were bugfixes requested by you... If everyone involved in compiler development spent 100% of their time on bearophile requests, you still might not get everything you want <g>. Actually you have at least 20% of my time. Stop complaining, and start prioritizing...
 
 
 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
Sep 21 2009
parent "Saaa" <empty needmail.com> writes:
Don wrote:
 You always seem to be so negative! In one recent DMD release, half the 
 changes were bugfixes requested by you...

 If everyone involved in compiler development spent 100% of their time on 
 bearophile requests, you still might not get everything you want <g>.
 Actually you have at least 20% of my time. Stop complaining, and start 
 prioritizing...
Say bearophile, do you keep a list of suggestions on your website? :)
Sep 21 2009