iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
nrt::Array< T > Class Template Reference

#include <nrt/Core/Memory/Array.H>

template<class T>
class nrt::Array< T >

The Array class is an efficient way to store memory and pass it between functions.

The data held by this Array class is managed by a copy-on-write policy, so that assignments are extremely fast - costing barely more than a pointer assignment and an integer increment. The class will then keep track of how many references there are to the internal data storage, and will delete the contained memory if the number of references to it drops to 0. Inspecting the data is also extremely fast (as fast as a standard C array) as long as you use one of the constant methods (const_begin(), at(), etc...). However, accessing the data though a non-constant method (begin(), operator[], etc...) may cause a deep-copy of the underlying data if there are multiple references to it. Therefore, users should strive to use the faster constant methods whenever possible to ensure maximum efficiency.

Examples:
tests/test-EigenConversions.C.

Definition at line 58 of file Array.H.

Classes

class  Memory
 A reference counted storage class for an array of data. More...
 

Public Types

typedef T const * const_iterator
 A constant iterator to the Array's data. More...
 
typedef T * iterator
 A non-constant iterator to the Array's data. More...
 

Public Member Functions

 Array ()
 Default construct an empty array.
 
 ~Array ()
 Destructor. More...
 
 Array (size_t size)
 Construct an array of a given size. More...
 
 Array (size_t size, T fillvalue)
 Construct a new array of a given size, and fill it with a value.
 
 Array (Array< T > const &a)
 Copy an existing array. More...
 
 Array (Array< T > &&a)
 Move construct an existing array. More...
 
 Array (T const *data, size_t size)
 Construct an array by making a deep-copy of the given data.
 
Array< T > & operator= (Array< T > const &a)
 Assign to an existing array. More...
 
Array< T > & operator= (Array< T > &&a)
 Move assign to an existing array. More...
 
bool operator== (Array< T > const &a)
 Compare to another array. More...
 
size_t size () const
 Get the size of the array.
 
T const & at (size_t index) const
 Get a constant reference to an item at the given index. More...
 
T & operator[] (size_t index)
 Get a non-constant reference to an item at the given index. More...
 
T const & operator[] (size_t index) const
 Get a const-reference to an item at the given index. More...
 
const_iterator begin () const
 Get a constant iterator to the beginning of the data. See the typedef for const_iterator for details.
 
const_iterator const_begin () const
 Get a constant iterator to the beginning of the data. See the typedef for const_iterator for details.
 
const_iterator end () const
 
const_iterator const_end () const
 
iterator begin ()
 
iterator end ()
 
void deepCopy ()
 Make a deep copy of our Memory. More...
 

Friends

class cereal::access
 

Member Typedef Documentation

template<class T>
typedef T const* nrt::Array< T >::const_iterator

A constant iterator to the Array's data.

Users should use const_iterators whenever possible, as they are guaranteed to never cause a deep copy of the underlying data

Definition at line 127 of file Array.H.

template<class T>
typedef T* nrt::Array< T >::iterator

A non-constant iterator to the Array's data.

Users should refrain from using these non-constant iterators unless they really do need to change the array's data. Getting a reference to this type of iterator may cause a deep-copy of the underyling data, which will cause a serious performance penalty.

Definition at line 133 of file Array.H.

Constructor & Destructor Documentation

template<class T >
nrt::Array< T >::~Array ( )
inline

Destructor.

Decrement the memory ref count, and free the memory when the last Array pointing to it is destroyed

Definition at line 91 of file ArrayImpl.H.

template<class T >
nrt::Array< T >::Array ( size_t  size)
inline

Construct an array of a given size.

This constructor will not initialize the data inside of the array, so it may be filled with garbage on construction.

Definition at line 65 of file ArrayImpl.H.

template<class T>
nrt::Array< T >::Array ( Array< T > const &  a)
inline

Copy an existing array.

This operation performs a shallow copy, meaning that the two arrays will both point to the exact same chunk of data, and the refcount on that data will be incremented.

Definition at line 79 of file ArrayImpl.H.

template<class T>
nrt::Array< T >::Array ( Array< T > &&  a)
inline

Move construct an existing array.

After move construction the old array will be invalid

Definition at line 85 of file ArrayImpl.H.

Member Function Documentation

template<class T>
nrt::Array< T > & nrt::Array< T >::operator= ( nrt::Array< T > const &  a)

Assign to an existing array.

This operation performs a shallow copy, meaning that the two arrays will both point to the exact same chunk of data, and the refcount on that data will be incremented.

Definition at line 96 of file ArrayImpl.H.

template<class T>
nrt::Array< T > & nrt::Array< T >::operator= ( nrt::Array< T > &&  a)

Move assign to an existing array.

After move assignment the old array will be invalid

Definition at line 104 of file ArrayImpl.H.

template<class T>
bool nrt::Array< T >::operator== ( nrt::Array< T > const &  a)

Compare to another array.

This operator will check to see if the two arrays are exactly the same, e.g point to the same chunk of memory

Definition at line 112 of file ArrayImpl.H.

template<class T >
T const & nrt::Array< T >::at ( size_t  index) const

Get a constant reference to an item at the given index.

Use this method to inspect a value at a given index. Because this method always returns a constant reference it will never cause a deep copy to occur, and thus is very fast. If you need to change the contents of an Array, you should use the [] operator.

Definition at line 122 of file ArrayImpl.H.

template<class T >
T & nrt::Array< T >::operator[] ( size_t  index)

Get a non-constant reference to an item at the given index.

Use this method to inspect or modify a value at a given index. Because this method returns a non-constant reference, it may cause a deep copy of the data, and thus a severe performance penalty. Use .at() if you don't need to change the data.

Definition at line 136 of file ArrayImpl.H.

template<class T >
T const & nrt::Array< T >::operator[] ( size_t  index) const

Get a const-reference to an item at the given index.

Note that this version of [] will only be called if your Array is const to begin with. If you're looking for a fast way to inpect data, you probably want the .at() method

Definition at line 131 of file ArrayImpl.H.

template<class T >
nrt::Array< T >::const_iterator nrt::Array< T >::end ( ) const

Get a constant iterator to the end of the data. See the typedef for const_iterator for details.

Definition at line 155 of file ArrayImpl.H.

Referenced by nrt::arrayToEigenVector().

template<class T >
nrt::Array< T >::const_iterator nrt::Array< T >::const_end ( ) const

Get a constant iterator to the end of the data. See the typedef for const_iterator for details.

Definition at line 160 of file ArrayImpl.H.

template<class T >
nrt::Array< T >::iterator nrt::Array< T >::begin ( )

Get a non-constant iterator to the beginning of the data. See the typedef for iterator for details.

Definition at line 165 of file ArrayImpl.H.

template<class T >
nrt::Array< T >::iterator nrt::Array< T >::end ( )

Get a non-constant iterator to the end of the data. See the typedef for iterator for details.

Definition at line 173 of file ArrayImpl.H.

template<class T >
void nrt::Array< T >::deepCopy ( )

Make a deep copy of our Memory.

Immediately after a call to deepCopy(), our reference count is guaranteed to be 1

Definition at line 180 of file ArrayImpl.H.


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