GithubHelp home page GithubHelp logo

janwilmans / ring_span Goto Github PK

View Code? Open in Web Editor NEW
15.0 4.0 3.0 137 KB

A ring_span implementation that allows zero construction and destruction

License: Boost Software License 1.0

CMake 0.20% C++ 99.80%
cpp23 span proposal

ring_span's Introduction

ring_span

Another search for the one ring. Thanks to Björn Fahller for writing the original code, I modified it to fit my use cases and extended it to make it have 'the usual methods' like capacity() and clear().

Usage:

    std::vector<int> v;
    v.resize(256);
    nostd::ring_span rs(v);
    rs.push_back(1);  // 'normal' push_back
    class Foo{ std::vector<int> values; } ;
    std::vector<Foo> v;
    v.resize(16);
    nostd::ring_span rs(v);
    Foo & foo = rs.push_back(); // modify / re-use existing elements without re-construction

features:

  • mostly constrexpr (with some obvious exceptions)
  • works on top of an existing container of choice, it must have .data() and .size()
  • allows insertion and removal without construction or destruction of contained objects
  • pushing a value beyond the capacity of the span overwrites the oldest value

restrictions:

  • requires to be used on memory area where the size is a power of 2

The restriction allows the span to be slightly more efficient.

design considerations:

  • keep the span simple
  • the span overwrites the oldest value if a value is pushed in when its full
  • minimal error handling, if needed it can layered on top, see safe_push
// example added exceptions for error handling
template <typename T>
void safe_push(nostd::ring_span<T> rs, T v)
{
    if (rs.size() == rs.capacity())
    {
        throw std::runtime_error("safe_push exceeds span capacity");
    }
    rs.push_back(v);
}

ring_span's People

Contributors

janwilmans avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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