www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Can I load an Dynamic Real Array on one line?

reply David L. Davis <SpottedTiger yahoo.com> writes:
Could someone please point out what I'm doing incorrectly in trying to assign a
series of real values on one line into a dynamic real array? 






















Thxs for any help in advance! :)

-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
Jul 17 2004
next sibling parent reply Andy Friesen <andy ikagames.com> writes:
David L. Davis wrote:

 Could someone please point out what I'm doing incorrectly in trying to assign a
 series of real values on one line into a dynamic real array? 
 




















 
 Thxs for any help in advance! :)
Maybe this needs to be added to Phobos. ;) import std.stdarg; template makeArray(T) { T[] makeArray(...) { T[] result; result.length = _arguments.length; for (int index, TypeInfo ti; _arguments) { // FIXME: handle derived class types and interfaces correctly. assert(ti === typeid(T)); result[index] = va_arg!(T)(_argptr); } } } Then you can just do: real[] realArray = makeArray!(real)(-70_000, 12_000, 15_000, 18_000, 21_000); -- andy
Jul 17 2004
next sibling parent reply "Matthew" <admin stlsoft.dot.dot.dot.dot.org> writes:
"Andy Friesen" <andy ikagames.com> wrote in message
news:cdbvuh$1im0$1 digitaldaemon.com...
 David L. Davis wrote:

 Could someone please point out what I'm doing incorrectly in trying to assign
a
 series of real values on one line into a dynamic real array?






















 Thxs for any help in advance! :)
Maybe this needs to be added to Phobos. ;) import std.stdarg; template makeArray(T) { T[] makeArray(...) { T[] result; result.length = _arguments.length; for (int index, TypeInfo ti; _arguments) { // FIXME: handle derived class types and interfaces correctly. assert(ti === typeid(T)); result[index] = va_arg!(T)(_argptr); } } } Then you can just do: real[] realArray = makeArray!(real)(-70_000, 12_000, 15_000, 18_000, 21_000);
If it's not added to Phobos, can we put this in dtl.utility.arrays; ? Please remind me of this once DTL 0.1 is out there.
Jul 17 2004
parent Regan Heath <regan netwin.co.nz> writes:
On Sun, 18 Jul 2004 06:40:07 +1000, Matthew 
<admin stlsoft.dot.dot.dot.dot.org> wrote:

 "Andy Friesen" <andy ikagames.com> wrote in message
 news:cdbvuh$1im0$1 digitaldaemon.com...
 David L. Davis wrote:

 Could someone please point out what I'm doing incorrectly in trying 
to assign
a
 series of real values on one line into a dynamic real array?














array


'statement'"






 Thxs for any help in advance! :)
Maybe this needs to be added to Phobos. ;) import std.stdarg; template makeArray(T) { T[] makeArray(...) { T[] result; result.length = _arguments.length; for (int index, TypeInfo ti; _arguments) { // FIXME: handle derived class types and interfaces correctly. assert(ti === typeid(T)); result[index] = va_arg!(T)(_argptr); } } } Then you can just do: real[] realArray = makeArray!(real)(-70_000, 12_000, 15_000, 18_000, 21_000);
If it's not added to Phobos, can we put this in dtl.utility.arrays; ? Please remind me of this once DTL 0.1 is out there.
It should be mentioned that the original poster required an 'array literal' which Walter has hinted will be possible eventually. I believe the stumbling blocks include not being able to find a 'nice' syntax (which is also unambiguous). Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 17 2004
prev sibling parent reply David L. Davis <SpottedTiger yahoo.com> writes:
Andy Friesen: Thanks for the reply and the template. Well, I hate to bug you,
but I can't seem to get it to work correctly, could please take another looks at
it?

First problem I ran into soon after I fixed the "for" to a "foreach", was that
it appears that writefln() and the template both define va_arg() differently, so
I get an error trying to use both...thus I moved to using printf() which
surprising works under std.stdio instead forcing std.c.stdio to be imported.

Secondly I had a problem with the assert( ti === typeid( T ) ) statement firing
off, meaning ti and typeid( T ) aren't equal to one another.

And then the Third problem poped up after I commented out the assert()
statement, in which of the four real numbers -200.0, 500.0, 324.0, -230.0 passed
into the template, only -200. and -230.0 showed up in the final array (as shown
below). Please note that -230.0 was suppose to be the last value in the array.


C:\dmd>dynarray
_arguments.length=4
typeid( real )=4257752, ti=4256136, result[ 0 ]= -200.0000
typeid( real )=4257752, ti=4256136, result[ 1 ]=    0.0000
typeid( real )=4257752, ti=4256136, result[ 2 ]= -230.0000
typeid( real )=4257752, ti=4256136, result[ 3 ]=    0.0000

I would really like to get this to work if all possible. Plus it just seems like
such a very "D" way of doing things "rArr[] = [ list of values, ... ];", but
that's me.

Thanks for your help.




















































_D3std6stdarg8va_arg_e6va_argFKPvZe








-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
Jul 17 2004
parent reply Andy Friesen <andy ikagames.com> writes:
David L. Davis wrote:
 Andy Friesen: Thanks for the reply and the template. Well, I hate to bug you,
 but I can't seem to get it to work correctly, could please take another looks
at
 it?
 
 Secondly I had a problem with the assert( ti === typeid( T ) ) statement firing
 off, meaning ti and typeid( T ) aren't equal to one another.
 
 And then the Third problem poped up after I commented out the assert()
 statement, in which of the four real numbers -200.0, 500.0, 324.0, -230.0
passed
 into the template, only -200. and -230.0 showed up in the final array (as shown
 below). Please note that -230.0 was suppose to be the last value in the array.
There's a few things going on here. The big one is that the assert is there for a reason. ;) If it trips, then the types don't match. Suffixing the arguments with an L makes them reals, which causes the assertion to pass, and the return value to look like it ought to. eg. rArr = makeArray!(real)(-200.0L, 500.0L, 324.0L, -230.0L); The link conflict bug is mystifying. I can only assume it's a bug in the compiler. -- andy
Jul 17 2004
parent David L. Davis <SpottedTiger yahoo.com> writes:
Andy Friesen: I've only been able to get the template to work so far with only
char[]s as long as I add a NULL to the string, but even this is pretty useful!
(Thought I'd post what I found thus far.) 

Thanks again for your replys.

---- Console Output -------------------
C:\dmd>dynarray
sArr[] = makeArray!(char[])( "-200.0\0", "1200.0\0", "25.56\0" );

makeArray.result.length=3
makeArray.result[ 0 ] = -200.0
makeArray.result[ 1 ] = 1200.0
makeArray.result[ 2 ] = 25.56
returning...

main.sArr.length=3
main.sArr[ 0 ] = -200.0
main.sArr[ 1 ] = 1200.0
main.sArr[ 2 ] = 25.56

C:\dmd>
----------------------------------------

























);












\"25.56\\0\" );\n\n" );














-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
Jul 19 2004
prev sibling next sibling parent Derek <derek psyc.ward> writes:
On Sat, 17 Jul 2004 19:33:05 +0000 (UTC), David L. Davis wrote:

 Could someone please point out what I'm doing incorrectly in trying to assign a
 series of real values on one line into a dynamic real array? 
 




















 
 Thxs for any help in advance! :)
I think its because D has not (yet?) implemented array literals. Thus any array initializations like this example, need to be done at run time rather than compile time. -- Derek Melbourne, Australia
Jul 17 2004
prev sibling parent reply Ilya Minkov <minkov cs.tum.edu> writes:
This is a well known "feature" in the compiler, though i believe leaving 
this undocumented suxx. I do this (although it's one line too many):

int main() {

    // Lay out the data
    const real [] arr_prototype = [ -200.0, 500.0, 324.0, -230.0 ];

    // Duplicate into a dynamic array
    real arr[] = arr_prototype.dup;

    // Make sure that assignment has taken place, just for demostration
    assert (arr.length==4);

    return 0;
}

Naturally, you can leave out the .dup and do a simple assignment, but 
then you may not write anything into arr because it would be aliased 
with arr_prototype, which is stored in a constant area.

Another thing you could make differently is have arr_prototype be a 
static instead of const, but then you could occasionaly modify a global 
variable, and that may not be what you want.

-eye


David L. Davis schrieb:
 Could someone please point out what I'm doing incorrectly in trying to assign a
 series of real values on one line into a dynamic real array? 
 




















 
 Thxs for any help in advance! :)
 
 -------------------------------------------------------------------
 "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
Jul 22 2004
parent reply Regan Heath <regan netwin.co.nz> writes:
On Thu, 22 Jul 2004 16:19:48 +0200, Ilya Minkov <minkov cs.tum.edu> wrote:

 This is a well known "feature" in the compiler, though i believe leaving 
 this undocumented suxx. I do this (although it's one line too many):

 int main() {

     // Lay out the data
     const real [] arr_prototype = [ -200.0, 500.0, 324.0, -230.0 ];

     // Duplicate into a dynamic array
     real arr[] = arr_prototype.dup;

     // Make sure that assignment has taken place, just for demostration
     assert (arr.length==4);

     return 0;
 }

 Naturally, you can leave out the .dup and do a simple assignment, but 
 then you may not write anything into arr because it would be aliased 
 with arr_prototype, which is stored in a constant area.
No it's not :) This is the problem I have with 'const' currently, in your example the array _reference_ arr_prototype is const, but the data it contains/references is not. example: int main() { // Lay out the data const real[] arr_prototype = [ -200.0, 500.0, 324.0, -230.0 ]; // Duplicate into a dynamic array //real arr[] = arr_prototype.dup; //you used C style declaration? real[] arr = arr_prototype; // Make sure that assignment has taken place, just for demostration assert (arr.length == 4); arr[0] = 12; assert (arr[0] == arr_prototype[0]); return 0; } Regan
 Another thing you could make differently is have arr_prototype be a 
 static instead of const, but then you could occasionaly modify a global 
 variable, and that may not be what you want.

 -eye


 David L. Davis schrieb:
 Could someone please point out what I'm doing incorrectly in trying to 
 assign a




















 Thxs for any help in advance! :)

 -------------------------------------------------------------------
 "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 22 2004
next sibling parent David L. Davis <SpottedTiger yahoo.com> writes:
Ilya Minkov and Regan Heath: Thxs for your replys on this subject! :)

-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
Jul 22 2004
prev sibling parent reply Ilya Minkov <minkov cs.tum.edu> writes:
Regan Heath schrieb:

 Naturally, you can leave out the .dup and do a simple assignment, but 
 then you may not write anything into arr because it would be aliased 
 with arr_prototype, which is stored in a constant area.
No it's not :)
OK, even if it's not it's illegal. it just doesn't always work as intended.
 This is the problem I have with 'const' currently, in your example the 
 array _reference_ arr_prototype is const, but the data it 
 contains/references is not.
    //real arr[] = arr_prototype.dup; //you used C style declaration?
    real[] arr = arr_prototype;
whoops! i would never write that myself, it's here because i first copied David's code verbatim. :) -eye
Jul 23 2004
parent Regan Heath <regan netwin.co.nz> writes:
On Fri, 23 Jul 2004 14:10:53 +0200, Ilya Minkov <minkov cs.tum.edu> wrote:
 Regan Heath schrieb:

 Naturally, you can leave out the .dup and do a simple assignment, but 
 then you may not write anything into arr because it would be aliased 
 with arr_prototype, which is stored in a constant area.
No it's not :)
OK, even if it's not it's illegal. it just doesn't always work as intended.
It has _never_ 'worked' for me, as in, given me a compile time or runtime error saying I cannot modify that const data. I think it's been implemented as I desribed in that "const int[]" means a constant reference to an array of non constant data.
 This is the problem I have with 'const' currently, in your example the 
 array _reference_ arr_prototype is const, but the data it 
 contains/references is not.
    //real arr[] = arr_prototype.dup; //you used C style declaration?
    real[] arr = arr_prototype;
whoops! i would never write that myself, it's here because i first copied David's code verbatim. :)
Just checking. :) Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 23 2004