Advanced Chunk Processing Library 0.2.0
A comprehensive C++ library for advanced data chunking strategies and processing operations
Loading...
Searching...
No Matches
CircularBuffer< T > Class Template Reference

#include <data_structures.hpp>

Public Member Functions

 CircularBuffer (size_t capacity)
 
size_t capacity () const
 
bool empty () const
 
bool full () const
 
pop ()
 
void push (const T &item)
 
size_t size () const
 
std::vector< T > to_vector () const
 

Private Attributes

std::vector< T > buffer_
 
size_t capacity_
 
size_t head_ = 0
 
size_t size_ = 0
 
size_t tail_ = 0
 

Detailed Description

template<typename T>
class CircularBuffer< T >

Definition at line 11 of file data_structures.hpp.

Constructor & Destructor Documentation

◆ CircularBuffer()

template<typename T >
CircularBuffer< T >::CircularBuffer ( size_t  capacity)
inlineexplicit

Definition at line 20 of file data_structures.hpp.

21 if (capacity == 0)
22 throw std::invalid_argument("Capacity must be positive");
23 }
std::vector< T > buffer_
size_t capacity() const

References CircularBuffer< T >::capacity().

Member Function Documentation

◆ capacity()

template<typename T >
size_t CircularBuffer< T >::capacity ( ) const
inline

Definition at line 53 of file data_structures.hpp.

53 {
54 return capacity_;
55 }

References CircularBuffer< T >::capacity_.

Referenced by CircularBuffer< T >::CircularBuffer().

◆ empty()

template<typename T >
bool CircularBuffer< T >::empty ( ) const
inline

Definition at line 44 of file data_structures.hpp.

44 {
45 return size_ == 0;
46 }

References CircularBuffer< T >::size_.

Referenced by CircularBuffer< T >::pop(), and TEST().

◆ full()

template<typename T >
bool CircularBuffer< T >::full ( ) const
inline

Definition at line 47 of file data_structures.hpp.

47 {
48 return size_ == capacity_;
49 }

References CircularBuffer< T >::capacity_, and CircularBuffer< T >::size_.

Referenced by TEST().

◆ pop()

template<typename T >
T CircularBuffer< T >::pop ( )
inline

Definition at line 35 of file data_structures.hpp.

35 {
36 if (empty())
37 throw std::runtime_error("Buffer is empty");
38 T item = buffer_[head_];
39 head_ = (head_ + 1) % capacity_;
40 --size_;
41 return item;
42 }
bool empty() const

References CircularBuffer< T >::buffer_, CircularBuffer< T >::capacity_, CircularBuffer< T >::empty(), CircularBuffer< T >::head_, and CircularBuffer< T >::size_.

◆ push()

template<typename T >
void CircularBuffer< T >::push ( const T &  item)
inline

Definition at line 25 of file data_structures.hpp.

25 {
26 buffer_[tail_] = item;
27 tail_ = (tail_ + 1) % capacity_;
28 if (size_ < capacity_) {
29 ++size_;
30 } else {
31 head_ = (head_ + 1) % capacity_;
32 }
33 }

References CircularBuffer< T >::buffer_, CircularBuffer< T >::capacity_, CircularBuffer< T >::head_, CircularBuffer< T >::size_, and CircularBuffer< T >::tail_.

Referenced by TEST().

◆ size()

template<typename T >
size_t CircularBuffer< T >::size ( ) const
inline

Definition at line 50 of file data_structures.hpp.

50 {
51 return size_;
52 }

References CircularBuffer< T >::size_.

Referenced by TEST().

◆ to_vector()

template<typename T >
std::vector< T > CircularBuffer< T >::to_vector ( ) const
inline

Definition at line 57 of file data_structures.hpp.

57 {
58 std::vector<T> result;
59 result.reserve(size_);
60 size_t current = head_;
61 for (size_t i = 0; i < size_; ++i) {
62 result.push_back(buffer_[current]);
63 current = (current + 1) % capacity_;
64 }
65 return result;
66 }

References CircularBuffer< T >::buffer_, CircularBuffer< T >::capacity_, CircularBuffer< T >::head_, and CircularBuffer< T >::size_.

Referenced by TEST().

Member Data Documentation

◆ buffer_

template<typename T >
std::vector<T> CircularBuffer< T >::buffer_
private

◆ capacity_

◆ head_

template<typename T >
size_t CircularBuffer< T >::head_ = 0
private

◆ size_

◆ tail_

template<typename T >
size_t CircularBuffer< T >::tail_ = 0
private

Definition at line 15 of file data_structures.hpp.

Referenced by CircularBuffer< T >::push().


The documentation for this class was generated from the following file: