https://github.com/raphiara/circ_buffer
STL-like circular buffer
https://github.com/raphiara/circ_buffer
circular-buffer container cpp cpp11 ring-buffer stl-like
Last synced: 6 months ago
JSON representation
STL-like circular buffer
- Host: GitHub
- URL: https://github.com/raphiara/circ_buffer
- Owner: RaphiaRa
- License: mit
- Created: 2022-01-29T00:17:01.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2025-07-11T19:31:03.000Z (7 months ago)
- Last Synced: 2025-07-11T21:21:59.908Z (7 months ago)
- Topics: circular-buffer, container, cpp, cpp11, ring-buffer, stl-like
- Language: C++
- Homepage:
- Size: 30.3 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# circ_buffer
**Requirements**
A compiler that supports at least C++11
**Installation**
Copy the header file into your project and rename the namespace to whatever you want.
**Usage**
The usage of the circular buffer is similar to that of a STL container.
The circular buffer is initialized with a fixed buffer size, no hidden allocations
are done while using the circular buffer. Just like with STL containers, a custom allocator can be used if needed (std::allocator is used by default).
```c++
#include "circ_buffer.hpp"
#include
int main()
{
raphia::circ_buffer circ(64);
circ.push_back('h');
circ.push_back('e');
circ.push_back('l');
circ.push_back('l');
circ.push_back('o');
for (auto c : circ)
std::cout << c;
std::cout << std::endl;
}
```
Output:
```bash
>> hello
```
If data is pushed into the buffer via push_back and the buffer is out of space, then
the data at the beginning will be overwritten.
```c++
int main()
{
raphia::circ_buffer circ(4);
circ.push_back('h');
circ.push_back('e');
circ.push_back('l');
circ.push_back('l');
circ.push_back('o');
for (auto c : circ)
std::cout << c;
std::cout << std::endl;
}
```
Output:
```bash
>> ello
```
The behaviour is similar when emblace_back is used.
On the contrary, if data is pushed into the buffer via push_front, then
the data in the back will be overwritten if the buffer is full.
**Building & running the tests**
```bash
git clone git@github.com:RaphiaRa/circ_buffer.git
mkdir circ_buffer/build; cd circ_buffer/build;
git submodule update --init
cmake ../
make
./Test
```