[erlang-questions] How would you do 100 ifs?

Knut Nesheim knutin@REDACTED
Tue Nov 15 15:06:49 CET 2011


On Tue, Nov 15, 2011 at 2:27 PM, Max Bourinov <bourinov@REDACTED> wrote:
> I have a value X which is integer, and I have a list of ranges
> [0....200),[200....600),[600...1000)....etc....[100000, infinity]

Hi Max,

Some time ago I had to solve the exact same problem. What I wanted was
fast lookups and low memory overhead. My data is not changing often.
Storing each key and value in a structure where you could lookup based
on key (dict, array or your own tuple), would require storing every
possible key in the range, this was not acceptable for me.

My solution was to implement a binary tree, where the key instead of
being an exact key, is a range of keys. So the key could be {100, 200}
and the value would be 'foo'. If you are searching for 101, the value
returned is 'foo'. My implementation is on github[1]

 [1]: https://github.com/knutin/tree_range

Regards
Knut



More information about the erlang-questions mailing list