Section 14.11

Interval data structure

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: [a,b)[a, b) contains every number xx with a≤x<ba \le x < b, so it includes its start, excludes its end and has length b−ab - a. 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: [3,8)[3, 8) and [8,12)[8, 12) 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 [a,b)[a, b) be a stored interval and [c,d)[c, d) a query interval, both nonempty, meaning a<ba < b and c<dc < d.

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 xx, use binary search, which halves the candidate range at each step, to find the last interval whose start is at most xx; xx lies in that interval if xx is below its end, and in no interval otherwise. To test containment of [c,d)[c, d), find the interval that contains cc and check that dd is at most its end. No other interval needs checking, because the intervals are disjoint and only one of them contains cc. Each query takes about log2n\log_2 n steps for nn intervals. An insertion must keep the array sorted and disjoint, which can mean moving up to nn 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 [c,d)[c, d):

  1. Start at the root.

  2. If the node’s interval overlaps [c,d)[c, d), report it and stop.

  3. Otherwise, if the node has a left child whose maxend is greater than cc, 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 cc, so it fails to overlap only by starting at or after dd, and every interval on the right starts no earlier than it.

  4. 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 O(logn)O(\log n) steps, meaning at most a constant times logn\log n. 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: R1=[0,8)R_1 = [0, 8) holds addresses 0 to 7, and R2=[8,16)R_2 = [8, 16) holds addresses 8 to 15. Address 8 lies only in R2R_2. For R1R_1, 0≤80 \le 8 holds but 8<88 < 8 fails; for R2R_2, 8≤88 \le 8 and 8<168 < 16 both hold. The two regions do not overlap: max(0,8)=8\max(0, 8) = 8 and min(8,16)=8\min(8, 16) = 8, and 8<88 < 8 is false.

A 4-byte access at address 6 uses addresses 6, 7, 8 and 9, the interval [6,6+4)=[6,10)[6, 6 + 4) = [6, 10). With R1R_1: max(0,6)=6\max(0, 6) = 6 and min(8,10)=8\min(8, 10) = 8, and 6<86 < 8, so they overlap in [6,8)[6, 8), 2 bytes. With R2R_2: max(8,6)=8\max(8, 6) = 8 and min(16,10)=10\min(16, 10) = 10, and 8<108 < 10, so they overlap in [8,10)[8, 10), 2 bytes. Containment in R1R_1 needs 0≤60 \le 6, true, and 10≤810 \le 8, false. Containment in R2R_2 needs 8≤68 \le 6, 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 [5,10)[5, 10) at the root, [0,3)[0, 3) as its left child and [12,20)[12, 20) as its right child. The maxend values are 3 for the left child, 20 for the right child, and max(10,3,20)=20\max(10, 3, 20) = 20 for the root. For the query [4,6)[4, 6), the root’s interval overlaps, because max(5,4)=5\max(5, 4) = 5 is less than min(10,6)=6\min(10, 6) = 6. For the query [3,5)[3, 5), the root does not overlap, because max(5,3)=5\max(5, 3) = 5 is not less than min(10,5)=5\min(10, 5) = 5. The left child’s maxend is 3, which is not greater than 3, so the search skips the left side and goes right. There, max(12,3)=12\max(12, 3) = 12 is not less than min(20,5)=5\min(20, 5) = 5, and the node has no children, so no stored interval overlaps [3,5)[3, 5). That is correct: [3,5)[3, 5) is exactly the gap between [0,3)[0, 3) and [5,10)[5, 10).

Overflow with 32-bit unsigned addresses: let the region be [a,b)=[𝟶𝚡𝟷𝟶𝟶𝟶,𝟶𝚡𝟸𝟶𝟶𝟶)[a, b) = [\texttt{0x1000}, \texttt{0x2000}) and the access 4 bytes at x=𝟶𝚡𝙵𝙵𝙵𝙵𝙵𝙵𝙵𝙴x = \texttt{0xFFFFFFFE}. The sum x+4=𝟶𝚡𝟷𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟸x + 4 = \texttt{0x100000002} needs 33 bits, and kept to 32 bits it becomes 𝟶𝚡𝟸\texttt{0x2}. The naive test a≤xa \le x and x+4≤bx + 4 \le b is then true, so an access far outside the region is accepted. The overflow-safe form checks x≥ax \ge a, then 4≤b−a4 \le b - a, then x−a≤(b−a)−4x - a \le (b - a) - 4. Here x−a=𝟶𝚡𝙵𝙵𝙵𝙵𝙴𝙵𝙵𝙴x - a = \texttt{0xFFFFEFFE} and (b−a)−4=𝟶𝚡𝙵𝙵𝙲(b - a) - 4 = \texttt{0xFFC}, so the last check fails and the access is rejected.

Stated precisely

Let a,b,c,d,xa, b, c, d, x be integers, and let [a,b)[a, b) be the set of integers xx with a≤x<ba \le x < b. Write max\max and min\min for the larger and smaller of two values, x∈Sx \in S for “xx is an element of the set SS”, S∩TS \cap T for the elements common to the sets SS and TT, ∅\emptyset for the empty set, and S⊆TS \subseteq T for “every element of SS is in TT”. For nonempty intervals, with a<ba < b and c<dc < d:

Adjacent intervals [a,b)[a, b) and [b,d)[b, d), with a<b<da < b < d, never overlap, because the overlap test becomes b<bb < b.

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 a<da < d and c<bc < b needs the nonempty assumption. For [0,8)[0,8) and the empty query [4,4)[4,4), both inequalities are true even though there is no shared point. The containment test, however, reports an empty query [c,c)[c, c) as not contained when c<ac < a or c>bc > b, 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: [5,3)[5, 3) passes 0≤50 \le 5 and 3≤83 \le 8 against [0,8)[0, 8).

Each structure rests on an invariant that every update must preserve. For a sorted array of disjoint intervals [a1,b1),…,[an,bn)[a_1, b_1), \dots, [a_n, b_n), the invariant is ai<bi≤ai+1a_i < b_i \le a_{i+1} for every ii from 1 to n−1n - 1, and an<bna_n < b_n. 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 [0,8)[0, 8) and [8,16)[8, 16) into [0,16)[0, 16) is correct only if both regions have the same permissions and the same lifetime.

Mixing conventions causes off-by-one errors. A closed interval [a,b][a, b] includes its end, so the closed interval [0,7][0, 7] and the half-open interval [0,8)[0, 8) 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 2322^{32}, 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 [a,b)[a, b) and [b,c)[b, c), with a<b<ca < b < c, share no point. A half-open interval [a,b)[a, b) contains every number xx with a≤x<ba \le x < b: it includes its start and excludes its end. The common endpoint bb 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 b<bb < b, which is false. For example, [0,8)[0, 8) holds 0 to 7 and [8,16)[8, 16) holds 8 to 15.

The convention must be stated, because closed intervals, which include both ends, behave differently: [0,8][0, 8] and [8,16][8, 16] share the point 8. Adjacent half-open intervals do touch, which matters when they are merged into one contiguous range such as [0,16)[0, 16). A range that crosses the boundary, such as [6,10)[6, 10), 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).