digitalmars.D - Fast switching..
- Regan Heath (13/13) Jun 11 2008 Hi all,
- Georg Wrede (17/33) Jun 11 2008 A language that allows you to write switch statements with strings
- Robert Fraser (3/8) Jun 11 2008 Which can be done with a string mixin + CTFE function ;-P. String
- Sean Kelly (22/46) Jun 11 2008 I've done this quite often with simple parsers:
- janderson (8/59) Jun 11 2008 I thought in a discussion earlier with Walter (a few years back) that
- Jarrett Billingsley (8/15) Jun 12 2008 Guessing won't be necessary :) The implementation of string switches is...
- janderson (5/25) Jun 12 2008 ic. I guess I should have just looked. I guess if anyone wanted to
- Lionello Lunesu (17/21) Jun 11 2008 Isn't the internet great? I started on that page (interesting read) and ...
- Fawzi Mohamed (8/35) Jun 12 2008 Isn't
- bearophile (6/10) Jun 12 2008 Oh, you have found the book I was talking about here:
Hi all, I've been AWOL from this NG for a while but recently a friend of mine sent me a link to this: http://blogs.msdn.com/jomo_fisher/archive/2007/03/28/fast-switching-with-linq.aspx and my first thought was; can something similar (or even better) be done in D. :) I admit, I haven't even spent 5 mins thinking about how I might do it, but I've never been particularly good with the compile time features of D so I figured I'd post something here and see what the people who are good at it can come up with. So, consider this a little challenge .. it may turn out to be trivial, it might not, I have no idea. R
Jun 11 2008
Regan Heath wrote:Hi all, I've been AWOL from this NG for a while but recently a friend of mine sent me a link to this: http://blogs.msdn.com/jomo_fisher/archive/2007/03/28/fast-swit hing-with-linq.aspx and my first thought was; can something similar (or even better) be done in D. :) I admit, I haven't even spent 5 mins thinking about how I might do it, but I've never been particularly good with the compile time features of D so I figured I'd post something here and see what the people who are good at it can come up with. So, consider this a little challenge .. it may turn out to be trivial, it might not, I have no idea.A language that allows you to write switch statements with strings should have this built-in. But that is actually a quality of implementation issue, and therefore transparent to the spec. In real life, if I were to tackle this and there were less than two dozen words, I'd figure it out manually. This would also allow for optimizations, like if I expect some of the words to occur more often than others. If this is something I expect to come across regularly, I'd write a small D program that takes as input a list of words with their number and another number that hints at how often I expet the word. This program would output the bit of D source code that I'd paste into my application. --- There ought to be a site collecting code snippets, idioms, and tiny utility programs that help in programming. And with good search capabilities.
Jun 11 2008
Georg Wrede wrote:If this is something I expect to come across regularly, I'd write a small D program that takes as input a list of words with their number and another number that hints at how often I expet the word. This program would output the bit of D source code that I'd paste into my application.Which can be done with a string mixin + CTFE function ;-P. String mixins, IMO, are the most powerful concept in D.
Jun 11 2008
== Quote from Georg Wrede (georg nospam.org)'s articleRegan Heath wrote:I've done this quite often with simple parsers: switch( token[0] ) { case 'o': if( strcmp( token, "one" ) ) throw new ParseFailure(); // blah break; case 't': if( !strcmp( token, "two" ) ) // stuff for "two" else if( !strcmp( token, "three" ) ) // stuff for "three" break; } It's easy enough to do it all with switch statements as well, but I like the insurance that checking the entire string provides. It would be nice if DMD optimized string-based switch statements this way. I'll admit to being a bit old-school in this respect--I've never actually used string switch statements in D. SeanHi all, I've been AWOL from this NG for a while but recently a friend of mine sent me a link to this: http://blogs.msdn.com/jomo_fisher/archive/2007/03/28/fast-switching-with-linq.aspx and my first thought was; can something similar (or even better) be done in D. :) I admit, I haven't even spent 5 mins thinking about how I might do it, but I've never been particularly good with the compile time features of D so I figured I'd post something here and see what the people who are good at it can come up with. So, consider this a little challenge .. it may turn out to be trivial, it might not, I have no idea.A language that allows you to write switch statements with strings should have this built-in. But that is actually a quality of implementation issue, and therefore transparent to the spec. In real life, if I were to tackle this and there were less than two dozen words, I'd figure it out manually. This would also allow for optimizations, like if I expect some of the words to occur more often than others.
Jun 11 2008
Sean Kelly wrote:== Quote from Georg Wrede (georg nospam.org)'s articleI thought in a discussion earlier with Walter (a few years back) that DMD would generate a string hash and then jump to that hash when you use a string as the input. In many cases the branching will be the expensive part so I guess a hash is pretty close to optimal performance for the general case. Although with optimisation you can always make things run faster. -JoelRegan Heath wrote:I've done this quite often with simple parsers: switch( token[0] ) { case 'o': if( strcmp( token, "one" ) ) throw new ParseFailure(); // blah break; case 't': if( !strcmp( token, "two" ) ) // stuff for "two" else if( !strcmp( token, "three" ) ) // stuff for "three" break; } It's easy enough to do it all with switch statements as well, but I like the insurance that checking the entire string provides. It would be nice if DMD optimized string-based switch statements this way. I'll admit to being a bit old-school in this respect--I've never actually used string switch statements in D. SeanHi all, I've been AWOL from this NG for a while but recently a friend of mine sent me a link to this: http://blogs.msdn.com/jomo_fisher/archive/2007/03/28/fast-switching-with-linq.aspx and my first thought was; can something similar (or even better) be done in D. :) I admit, I haven't even spent 5 mins thinking about how I might do it, but I've never been particularly good with the compile time features of D so I figured I'd post something here and see what the people who are good at it can come up with. So, consider this a little challenge .. it may turn out to be trivial, it might not, I have no idea.A language that allows you to write switch statements with strings should have this built-in. But that is actually a quality of implementation issue, and therefore transparent to the spec. In real life, if I were to tackle this and there were less than two dozen words, I'd figure it out manually. This would also allow for optimizations, like if I expect some of the words to occur more often than others.
Jun 11 2008
"janderson" <askme me.com> wrote in message news:g2q2bp$13ef$3 digitalmars.com...I thought in a discussion earlier with Walter (a few years back) that DMD would generate a string hash and then jump to that hash when you use a string as the input. In many cases the branching will be the expensive part so I guess a hash is pretty close to optimal performance for the general case. Although with optimisation you can always make things run faster. -JoelGuessing won't be necessary :) The implementation of string switches is in dmd/src/phobos/internal/switch.d. The compiler puts the case names into a sorted array of strings, and the string switch just does a binary search on it. It's somewhat optimized though -- it won't actually do a string comparison unless the strings are of the same length and their first character is the same.
Jun 12 2008
Jarrett Billingsley wrote:"janderson" <askme me.com> wrote in message news:g2q2bp$13ef$3 digitalmars.com...ic. I guess I should have just looked. I guess if anyone wanted to write a faster one they could send an updated version to Walter (after they had run many benchmark tests). -JoelI thought in a discussion earlier with Walter (a few years back) that DMD would generate a string hash and then jump to that hash when you use a string as the input. In many cases the branching will be the expensive part so I guess a hash is pretty close to optimal performance for the general case. Although with optimisation you can always make things run faster. -JoelGuessing won't be necessary :) The implementation of string switches is in dmd/src/phobos/internal/switch.d. The compiler puts the case names into a sorted array of strings, and the string switch just does a binary search on it. It's somewhat optimized though -- it won't actually do a string comparison unless the strings are of the same length and their first character is the same.
Jun 12 2008
"Regan Heath" <regan netmail.co.nz> wrote in message news:g2o13n$1oka$1 digitalmars.com...Hi all, I've been AWOL from this NG for a while but recently a friend of mine sent me a link to this: http://blogs.msdn.com/jomo_fisher/archive/2007/03/28/fast-switching-with-linq.aspxIsn't the internet great? I started on that page (interesting read) and a few clicks later I ended up at http://www.cs.cmu.edu/~rwh/theses/okasaki.pdf "Purely Functional Data Structures", it's a paper from 1996! Some quotes: "[S]trict languages can describe worst-case data structures, but not amortized ones, and lazy languages can describe amortized data structures, but not worst-case ones. To be able to describe both kinds of data structures, we need a programming language that supports both evaluation orders." "Functional programmers have long debated the relative merits of strict and lazy evaluation. This thesis shows that both are algorithmically im- portant and suggests that the ideal functional language should seamlessly integrate both."
Jun 11 2008
On 2008-06-12 04:56:33 +0200, "Lionello Lunesu" <lionello lunesu.remove.com> said:"Regan Heath" <regan netmail.co.nz> wrote in message news:g2o13n$1oka$1 digitalmars.com...Isn'tHi all, I've been AWOL from this NG for a while but recently a friend of mine sent me a link to this: http://blogs.msdn.com/jomo_fisher/archive/2007/03/28/fast-switching-with-linq.aspxHe also wrote a very nice book taken from his papers: Chris Okasaki Purely Functional Data Structures http://www.amazon.com/Purely-Functional-Structures-Chris-Okasaki/dp/0521663504/ref=sr_1_1?ie=UTF8&s=books&qid=1213262984&sr=8-1 Fawzithe internet great? I started on that page (interesting read) and a few clicks later I ended up at http://www.cs.cmu.edu/~rwh/theses/okasaki.pdf "Purely Functional Data Structures", it's a paper from 1996! Some quotes: "[S]trict languages can describe worst-case data structures, but not amortized ones, and lazy languages can describe amortized data structures, but not worst-case ones. To be able to describe both kinds of data structures, we need a programming language that supports both evaluation orders." "Functional programmers have long debated the relative merits of strict and lazy evaluation. This thesis shows that both are algorithmically im- portant and suggests that the ideal functional language should seamlessly integrate both."
Jun 12 2008
Fawzi Mohamed:He also wrote a very nice book taken from his papers: Chris Okasaki Purely Functional Data Structures http://www.amazon.com/Purely-Functional-Structures-Chris-Okasaki/dp/0521663504/ref=sr_1_1?ie=UTF8&s=books&qid=1213262984&sr=8-1Oh, you have found the book I was talking about here: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=72494 I think the author wants to write a new edition of that book soon. Bye, bearophile
Jun 12 2008