www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Associative array of dynamic array

reply "Samuele Carcagno" <sam.carcagno gmail.com> writes:
I would like to create an associative array that maps a string to 
a multidimensional dynamic array. In other words, I would like a 
data structure where I can access elements like this:

foo["key"][0][0]

Is this possible? If so I'm having trouble figuring out the 
correct syntax, the following (for one dimension) doesn't work:

auto foo = new int[5][string];

compilation fails with the following message:
Error: cannot implicitly convert expression (string) of type 
string to ulong

Thanks for any help!
Sep 09 2012
next sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Sun, Sep 9, 2012 at 2:18 PM, Samuele Carcagno <sam.carcagno gmail.com> wrote:
 I would like to create an associative array that maps a string to a
 multidimensional dynamic array. In other words, I would like a data
 structure where I can access elements like this:

 foo["key"][0][0]
The type you want is int[][][string]. You can use it like this: alias int[][][string] MyArray; void main() { MyArray foo; foo["abc"] = [[0,1,2], [3,4], []]; foo["def"] = [[0]]; assert(foo["abc"][0][1] == 1); }
Sep 09 2012
prev sibling parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Sunday, 9 September 2012 at 12:17:47 UTC, Samuele Carcagno 
wrote:
 I would like to create an associative array that maps a string 
 to a multidimensional dynamic array. In other words, I would 
 like a data structure where I can access elements like this:

 foo["key"][0][0]

 Is this possible? If so I'm having trouble figuring out the 
 correct syntax, the following (for one dimension) doesn't work:

 auto foo = new int[5][string];

 compilation fails with the following message:
 Error: cannot implicitly convert expression (string) of type 
 string to ulong

 Thanks for any help!
Other way round. Unlike C++, everything goes before the identifier: -------- import std.stdio; void main() { int[][][string] foo; foo["hello"]=[[1],[2]]; writeln(foo["hello"]); writeln(foo["hello"][0][0]); } ------- [[1], [2]] 1 ------- foo is an associative array, the key is "string", and the type is "int[][]"
Sep 09 2012
parent reply "Samuele Carcagno" <sam.carcagno gmail.com> writes:
thanks a lot! both solutions work, to initialize the arrays of 
int I'm doing:

int[][][string] foo;
foo["key"] = new int[][](6,6);
foo["key"][0][0] = 5;

it seems to work.
Sep 09 2012
parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Sun, Sep 9, 2012 at 2:48 PM, Samuele Carcagno <sam.carcagno gmail.com> wrote:
 thanks a lot! both solutions work, to initialize the arrays of int I'm
 doing:

 int[][][string] foo;
 foo["key"] = new int[][](6,6);
 foo["key"][0][0] = 5;

 it seems to work.
Great! Keep in mind all these structures (AA and dynamic arrays) are reference types. If you copy the associative array, you just copy the references and any change in one will affect the other. Also, internally: alias int[][][string] MyArray; void main() { MyArray foo; foo["abc"] = [[0,1,2], [3,4], []]; assert(foo["abc"][0][1] == 1); // internal copy foo["def"] = foo["abc"]; foo["def"][0][1] = 2; assert(foo["abc"][0][1] == 2); // external copy auto bar = foo; bar["ghi"] = foo["abc"]; bar["ghi"][0][1] = 3; assert(foo["abc"][0][1] == 3 && foo["def"][0][1] == 3); }
Sep 09 2012
parent "bearophile" <bearophileHUGS lycos.com> writes:
Philippe Sigaud:

 Keep in mind all these structures (AA and dynamic arrays) are
 reference types.
There are bug-prone things you have to keep in mind. Not exactly a reference type, this is by D specs: import std.stdio; import std.array: popFront; void main() { auto d = [1:[2, 3]]; auto l = d[1]; auto l2 = l[0]; l.popFront(); writeln(d, " ", l, " ", l2); } Even AAs (I don't know if this is an implementation bug or if this is by D specs, opinions welcome): void test(int[int] arraya, int x) { arraya[x] = x; } void main() { int[int] d; test(d, 0); int[int] d0; assert(d == d0); // d is empty, 0:0 is lost d[1] = 1; test(d, 2); assert(d == [1: 1, 2: 2]); // now 2:2 is not lost } Bye, bearophile
Sep 09 2012