Re: How about a Hash template?

2011-05-02 Thread Kai Meyer
On 04/29/2011 07:13 AM, Jacob Carlborg wrote: On 2011-04-28 21:46, Timon Gehr wrote: Andrei Alexandrescu wrote: On 4/28/11 11:00 AM, Alexander wrote: On 28.04.2011 17:46, Andrej Mitrovic wrote: It works, but it makes the if statement ugly again. :) Well, just a bit - still much better

Re: How about a Hash template?

2011-04-30 Thread Andrei Alexandrescu
On 4/29/11 8:50 AM, Denis Koroskin wrote: On Thu, 28 Apr 2011 03:10:15 +0400, Andrej Mitrovic andrej.mitrov...@gmail.com wrote: I often see code written like this: if (value == somevalue || value == someothervalue || value == yetanothervalue); You could use the switch statement. But that

Re: How about a Hash template?

2011-04-30 Thread KennyTM~
On Apr 30, 11 03:12, Andrei Alexandrescu wrote: On 4/29/11 8:50 AM, Denis Koroskin wrote: On Thu, 28 Apr 2011 03:10:15 +0400, Andrej Mitrovic andrej.mitrov...@gmail.com wrote: I often see code written like this: if (value == somevalue || value == someothervalue || value == yetanothervalue);

Re: How about a Hash template?

2011-04-30 Thread Andrei Alexandrescu
On 4/29/11 2:44 PM, KennyTM~ wrote: [snip] You need to replace the assert and compile with -O -release -inline. My results: find in array: 163 compile-time 1: 10 either: 17 straight comparison: 8 compile-time 2: 11 Code: import std.datetime, std.algorithm, std.typecons, std.stdio; auto

Re: How about a Hash template?

2011-04-30 Thread Alexander
On 29.04.2011 21:58, Andrei Alexandrescu wrote: You need to replace the assert and compile with -O -release -inline. My results: [snip] Still, straight comparison wins - 2x faster ;) /Alexander

Re: How about a Hash template?

2011-04-30 Thread Denis Koroskin
On Sat, 30 Apr 2011 03:19:37 +0400, Alexander aldem+dm...@nk7.net wrote: On 29.04.2011 21:58, Andrei Alexandrescu wrote: You need to replace the assert and compile with -O -release -inline. My results: [snip] Still, straight comparison wins - 2x faster ;) /Alexander That's just an

Re: How about a Hash template?

2011-04-30 Thread Andrei Alexandrescu
On 4/29/11 6:21 PM, Denis Koroskin wrote: On Sat, 30 Apr 2011 03:19:37 +0400, Alexander aldem+dm...@nk7.net wrote: On 29.04.2011 21:58, Andrei Alexandrescu wrote: You need to replace the assert and compile with -O -release -inline. My results: [snip] Still, straight comparison wins - 2x

Re: How about a Hash template?

2011-04-30 Thread Alexander
On 30.04.2011 01:23, Andrei Alexandrescu wrote: Yah. I think the difference is immaterial outside inner loops. If it were like 20x slower that would've been a problem. Outside of inner loops even 20x is not a real problem ;) /Alexander

Re: How about a Hash template?

2011-04-30 Thread Alexander
On 30.04.2011 01:21, Denis Koroskin wrote: That's just an optimizer issue. Even if you replace the code body with return false; (i.e. not found) it's STILL slower than straight comparison. 2x? Hardly, IMHO. /Alexander

Re: How about a Hash template?

2011-04-30 Thread Sean Cavanaugh
On 4/29/2011 6:19 PM, Alexander wrote: On 29.04.2011 21:58, Andrei Alexandrescu wrote: You need to replace the assert and compile with -O -release -inline. My results: [snip] Still, straight comparison wins - 2x faster ;) /Alexander When understanding the CPU platform you are on, one of

Re: How about a Hash template?

2011-04-29 Thread Jacob Carlborg
On 2011-04-28 18:20, Andrej Mitrovic wrote: Well, DMD accepts this kind of syntax already: if (value == val1, val2, val3) { } I think this turns the expression value==val1 into a bool, and then turns val2 and val3 into bools and compares against each of them. Or something like that. It's odd

Re: How about a Hash template?

2011-04-29 Thread Robert Clipsham
On 29/04/2011 10:31, Jacob Carlborg wrote: On 2011-04-28 18:20, Andrej Mitrovic wrote: Well, DMD accepts this kind of syntax already: if (value == val1, val2, val3) { } I think this turns the expression value==val1 into a bool, and then turns val2 and val3 into bools and compares against each

Re: How about a Hash template?

2011-04-29 Thread Peter Alexander
On 29/04/11 10:31 AM, Jacob Carlborg wrote: On 2011-04-28 18:20, Andrej Mitrovic wrote: Well, DMD accepts this kind of syntax already: if (value == val1, val2, val3) { } I think this turns the expression value==val1 into a bool, and then turns val2 and val3 into bools and compares against

Re: How about a Hash template?

2011-04-29 Thread Jacob Carlborg
On 2011-04-28 21:46, Timon Gehr wrote: Andrei Alexandrescu wrote: On 4/28/11 11:00 AM, Alexander wrote: On 28.04.2011 17:46, Andrej Mitrovic wrote: It works, but it makes the if statement ugly again. :) Well, just a bit - still much better than (var == ... || var = ...|| var == ...

Re: How about a Hash template?

2011-04-29 Thread Jacob Carlborg
On 2011-04-29 14:22, Peter Alexander wrote: On 29/04/11 10:31 AM, Jacob Carlborg wrote: On 2011-04-28 18:20, Andrej Mitrovic wrote: Well, DMD accepts this kind of syntax already: if (value == val1, val2, val3) { } I think this turns the expression value==val1 into a bool, and then turns val2

Re: How about a Hash template?

2011-04-29 Thread Denis Koroskin
On Thu, 28 Apr 2011 03:10:15 +0400, Andrej Mitrovic andrej.mitrov...@gmail.com wrote: I often see code written like this: if (value == somevalue || value == someothervalue || value == yetanothervalue); You could use the switch statement. But that introduces indentation, and is rarely

Re: How about a Hash template?

2011-04-28 Thread Alexander
On 28.04.2011 01:10, Andrej Mitrovic wrote: I really like the in keyword, and I also like hashes since they can potentially speed up look-ups compared to conventional arrays. The problem is, potentially means for [relatively] large sets, as hashing overhead will kill the benefits for

Re: How about a Hash template?

2011-04-28 Thread Andrej Mitrovic
You seem to be right. It looks like DMD is pretty good at optimizing if statements. The Hash version is quite a bit slower actually. Well, that's what happens when I assume things. :) Here's a little benchmark of searching an array and hash of randomly generated strings:

Re: How about a Hash template?

2011-04-28 Thread Alexander
On 28.04.2011 16:25, Andrej Mitrovic wrote: Here's a little benchmark of searching an array and hash of randomly generated strings: Make it 16 values and run again :) Sure, the more elements we have, the better hash performs. But in case of ints/floats, for instance, for small sets

Re: How about a Hash template?

2011-04-28 Thread Andrej Mitrovic
InSet!(var, values...). That wouldn't work because var is known only at runtime. Ideally I'd like to write something like: string needle = foo; if (needle.InSet!(bar, barfoo, doo, foo)) { } And InSet would mixin and call a function such as: bool inSet(string needle) { if (needle = bar ||

Re: How about a Hash template?

2011-04-28 Thread Peter Alexander
On 28/04/11 4:24 PM, Andrej Mitrovic wrote: InSet!(var, values...). That wouldn't work because var is known only at runtime. Ideally I'd like to write something like: string needle = foo; if (needle.InSet!(bar, barfoo, doo, foo)) { } And InSet would mixin and call a function such as: bool

Re: How about a Hash template?

2011-04-28 Thread Andrej Mitrovic
It works, but it makes the if statement ugly again. :)

Re: How about a Hash template?

2011-04-28 Thread Alexander
On 28.04.2011 17:46, Andrej Mitrovic wrote: It works, but it makes the if statement ugly again. :) Well, just a bit - still much better than (var == ... || var = ... || var == ... ...), don't you think so? ;) If compiler would support special in usage, automatically expanding values on

Re: How about a Hash template?

2011-04-28 Thread Andrei Alexandrescu
On 4/28/11 11:00 AM, Alexander wrote: On 28.04.2011 17:46, Andrej Mitrovic wrote: It works, but it makes the if statement ugly again. :) Well, just a bit - still much better than (var == ... || var = ... || var == ... ...), don't you think so? ;) If compiler would support special in

Re: How about a Hash template?

2011-04-28 Thread Andrej Mitrovic
Well, DMD accepts this kind of syntax already: if (value == val1, val2, val3) { } I think this turns the expression value==val1 into a bool, and then turns val2 and val3 into bools and compares against each of them. Or something like that. It's odd and I've never seen it used anywhere. Maybe we

Re: How about a Hash template?

2011-04-28 Thread Jonathan M Davis
Well, DMD accepts this kind of syntax already: if (value == val1, val2, val3) { } I think this turns the expression value==val1 into a bool, and then turns val2 and val3 into bools and compares against each of them. Or something like that. It's odd and I've never seen it used anywhere.

Re: How about a Hash template?

2011-04-28 Thread Andrej Mitrovic
I know it's not doing that, it was a hypothetical. What I'm saying is I've never seen the comma used this way in code.

Re: How about a Hash template?

2011-04-28 Thread Jonathan M Davis
I know it's not doing that, it was a hypothetical. What I'm saying is I've never seen the comma used this way in code. Well, it would almost certainly be bad practice to do so. The comma operator can be very useful in some cases (such as for loops) but should generally be avoided. Here, it's

Re: How about a Hash template?

2011-04-28 Thread Timon Gehr
Andrei Alexandrescu wrote: On 4/28/11 11:00 AM, Alexander wrote: On 28.04.2011 17:46, Andrej Mitrovic wrote: It works, but it makes the if statement ugly again. :) Well, just a bit - still much better than (var == ... || var = ... || var == ... ...), don't you think so? ;) If

Re: How about a Hash template?

2011-04-28 Thread Alexander
On 28.04.2011 21:46, Timon Gehr wrote: Why is 'in' not currently defined on arrays? To me it seems like a left-out that should be fixed, because it has quite obvious and useful semantics. +1. It would be very useful and convenient this way, especially if search will be optimized to series

Re: How about a Hash template?

2011-04-28 Thread Torarin
2011/4/29 Alexander aldem+dm...@nk7.net: On 28.04.2011 21:46, Timon Gehr wrote: Why is 'in' not currently defined on arrays? To me it seems like a left-out that should be fixed, because it has quite obvious and useful semantics.  +1. It would be very useful and convenient this way,

How about a Hash template?

2011-04-27 Thread Andrej Mitrovic
I often see code written like this: if (value == somevalue || value == someothervalue || value == yetanothervalue); You could use the switch statement. But that introduces indentation, and is rarely used for a couple of values. I really like the in keyword, and I also like hashes since

Re: How about a Hash template?

2011-04-27 Thread Andrej Mitrovic
I guess also a hash() for runtime values would be nice, e.g.: http://codepad.org/a3aCokKT

Re: How about a Hash template?

2011-04-27 Thread bearophile
Andrej Mitrovic: http://codepad.org/a3aCokKT Suggestion for the run-time version: assert(color in hash(red, green, blue)); == assert(color in set([red, green, blue])); Bye, bearophile