tiny_ring

Overview

The estd::tiny_ring is a linear data structure that follows ‘first-in, first-out’ (FIFO) principle but in a circular manner. The class is specifically tailored for small ring buffers, limiting the maximum size to 255 elements.

Static assertion is thrown if attempt to create tiny_ring with size zero and with size greater than 256.

Usage

The usage constraints and guidelines apply for tiny_ring:

Constraints

tiny_ring of size greater than 255 cannot be created.

Usage guidance

Size of tiny_ring should be checked before accessing the elements.

Accessing outside the bounds of tiny_ring using data() function will result in assertion.

Example

The tiny_ring class is specifically tailored for small ring buffers.The operations are performed based on FIFO (First In First Out) principle.

The example shows the creation of tiny_ring and usage of various functions in tiny_ring:

// tiny_ring of size 3 is created.
::estd::tiny_ring<uint16_t, 3> r;
// 'r.empty()' returns true, since there are no elements in the tiny_ring.
ASSERT_TRUE(r.empty());

// Pushing elements at the back of the tiny_ring using push_back() function.
r.push_back(1);
r.push_back(2);
r.push_back(3);

// 'r.full()' returns true, since tiny_ring is at maximum capacity.
ASSERT_TRUE(r.full());

// Get a copy of the oldest element in the tiny_ring.
ASSERT_EQ(1, r.front());

// Popping front element from the tiny_ring.
r.pop_front();
// The front element will be 2 after 'pop_front()'.
ASSERT_EQ(2, r.front());

The following example shows that pushing an element into the full tiny_ring will overwrite the oldest element in the tiny_ring:

// tiny_ring of size 3 is created.
::estd::tiny_ring<uint16_t, 3> r;

// Pushing elements at the back of the tiny_ring using push_back() function.
r.push_back(1);
r.push_back(2);
r.push_back(3);

// 'r.full()' returns true, since tiny_ring is at maximum capacity.
ASSERT_TRUE(r.full());
// The front element is 1.
EXPECT_EQ(1, r.front());
// Pushing element to overwrite the tiny_ring.
r.push_back(4);
// The front element will be 2 since 1 is overwritten.
EXPECT_EQ(2, r.front());

The example shows the use of data function:

::estd::tiny_ring<uint16_t, 5> r;
// r.data().size() returns 5, since the size of the tiny_ring is 5.
ASSERT_EQ(5U, r.data().size());
r.push_back(1);
r.push_back(2);
// r.data()[0] Returns 1, since 1 is at the index 0.
ASSERT_EQ(1, r.data()[0]);
// r.data()[1] Returns 2, since 2 is at the index 1.
ASSERT_EQ(2, r.data()[1]);