Sub topic 3
Head and tail indices
This chapter assumes you know arrays with zero-based indexing and the FIFO queue idea (enqueue at the back, dequeue from the front). It also assumes basic Python. Wraparound comes in the next chapter, so the indices here only move forward.
Two numbers describe the whole queue
A ring buffer stores items in a fixed array. It never moves them. Two integer indices record where the queue starts and ends:
- head is the index of the oldest item. It is the slot that the next
popreads. - tail is the index of the next free slot. It is the slot that the next
pushwrites.
Reading happens at the head and writing happens at the tail. The two operations touch different slots, so neither one shifts any data. That is why both run in O(1) time.
The convention used in this tutorial
Libraries differ on what the tail means. Some store the index of the newest item instead of the next free slot. Both work, but the code must use one meaning everywhere. This tutorial always uses these rules:
headis the index of the oldest item.tailis the index where the next item will be written.pushwrites attail, then advancestail.popreads athead, then advanceshead.
Under these rules, the items in the queue sit in slots head up to, but not including, tail. Without wraparound, the item count is tail - head.
Watching the indices move
The sketch below has no wraparound and no safety checks. It exists only to show how the indices change.
buf = [None] * 5
head = 0 # oldest item
tail = 0 # next free slot
def push(item):
global tail
buf[tail] = item
tail += 1
def pop():
global head
item = buf[head]
head += 1
return item
push("A")
push("B")
push("C")
print(head, tail, tail - head) # 0 3 3
print(pop()) # A
print(head, tail, tail - head) # 1 3 2
push("D")
print(head, tail, tail - head) # 1 4 3Notice three things:
pushchanges onlytail, andpopchanges onlyhead.- Slot 0 still holds
"A"after the pop. Movingheadis what removes an item from the queue. Clearing the slot is optional housekeeping. - Two more pushes would take
tailto 5, which is past the end of the array. The next chapter fixes this by wrapping the index back to 0.
Empty is when the indices meet
At the start, head == tail == 0, and the queue is empty. After the same number of pushes and pops, the two indices meet again. So head == tail means empty in this sketch.
The sketch has no way to say "full". A full buffer will turn out to look the same as an empty one once wraparound is added. This is the full-versus-empty problem, covered in its own chapter.
Common errors
- Reading the tail slot. The tail points at a free slot, so
buf[tail]holds stale data orNone. The newest item is attail - 1, and that is only true before wraparound. - Advancing before the access. If
popincrementsheadfirst and then readsbuf[head], it returns the second-oldest item and skips the first. - Mixing conventions. If one function treats
tailas the next free slot and another treats it as the newest item, every count is off by one. - Changing both indices in one operation. In a plain queue,
pushowns the tail andpopowns the head. Keeping this ownership clear is what later makes a lock-free design possible.
Exercise
A five-slot buffer starts with head = 0 and tail = 0. You run push, push, push, pop, pop, push. What are head, tail, and the item count at the end, ignoring wraparound?
Show answer
head = 2, tail = 4, and the count is tail - head = 2. Four pushes move tail from 0 to 4, and two pops move head from 0 to 2. The queue holds the third and fourth items pushed.