Gromacs  2020.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
List of all members | Public Types | 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 interface to a C array of T (or part of a std container of T).

Template Parameters
TValue type of elements.

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

This class is useful for writing wrappers that expose a view of the internal data stored as a single vector/array, which can be a whole or part of the underlying storage.

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.

To refer to const data of type T, ArrayRef<const T> is used. For both const and non-const std::vector and std::array an ArrayRef view can be created. Attempting to create a non-const ArrayRef of a const vector/array will result in a compiler error in the respective constructor.

For SIMD types there is template specialization available (e.g. ArrayRef<SimdReal>) in gromacs/simd/simd_memory.h which should have the same functionality as much as possible.

Todo:
This class is not complete. There are likely also methods missing (not required for current usage).

Public Types

typedef T value_type
 Type of values stored in the reference.
 
typedef size_t size_type
 Type for representing size of the reference.
 
typedef ptrdiff_t difference_type
 Type for representing difference between two indices.
 
typedef const T & const_reference
 Const reference to an element.
 
typedef const T * const_pointer
 Const pointer to an element.
 
typedef const T * const_iterator
 Const iterator type to an element.
 
typedef T & reference
 Reference to an element.
 
typedef T * pointer
 Pointer to an element.
 
typedef T * iterator
 Iterator type to an element.
 
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.
 
template<typename U , typename = std::enable_if_t<std::is_convertible<typename std::remove_reference_t<U>::pointer, pointer>::value>>
 ArrayRef (U &&o)
 Constructs a reference to a container or reference. More...
 
 ArrayRef (pointer begin, pointer end)
 Constructs a reference to a particular range. More...
 
ArrayRef subArray (size_type start, size_type count) const
 Returns a reference to part of the memory.
 
iterator begin () const
 Returns an iterator to the beginning of the reference.
 
iterator end () const
 Returns an iterator to the end of the reference.
 
reverse_iterator rbegin () const
 Returns an iterator to the reverse beginning of the reference.
 
reverse_iterator rend () const
 Returns an iterator to the reverse end of the reference.
 
size_type size () const
 Returns the size of the reference. More...
 
index ssize () const
 Returns the signed size of the reference.
 
size_type capacity () const
 Identical to size().
 
bool empty () const
 Whether the reference refers to no memory.
 
reference operator[] (size_type n) const
 Access an element.
 
reference at (size_type n) const
 Access an element (throws on out-of-range error).
 
reference front () const
 Returns the first element.
 
reference back () const
 Returns the first element.
 
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...
 

Related Functions

(Note that these are not member functions.)

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

Constructor & Destructor Documentation

template<typename T>
template<typename U , typename = std::enable_if_t<std::is_convertible<typename std::remove_reference_t<U>::pointer, pointer>::value>>
gmx::ArrayRef< T >::ArrayRef ( U &&  o)
inline

Constructs a reference to a container or reference.

Parameters
[in]ocontainer to reference.

Can be used to create a reference to a whole vector, std::array or an ArrayRef. The destination has to have a convertible pointer type (identical besides const or base class).

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

This constructor is not explicit to allow directly passing a container to a method that takes ArrayRef.

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.

Member Function Documentation

template<typename T>
size_type gmx::ArrayRef< T >::size ( ) const
inline

Returns the size of the reference.

Note
Use ssize for any expression involving arithmetic operations (including loop indices).
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

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


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