C++ API Reference RNBO: RNBO::listbase< T > Class Template Reference

RNBO: RNBO::listbase< T > Class Template Reference

A container similar to a JavaScript array. More...

#include <RNBO_List.h>

Public Member Functions

 listbase ()
 Default constructor.
 
template<typename... Ts>
 listbase (Ts ... args)
 Constructor. More...
 
 listbase (const listbase &l)
 Copy constructor. More...
 
 listbase (listbase &&l)
 Move constructor.
 
 ~listbase ()
 Destructor.
 
T & operator[] (size_t i)
 Get a reference to the element at specified location with bounds checking. More...
 
operator[] (size_t i) const
 Get an element at a specified location with bounds checking. More...
 
listbaseoperator-> ()
 
const listbaseoperator-> () const
 
listbaseoperator= (const listbase &l)
 Copy assignment operator.
 
void push (T item)
 Append an item to the list. More...
 
pop ()
 Remove an item from the end of the list and return it. More...
 
shift ()
 Remove the first item from the list and return it. More...
 
listbase concat (T item) const
 Create a new listbase by copying the original and appending an item. More...
 
listbase concat (const listbase &item) const
 Concatenate a list with another. More...
 
listbasefill (T value, size_t start=0, size_t end=0)
 Fill a list with a value. More...
 
template<typename... Ts>
void unshift (Ts ... args)
 Add one or more elements to the beginning of the list. More...
 
template<typename... Ts>
listbase< T > splice (Int start, size_t deleteCount, Ts ... args)
 Modify part a list in place. More...
 
listbase slice (int start=0, int end=0) const
 Get a shallow copy of a portion of a list. More...
 
bool includes (T value, int fromIndex=0) const
 Determine whether an element is in the list. More...
 
int indexOf (T value, int fromIndex=0) const
 Find the first index of an element's occurrence in the list. More...
 
listbasereverse ()
 Reverse the list in place. More...
 
void reserve (size_t size)
 Pre-allocate memory for a list. More...
 
T * inner () const
 get a pointer to the internal values, for memcopy etc
 

Data Fields

size_t length = 0
 the number of elements in the list
 

Protected Member Functions

void allocate (size_t size, bool keepValues)
 

Protected Attributes

T * _values
 the contents of the list
 
size_t _allocatedLength
 how much memory has been allocated for the list
 
_dummy = static_cast<T>(0)
 a dummy variable to accept out of bounds array assignments rather than allow out of bounds memory acess
 

Detailed Description

template<typename T>
class RNBO::listbase< T >

A container similar to a JavaScript array.

Elements of listbase are stored contiguously in memory making this container more similar to std::vector than std::list. The listbase is resized only when adding elements; memory is allocated when elements are added, but memory is not freed when removing elements.

Template Parameters
typeof object stored in the container

Constructor & Destructor Documentation

◆ listbase() [1/2]

template<typename T >
template<typename... Ts>
RNBO::listbase< T >::listbase ( Ts ...  args)
inline

Constructor.

Template Parameters
typeof items in the listbase
listbase<number> foo(3.14, 5.2, 4); // foo = [3.14, 5.2, 4.0]
listbase<int> bar(74, 42, 99, 44100); // bar = [74, 42, 99, 44100]

◆ listbase() [2/2]

template<typename T >
RNBO::listbase< T >::listbase ( const listbase< T > &  l)
inline

Copy constructor.

Template Parameters
listbasethe listbase to copy from
listbase<int> foo(3.14, 5.2, 4);
listbase<int> bar(foo);

Member Function Documentation

◆ concat() [1/2]

template<typename T >
listbase RNBO::listbase< T >::concat ( const listbase< T > &  item) const
inline

Concatenate a list with another.

Parameters
itemthe item to append to the list copy
Returns
a copy of the original list with item appended
listbase<number> foo(3.1, 4.2);
listbase<number> bar(5.0, 1.0);
listbase<number> meow = foo.concat(bar); // meow = [3.1, 4.2, 5.0, 1.0]

◆ concat() [2/2]

template<typename T >
listbase RNBO::listbase< T >::concat ( item) const
inline

Create a new listbase by copying the original and appending an item.

Parameters
itemthe item to append to the list copy
Returns
a copy of the original list with item appended
listbase<number> foo(3.1, 4.2, 7.4);
listbase<number> bar = foo.concat(0.5); // foo = [3.1, 4.2, 7.4]
// bar = [3.1, 4.2, 7.4, 0.5]

◆ fill()

template<typename T >
listbase& RNBO::listbase< T >::fill ( value,
size_t  start = 0,
size_t  end = 0 
)
inline

Fill a list with a value.

Parameters
valuethe value to fill with
startthe index to begin filling from
endthe index to end filling at; end = 0 sets the endpoint to the last element
Returns
a reference to the list

◆ includes()

template<typename T >
bool RNBO::listbase< T >::includes ( value,
int  fromIndex = 0 
) const
inline

Determine whether an element is in the list.

Parameters
valuethe element to search for
fromIndexthe position to begin searching from
Returns
true if the value is found, false otherwise

◆ indexOf()

template<typename T >
int RNBO::listbase< T >::indexOf ( value,
int  fromIndex = 0 
) const
inline

Find the first index of an element's occurrence in the list.

Parameters
valuethe element to locate
fromIndexthe index to begin searching from. If the value is negative, begins searching from 0.
Returns
the index of the first matching element in the list; -1 if the element is not found

◆ operator[]() [1/2]

template<typename T >
T& RNBO::listbase< T >::operator[] ( size_t  i)
inline

Get a reference to the element at specified location with bounds checking.

listbase<int> foo(3, 1, 4);
foo[2] = 74; // foo = [3, 1, 74]
foo[3] = 100; // returns runtime error and doesn't assign 100 to
// unowned memory

◆ operator[]() [2/2]

template<typename T >
T RNBO::listbase< T >::operator[] ( size_t  i) const
inline

Get an element at a specified location with bounds checking.

listbase<int> foo(3, 1, 4);
int x = foo[2]; // x = 4
int y = foo[3]; // returns runtime error and sets y = 0

◆ pop()

template<typename T >
T RNBO::listbase< T >::pop ( )
inline

Remove an item from the end of the list and return it.

Returns
the last item from the list
listbase<number> foo(3.1, 4.2, 7.4);
number x = foo.pop(); // x = 7.4

◆ push()

template<typename T >
void RNBO::listbase< T >::push ( item)
inline

Append an item to the list.

Parameters
iteman item of the same type as other elements in the list
listbase<int> foo();
foo.push(2); // foo = [2];

◆ reserve()

template<typename T >
void RNBO::listbase< T >::reserve ( size_t  size)
inline

Pre-allocate memory for a list.

Parameters
sizethe number of elements for which to reserve space

◆ reverse()

template<typename T >
listbase& RNBO::listbase< T >::reverse ( )
inline

Reverse the list in place.

Returns
a reference to the list

◆ shift()

template<typename T >
T RNBO::listbase< T >::shift ( )
inline

Remove the first item from the list and return it.

Note that this moves all items in memory from locations 1..n to 0..n-1.

Returns
the first item of the list if one exists, 0 otherwise

◆ slice()

template<typename T >
listbase RNBO::listbase< T >::slice ( int  start = 0,
int  end = 0 
) const
inline

Get a shallow copy of a portion of a list.

Parameters
startthe beginning index
endthe end index; 0 is the final element and negative values are offset from the end
Returns
a shallow copy of a portion of a list

◆ splice()

template<typename T >
template<typename... Ts>
listbase<T> RNBO::listbase< T >::splice ( Int  start,
size_t  deleteCount,
Ts ...  args 
)
inline

Modify part a list in place.

Template Parameters
Ts
Parameters
startthe beginning index. If start is greater than the length of the list, splice uses the length of the list.
deleteCountthe number of elements to remove from the beginning of the list
argsitems to add to the list

◆ unshift()

template<typename T >
template<typename... Ts>
void RNBO::listbase< T >::unshift ( Ts ...  args)
inline

Add one or more elements to the beginning of the list.

Template Parameters
Tstype of elements to add to the list
Parameters
argselements to add

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