[erlang-questions] How would you do 100 ifs?
Max Bourinov
bourinov@REDACTED
Tue Nov 15 20:50:14 CET 2011
Good to know!
That might be useful for me too.
Best regards,
Max
On Tue, Nov 15, 2011 at 9:16 PM, Anthony Molinaro <
anthonym@REDACTED> wrote:
> I had a similar problem where I had to do 10182857 ifs, which I solved with
> and ordered set ets table. I generate the table ahead of time since it can
> take a while, and use ets:file2tab/1 to read it in quickly (takes about 5
> minutes to generate but only a few seconds to load).
>
> You use the start of your range as the key of the ets table, the value
> is whatever you want, in your case you could use the top end of the range,
> or the index.
>
> To look thing up you then use something like
>
> case ets:lookup (Tab, X) of
> [] ->
> case ets:lookup (Tab, ets:prev (Tab, X)) of
> [] -> out_of_range;
> V -> hd (V)
> end;
> V -> hd (V)
> end
>
> Even with the double lookups, its still very fast (< .2 millis per lookup).
> Might be overkill for just 100 ifs but useful if you have 10 million.
>
> -Anthony
>
> PS. Thanks to Paul Mineiro and Ulf Wiger who I believe were the ones who
> first turned me on to this technique.
>
> On Tue, Nov 15, 2011 at 04:27:22PM +0300, Max Bourinov wrote:
> > Hi guys,
> >
> > I have a value X which is integer, and I have a list of ranges
> > [0....200),[200....600),[600...1000)....etc....[100000, infinity] (this
> is
> > just an example). We can assume that the list is static.
> >
> > I have frequently check the index of the range X belongs to.
> >
> > How would you implement it in Erlang?
> >
> > Best regards,
> > Max
>
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED
> > http://erlang.org/mailman/listinfo/erlang-questions
>
>
> --
> ------------------------------------------------------------------------
> Anthony Molinaro <anthonym@REDACTED>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20111115/132c5aa4/attachment.htm>
More information about the erlang-questions
mailing list