digitalmars.D - Q: associative arrays
- larrycowan (4/4) Sep 30 2004 Can an associative array be initialized with compiled contents?
- Derek Parnell (17/23) Sep 30 2004 LOL! Just today I wanted to do exactly this, but I couldn't see how to d...
- larrycowan (34/34) Sep 30 2004 What I did was:
- Arcane Jill (8/13) Oct 01 2004 I've often wanted to set up a /const/ associated array, initialized at c...
- Walter (7/13) Oct 03 2004 compile
- Ben Hinkle (4/27) Oct 01 2004 just to state the obvious Init can be called from a module constructor s...
- Derek (6/35) Oct 01 2004 Yes, but the point being that it seems that one ought to be able to do t...
- Joe Greer (9/9) Oct 07 2004 If the number of items in the "associative array" is constant, you will
- Sean Kelly (5/12) Oct 07 2004 True enough, assuming the associative array is implemented as a binary t...
- Joe Greer (8/11) Oct 12 2004 Ok, I wasn't aware that the default associative array was hashed, howeve...
Can an associative array be initialized with compiled contents? If so, how? e.g., char[][char[]] wordword = ??? I know it must be static - global defaults to static, so ...
Sep 30 2004
On Fri, 1 Oct 2004 03:08:42 +0000 (UTC), larrycowan wrote:Can an associative array be initialized with compiled contents? If so, how? e.g., char[][char[]] wordword = ??? I know it must be static - global defaults to static, so ...LOL! Just today I wanted to do exactly this, but I couldn't see how to do it either. But as it is in initialization code, it only gets run just the once and its fast enough for that. bit[ char[] ] AttributeBlocks; void Init() { AttributeBlocks["private"] = true; AttributeBlocks["package"] = true; AttributeBlocks["protected"] = true; AttributeBlocks["public"] = true; AttributeBlocks["export"] = true; } -- Derek Melbourne, Australia 1/10/2004 2:24:43 PM
Sep 30 2004
What I did was: struct handtype { char[] name; int lovalue; int hivalue; int count; }; handtype[] winners = [ { "straight flush",260,272,0 }, { "four of a kind",247,259,0 }, .. { "high card",8,12,0 } ]; int[int] ix; void init_ix ( ) { foreach ( int i, handtype w; winners ) for ( int j = w.lovalue; j <= w.hivalue ;j++ ) ix[j] = i; } void main ( char[][] args ) { init_ix(); .. winningHand = ... .. winners[ix[winningHand]].count++; .. foreach ( handtype w; winners ) printf("%2.0f %.*s\n,(100.0 * w.count)/handsDealt,w.name); .. } Well, actually spread over many functions, but that's the idea. Anyway, I guess we're still waiting for static initialization of associative arrays - there was some thunder and some suggestions for syntax a few months back, but apparently nothing yet.
Sep 30 2004
In article <cjiquv$3178$1 digitaldaemon.com>, Derek Parnell says...On Fri, 1 Oct 2004 03:08:42 +0000 (UTC), larrycowan wrote:I've often wanted to set up a /const/ associated array, initialized at compile time. The annoying thing is, /this would actually be possible, currently/, if only the ABI for associative arrays were published in the D documentation. (There is very little you can't hand-compile, if you know the ABI and you're happy for it to be const). Any chance this ABI could be documented? Arcane JillCan an associative array be initialized with compiled contents? If so, how?LOL! Just today I wanted to do exactly this, but I couldn't see how to do it either.
Oct 01 2004
"Arcane Jill" <Arcane_member pathlink.com> wrote in message news:cjj059$2p7$1 digitaldaemon.com...I've often wanted to set up a /const/ associated array, initialized atcompiletime. The annoying thing is, /this would actually be possible, currently/,ifonly the ABI for associative arrays were published in the D documentation. (There is very little you can't hand-compile, if you know the ABI andyou'rehappy for it to be const). Any chance this ABI could be documented?All associative arrays are are an array (struct Array) of the tree structure (struct aaA) found in phobos\internal\aaA.d.
Oct 03 2004
Derek Parnell wrote:On Fri, 1 Oct 2004 03:08:42 +0000 (UTC), larrycowan wrote:just to state the obvious Init can be called from a module constructor so to the rest of the program it looks like AttributeBlocks or wordword was never empty.Can an associative array be initialized with compiled contents? If so, how? e.g., char[][char[]] wordword = ??? I know it must be static - global defaults to static, so ...LOL! Just today I wanted to do exactly this, but I couldn't see how to do it either. But as it is in initialization code, it only gets run just the once and its fast enough for that. bit[ char[] ] AttributeBlocks; void Init() { AttributeBlocks["private"] = true; AttributeBlocks["package"] = true; AttributeBlocks["protected"] = true; AttributeBlocks["public"] = true; AttributeBlocks["export"] = true; }
Oct 01 2004
On Fri, 01 Oct 2004 08:07:55 -0400, Ben Hinkle wrote:Derek Parnell wrote:Yes, but the point being that it seems that one ought to be able to do this at compile time rather than at run time. -- Derek Melbourne, AustraliaOn Fri, 1 Oct 2004 03:08:42 +0000 (UTC), larrycowan wrote:just to state the obvious Init can be called from a module constructor so to the rest of the program it looks like AttributeBlocks or wordword was never empty.Can an associative array be initialized with compiled contents? If so, how? e.g., char[][char[]] wordword = ??? I know it must be static - global defaults to static, so ...LOL! Just today I wanted to do exactly this, but I couldn't see how to do it either. But as it is in initialization code, it only gets run just the once and its fast enough for that. bit[ char[] ] AttributeBlocks; void Init() { AttributeBlocks["private"] = true; AttributeBlocks["package"] = true; AttributeBlocks["protected"] = true; AttributeBlocks["public"] = true; AttributeBlocks["export"] = true; }
Oct 01 2004
If the number of items in the "associative array" is constant, you will probably get a higher performing implementation by storing the data as a sorted array and binary searching that array for the key values. Wrap that in an interface that looks like an associative array and you are done. This avoids dynamic node allocation and tree traversal logic, which is slower than just binary search in cases where the dynamic nature of the tree isn't required. just the thoughts of a C++ programmer who has an interest in D, joe
Oct 07 2004
In article <ck3ogg$1le1$1 digitaldaemon.com>, Joe Greer says...If the number of items in the "associative array" is constant, you will probably get a higher performing implementation by storing the data as a sorted array and binary searching that array for the key values. Wrap that in an interface that looks like an associative array and you are done. This avoids dynamic node allocation and tree traversal logic, which is slower than just binary search in cases where the dynamic nature of the tree isn't required.True enough, assuming the associative array is implemented as a binary tree. D AA's are hashtables though, and should offer better performance than sorted arrays in most instances. Sean
Oct 07 2004
True enough, assuming the associative array is implemented as a binarytree. DAA's are hashtables though, and should offer better performance thansortedarrays in most instances.Ok, I wasn't aware that the default associative array was hashed, however a similar rule still applies. You can determine your hash algorithm, calculate the values, initialize the appropiate array elements. This is much more of a pain though, so I probably wouldn't really want to do it either. ;) joe
Oct 12 2004