GithubHelp home page GithubHelp logo

raphiara / circ_buffer Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 29 KB

STL-like circular buffer

License: MIT License

CMake 1.95% C++ 98.05%
circular-buffer container cpp ring-buffer cpp11 stl-like

circ_buffer's Introduction

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).

#include "circ_buffer.hpp"
#include <iostream>
int main()
{
    raphia::circ_buffer<char> 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:

>> 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.

int main()
{
    raphia::circ_buffer<char> 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:

>> 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

git clone [email protected]:RaphiaRa/circ_buffer.git
mkdir circ_buffer/build; cd circ_buffer/build;
git submodule update --init
cmake ../
make
./Test

circ_buffer's People

Contributors

raphiara avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.