What is the best data structure for storing ranges for quick queries?

advertisements

I have this situation where I have N timelines each containing blocks. The blocks contain tokens with a specific index and know their maximum and minimum token indexes. There's also an index mapping blocks' first indexes to a (timeline, block) pair. Here is an example:

Timeline 1: [1 2 5 8 9 11] [14 17 18 21] [22 23 25 26] ...
Timeline 2: [3 4 6 7 10 12] [13 15 16 19 20 24] [27 28 34 45] ...

Index:
  1 -> timeline 1, block 1
  3 -> timeline 2, block 1
  13 -> timeline 2, block 2
  14 -> timeline 1, block 2
  22 -> timeline 1, block 3
  27 -> timeline 2, block 3

As you can see, there's no missing token (no gap).

Those data structures are what I have initially. What would be the best alternative data structure to optimize queries of a specific token index? Say I want to retrieve token 19. Now what I have to do is: a dichotomic search in the index to find the good blocks for each timeline, and then a full search within each block. With token 19, the dichotomic search would result in blocks (1, 2) and (2, 2) which can contain 19, and then do a full linear search to find token 19 (no dichotomic search within blocks is possible here since tokens have various sizes and are not contained in any data structure yet).

Thank you!

Edit: I'm thinking about using an interval tree containing intervals of all the timelines. The problem is that a query would still result in many intervals. Plus, it doesn't optimize too much compared to binary searches.


You could have an array A of t pointers to objects that hold a pointer to the token, its timeline, and block. If you can hold references in an array using whatever mechanism your language likes.. I'm not sure what you can do if you can't binary search inside blocks.