A dictionary object that uses ranges of values for keys

前端 未结 8 846
情深已故
情深已故 2020-11-27 19:26

I have need of a sort of specialized dictionary. My use case is this: The user wants to specify ranges of values (the range could be a single point as well) and assign a v

8条回答
  •  没有蜡笔的小新
    2020-11-27 19:58

    A dictionary is not the appropriate data structure for the operations you are describing.

    If the intervals are required to never overlap then you can just build a sorted list of intervals and binary search it.

    If the intervals can overlap then you have a more difficult problem to solve. To solve that problem efficiently you'll want to build an interval tree:

    http://en.wikipedia.org/wiki/Interval_tree

    This is a well-known data structure. See "Introduction To Algorithms" or any other decent undergraduate text on data structures.

提交回复
热议问题