A circular buffer is a fixed-capacity queue backed by an array. It tracks the head, tail, and current count; insertion and removal advance indexes that wrap from the array’s end to its beginning. section 6.3.1
The core operations follow a simple contract:
- add(value) inserts at the tail, advances the tail, and requires that the buffer is not full. section 6.3.1 section 6.3.2
- remove() returns the value at the head, advances the head, and requires that the buffer is not empty. section 6.3.1 section 6.3.2
- Programming by contract expresses these requirements with preconditions, postconditions, and a class invariant checking valid head and tail indexes. section 6.3 section 6.3.3
Filling, draining, and detecting misuse
A capacity-five buffer accepts 10, 20, 30, 40, and 50, then removes them in the same order. Calling remove() once more violates its precondition and triggers an assertion failure; callers should check get_count() first.
