node.js - How to make a map of range to a single value? -


what's data structure of choice map range single value? e.g. a has values 1-1000, b has values 1001-20000, efficient way answer question "what key of 11234?" (answer: b). straighforward way through keys , check if range contains searched value, seems performance worse every new key.

in particular case i'd prefer node.js solution, suppose should not matter.

you can use balanced tree. sort ranges lower bound in node keep info higher bound , of course mapped value.

   1001 (20000, a)   / 1 (1000, b) 

then when key x, return max key not grater x. it's standard binary tree lookup o(log n). in node have information range can check if x within range.

when 11234 node 1001 (20000 b). 11234 inside 1001-20000 value b. if 0 null/none key not mapped. if 1000,5 1 (1000 a). 1000,5 out of range key not mapped.

of course ranges can't overlap


Comments