CircularBuffer< T > Class Template Reference

A simple list of objects of type <T> that is intended to be used as a circular buffer (eg, a FIFO) when the alloc/free overhead associated with a linked-list approach is to be avoided. More...

Classes

class  const_iterator
 A simple forward const iterator for a circular buffer. More...
 

Public Types

typedef T value_type
 The value type the list contains. More...
 
typedef Tpointer
 The pointer type for non-const access to value_type items. More...
 
typedef const Tconst_pointer
 The pointer type for const access to value_type items. More...
 
typedef Treference
 The type used for storing into value_type objects. More...
 
typedef const Tconst_reference
 The type used for reading from constant value_type objects. More...
 
typedef label size_type
 The type to represent the size of a buffer. More...
 
typedef label difference_type
 The difference between iterator objects. More...
 

Public Member Functions

constexpr CircularBuffer () noexcept
 Default construct, empty buffer without allocation. More...
 
 CircularBuffer (const label len)
 Construct an empty buffer with given reserve size. More...
 
 CircularBuffer (const CircularBuffer< T > &list)
 Copy construct. More...
 
 CircularBuffer (CircularBuffer< T > &&list)
 Move construct. More...
 
 CircularBuffer (Istream &is)
 Construct from Istream - uses readList. More...
 
label capacity () const noexcept
 Size of the underlying storage. More...
 
bool empty () const noexcept
 Empty or exhausted buffer. More...
 
label size () const noexcept
 The current number of buffer items. More...
 
label space () const noexcept
 
labelRange range_one () const noexcept
 The addressing range covered by array_one() More...
 
labelRange range_two () const noexcept
 The addressing range covered by array_two() More...
 
SubList< Tarray_one ()
 The contents of the first internal array. More...
 
SubList< Tarray_two ()
 The contents of the first internal array. More...
 
const SubList< Tarray_one () const
 The contents of the second internal array. More...
 
const SubList< Tarray_two () const
 The contents of the second internal array. More...
 
Tfirst ()
 Access the first element (front). Requires !empty(). More...
 
Tlast ()
 Access the last element (back). Requires !empty(). More...
 
const Tfirst () const
 Const access to the first element (front). Requires !empty(). More...
 
const Tlast () const
 Const access to the last element (back). Requires !empty(). More...
 
void reserve (const label len)
 
void reserve_nocopy (const label len)
 
void clear () noexcept
 Clear the addressed buffer, does not change allocation. More...
 
void clearStorage ()
 Clear the buffer and delete storage. More...
 
void swap (CircularBuffer< T > &other)
 Swap content, independent of sizing parameter. More...
 
label find (const T &val, label pos=0) const
 Find index of the first occurrence of the value. More...
 
bool found (const T &val, label pos=0) const
 True if the value if found in the list. More...
 
void push_front (const T &val)
 Copy prepend an element to the front of the buffer. More...
 
void push_front (T &&val)
 Move prepend an element to the front of the buffer. More...
 
void push_back (const T &val)
 Copy append an element to the end of the buffer. More...
 
void push_back (T &&val)
 Move Append an element to the end of the buffer. More...
 
void pop_front (label n=1)
 Shrink by moving the front of the buffer 1 or more times. More...
 
void pop_back (label n=1)
 Shrink by moving the end of the buffer 1 or more times. More...
 
void append (const T &val)
 Copy append an element to the end of the buffer. More...
 
void append (T &&val)
 Move append an element to the end of the buffer. More...
 
void append (const UList< T > &list)
 Copy append multiple elements the end of the buffer. More...
 
template<class Addr >
void append (const IndirectListBase< T, Addr > &list)
 Copy append IndirectList elements the end of the buffer. More...
 
label appendUniq (const T &val)
 Append an element if not already in the buffer. More...
 
void reverse ()
 Reverse the buffer order, swapping elements. More...
 
Toperator[] (const label i)
 Non-const access to an element in the list. More...
 
const Toperator[] (const label i) const
 Const access to an element in the list. More...
 
void operator= (const CircularBuffer< T > &list)
 Copy construct. More...
 
void operator= (CircularBuffer< T > &&list)
 Move construct. More...
 
void operator= (const T &val)
 Assign all addressed elements to the given value. More...
 
void operator= (const Foam::zero)
 Assignment of all entries to zero. More...
 
void operator= (const UList< T > &rhs)
 Deep copy values from a list of the addressed elements. More...
 
template<class AnyAddr >
void operator= (const IndirectListBase< T, AnyAddr > &rhs)
 Deep copy values from a list of the addressed elements. More...
 
Ostreaminfo (Ostream &os) const
 Print information. More...
 
IstreamreadList (Istream &is)
 Read buffer contents from Istream. More...
 
OstreamwriteList (Ostream &os, const label shortLen=0) const
 
const_iterator cbegin () const
 Return a const_iterator at begin of buffer. More...
 
const_iterator cend () const
 Return a const_iterator at end of buffer. More...
 
const_iterator begin () const
 Return a const_iterator at begin of buffer. More...
 
const_iterator end () const
 Return a const_iterator at end of buffer. More...
 

Static Public Member Functions

static constexpr label min_size () noexcept
 Lower capacity limit. More...
 

Friends

Istreamoperator>> (Istream &is, CircularBuffer< T > &list)
 Use the readList() method to read contents from Istream. More...
 
Ostreamoperator (Ostream &os, const CircularBuffer< T > &list)
 Write to Ostream. More...
 

Detailed Description

template<class T>
class Foam::CircularBuffer< T >

A simple list of objects of type <T> that is intended to be used as a circular buffer (eg, a FIFO) when the alloc/free overhead associated with a linked-list approach is to be avoided.

The internal storage is addressed by independent begin/end markers.

  • The begin marker points to the front.
  • The end marker is a one-past the back.

This results in a variety ofr different possible buffer states:

  1. empty (begin == end)
  2. simple/linear (begin < end) has no wrapping:
       |.|.|.|a|b|c|d|.|.|.|
       beg ___^
       end ___________^
  3. split (begin > end):
       |f|g|h|i|.|.|.|a|b|c|d|e|
       end _____^
       beg ___________^

The methods range_one(), range_two() return the internal indexing and the methods array_one(), array_two() provide direct access to the internal contents.

When filling the buffer, the internal storage will be resized (doubling strategy) as required. When this occurs, the new list will be linearized with begin = 0.

Simultaneously when filling, the storage buffer will be over-allocated to avoid ambiguity when (begin == end), which represents an empty buffer and not a full buffer. Eg,

    |c|d|.|a|b|
    end _^
    beg ___^

after appending one more, it would be incorrect to simply fill the available space:

    |c|d|e|a|b|
    end ___^        WRONG : would represent empty!
    beg ___^

the storage is instead increased (doubled) and rebalanced before the append occurs (old capacity 5, new capacity 10):

    |a|b|c|d|e|.|.|.|.|.|
    _^_ beg
    end _______^
Source files

Definition at line 120 of file CircularBuffer.H.

Member Typedef Documentation

◆ value_type

typedef T value_type

The value type the list contains.

Definition at line 160 of file CircularBuffer.H.

◆ pointer

typedef T* pointer

The pointer type for non-const access to value_type items.

Definition at line 163 of file CircularBuffer.H.

◆ const_pointer

typedef const T* const_pointer

The pointer type for const access to value_type items.

Definition at line 166 of file CircularBuffer.H.

◆ reference

typedef T& reference

The type used for storing into value_type objects.

Definition at line 169 of file CircularBuffer.H.

◆ const_reference

typedef const T& const_reference

The type used for reading from constant value_type objects.

Definition at line 172 of file CircularBuffer.H.

◆ size_type

typedef label size_type

The type to represent the size of a buffer.

Definition at line 175 of file CircularBuffer.H.

◆ difference_type

typedef label difference_type

The difference between iterator objects.

Definition at line 178 of file CircularBuffer.H.

Constructor & Destructor Documentation

◆ CircularBuffer() [1/5]

constexpr CircularBuffer
inlineconstexprnoexcept

Default construct, empty buffer without allocation.

Definition at line 114 of file CircularBufferI.H.

◆ CircularBuffer() [2/5]

CircularBuffer ( const label  len)
inlineexplicit

Construct an empty buffer with given reserve size.

Definition at line 123 of file CircularBufferI.H.

◆ CircularBuffer() [3/5]

CircularBuffer ( const CircularBuffer< T > &  list)
inline

Copy construct.

Definition at line 132 of file CircularBufferI.H.

◆ CircularBuffer() [4/5]

CircularBuffer ( CircularBuffer< T > &&  list)
inline

Move construct.

Definition at line 144 of file CircularBufferI.H.

◆ CircularBuffer() [5/5]

CircularBuffer ( Istream is)
explicit

Construct from Istream - uses readList.

Definition at line 35 of file CircularBufferIO.C.

Member Function Documentation

◆ min_size()

static constexpr label min_size ( )
inlinestaticconstexprnoexcept

Lower capacity limit.

Definition at line 207 of file CircularBuffer.H.

◆ capacity()

Foam::label capacity
inlinenoexcept

Size of the underlying storage.

Definition at line 161 of file CircularBufferI.H.

◆ empty()

bool empty
inlinenoexcept

Empty or exhausted buffer.

Definition at line 168 of file CircularBufferI.H.

Referenced by Foam::meshTools::bandCompression().

Here is the caller graph for this function:

◆ size()

Foam::label size
inlinenoexcept

The current number of buffer items.

Definition at line 175 of file CircularBufferI.H.

References Foam::diff().

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ space()

Foam::label space
inlinenoexcept

The nominal space available to fill. Subtract 1 for the number to append before re-balancing is needed.

Definition at line 189 of file CircularBufferI.H.

◆ range_one()

Foam::labelRange range_one
inlinenoexcept

The addressing range covered by array_one()

Definition at line 196 of file CircularBufferI.H.

◆ range_two()

Foam::labelRange range_two
inlinenoexcept

The addressing range covered by array_two()

Definition at line 208 of file CircularBufferI.H.

◆ array_one() [1/2]

Foam::SubList< T > array_one

The contents of the first internal array.

Definition at line 77 of file CircularBuffer.C.

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

Here is the caller graph for this function:

◆ array_two() [1/2]

Foam::SubList< T > array_two

The contents of the first internal array.

Definition at line 85 of file CircularBuffer.C.

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

Here is the caller graph for this function:

◆ array_one() [2/2]

const Foam::SubList< T > array_one

The contents of the second internal array.

Definition at line 93 of file CircularBuffer.C.

◆ array_two() [2/2]

const Foam::SubList< T > array_two

The contents of the second internal array.

Definition at line 101 of file CircularBuffer.C.

◆ first() [1/2]

T & first
inline

Access the first element (front). Requires !empty().

Definition at line 266 of file CircularBufferI.H.

References Foam::abort(), Foam::FatalError, and FatalErrorInFunction.

Referenced by Foam::meshTools::bandCompression().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ last() [1/2]

T & last
inline

Access the last element (back). Requires !empty().

Definition at line 290 of file CircularBufferI.H.

References Foam::abort(), Foam::FatalError, and FatalErrorInFunction.

Here is the call graph for this function:

◆ first() [2/2]

const T & first
inline

Const access to the first element (front). Requires !empty().

Definition at line 278 of file CircularBufferI.H.

References Foam::abort(), Foam::FatalError, and FatalErrorInFunction.

Here is the call graph for this function:

◆ last() [2/2]

const T & last
inline

Const access to the last element (back). Requires !empty().

Definition at line 302 of file CircularBufferI.H.

References Foam::abort(), Foam::FatalError, and FatalErrorInFunction.

Here is the call graph for this function:

◆ reserve()

void reserve ( const label  len)
inline

Reserve allocation space for at least this size, allocating new space if required and retaining old content.

Never shrinks.

Definition at line 245 of file CircularBufferI.H.

◆ reserve_nocopy()

void reserve_nocopy ( const label  len)
inline

Reserve allocation space for at least this size, allocating new space if required without retaining old content.

Never shrinks.

Definition at line 252 of file CircularBufferI.H.

◆ clear()

void clear
inlinenoexcept

Clear the addressed buffer, does not change allocation.

Definition at line 215 of file CircularBufferI.H.

◆ clearStorage()

void clearStorage
inline

Clear the buffer and delete storage.

Definition at line 222 of file CircularBufferI.H.

◆ swap()

void swap ( CircularBuffer< T > &  other)
inline

Swap content, independent of sizing parameter.

Definition at line 230 of file CircularBufferI.H.

◆ find()

Foam::label find ( const T val,
label  pos = 0 
) const

Find index of the first occurrence of the value.

Any occurrences before the start pos are ignored. Linear search.

Returns
position in list or -1 if not found.

Definition at line 109 of file CircularBuffer.C.

References Foam::pos().

Here is the call graph for this function:

◆ found()

bool found ( const T val,
label  pos = 0 
) const
inline

True if the value if found in the list.

Any occurrences before the start pos are ignored. Linear search.

Returns
true if found.

Definition at line 259 of file CircularBufferI.H.

References Foam::pos().

Here is the call graph for this function:

◆ push_front() [1/2]

void push_front ( const T val)
inline

Copy prepend an element to the front of the buffer.

Definition at line 314 of file CircularBufferI.H.

◆ push_front() [2/2]

void push_front ( T &&  val)
inline

Move prepend an element to the front of the buffer.

Definition at line 326 of file CircularBufferI.H.

◆ push_back() [1/2]

void push_back ( const T val)
inline

Copy append an element to the end of the buffer.

Definition at line 338 of file CircularBufferI.H.

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

Here is the caller graph for this function:

◆ push_back() [2/2]

void push_back ( T &&  val)
inline

Move Append an element to the end of the buffer.

Definition at line 350 of file CircularBufferI.H.

◆ pop_front()

void pop_front ( label  n = 1)
inline

Shrink by moving the front of the buffer 1 or more times.

Definition at line 362 of file CircularBufferI.H.

References n.

Referenced by Foam::meshTools::bandCompression().

Here is the caller graph for this function:

◆ pop_back()

void pop_back ( label  n = 1)
inline

Shrink by moving the end of the buffer 1 or more times.

Definition at line 379 of file CircularBufferI.H.

References n.

◆ append() [1/4]

void append ( const T val)
inline

Copy append an element to the end of the buffer.

Definition at line 318 of file CircularBuffer.H.

References CircularBuffer< T >::push_back().

Referenced by Foam::meshTools::bandCompression().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ append() [2/4]

void append ( T &&  val)
inline

Move append an element to the end of the buffer.

Definition at line 321 of file CircularBuffer.H.

References CircularBuffer< T >::push_back().

Here is the call graph for this function:

◆ append() [3/4]

void append ( const UList< T > &  list)
inline

Copy append multiple elements the end of the buffer.

Definition at line 411 of file CircularBufferI.H.

References UList< T >::fcIndex(), and UList< T >::size().

Here is the call graph for this function:

◆ append() [4/4]

void append ( const IndirectListBase< T, Addr > &  list)
inline

Copy append IndirectList elements the end of the buffer.

Definition at line 432 of file CircularBufferI.H.

References IndirectListBase< T, Addr >::fcIndex(), and IndirectListBase< T, Addr >::size().

Here is the call graph for this function:

◆ appendUniq()

Foam::label appendUniq ( const T val)
inline

Append an element if not already in the buffer.

Returns
the change in the buffer length

Definition at line 396 of file CircularBufferI.H.

References found.

◆ reverse()

void reverse

Reverse the buffer order, swapping elements.

Definition at line 131 of file CircularBuffer.C.

References n, and Foam::Swap().

Here is the call graph for this function:

◆ operator[]() [1/2]

T & operator[] ( const label  i)
inline

Non-const access to an element in the list.

The index is allowed to wrap in both directions

Definition at line 457 of file CircularBufferI.H.

◆ operator[]() [2/2]

const T & operator[] ( const label  i) const
inline

Const access to an element in the list.

The index is allowed to wrap in both directions

Definition at line 465 of file CircularBufferI.H.

◆ operator=() [1/6]

void operator= ( const CircularBuffer< T > &  list)
inline

Copy construct.

Definition at line 473 of file CircularBufferI.H.

References CircularBuffer< T >::array_one(), CircularBuffer< T >::array_two(), clear(), and T.

Here is the call graph for this function:

◆ operator=() [2/6]

void operator= ( CircularBuffer< T > &&  list)
inline

Move construct.

Definition at line 510 of file CircularBufferI.H.

◆ operator=() [3/6]

void operator= ( const T val)
inline

Assign all addressed elements to the given value.

Definition at line 523 of file CircularBufferI.H.

◆ operator=() [4/6]

void operator= ( const Foam::zero  )
inline

Assignment of all entries to zero.

Definition at line 531 of file CircularBufferI.H.

References Foam::Zero.

◆ operator=() [5/6]

void operator= ( const UList< T > &  rhs)
inline

Deep copy values from a list of the addressed elements.

Definition at line 539 of file CircularBufferI.H.

◆ operator=() [6/6]

void operator= ( const IndirectListBase< T, AnyAddr > &  rhs)
inline

Deep copy values from a list of the addressed elements.

Definition at line 547 of file CircularBufferI.H.

◆ info()

Foam::Ostream & info ( Ostream os) const

Print information.

<< " one=" << this->range_one() << this->array_one() << " two=" << this->range_two() << this->array_two()

Definition at line 44 of file CircularBufferIO.C.

References Foam::nl, and os().

Here is the call graph for this function:

◆ readList()

Foam::Istream & readList ( Istream is)

Read buffer contents from Istream.

Definition at line 58 of file CircularBufferIO.C.

References Foam::dynamicCast(), Foam::exit(), IOstream::fatalCheck(), Foam::FatalIOError, FatalIOErrorInFunction, FUNCTION_NAME, token::info(), token::isCompound(), token::isLabel(), token::isPunctuation(), token::labelToken(), Foam::nl, Istream::putBack(), UList< T >::readList(), LList< LListBase, T >::removeHead(), and token::transferCompoundToken().

Here is the call graph for this function:

◆ writeList()

Foam::Ostream & writeList ( Ostream os,
const label  shortLen = 0 
) const

Write buffer contents with line-breaks in ASCII when length exceeds shortLen.

Using '0' suppresses line-breaks entirely.

Definition at line 151 of file CircularBufferIO.C.

References Foam::abort(), Foam::FatalError, FatalErrorInFunction, FUNCTION_NAME, Foam::nl, os(), and T.

Referenced by Foam::operator<<().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cbegin()

const_iterator cbegin ( ) const
inline

Return a const_iterator at begin of buffer.

Definition at line 461 of file CircularBuffer.H.

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

Here is the caller graph for this function:

◆ cend()

const_iterator cend ( ) const
inline

Return a const_iterator at end of buffer.

Definition at line 467 of file CircularBuffer.H.

References CircularBuffer< T >::size().

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ begin()

const_iterator begin ( ) const
inline

Return a const_iterator at begin of buffer.

Definition at line 473 of file CircularBuffer.H.

References CircularBuffer< T >::cbegin().

Here is the call graph for this function:

◆ end()

const_iterator end ( ) const
inline

Return a const_iterator at end of buffer.

Definition at line 476 of file CircularBuffer.H.

References CircularBuffer< T >::cend().

Here is the call graph for this function:

Friends And Related Function Documentation

◆ operator>>

Istream & operator>> ( Istream is,
CircularBuffer< T > &  list 
)
friend

Use the readList() method to read contents from Istream.

Definition at line 484 of file CircularBuffer.H.

◆ operator

Ostream & operator ( Ostream os,
const CircularBuffer< T > &  list 
)
friend

Write to Ostream.


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