Babel
Epitech's C++ VoIP project
Public Member Functions | Static Public Attributes | Private Attributes | List of all members
Babel::FreeList< T > Class Template Reference

Provides an indexed free list with constant-time removals from anywhere in the list without invalidating indices. T must be trivially constructible and destructible. More...

#include <FreeList.hpp>

Collaboration diagram for Babel::FreeList< T >:

Public Member Functions

 FreeList ()
 Creates a new free list. More...
 
int insert (T element)
 Inserts an element to the free list and returns an index to it. More...
 
void remove (int n)
 Removes the nth element from the free list. More...
 
void clear ()
 Removes all elements from the free list and free the memory. More...
 
size_t size () const
 The number of active elements in the list. More...
 
void reset ()
 Removes all elements from the list but doesn't free the memory. More...
 
std::vector< T > toVector () const
 Create a vector with all the values of the list. More...
 
int range () const
 Returns the range of valid indices. More...
 
int findIndex (T element) const
 Gives the index of an element, if not found returns -1. More...
 
T & operator[] (int n)
 Returns the nth element. More...
 
const T & operator[] (int n) const
 Returns the nth element. More...
 
void forEach (std::function< bool(T &, int)> pred)
 call pred with all the active elements More...
 
void forEach (std::function< bool(T &)> pred)
 call pred with all the active elements More...
 
void forEach (std::function< bool(const T &, int)> pred) const
 call pred with all the active elements More...
 
void forEach (std::function< bool(const T &)> pred) const
 call pred with all the active elements More...
 

Static Public Attributes

static constexpr int EndOfList = -1
 When this value is encountered in traversing a singly list it means it's the end of the list. More...
 

Private Attributes

std::vector< std::pair< T, int > > _data {}
 The internal vector. More...
 
int _firstFree
 first free index More...
 

Detailed Description

template<class T>
class Babel::FreeList< T >

Provides an indexed free list with constant-time removals from anywhere in the list without invalidating indices. T must be trivially constructible and destructible.

Constructor & Destructor Documentation

◆ FreeList()

template<class T >
Babel::FreeList< T >::FreeList

Creates a new free list.

Member Function Documentation

◆ clear()

template<class T >
void Babel::FreeList< T >::clear

Removes all elements from the free list and free the memory.

◆ findIndex()

template<class T >
int Babel::FreeList< T >::findIndex ( element) const

Gives the index of an element, if not found returns -1.

◆ forEach() [1/4]

template<class T >
void Babel::FreeList< T >::forEach ( std::function< bool(const T &)>  pred) const

call pred with all the active elements

Note
pred argument element const ref
if the pred return false the forEach stop iterating and returns

◆ forEach() [2/4]

template<class T >
void Babel::FreeList< T >::forEach ( std::function< bool(const T &, int)>  pred) const

call pred with all the active elements

Note
pred arguments 1: element ref, 2: index of the element
if the pred return false the forEach stop iterating and returns

◆ forEach() [3/4]

template<class T >
void Babel::FreeList< T >::forEach ( std::function< bool(T &)>  pred)

call pred with all the active elements

Note
pred argument element const ref
if the pred return false the forEach stop iterating and returns

◆ forEach() [4/4]

template<class T >
void Babel::FreeList< T >::forEach ( std::function< bool(T &, int)>  pred)

call pred with all the active elements

Note
pred arguments 1: element ref, 2: index of the element
if the pred return false the forEach stop iterating and returns

◆ insert()

template<class T >
int Babel::FreeList< T >::insert ( element)

Inserts an element to the free list and returns an index to it.

◆ operator[]() [1/2]

template<class T >
T & Babel::FreeList< T >::operator[] ( int  n)

Returns the nth element.

◆ operator[]() [2/2]

template<class T >
const T & Babel::FreeList< T >::operator[] ( int  n) const

Returns the nth element.

◆ range()

template<class T >
int Babel::FreeList< T >::range

Returns the range of valid indices.

◆ remove()

template<class T >
void Babel::FreeList< T >::remove ( int  n)

Removes the nth element from the free list.

◆ reset()

template<class T >
void Babel::FreeList< T >::reset

Removes all elements from the list but doesn't free the memory.

Note
Function used to clear and quickly refill the list

◆ size()

template<class T >
size_t Babel::FreeList< T >::size

The number of active elements in the list.

◆ toVector()

template<class T >
std::vector< T > Babel::FreeList< T >::toVector

Create a vector with all the values of the list.

Member Data Documentation

◆ _data

template<class T >
std::vector<std::pair<T, int> > Babel::FreeList< T >::_data {}
private

The internal vector.

◆ _firstFree

template<class T >
int Babel::FreeList< T >::_firstFree
private

first free index

◆ EndOfList

template<class T >
constexpr int Babel::FreeList< T >::EndOfList = -1
staticconstexpr

When this value is encountered in traversing a singly list it means it's the end of the list.


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