rcolyer.net
RC Lib  Version 201607061602
Public Member Functions | Static Public Attributes | List of all members
RC::Data1D< T > Class Template Reference

A bounds-safe one-dimensional vector-like structure. More...

#include <Data1D.h>

Public Member Functions

 Data1D (size_t d_size)
 Constructor which sets the initial size to d_size. More...
 
 Data1D (const Data1D< T > &copy)
 Copy constructor that copies all elements. More...
 
 Data1D (const std::initializer_list< T > &new_data)
 Initializer list constructor, for initializing with bracketed data. More...
 
 Data1D (Data1D< T > &&other)
 Move constructor that reassigns all the data to this Data1D. More...
 
 Data1D (size_t d_size, T *new_data, bool auto_delete=false)
 Efficiently wraps in-place C-pointer data in a bounds-safe container. More...
 
void Delete ()
 Delete all the elements and free all allocated memory.
 
 ~Data1D ()
 Deletes all contents upon destruction. More...
 
bool IsEmpty () const
 Returns true if there are no elements / the size is 0.
 
template<class T2 >
size_t Find (const T2 &elem, size_t start_at=0) const
 Returns the first index at or after start_at for which the data equals elem. More...
 
template<class T2 >
bool Contains (const T2 &elem) const
 Returns true if at least one entry equals elem.
 
bool Check (const size_t x) const
 Check if index x is in bounds. More...
 
void Assert (const size_t x) const
 Throws an ErrorMsgBounds exception if x is out of bounds. More...
 
void Reserve (const size_t reserve_size)
 Reserve storage without resizing the array. More...
 
void Resize (const size_t resize_size)
 Resize the array, reallocating if necessary. More...
 
void SetOffset (const size_t new_offset)
 Set a new offset position for index 0. More...
 
void SetRange (const size_t new_offset, const size_t new_size)
 Set a new offset and data size, resizing if necessary. More...
 
T * Raw () const
 Access a raw unprotected C-pointer to the enclosed data. More...
 
size_t size () const
 Returns the current number of elements.
 
size_t TypeSize () const
 Returns the size of this Data1D's type.
 
size_t ByteSize () const
 Returns the current size in bytes.
 
size_t reserved () const
 Returns the number of elements for which space is reserved.
 
size_t ByteReserved () const
 Returns the current allocated storage in bytes.
 
size_t GetOffset () const
 Returns the current offset for index 0. More...
 
Data1D Copy (const size_t pos=0, const size_t amnt=npos) const
 Creates a duplicate copy of the contents, with up to amnt elements from pos.
 
template<class T2 >
Data1DCopyFrom (const Data1D< T2 > &other)
 Copy data from any type with a compatible assignment operator. More...
 
template<class T2 >
Data1DCopyFrom (const Data1D< T2 > &other, size_t pos, size_t num_elem=npos)
 assignment operator. More...
 
template<class T2 >
void CopyAt (const size_t pos, const Data1D< T2 > &other)
 Overwrite a range of data using all data from any compatible type, expanding if necessary. More...
 
template<class T2 >
void CopyAt (const size_t pos, const Data1D< T2 > &other, const size_t other_start, const size_t amnt=npos)
 Overwrite a range of data from any compatible type, expanding if necessary. More...
 
void CopyData (const size_t dest, const size_t source, const size_t amnt=npos)
 Copy data from any location in this Data1D to another location, handling overlap automatically. More...
 
Data1Doperator= (const Data1D &other)
 Assignment operator which copies all contents from other, respecting offsets. More...
 
Data1Doperator= (Data1D &&other)
 Assignment operator which copies all contents from other, respecting offsets. More...
 
template<class T2 >
Data1D< T2 > Cast ()
 Returns a new array with all the elements of this array assigned to type T2. More...
 
template<class Conv >
auto CastWith (Conv converter) -> Data1D< decltype(converter(T
 Returns a new array with all the elements of this array converted to another type by converter.
 

Static Public Attributes

static const size_t npos = size_t(-1)
 The largest possible value of size_t.
 

Detailed Description

template<class T>
class RC::Data1D< T >

A bounds-safe one-dimensional vector-like structure.

It provides efficient resizing and offsets, as well as convenience functions for assignments and comparisons. It also provides bounds-safe iterators. Note: Non-POD classes stored in Data1D containers must have a default constructor with default values or no arguments, which is necessary for efficient resizing. This requirement also permits safe use of offsets and temporary shrinking, because all allocated space contains defined data with properly constructed objects.

See also
Data2D
Data3D

Constructor & Destructor Documentation

template<class T>
RC::Data1D< T >::Data1D ( size_t  d_size)
inlineexplicit

Constructor which sets the initial size to d_size.

Parameters
d_sizeThe initial number of elements.
template<class T>
RC::Data1D< T >::Data1D ( const Data1D< T > &  copy)
inline

Copy constructor that copies all elements.

Parameters
copyA source Data1D from which elements should be copied.
template<class T>
RC::Data1D< T >::Data1D ( const std::initializer_list< T > &  new_data)
inline

Initializer list constructor, for initializing with bracketed data.

Parameters
new_dataAn initialization list of the form {elem1, elem2, ...}.
template<class T>
RC::Data1D< T >::Data1D ( Data1D< T > &&  other)
inline

Move constructor that reassigns all the data to this Data1D.

Parameters
otherA source Data1D from which elements should be moved.
template<class T>
RC::Data1D< T >::Data1D ( size_t  d_size,
T *  new_data,
bool  auto_delete = false 
)
inline

Efficiently wraps in-place C-pointer data in a bounds-safe container.

Note: If auto_delete=true, new_data must have been acquired with new[]. delete[] is used internally, so behavior may vary if it was acquired with new, malloc, or others.

Parameters
d_sizeThe initial number of elements.
new_dataC-pointer data to wrap.
auto_deleteShould the Data1D delete new_data upon destruction?
template<class T>
RC::Data1D< T >::~Data1D ( )
inline

Deletes all contents upon destruction.

Note: For wrapped pointers, if auto_delete was false no deletion of the original data occurs.

Member Function Documentation

template<class T>
void RC::Data1D< T >::Assert ( const size_t  x) const
inline

Throws an ErrorMsgBounds exception if x is out of bounds.

Parameters
xThe index to check.
template<class T>
template<class T2 >
Data1D<T2> RC::Data1D< T >::Cast ( )
inline

Returns a new array with all the elements of this array assigned to type T2.

See also
CopyFrom
template<class T>
bool RC::Data1D< T >::Check ( const size_t  x) const
inline

Check if index x is in bounds.

Parameters
xThe index to check.
Returns
True if in bounds.
template<class T>
template<class T2 >
void RC::Data1D< T >::CopyAt ( const size_t  pos,
const Data1D< T2 > &  other 
)
inline

Overwrite a range of data using all data from any compatible type, expanding if necessary.

Parameters
posThe offset in this Data1D to begin overwriting.
otherThe compatible Data1D to copy from.
template<class T>
template<class T2 >
void RC::Data1D< T >::CopyAt ( const size_t  pos,
const Data1D< T2 > &  other,
const size_t  other_start,
const size_t  amnt = npos 
)
inline

Overwrite a range of data from any compatible type, expanding if necessary.

Parameters
posThe offset in this Data1D to begin overwriting.
otherThe compatible Data1D to copy from.
other_startThe offset in the other Data1D to begin copying from.
amntThe quantity of data to copy (automatically bounds-capped, defaults to all).
template<class T>
void RC::Data1D< T >::CopyData ( const size_t  dest,
const size_t  source,
const size_t  amnt = npos 
)
inline

Copy data from any location in this Data1D to another location, handling overlap automatically.

Parameters
destThe offset in this Data1D to begin overwriting.
sourceThe offset in this Data1D to copy from.
amntThe quantity of data to copy (automatically bounds-capped, defaults to all).
template<class T>
template<class T2 >
Data1D& RC::Data1D< T >::CopyFrom ( const Data1D< T2 > &  other)
inline

Copy data from any type with a compatible assignment operator.

Parameters
otherThe compatible Data1D from which data should be copied.
Returns
A reference to this Data1D.
template<class T>
template<class T2 >
Data1D& RC::Data1D< T >::CopyFrom ( const Data1D< T2 > &  other,
size_t  pos,
size_t  num_elem = npos 
)
inline

assignment operator.

Note, to copy data around in the same Data1D, use CopyData.

Parameters
otherThe compatible Data1D from which data should be copied.
posThe offset in other to start copying from.
num_elemThe number of elements to copy.
Returns
A reference to this Data1D.
template<class T>
template<class T2 >
size_t RC::Data1D< T >::Find ( const T2 &  elem,
size_t  start_at = 0 
) const
inline

Returns the first index at or after start_at for which the data equals elem.

Parameters
elemThe element to compare to each entry in this Data1D
start_atThe first index to begin comparing at.
Returns
The index of the item found, or npos if no matching data is found.
template<class T>
size_t RC::Data1D< T >::GetOffset ( ) const
inline

Returns the current offset for index 0.

See also
SetOffset
SetRange
template<class T>
Data1D& RC::Data1D< T >::operator= ( const Data1D< T > &  other)
inline

Assignment operator which copies all contents from other, respecting offsets.

Parameters
otherData1D to copy from, from offset (default 0) to size.
Returns
This object.
template<class T>
Data1D& RC::Data1D< T >::operator= ( Data1D< T > &&  other)
inline

Assignment operator which copies all contents from other, respecting offsets.

Parameters
otherData1D to copy from, from offset (default 0) to size.
Returns
This object.
template<class T>
T* RC::Data1D< T >::Raw ( ) const
inline

Access a raw unprotected C-pointer to the enclosed data.

Warning: This convenience function bypasses the bounds protections provided by this class. The returned pointer is likely to become invalid after any of the operations on this object which change the size.

Returns
C-style pointer to the contents at the offset (0 by default).
template<class T>
void RC::Data1D< T >::Reserve ( const size_t  reserve_size)
inline

Reserve storage without resizing the array.

Parameters
reserve_sizeThe number of elements worth of storage to reserve.
template<class T>
void RC::Data1D< T >::Resize ( const size_t  resize_size)
inline

Resize the array, reallocating if necessary.

This may trigger a copy operation upon expansion. For efficiency it never reallocates or copies while shrinking or expanding within a previous size range. Use Crop if necessary to shrink storage to the current size.

Parameters
resize_sizeThe new number of elements.
template<class T>
void RC::Data1D< T >::SetOffset ( const size_t  new_offset)
inline

Set a new offset position for index 0.

This also adjusts the size to reach the same last element.

Parameters
new_offsetThe new absolute offset from the 0th allocated element.
See also
SetRange
template<class T>
void RC::Data1D< T >::SetRange ( const size_t  new_offset,
const size_t  new_size 
)
inline

Set a new offset and data size, resizing if necessary.

Parameters
new_offsetThe new absolute offset from the 0th allocated element.
new_sizeThe new number of elements.
See also
SetOffset
Resize

The documentation for this class was generated from the following file:
email address
— (c) 2015