The basic motivation behind the circular_buffer
was to create a container which would work seamlessly
with STL.
Additionally, the design of the circular_buffer
was guided by the following principles:
-
Maximum efficiency for envisaged applications.
-
Suitable for general purpose use.
-
The behaviour of the buffer as intuitive as possible.
-
Suitable for specialization by means of adaptors.
(The
circular_buffer_space_optimized
is such an example of the adaptor.)
-
Easy to debug. (See Debug Support for details.)
In order to achieve maximum efficiency, the circular_buffer
and circular_buffer_space_optimized
store their elements in a contiguous region of memory,
which then enables:
-
Use of fixed memory and no implicit or unexpected memory allocation.
-
Fast constant-time insertion and removal of elements from the front and
back.
-
Fast constant-time random access of elements.
-
Suitability for real-time and performance critical applications.
Possible applications of the circular buffer include:
-
Storage of the most recently received samples, overwriting
the oldest as new samples arrive.
-
As an underlying container for a bounded buffer (see
the Bounded Buffer example, code at circular_buffer_bound_example.cpp).
-
A kind of cache storing a specified number of last
inserted elements.
-
Efficient fixed capacity FIFO (First In, First Out),
-
Efficient fixed capacity LIFO (Last In, First Out)
queue which removes the oldest (inserted as first) elements when full.