www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Mixin demo: associative array initializers (with support for comments)

reply Reiner Pope <xxxx xxx.xxx> writes:
After considerable work, I've made an associative array initializer that 
works via mixin(), so you can write the following code:

 auto myAA = AAInit!(int, char[], `
 "11" => 11; // Comment
 /* Another comment */
 "fruit /+ this isn't a comment+/ " => 42;
 "apple" => -1;
 `);
 
 foreach (key, value; myAA)
 {
     writefln("[%s => %s]", key, value);
 }
and get:
 [11 => 11]
 [fruit /+ this isn't a comment+/  => 42]
 [apple => -1]
I've attached the code. Most of the work was in the parser (specifically, BracketedIndexOf!(needle, haystack), which can find the index of a symbol, taking into account the D rules on comments and quotes). However, although it looks pretty horrible, it's a *much* easier job than having to parse the whole thing -- all it needs to do is read through and ignore comments and things inside quotes, and DMD will actually format it all correctly. Enjoy! Reiner
Feb 11 2007
next sibling parent janderson <askme me.com> writes:
Neat!

Reiner Pope wrote:
 After considerable work, I've made an associative array initializer that 
 works via mixin(), so you can write the following code:
 
 auto myAA = AAInit!(int, char[], `
 "11" => 11; // Comment
 /* Another comment */
 "fruit /+ this isn't a comment+/ " => 42;
 "apple" => -1;
 `);

 foreach (key, value; myAA)
 {
     writefln("[%s => %s]", key, value);
 }
and get:
 [11 => 11]
 [fruit /+ this isn't a comment+/  => 42]
 [apple => -1]
I've attached the code. Most of the work was in the parser (specifically, BracketedIndexOf!(needle, haystack), which can find the index of a symbol, taking into account the D rules on comments and quotes). However, although it looks pretty horrible, it's a *much* easier job than having to parse the whole thing -- all it needs to do is read through and ignore comments and things inside quotes, and DMD will actually format it all correctly. Enjoy! Reiner
Neat!
Feb 11 2007
prev sibling parent reply Walter Bright <newshound digitalmars.com> writes:
Reiner Pope wrote:
 However, although it looks pretty horrible, it's a *much* easier job 
 than having to parse the whole thing -- all it needs to do is  read 
 through and ignore comments and things inside quotes, and DMD will 
 actually format it all correctly.
This is pretty cool that you were able to figure out a way to do this. My job is to find a way that you can do such things without it looking horrible <g>.
Feb 11 2007
next sibling parent Dan <murpsoft hotmail.com> writes:
Walter Bright Wrote:
 This is pretty cool that you were able to figure out a way to do this. 
 My job is to find a way that you can do such things without it looking 
 horrible <g>.
No offense man, 'cause I have alot of respect and appreciation for what you've done here... but associative arrays are still hugely broken in D. I have the latest version and I can't seem to use anything to do with them. I'm thinking I may have to implement my own object setup instead of simply using Value[char[]] since DMD doesn't quite yet live up to the D language spec. For my needs, I was hoping to use a left-median left-minimum binary search array on the char[] struct. (x << 1 == goleft, x << 1 + 1 == goright) It's vastly simpler, and faster than your hash algorithm for most cases < 4096 items, and it maintains cache coherency during a search (progresses left to right (skipping) instead of hopping back and forth like median-median left-mininum binary arrays) 3 1 5 0 2 4 6 order = [3][1][5][0][2][4][6] Let me know what you think.
Mar 14 2007
prev sibling parent Dan <murpsoft hotmail.com> writes:
Walter Bright Wrote:
 This is pretty cool that you were able to figure out a way to do this. 
 My job is to find a way that you can do such things without it looking 
 horrible <g>.
No offense man, 'cause I respect what you've done here. Associative arrays are royally b0rked right now, and I have the latest version of D. While I understand that there's probably a few issues with the use of pointers in your hashtable. I was wondering if it was possible to let us use operator overrides to use associative arrays, much like we can for opArraySlice() and such? That would let us implement custom hashing/searching functions as appropriate to the situation. Alternatively, you can use a cascading system based on the length of the array. Under 4096 items, using a left-median left-mininum binary search array will outperform your hash. x = [_,3,1,5,0,2,4,6]; y = &x[1]; y <<= 1 // goes left y <<= 1; y++; // goes right Rebalancing takes only a few cycles, and extending the array for another depth increases at log(n) instead of n for median-median left-mininum search arrays. Anyways... best of luck.
Mar 14 2007