Sub topic 4
Wrapping indices with modulo
This chapter assumes you know zero-based array indexing, the % operator, and basic Python or C syntax. It also assumes you have seen the head and tail indices from the previous chapter.
The problem at the end of the array
A ring buffer stores items in a plain array. Items are added at the tail and removed from the head. Both indices only move forward, so sooner or later one of them reaches the last slot. The next step must land on slot 0, not on slot capacity, which does not exist.
The modulo operator does this wrap in one expression. For an array of CAP slots:
next_index = (index + 1) % CAP% returns the remainder after division. Any result is always in the range 0 to CAP - 1, so it is always a valid slot.
Mapping a logical position to a slot
Modulo does more than step forward by one. It converts any offset from the head into a physical slot:
slot = (head + offset) % CAPHere offset is how far the item sits from the oldest item. Offset 0 is the head. Offset count - 1 is the newest item. The tail slot, where the next item goes, is (head + count) % CAP.
Worked example
Take a buffer with CAP = 4 whose head is at slot 2 and which holds four items. This script prints where each offset lands:
CAP = 4
head = 2
for offset in range(4):
print(offset, "->", (head + offset) % CAP)It prints:
| Offset | head + offset | Slot (mod 4) |
|---|---|---|
| 0 | 2 | 2 |
| 1 | 3 | 3 |
| 2 | 4 | 0 |
| 3 | 5 | 1 |
Offsets 2 and 3 wrap around to the front of the array. The data never moves. Only the arithmetic that locates it changes.
Common errors
Forgetting to wrap one of the indices. If you wrap the tail but increment the head with a bare head += 1, the head eventually runs past the array. Every place that advances an index needs the modulo.
Negative operands in C. Python's % always returns a result with the sign of the divisor, so -1 % 4 is 3. In C, the result takes the sign of the dividend, so -1 % 4 is -1, which is not a valid index. If you ever step backwards, for example to peek at the newest item, add the capacity first:
#include <stdio.h>
#define CAP 4
int main(void) {
int tail = 0;
int last = (tail + CAP - 1) % CAP; /* 3, not -1 */
printf("%d\n", last);
return 0;
}Unsigned wraparound that does not match the capacity. If an index is an unsigned integer that overflows on its own, the result stays correct only when CAP divides the integer's range evenly. A capacity of 10 breaks after a 32-bit overflow. A capacity of 8 does not. A later chapter uses this fact.
Cost. Modulo by a variable needs a division instruction, which is slower than addition. For most programs this does not matter. A later chapter shows how a power-of-two capacity replaces modulo with a cheaper bit mask.
Exercise
A buffer has CAP = 5, head = 3, and holds 4 items. Which slot does the next push write to, and which slot holds the newest item?
Show answer
The next push writes to (3 + 4) % 5 = 2. The newest item is at offset 3, so it sits in slot (3 + 3) % 5 = 1.