indexing - Data structure to represent piecewise continuous range? -
say have integer-indexed array of length 400, , want drop out few elements beginning, lots end, , middle too, without altering original array. is, instead of looping through array using indices {0...399}
, want use piecewise continuous range such as
{3...15} ∪ {18...243} ∪ {250...301} ∪ {305...310}
what data structure describe kind of index ranges? obvious solution make "index mediator" array, containing mappings continuos zero-based indexing new coordinates above, feels quite wasteful, since elements in sequential numbers, few occasional "jumps". besides, if find that, oh, want modify range bit? whole index array have rebuilt. not nice.
a few points note:
- the ranges never overlap. if new range added data structure, , overlaps existing ranges, whole thing should merged. is, if add above example range
{300... 308}
, should instead replace last 2 ranges{250...310}
. - it should quite cheap loop through whole range.
- it should relatively cheap query value directly: "give me original index corresponding 42nd index in mapped coordinates".
- it should possible (though maybe not quite cheap) work other way round: "give me mapped coordinate corresponding 42 in original coordinates, or tell if it's mapped @ all."
before rolling own solution, i'd know if there exists well-known data structure solves class of problems elegantly.
thanks!
seems array or list of integer pairs best data structure. choice whether second integer of pair end point or count first integer.
edit: on further reflection, problem database index has do. if integer pairs don't have in numeric order, can handle splits easier. if number sequence has remain in order, need data structure allows add integer pairs middle of array or list.
a split having change (6, 12) integer pair (6, 9) (11, 12), when 10 removed, example.
besides, if find that, oh, want modify range bit? whole index array have rebuilt. not nice.
true. perhaps 1 integer pair needs change. worst case, you'd have rebuild entire array or list.
Comments
Post a Comment