digitalmars.D - Q: associative arrays
- larrycowan <larrycowan_member pathlink.com> Sep 30 2004
- Derek Parnell <derek psych.ward> Sep 30 2004
- larrycowan <larrycowan_member pathlink.com> Sep 30 2004
- Arcane Jill <Arcane_member pathlink.com> Oct 01 2004
- "Walter" <newshound digitalmars.com> Oct 03 2004
- Ben Hinkle <bhinkle4 juno.com> Oct 01 2004
- Derek <derek psyc.ward> Oct 01 2004
- "Joe Greer" <remove.this.jgreer remove.this.nsisoftware.com> Oct 07 2004
- Sean Kelly <sean f4.ca> Oct 07 2004
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:Can 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.
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 Jill
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 at
time. The annoying thing is, /this would actually be possible, currently/,
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
happy 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: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; }
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.
Oct 01 2004
On Fri, 01 Oct 2004 08:07:55 -0400, Ben Hinkle wrote:Derek Parnell wrote: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; }
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.
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, Australia
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 binary
AA's are hashtables though, and should offer better performance than
arrays 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









larrycowan <larrycowan_member pathlink.com> 