Definition
An interval data structure is a data structure that stores intervals, ranges of numbers such as memory addresses or times given by a start and an end, and answers questions about them: which stored intervals contain a point, which overlap a query interval, or whether a query interval lies inside a stored one. Common forms are a sorted array §14.5 of non-overlapping intervals, searched by binary search, and an interval tree: a balanced search tree §14.10 ordered by start, whose nodes also record the largest end in their subtree.
The idea
A memory protection check must decide whether a program may read 4 bytes at address 6. The allowed memory is a list of regions, each a range of addresses with its own permissions. With 3 regions, the check can test each one. With 10,000 regions, such as the memory blocks that a memory-error detector tracks, testing every region on every access is too slow, and the check needs a structure that finds the relevant region in a few steps. The check also needs the right question: the access must lie entirely inside one region it may use, not merely touch one.
Every interval structure first needs an endpoint convention. This entry uses half-open intervals §14.12: contains every number with , so it includes its start, excludes its end and has length . A hotel booking calendar is a fair picture. A stay from the 3rd to the 8th of a month occupies the nights of the 3rd to the 7th, and a guest who arrives on the 8th does not conflict with it. Half-open intervals behave the same way: and share no point. The picture stops at the question asked. A hotel needs to know only whether two stays overlap, while an access check needs to know whether the access is contained in one allowed region.
How it works
Three tests answer the basic questions. Let be a stored interval and a query interval, both nonempty, meaning and .
Point membership: a point lies in when and .
Overlap: the two intervals share at least one point when , where picks the larger of two values and the smaller. In words, the later start must come before the earlier end. The shared part is .
Containment: lies inside when and .
Sorted array of disjoint intervals. Keep the intervals in an array sorted by start, with no two overlapping, so that each one ends at or before the start of the next. To find the interval that contains a point , use binary search, which halves the candidate range at each step, to find the last interval whose start is at most ; lies in that interval if is below its end, and in no interval otherwise. To test containment of , find the interval that contains and check that is at most its end. No other interval needs checking, because the intervals are disjoint and only one of them contains . Each query takes about steps for intervals. An insertion must keep the array sorted and disjoint, which can mean moving up to entries and rejecting or merging an overlapping interval.
Interval tree. When stored intervals may overlap, a balanced search tree holds one interval per node, ordered by start, and each node also stores maxend, the largest end among all intervals in its subtree. To find an interval that overlaps the query :
Start at the root.
If the node’s interval overlaps , report it and stop.
Otherwise, if the node has a left child whose
maxendis greater than , go left. If no interval on the left overlaps the query, then none on the right does either: the left interval that ends latest ends after , so it fails to overlap only by starting at or after , and every interval on the right starts no earlier than it.Otherwise go right. If there is no child to go to, report that no interval overlaps.
The search visits one node per level, so it takes steps, meaning at most a constant times . Every insertion, removal and rotation must recompute maxend for the nodes whose subtrees changed, from the bottom up. A stale maxend makes the search skip a subtree that holds an overlap.
Machine endpoints. Endpoints are stored as machine integers of fixed width. Computing the end of an access as start plus length can pass the largest representable value and wrap around to a small number, as unsigned wraparound §4.16 does. An overflow-safe range check §4.17 compares differences instead of sums.
Worked example
Two memory regions are adjacent: holds addresses 0 to 7, and holds addresses 8 to 15. Address 8 lies only in . For , holds but fails; for , and both hold. The two regions do not overlap: and , and is false.
A 4-byte access at address 6 uses addresses 6, 7, 8 and 9, the interval . With : and , and , so they overlap in , 2 bytes. With : and , and , so they overlap in , 2 bytes. Containment in needs , true, and , false. Containment in needs , false. The access overlaps both regions and lies inside neither, so a rule that every access must stay within one region rejects it, although an overlap query answers yes for each region.
An interval tree holds at the root, as its left child and as its right child. The maxend values are 3 for the left child, 20 for the right child, and for the root. For the query , the root’s interval overlaps, because is less than . For the query , the root does not overlap, because is not less than . The left child’s maxend is 3, which is not greater than 3, so the search skips the left side and goes right. There, is not less than , and the node has no children, so no stored interval overlaps . That is correct: is exactly the gap between and .
Overflow with 32-bit unsigned addresses: let the region be and the access 4 bytes at . The sum needs 33 bits, and kept to 32 bits it becomes . The naive test and is then true, so an access far outside the region is accepted. The overflow-safe form checks , then , then . Here and , so the last check fails and the access is rejected.
Stated precisely
Let be integers, and let be the set of integers with . Write and for the larger and smaller of two values, for “ is an element of the set ”, for the elements common to the sets and , for the empty set, and for “every element of is in ”. For nonempty intervals, with and :
if and only if ;
if and only if , which is equivalent to and ;
if and only if and .
Adjacent intervals and , with , never overlap, because the overlap test becomes .
Empty and malformed intervals need explicit rules. An empty interval, with start equal to end, contains no point, and the maximum-start/minimum-end test correctly reports no overlap. The shorter test and needs the nonempty assumption. For and the empty query , both inequalities are true even though there is no shared point. The containment test, however, reports an empty query as not contained when or , although the empty set is contained in every set, so a structure must decide whether zero-length queries are allowed. An interval with start greater than end is malformed and must be rejected before any test, because the containment test can accept it: passes and against .
Each structure rests on an invariant that every update must preserve. For a sorted array of disjoint intervals , the invariant is for every from 1 to , and . For an interval tree, the starts obey the search-tree order, and every node’s maxend equals the largest of its own end and the maxend values of its children. All tests assume that the endpoints were computed without overflow.
Limits and common mistakes
Overlap is not containment. An access that overlaps an allowed region can still reach outside it, so access checks need containment in one region.
Adjacent regions are not one region. Merging and into is correct only if both regions have the same permissions and the same lifetime.
Mixing conventions causes off-by-one errors. A closed interval includes its end, so the closed interval and the half-open interval describe the same 8 addresses. Storing one form and testing with the formulas of the other admits one address too many, or one too few, at the boundary.
A half-open end can be unrepresentable. A region that reaches the last 32-bit address, 0xFFFFFFFF, has the end , which does not fit in 32 bits. Such regions are stored as a start and a length, or as a start and a last address, and the tests are adapted to match.
Computing an end as start plus length can overflow, as the example shows. Use an overflow-safe range check, which compares differences.
Overlapping stored intervals need a rule for conflicts. When a point lies in several stored regions with different permissions, the structure must define which region decides, for example the most specific one or the most restrictive one.
The structure answers questions about numbers, not about authority. Finding a region proves nothing about whether its permissions are right, whether the region is still in use, or whether the memory changed after the check.
Questions answered
Do adjacent half-open intervals overlap?
No. Two adjacent half-open intervals and , with , share no point. A half-open interval contains every number with : it includes its start and excludes its end. The common endpoint is therefore excluded from the first interval and included in the second. The standard overlap test, that the later start must be less than the earlier end, gives , which is false. For example, holds 0 to 7 and holds 8 to 15.
The convention must be stated, because closed intervals, which include both ends, behave differently: and share the point 8. Adjacent half-open intervals do touch, which matters when they are merged into one contiguous range such as . A range that crosses the boundary, such as , overlaps both intervals but lies inside neither, which is why an access check tests containment, not overlap.
Related entries: Half-open interval (§14.12), Overflow-safe range check (§4.17), Helper contract (§2.21), Balanced search tree (§14.10), Unsigned modular arithmetic (§4.16).