Gromacs  2016.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
List of all members | Public Types | Public Member Functions | Static Public Member Functions | Related Functions
gmx::ArrayRef< T > Class Template Reference

#include <gromacs/utility/arrayref.h>

Description

template<typename T>
class gmx::ArrayRef< T >

STL-like container for an interface to a C array (or part of a std::vector).

Template Parameters
TValue type of elements.

This class provides an interface similar to std::vector<T>, with the following main differences:

This class is useful for writing wrappers that expose a different view of the internal data stored as a single vector/array.

Methods in this class do not throw, except where indicated.

Note that due to a Doxygen limitation, the constructor that takes a C array whose size is known at compile time does not appear in the documentation.

Todo:
This class is not complete. At least, it should be possible to convert an ArrayRef to a ConstArrayRef. There are likely also methods missing (not required for current usage).

Public Types

typedef T value_type
 Type of values stored in the container.
 
typedef size_t size_type
 Type for representing size of the container.
 
typedef ptrdiff_t difference_type
 Type for representing difference between two container indices.
 
typedef const T & const_reference
 Const reference to a container element.
 
typedef const T * const_pointer
 Const pointer to a container element.
 
typedef const T * const_iterator
 Const iterator type for the container.
 
typedef T & reference
 Reference to a container element.
 
typedef T * pointer
 Pointer to a container element.
 
typedef T * iterator
 Iterator type for the container.
 
typedef std::reverse_iterator
< iterator
reverse_iterator
 Standard reverse iterator.
 
typedef std::reverse_iterator
< const_iterator
const_reverse_iterator
 Standard reverse iterator.
 

Public Member Functions

 ArrayRef ()
 Constructs an empty reference.
 
 ArrayRef (const EmptyArrayRef &)
 Constructs an empty reference. More...
 
 ArrayRef (pointer begin, pointer end)
 Constructs a reference to a particular range. More...
 
 ArrayRef (std::vector< T > &v)
 Constructs a reference to a whole vector. More...
 
iterator begin ()
 Returns an iterator to the beginning of the container.
 
const_iterator begin () const
 Returns an iterator to the beginning of the container.
 
iterator end ()
 Returns an iterator to the end of the container.
 
const_iterator end () const
 Returns an iterator to the end of the container.
 
reverse_iterator rbegin ()
 Returns an iterator to the reverse beginning of the container.
 
const_reverse_iterator rbegin () const
 Returns an iterator to the reverse beginning of the container.
 
reverse_iterator rend ()
 Returns an iterator to the reverse end of the container.
 
const_reverse_iterator rend () const
 Returns an iterator to the reverse end of the container.
 
size_type size () const
 Returns the size of the container.
 
size_type capacity () const
 Identical to size().
 
bool empty () const
 Whether the container is empty.
 
reference operator[] (size_type n)
 Access container element.
 
const_reference operator[] (size_type n) const
 Access container element.
 
reference at (size_type n)
 Access container element (throws on out-of-range error).
 
const_reference at (size_type n) const
 Access container element (throws on out-of-range error).
 
reference front ()
 Returns the first element in the container.
 
const_reference front () const
 Returns the first element in the container.
 
reference back ()
 Returns the last element in the container.
 
const_reference back () const
 Returns the last element in the container.
 
pointer data ()
 Returns a raw pointer to the contents of the array.
 
const_pointer data () const
 Returns a raw pointer to the contents of the array.
 
void swap (ArrayRef< T > &other)
 Swaps referenced memory with the other object. More...
 

Static Public Member Functions

static ArrayRef< value_typefromPointers (value_type *begin, value_type *end)
 Constructs a reference to a particular range from two pointers. More...
 
static ArrayRef< value_typefromArray (value_type *begin, size_t size)
 Constructs a reference to an array. More...
 
static ArrayRef< value_typefromVector (typename std::vector< value_type >::iterator begin, typename std::vector< value_type >::iterator end)
 Constructs a reference to a particular range in a std::vector. More...
 

Related Functions

(Note that these are not member functions.)

template<typename T >
ArrayRef< T > arrayRefFromPointers (T *begin, T *end)
 Constructs a reference to a particular range from two pointers. More...
 
template<typename T >
ArrayRef< T > arrayRefFromArray (T *begin, size_t size)
 Constructs a reference to an array. More...
 
template<typename T >
ArrayRef< T > arrayRefFromVector (typename std::vector< T >::iterator begin, typename std::vector< T >::iterator end)
 Constructs a reference to a particular range in a std::vector. More...
 

Constructor & Destructor Documentation

template<typename T>
gmx::ArrayRef< T >::ArrayRef ( const EmptyArrayRef< T > &  )
inline

Constructs an empty reference.

This is provided for convenience, such that EmptyArrayRef can be used to initialize any ArrayRef, without specifying the template type. It is not explicit to enable that usage.

template<typename T>
gmx::ArrayRef< T >::ArrayRef ( pointer  begin,
pointer  end 
)
inline

Constructs a reference to a particular range.

Parameters
[in]beginPointer to the beginning of a range.
[in]endPointer to the end of a range.

Passed pointers must remain valid for the lifetime of this object.

Note
For clarity, use the non-member function arrayRefFromPointers instead.
template<typename T>
gmx::ArrayRef< T >::ArrayRef ( std::vector< T > &  v)
inline

Constructs a reference to a whole vector.

Parameters
[in]vVector to reference.

Passed vector must remain valid and not be reallocated for the lifetime of this object.

This constructor is not explicit to allow directly passing std::vector to a method that takes ArrayRef.

Member Function Documentation

template<typename T>
static ArrayRef<value_type> gmx::ArrayRef< T >::fromArray ( value_type begin,
size_t  size 
)
inlinestatic

Constructs a reference to an array.

Parameters
[in]beginPointer to the beginning of the array. May be NULL if size is zero.
[in]sizeNumber of elements in the array.

Passed pointer must remain valid for the lifetime of this object.

template<typename T>
static ArrayRef<value_type> gmx::ArrayRef< T >::fromPointers ( value_type begin,
value_type end 
)
inlinestatic

Constructs a reference to a particular range from two pointers.

Parameters
[in]beginPointer to the beginning of a range.
[in]endPointer to the end of a range.

Passed pointers must remain valid for the lifetime of this object.

template<typename T>
static ArrayRef<value_type> gmx::ArrayRef< T >::fromVector ( typename std::vector< value_type >::iterator  begin,
typename std::vector< value_type >::iterator  end 
)
inlinestatic

Constructs a reference to a particular range in a std::vector.

Parameters
[in]beginIterator to the beginning of a range.
[in]endIterator to the end of a range.

The referenced vector must remain valid and not be reallocated for the lifetime of this object.

template<typename T>
void gmx::ArrayRef< T >::swap ( ArrayRef< T > &  other)
inline

Swaps referenced memory with the other object.

The actual memory areas are not modified, only the references are swapped.

Friends And Related Function Documentation

template<typename T >
ArrayRef< T > arrayRefFromArray ( T *  begin,
size_t  size 
)
related

Constructs a reference to an array.

Parameters
[in]beginPointer to the beginning of the array. May be NULL if size is zero.
[in]sizeNumber of elements in the array.

Passed pointer must remain valid for the lifetime of this object.

template<typename T >
ArrayRef< T > arrayRefFromPointers ( T *  begin,
T *  end 
)
related

Constructs a reference to a particular range from two pointers.

Parameters
[in]beginPointer to the beginning of a range.
[in]endPointer to the end of a range.

Passed pointers must remain valid for the lifetime of this object.

template<typename T >
ArrayRef< T > arrayRefFromVector ( typename std::vector< T >::iterator  begin,
typename std::vector< T >::iterator  end 
)
related

Constructs a reference to a particular range in a std::vector.

Parameters
[in]beginIterator to the beginning of a range.
[in]endIterator to the end of a range.

The referenced vector must remain valid and not be reallocated for the lifetime of this object.


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