Simbody

SimTK::ArrayView_< T, X > Class Template Reference

This Array_ helper class is the base class for Array_, extending ArrayViewConst_ to add the ability to modify elements, but not the ability to change size or reallocate. More...

#include <Array.h>

Inheritance diagram for SimTK::ArrayView_< T, X >:

List of all members.

Public Types

Typedefs

Types required of STL containers, plus index_type which is an extension, and packed_size_type which is an implementation detail.

typedef T value_type
 The type of object stored in this container.
typedef X index_type
 The index type (an extension).
typedef T * pointer
 A writable pointer to a value_type.
typedef const T * const_pointer
 A const pointer to a value_type.
typedef T & reference
 A writable value_type reference.
typedef const T & const_reference
 A const value_type reference.
typedef T * iterator
 A writable iterator for this container (same as pointer here).
typedef const T * const_iterator
 A const iterator for this container (same as const_pointer here).
typedef std::reverse_iterator
< iterator
reverse_iterator
 A writable reverse iterator for this container.
typedef std::reverse_iterator
< const_iterator
const_reverse_iterator
 A const reverse iterator for this container.
typedef ArrayIndexTraits< X >
::size_type 
size_type
 An integral type suitable for all indices and sizes for this array.
typedef ArrayIndexTraits< X >
::difference_type 
difference_type
 A signed integral type that can represent the difference between any two legitimate index values for this array.
typedef ArrayIndexPackType
< size_type >
::packed_size_type 
packed_size_type
 The integral type we actually use internally to store size_type values.

Public Member Functions

Construction, conversion, and destruction

Constructors here are limited to those that don't allocate new data, however they can reference writable data.

 ArrayView_ ()
 Default constructor allocates no heap space and is very fast.
 ArrayView_ (const ArrayView_ &src)
 Copy constructor is shallow.
 ArrayView_ (T *first, const T *last1)
 Construct from a range of writable memory.
template<class A >
 ArrayView_ (std::vector< T, A > &v)
 Construct to reference memory owned by a writable std::vector.
 operator const Array_< T, X > & () const
 Implicit conversion of const ArrayView_ to const Array_& (zero cost).
 operator Array_< T, X > & ()
 Implicit conversion of non-const ArrayView_ to Array_& (zero cost).
void disconnect ()
 Forward to base class disconnect() method -- clears the handle without doing anything to the data.
 ~ArrayView_ ()
 The destructor just disconnects the array view handle from its data; see ArrayViewConst_<T,X>::disconnect() for more information.
Assignment

Assignment is permitted only if the source and destination are the same size.

The semantics here are different than for a resizeable Array_ object: here the meaning is elementwise assignment rather than destruction followed by copy construction. That is, if our elements are of type T, and the source elements are of type T2, we will use the operator of T that best matches the signature T::operator=(const T2&) to perform the assignments. When the source also has type T, this is just T's copy assignment operator. We never perform any element destruction or construction here.

ArrayView_operator= (const ArrayView_ &src)
 Copy assignment; source must be the same size as this array.
template<class T2 , class X2 >
ArrayView_operator= (const ArrayViewConst_< T2, X2 > &src)
 Assignment from any other array object is allowed as long as the number of elements matches and the types are assignment compatible.
template<class T2 , class X2 >
ArrayView_operator= (const ArrayView_< T2, X2 > &src)
 Assignment from any other array object is allowed as long as the number of elements matches and the types are assignment compatible.
template<class T2 , class X2 >
ArrayView_operator= (const Array_< T2, X2 > &src)
 Assignment from any other array object is allowed as long as the number of elements matches and the types are assignment compatible.
template<class T2 , class A2 >
ArrayView_operator= (const std::vector< T2, A2 > &src)
 Assignment from any std::vector object is allowed as long as the number of elements matches and the types are assignment compatible.
ArrayView_operator= (const T &fillValue)
 Fill assignment -- all elements are set to fillValue.
ArrayView_fill (const T &fillValue)
 Assign the supplied fill value to each element of this array, using T's copy assignment operator for each element.
void assign (size_type n, const T &fillValue)
 This is the same as fill() but has the usual std::vector signature for compatibility; it will only work if the given number of elements is the same as this array's (fixed) size.
template<class T2 >
void assign (const T2 *first, const T2 *last1)
 Assign to this array to make it a copy of the elements in range [first,last1) given by ordinary pointers, provided that the range is the same size as the array.
template<class Iter >
void assign (const Iter &first, const Iter &last1)
 Assign to this array to make it a copy of the elements in range [first,last1) given by non-pointer iterators (the pointer case is handled with a specialized assign() variant).
Element access

These methods provide read and write access to individual elements that are currently present in the array; the ArrayViewConst_<T,X> base class provides the read-only (const) methods.

const T & operator[] (index_type i) const
 Select an element by its index, returning a const reference.
T & operator[] (index_type i)
 Select an element by its index, returning a writable (lvalue) reference.
const T & at (index_type i) const
 Same as operator[] but always range-checked, even in a Release build.
T & at (index_type i)
 Same as operator[] but always range-checked, even in a Release build.
const T & getElt (index_type i) const
 Same as the const form of operator[]; exists to provide a non-operator method for element access in case that's needed.
T & updElt (index_type i)
 Same as the non-const form of operator[]; exists to provide a non-operator method for element access in case that's needed.
const T & front () const
 Return a const reference to the first element in this array, which must not be empty.
T & front ()
 Return a writable reference to the first element in this array, which must not be empty.
const T & back () const
 Return a const reference to the last element in this array, which must not be empty.
T & back ()
 Return a writable reference to the last element in this array, which must not be empty.
ArrayView_ operator() (index_type index, size_type length)
 Select a contiguous subarray of the elements of this array and create another ArrayView_ that refers only to those element (without copying).
ArrayView_ updSubArray (index_type index, size_type length)
 Same as non-const operator()(index,length); exists to provide non-operator access to that functionality in case it is needed.
Iterators

These methods deal in iterators, which are STL generalized pointers.

For this class, iterators are just ordinary pointers to T, and you may depend on that. By necessity, reverse iterators can't be just pointers; however, they contain an ordinary iterator (i.e. a pointer) that can be obtained by calling the reverse iterator's base() method.

const T * cbegin () const
 Return a const pointer to the first element of this array if any, otherwise end(), which may be null (0) in that case but does not have to be.
const T * begin () const
 The const version of begin() is the same as cbegin().
T * begin ()
 Return a writable pointer to the first element of this array if any, otherwise end().
const T * cend () const
 Return a const pointer to what would be the element just after the last one in the array; this may be null (0) if there are no elements but doesn't have to be.
const T * end () const
 The const version of end() is the same as cend().
T * end ()
 Return a writable pointer to what would be the element just after the last one in this array.
const_reverse_iterator crbegin () const
 Return a const reverse iterator pointing to the last element in the array or crend() if the array is empty.
const_reverse_iterator rbegin () const
 The const version of rbegin() is the same as crbegin().
reverse_iterator rbegin ()
 Return a writable reverse iterator pointing to the last element in the array or rend() if the array is empty.
const_reverse_iterator crend () const
 Return the past-the-end reverse iterator that tests equal to a reverse iterator that has been incremented past the front of the array.
const_reverse_iterator rend () const
 The const version of rend() is the same as crend().
reverse_iterator rend ()
 Return a writable past-the-end reverse iterator that tests equal to a reverse iterator that has been incremented past the front of the array.
const T * cdata () const
 Return a const pointer to the first element of the array, or possibly (but not necessarily) null (0) if the array is empty.
const T * data () const
 The const version of the data() method is identical to cdata().
T * data ()
 Return a writable pointer to the first allocated element of the array, or a null pointer if no space is associated with the array.
Size and capacity

These methods report the number of elements (size) or the amount of allocated heap space (capacity) or both but cannot be used to change size.

size_type size () const
 Return the current number of elements stored in this array.
size_type max_size () const
 Return the maximum allowable size for this array.
bool empty () const
 Return true if there are no elements currently stored in this array.
size_type capacity () const
 Return the number of elements this array can currently hold without requiring reallocation.
size_type allocated () const
 Return the amount of heap space owned by this array; this is the same as capacity() for owner arrays but is zero for non-owners.
bool isOwner () const
 Does this array own the data to which it refers? If not, it can't be resized, and the destructor will not free any heap space nor call any element destructors.

Related Functions

(Note that these are not member functions.)
Array_<T> serialization and I/O

These methods are at namespace scope but are logically part of the Array classes.

These deal with reading and writing Arrays from and to streams, which places an additional requirement on the element type T: the element must support the same operation you are trying to do on the Array as a whole.

template<class T , class X >
static std::istream & fillArrayViewFromStream (std::istream &in, ArrayView_< T, X > &out)
 Read in a fixed number of elements from a stream into an ArrayView.
template<class T , class X >
std::istream & operator>> (std::istream &in, ArrayView_< T, X > &out)
 Read a (fixed size n) ArrayView_<T> from a stream as a sequence of space- or comma-separated values of type T, optionally delimited by parentheses, square brackets, or curly braces.

Detailed Description

template<class T, class X>
class SimTK::ArrayView_< T, X >

This Array_ helper class is the base class for Array_, extending ArrayViewConst_ to add the ability to modify elements, but not the ability to change size or reallocate.

Template Parameters:
TThe type of object to be stored in this container.
XThe type to be used for indexing this container, with default unsigned (not size_t). Any integral type may be used, as well as user types that satisfy the requirements discussed with class ArrayIndexTraits.
See also:
Array_, ArrayViewConst_, ArrayIndexTraits

Member Typedef Documentation

template<class T, class X>
typedef T SimTK::ArrayView_< T, X >::value_type
template<class T, class X>
typedef X SimTK::ArrayView_< T, X >::index_type
template<class T, class X>
typedef T* SimTK::ArrayView_< T, X >::pointer
template<class T, class X>
typedef const T* SimTK::ArrayView_< T, X >::const_pointer
template<class T, class X>
typedef T& SimTK::ArrayView_< T, X >::reference
template<class T, class X>
typedef const T& SimTK::ArrayView_< T, X >::const_reference
template<class T, class X>
typedef T* SimTK::ArrayView_< T, X >::iterator
template<class T, class X>
typedef const T* SimTK::ArrayView_< T, X >::const_iterator
template<class T, class X>
typedef std::reverse_iterator<iterator> SimTK::ArrayView_< T, X >::reverse_iterator
template<class T, class X>
typedef std::reverse_iterator<const_iterator> SimTK::ArrayView_< T, X >::const_reverse_iterator
template<class T, class X>
typedef ArrayIndexTraits<X>::size_type SimTK::ArrayView_< T, X >::size_type
template<class T, class X>
typedef ArrayIndexTraits<X>::difference_type SimTK::ArrayView_< T, X >::difference_type
template<class T, class X>
typedef ArrayIndexPackType<size_type>::packed_size_type SimTK::ArrayView_< T, X >::packed_size_type

Constructor & Destructor Documentation

template<class T, class X>
SimTK::ArrayView_< T, X >::ArrayView_ ( ) [inline]

Default constructor allocates no heap space and is very fast.

template<class T, class X>
SimTK::ArrayView_< T, X >::ArrayView_ ( const ArrayView_< T, X > &  src) [inline]

Copy constructor is shallow.

template<class T, class X>
SimTK::ArrayView_< T, X >::ArrayView_ ( T *  first,
const T *  last1 
) [inline]

Construct from a range of writable memory.

template<class T, class X>
template<class A >
SimTK::ArrayView_< T, X >::ArrayView_ ( std::vector< T, A > &  v) [inline]

Construct to reference memory owned by a writable std::vector.

template<class T, class X>
SimTK::ArrayView_< T, X >::~ArrayView_ ( ) [inline]

The destructor just disconnects the array view handle from its data; see ArrayViewConst_<T,X>::disconnect() for more information.


Member Function Documentation

template<class T, class X>
SimTK::ArrayView_< T, X >::operator const Array_< T, X > & ( ) const [inline]

Implicit conversion of const ArrayView_ to const Array_& (zero cost).

Reimplemented from SimTK::ArrayViewConst_< T, X >.

template<class T, class X>
SimTK::ArrayView_< T, X >::operator Array_< T, X > & ( ) [inline]

Implicit conversion of non-const ArrayView_ to Array_& (zero cost).

template<class T, class X>
void SimTK::ArrayView_< T, X >::disconnect ( ) [inline]

Forward to base class disconnect() method -- clears the handle without doing anything to the data.

Reimplemented from SimTK::ArrayViewConst_< T, X >.

template<class T, class X>
ArrayView_& SimTK::ArrayView_< T, X >::operator= ( const ArrayView_< T, X > &  src) [inline]

Copy assignment; source must be the same size as this array.

template<class T, class X>
template<class T2 , class X2 >
ArrayView_& SimTK::ArrayView_< T, X >::operator= ( const ArrayViewConst_< T2, X2 > &  src) [inline]

Assignment from any other array object is allowed as long as the number of elements matches and the types are assignment compatible.

template<class T, class X>
template<class T2 , class X2 >
ArrayView_& SimTK::ArrayView_< T, X >::operator= ( const ArrayView_< T2, X2 > &  src) [inline]

Assignment from any other array object is allowed as long as the number of elements matches and the types are assignment compatible.

template<class T, class X>
template<class T2 , class X2 >
ArrayView_& SimTK::ArrayView_< T, X >::operator= ( const Array_< T2, X2 > &  src) [inline]
template<class T, class X>
template<class T2 , class A2 >
ArrayView_& SimTK::ArrayView_< T, X >::operator= ( const std::vector< T2, A2 > &  src) [inline]

Assignment from any std::vector object is allowed as long as the number of elements matches and the types are assignment compatible.

template<class T, class X>
ArrayView_& SimTK::ArrayView_< T, X >::operator= ( const T &  fillValue) [inline]

Fill assignment -- all elements are set to fillValue.

See also:
fill()
template<class T, class X>
ArrayView_& SimTK::ArrayView_< T, X >::fill ( const T &  fillValue) [inline]

Assign the supplied fill value to each element of this array, using T's copy assignment operator for each element.

Note that this also serves to allow fill from an object whose type T2 is different from T, as long as there is a constructor T(T2) that works since that can be invoked (implicitly or explicitly) to convert the T2 object to type T prior to the call.

Reimplemented in SimTK::Array_< T, X >.

template<class T, class X>
void SimTK::ArrayView_< T, X >::assign ( size_type  n,
const T &  fillValue 
) [inline]

This is the same as fill() but has the usual std::vector signature for compatibility; it will only work if the given number of elements is the same as this array's (fixed) size.

Reimplemented in SimTK::Array_< T, X >.

template<class T, class X>
template<class T2 >
void SimTK::ArrayView_< T, X >::assign ( const T2 *  first,
const T2 *  last1 
) [inline]

Assign to this array to make it a copy of the elements in range [first,last1) given by ordinary pointers, provided that the range is the same size as the array.

It is not allowed for the source range to include any of the elements currently in the array. The source elements can be of a type T2 that may be the same or different than this array's element type T as long as there is a T=T2 assignment operator that works. Note that although the source arguments are pointers, those may be iterators for some container depending on implementation details of the container. Specifically, any ArrayViewConst_, ArrayView_, or Array_ iterator is an ordinary pointer.

Parameters:
[in]firstA pointer to the first element to be copied.
[in]last1A pointer to the element one past the last element to be copied.
Precondition:
last1-first == size()
Complexity:
The T=T2 assignment operator will be called exactly size() times.

Reimplemented in SimTK::Array_< T, X >, SimTK::Array_< ObservationIx, MarkerIx >, SimTK::Array_< const EventReporter * >, SimTK::Array_< ContactCliqueId, short >, SimTK::Array_< QIndex, FreeQIndex >, SimTK::Array_< Real, AssemblyConditionIndex >, SimTK::Array_< std::string >, SimTK::Array_< int >, SimTK::Array_< AssemblyConditionIndex >, SimTK::Array_< Marker, MarkerIx >, SimTK::Array_< Vertex >, SimTK::Array_< Contact, int >, SimTK::Array_< QIndex >, SimTK::Array_< MarkerIx, ObservationIx >, SimTK::Array_< Face >, SimTK::Array_< Edge >, SimTK::Array_< ContactDetail >, SimTK::Array_< FreeQIndex, QIndex >, SimTK::Array_< Vec3, ObservationIx >, SimTK::Array_< CacheEntryIndex >, SimTK::Array_< AssemblyCondition *, AssemblyConditionIndex >, and SimTK::Array_< T *, size_t >.

template<class T, class X>
template<class Iter >
void SimTK::ArrayView_< T, X >::assign ( const Iter &  first,
const Iter &  last1 
) [inline]

Assign to this array to make it a copy of the elements in range [first,last1) given by non-pointer iterators (the pointer case is handled with a specialized assign() variant).

It is not allowed for this range to include any of the elements currently in the array. The source elements can be of a type T2 that may be the same or different than this array's element type T as long as there is a T=T2 operator that works.

The source must have the same number of elements as the current (fixed) size of this ArrayView. For input_iterators we'll be happy if we get enough elements and won't insist that the input stream is empty after that. For forward_ and bidirectional_iterators we'll copy the elements and complain at the end if there are too few or too many. For random_access_iterators we'll check in advance since we can do that fast.

Parameters:
[in]firstAn iterator pointing to the first element to be copied.
[in]last1An iterator pointing to the element one past the last element to be copied.
Remarks:
This variant of assign() will not be called when the iterators are forward iterators from ArrayViewConst_, ArrayView_, or Array_ objects since those are ordinary pointers.
Precondition:
last1 is reachable from first
distance(first,last1)==size()
Complexity:
The T=T2 assignment operator will be called exactly size() times.

Reimplemented in SimTK::Array_< T, X >, SimTK::Array_< ObservationIx, MarkerIx >, SimTK::Array_< const EventReporter * >, SimTK::Array_< ContactCliqueId, short >, SimTK::Array_< QIndex, FreeQIndex >, SimTK::Array_< Real, AssemblyConditionIndex >, SimTK::Array_< std::string >, SimTK::Array_< int >, SimTK::Array_< AssemblyConditionIndex >, SimTK::Array_< Marker, MarkerIx >, SimTK::Array_< Vertex >, SimTK::Array_< Contact, int >, SimTK::Array_< QIndex >, SimTK::Array_< MarkerIx, ObservationIx >, SimTK::Array_< Face >, SimTK::Array_< Edge >, SimTK::Array_< ContactDetail >, SimTK::Array_< FreeQIndex, QIndex >, SimTK::Array_< Vec3, ObservationIx >, SimTK::Array_< CacheEntryIndex >, SimTK::Array_< AssemblyCondition *, AssemblyConditionIndex >, and SimTK::Array_< T *, size_t >.

template<class T, class X>
const T& SimTK::ArrayView_< T, X >::operator[] ( index_type  i) const [inline]
template<class T, class X>
T& SimTK::ArrayView_< T, X >::operator[] ( index_type  i) [inline]
template<class T, class X>
const T& SimTK::ArrayView_< T, X >::at ( index_type  i) const [inline]
template<class T, class X>
T& SimTK::ArrayView_< T, X >::at ( index_type  i) [inline]
template<class T, class X>
const T& SimTK::ArrayView_< T, X >::getElt ( index_type  i) const [inline]
template<class T, class X>
T& SimTK::ArrayView_< T, X >::updElt ( index_type  i) [inline]
template<class T, class X>
const T& SimTK::ArrayView_< T, X >::front ( ) const [inline]
template<class T, class X>
T& SimTK::ArrayView_< T, X >::front ( ) [inline]
template<class T, class X>
const T& SimTK::ArrayView_< T, X >::back ( ) const [inline]
template<class T, class X>
T& SimTK::ArrayView_< T, X >::back ( ) [inline]
template<class T, class X>
ArrayView_ SimTK::ArrayView_< T, X >::operator() ( index_type  index,
size_type  length 
) [inline]

Select a contiguous subarray of the elements of this array and create another ArrayView_ that refers only to those element (without copying).

Parameters:
[in]indexThe index of the first element to be included in the subarray; this can be one past the end of the array if length is zero.
[in]lengthThe length of the subarray to be produced.
Returns:
A new ArrayView_<T,X> object referencing the original data.
Note:
If length==0 the returned array will be in a default-constructed, all-zero and null state with no connection to the original data.
Precondition:
index >= 0, length >= 0
index + length <= size()
We'll validate preconditions in Debug builds but not Release.
Complexity:
Dirt cheap; no element construction or destruction or heap allocation is required.

Reimplemented in SimTK::Array_< T, X >, SimTK::Array_< ObservationIx, MarkerIx >, SimTK::Array_< const EventReporter * >, SimTK::Array_< ContactCliqueId, short >, SimTK::Array_< QIndex, FreeQIndex >, SimTK::Array_< Real, AssemblyConditionIndex >, SimTK::Array_< std::string >, SimTK::Array_< int >, SimTK::Array_< AssemblyConditionIndex >, SimTK::Array_< Marker, MarkerIx >, SimTK::Array_< Vertex >, SimTK::Array_< Contact, int >, SimTK::Array_< QIndex >, SimTK::Array_< MarkerIx, ObservationIx >, SimTK::Array_< Face >, SimTK::Array_< Edge >, SimTK::Array_< ContactDetail >, SimTK::Array_< FreeQIndex, QIndex >, SimTK::Array_< Vec3, ObservationIx >, SimTK::Array_< CacheEntryIndex >, SimTK::Array_< AssemblyCondition *, AssemblyConditionIndex >, and SimTK::Array_< T *, size_t >.

template<class T, class X>
ArrayView_ SimTK::ArrayView_< T, X >::updSubArray ( index_type  index,
size_type  length 
) [inline]
template<class T, class X>
const T* SimTK::ArrayView_< T, X >::cbegin ( ) const [inline]
template<class T, class X>
const T* SimTK::ArrayView_< T, X >::begin ( ) const [inline]
template<class T, class X>
T* SimTK::ArrayView_< T, X >::begin ( ) [inline]
template<class T, class X>
const T* SimTK::ArrayView_< T, X >::cend ( ) const [inline]
template<class T, class X>
const T* SimTK::ArrayView_< T, X >::end ( ) const [inline]
template<class T, class X>
T* SimTK::ArrayView_< T, X >::end ( ) [inline]
template<class T, class X>
const_reverse_iterator SimTK::ArrayView_< T, X >::crbegin ( ) const [inline]
template<class T, class X>
const_reverse_iterator SimTK::ArrayView_< T, X >::rbegin ( ) const [inline]
template<class T, class X>
reverse_iterator SimTK::ArrayView_< T, X >::rbegin ( ) [inline]
template<class T, class X>
const_reverse_iterator SimTK::ArrayView_< T, X >::crend ( ) const [inline]
template<class T, class X>
const_reverse_iterator SimTK::ArrayView_< T, X >::rend ( ) const [inline]
template<class T, class X>
reverse_iterator SimTK::ArrayView_< T, X >::rend ( ) [inline]
template<class T, class X>
const T* SimTK::ArrayView_< T, X >::cdata ( ) const [inline]
template<class T, class X>
const T* SimTK::ArrayView_< T, X >::data ( ) const [inline]
template<class T, class X>
T* SimTK::ArrayView_< T, X >::data ( ) [inline]
template<class T, class X>
size_type SimTK::ArrayView_< T, X >::size ( ) const [inline]
template<class T, class X>
size_type SimTK::ArrayView_< T, X >::max_size ( ) const [inline]
template<class T, class X>
bool SimTK::ArrayView_< T, X >::empty ( ) const [inline]
template<class T, class X>
size_type SimTK::ArrayView_< T, X >::capacity ( ) const [inline]
template<class T, class X>
size_type SimTK::ArrayView_< T, X >::allocated ( ) const [inline]
template<class T, class X>
bool SimTK::ArrayView_< T, X >::isOwner ( ) const [inline]

Friends And Related Function Documentation

template<class T , class X >
static std::istream & fillArrayViewFromStream ( std::istream &  in,
ArrayView_< T, X > &  out 
) [related]

Read in a fixed number of elements from a stream into an ArrayView.

See fillArrayFromStream() for more information; this works the same way.

See also:
fillArrayFromStream()
template<class T , class X >
std::istream & operator>> ( std::istream &  in,
ArrayView_< T, X > &  out 
) [related]

Read a (fixed size n) ArrayView_<T> from a stream as a sequence of space- or comma-separated values of type T, optionally delimited by parentheses, square brackets, or curly braces.

If there are no delimiters then we will read size() values and then stop. Otherwise, there must be exactly size() values within the brackets. Each element is read in with its own operator ">>" so this won't work if no such operator is defined for type T.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines