https://github.com/janwilmans/ring_span
A ring_span implementation that allows zero construction and destruction
https://github.com/janwilmans/ring_span
cpp23 proposal span
Last synced: 10 months ago
JSON representation
A ring_span implementation that allows zero construction and destruction
- Host: GitHub
- URL: https://github.com/janwilmans/ring_span
- Owner: janwilmans
- License: bsl-1.0
- Created: 2020-06-07T00:32:09.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-06-07T22:47:40.000Z (over 5 years ago)
- Last Synced: 2025-03-29T17:41:30.869Z (11 months ago)
- Topics: cpp23, proposal, span
- Language: C++
- Homepage:
- Size: 134 KB
- Stars: 16
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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 v;
v.resize(256);
nostd::ring_span rs(v);
rs.push_back(1); // 'normal' push_back
```
```
class Foo{ std::vector values; } ;
std::vector 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
void safe_push(nostd::ring_span rs, T v)
{
if (rs.size() == rs.capacity())
{
throw std::runtime_error("safe_push exceeds span capacity");
}
rs.push_back(v);
}
```