various questions
Jason Spencer
spencer8 at sbcglobal.net
Wed Jul 28 11:52:11 PDT 2010
I'm working on a program to do statistics on matrices of different sizes, and
I've run into a handful of situations where I just can't seem to find the
trick I need. In general, I'm trying to make my functions work on static
arrays of the proper size, and template that up for the different sizes I need
to support. I appeal to your experience and mercy on the various questions
below. Any help on any point appreciated.
1. How can I cast a single dimension dynamic array to a multi-dimension
static array? I'm trying to do roughly the following:
auto data = cast(float[100][100])std.file.read(fname, fsize);
which fails on the cast. Is there any way to treat they memory returned
from read as a static array? If I want to avoid the copy, am I relegated back
to pointers? Is there a cast path through a pointer that would work? I think
I'm essentially asking if I can make a reference to a static array and assign
that to the memory contained in a dynamic array. I have a suspicion about the
answer....
2. To work around 1., I've left one dimension of the cast floating. This
means to bind both dimensions in my template args, I can no longer use
type parameter inference, so I have to explicitly instantiate the template
with the static size, but pass in a type that's dynamic in one dimension. Is
there a way to deduce the equivalent static array type from the dynamic array?
(See code below). How about just from a static array? Even the following
fails:
U foo(T: U[C][R], U, size_t C, size_t R)(T data, size_t c, size_t r)
{
return data[r][c];
}
int[4][5] data = [ [...] ];
int bar = foo(data, 2, 3);
but
int bar = foo!(int[4][5])(data, 2, 3);
works. Why?
If I could solve that, then next I'd need to make this work:
U foo(T: U[C][R], U, size_t C, size_t R)(U[C][] data, size_t c, size_t r)
{
return data[r][c];
}
int[4][] data = [ [...] ];
int bar = foo(data, 2, 3);
Extra points for that one!
3. I wanted to have a const static lookup table in a struct to map a string to an
int. I figured an Associative array would be a good way to index by string.
But I was not able to give a literal AA. I had to put it in static this(). I
assume that's because AA's aren't really initialized by the compiler, so
they're not really literals. They're just arguments to some run-time function
that actually initializes them. Is that right? If so, that's too bad--making
different rules for different array types makes them feel like they're not
/really/ part of the language. Plus, having immutable lookups, fixed at
compile time, would be really useful.
4. Lastly, compiling. In src/main.d, I had "import data.stats;", and in
src/data/stats.d, I had "module data.stats;". On the command line in src, I figured
I'd just have to say "dmd main.d" and I'd be good to go. Or maybe "dmd -I.
main.d" or "dmd -Idata main.d". But I had to specify both files for the
compiler, as in "dmd main.d data/stats.d" to avoid undefined symbols from
stat.d at link time. Is that right? If so, how does dmd know where to get
phobos files from? Is there a slicker way to do this with dmd?
Thanks,
Jason
More information about the Digitalmars-d-learn
mailing list