vibe.d beta banner
get vibe.d
0.10.0

Asynchronous I/O that doesn’t get in your way, written in D

Struct RingBuffer

Ring buffer implementation.

struct RingBuffer(T, ulong N = 0, bool INITIALIZE = true) ;

This implementation supports dynamic capacity, where the memory is allocated dynamically using the garbage collector (N == 0), as well as fixed capacity where the array contents are part of the struct itself (e.g. stack allocated, N > 0).

RingBuffer implements an output range interface, extended with additional putN and peekDst methods.

Reading follows the conventions of the D standard library, providing empty, length, front and back properties, as well as slice and index based random access.

Both, FIFO and LIFO operation modes are supported, using removeFront and removeBack.

This struct has value semantics - copying the a RingBuffer value will copy the whole buffer. Copy-on-write optimization may be implemented in the future, but generally copying is discouraged.

Constructors

NameDescription
this (capacity) Constructs a new rung buffer with given capacity (only if N == 0).

Fields

NameTypeDescription
m_buffer T[N]
m_fill size_t
m_start size_t

Properties

NameTypeDescription
back[get] inout(T)Returns a reference to the last element.
capacity[get, set] size_tOverall number of elements that fit into the buffer
empty[get] boolTests whether there are any elements in the buffer.
freeSpace[get] size_tNumber of elements that can still be put into the buffer
front[get] inout(T)Returns a reference to the first element.
full[get] boolTests whether there is any space left in the buffer.
length[get] size_tNumber of elements contained in the buffer

Methods

NameDescription
clear () Removes all elements.
dispose () Resets the capacity to zero and explicitly frees the memory for the buffer.
linearRemove (r) Removes elements from the buffer.
mod (n)
opApply (del) Enables foreach iteration over all elements.
opApply (del) Enables foreach iteration over all elements along with their indices.
opDollar () Returns the number of elements for using with the index/slice operators.
opIndex (idx) Accesses the n-th element in the buffer.
opSlice () Returns a range spanning all elements currently in the buffer.
opSlice (from, to) Returns a range spanning the given range of element indices.
peek () Returns a slice of the first elements in the buffer.
peekDst () Returns a slice of the unused buffer slots following the last element.
putBack (itm) Adds elements to the back of the buffer.
putBackN (n) Adds elements to the back of the buffer without overwriting the buffer.
putFront (itm) Adds an element to the front of the buffer.
read (dst) Moves elements from the front of the ring buffer into a supplied buffer.
removeBack () Removes the last element from the buffer.
removeBackN (n) Removes the last N elements from the buffer.
removeFront () Removes the first element from the buffer.
removeFrontN (n) Removes the first N elements from the buffer.

Inner structs

NameDescription
Range Represents a range of elements within the ring buffer.

Aliases

NameDescription
put Adds elements to the back of the buffer.
putN Adds elements to the back of the buffer without overwriting the buffer.
Authors

Sönke Ludwig

Copyright

© 2013-2024 Sönke Ludwig

License

Subject to the terms of the MIT license, as written in the included LICENSE.txt file.