Good to know!<div>That might be useful for me too.</div><div><br clear="all"><div>Best regards,</div><div>Max</div><br><br>
<br><br><div class="gmail_quote">On Tue, Nov 15, 2011 at 9:16 PM, Anthony Molinaro <span dir="ltr"><<a href="mailto:anthonym@alumni.caltech.edu">anthonym@alumni.caltech.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
I had a similar problem where I had to do 10182857 ifs, which I solved with<br>
and ordered set ets table. I generate the table ahead of time since it can<br>
take a while, and use ets:file2tab/1 to read it in quickly (takes about 5<br>
minutes to generate but only a few seconds to load).<br>
<br>
You use the start of your range as the key of the ets table, the value<br>
is whatever you want, in your case you could use the top end of the range,<br>
or the index.<br>
<br>
To look thing up you then use something like<br>
<br>
case ets:lookup (Tab, X) of<br>
[] -><br>
case ets:lookup (Tab, ets:prev (Tab, X)) of<br>
[] -> out_of_range;<br>
V -> hd (V)<br>
end;<br>
V -> hd (V)<br>
end<br>
<br>
Even with the double lookups, its still very fast (< .2 millis per lookup).<br>
Might be overkill for just 100 ifs but useful if you have 10 million.<br>
<br>
-Anthony<br>
<br>
PS. Thanks to Paul Mineiro and Ulf Wiger who I believe were the ones who<br>
first turned me on to this technique.<br>
<div class="HOEnZb"><div class="h5"><br>
On Tue, Nov 15, 2011 at 04:27:22PM +0300, Max Bourinov wrote:<br>
> Hi guys,<br>
><br>
> I have a value X which is integer, and I have a list of ranges<br>
> [0....200),[200....600),[600...1000)....etc....[100000, infinity] (this is<br>
> just an example). We can assume that the list is static.<br>
><br>
> I have frequently check the index of the range X belongs to.<br>
><br>
> How would you implement it in Erlang?<br>
><br>
> Best regards,<br>
> Max<br>
<br>
</div></div><div class="HOEnZb"><div class="h5">> _______________________________________________<br>
> erlang-questions mailing list<br>
> <a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
> <a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br>
<br>
</div></div><span class="HOEnZb"><font color="#888888">--<br>
------------------------------------------------------------------------<br>
Anthony Molinaro <<a href="mailto:anthonym@alumni.caltech.edu">anthonym@alumni.caltech.edu</a>><br>
</font></span></blockquote></div><br></div>