Section 14.12

Half-open interval

Definition

A half-open interval is a range that includes its lower endpoint and excludes its upper endpoint. It is written [a,b)[a, b): the square bracket marks an endpoint that belongs to the range and the round parenthesis one that does not, so the range holds every xx with a≤x<ba \le x < b. In programming the endpoints are usually whole numbers, such as array §14.5 indexes or memory addresses, and [a,b)[a, b) then holds exactly b−ab - a positions. Mathematics also calls (a,b](a, b] half-open; in programming the term normally means [a,b)[a, b).

Where it fits

Main entry: Interval data structure (§14.11).

The interval data structure entry describes structures that store many intervals and find those that overlap a query; this entry covers the endpoint convention they use. Python’s §12.23 range(a, b) and slice s[a:b] stop before b, and C++ marks a sequence by its first element and the position just past its last.

The convention has three useful properties.

All empty intervals hold nothing, but each still records a position. Searching the sorted array ⟨2,5,9⟩\langle 2, 5, 9 \rangle, with positions 0, 1 and 2, for the value 7 can return [2,2)[2, 2): no element equals 7, and position 2 is where 7 would be inserted.

In C, [p,p)[p, p) with pp one past the end of a buffer is a valid empty range: a pointer one past the last element of an array may be formed and compared, but not read through. To test that ℓ\ell bytes at offset oo fit in nn bytes, test ℓ≤n\ell \le n and o≤n−ℓo \le n - \ell; the overflow-safe range check §4.17 entry explains why o+ℓ≤no + \ell \le n can wrongly pass when the sum wraps around.

Limits and common mistakes

Converting to an inclusive last position breaks on the empty range. With 32-bit unsigned integers, b−1b - 1 for [0,0)[0, 0) wraps around to 4,294,967,295, so a loop that runs while i≤b−1i \le b - 1 never stops on its own and reads far past the array. Loop while i<bi < b instead.

The common overlap test for [a,b)[a, b) and [c,d)[c, d), a<da < d and c<bc < b, assumes both are nonempty: for [5,5)[5, 5) and [3,8)[3, 8) it reports an overlap, since 5<85 < 8 and 3<53 < 5. The exact test is max(a,c)<min(b,d)\max(a, c) < \min(b, d), here 5<55 < 5, which is false.

Covering all 2322^{32} addresses of a 32-bit machine needs the end value 2322^{32}, which only a wider integer type holds.

Questions answered

How is an empty interval represented?

An empty interval is represented as [a,a)[a, a): a half-open interval, which includes its lower endpoint and excludes its upper endpoint, with both endpoints equal. It contains no position, because no xx satisfies a≤x<aa \le x < a, and its length a−aa - a is 0, so length code needs no special case. Any aa works, and aa can carry meaning, such as where a missing value would be inserted. Do not represent emptiness by a>ba > b, which should be rejected as invalid, and do not convert [a,a)[a, a) to an inclusive last position a−1a - 1, which wraps around to the largest unsigned value when a=0a = 0.

Related entries: Interval data structure (§14.11), Overflow-safe range check (§4.17), Array (§14.5).