Block< T, STRIDED, MOVE > Class Template Reference

#include <block.h>

Inheritance diagram for Block< T, STRIDED, MOVE >:

Detailed Description

template<typename T, Bool STRIDED, Bool MOVE>
class maxon::Block< T, STRIDED, MOVE >

A Block stands for a number of elements with a regular memory layout. It consists of a pointer for the first element, the element count and optionally a stride. All elements are placed consecutively in memory, but with a possible padding inbetween: The stride is the pointer difference between consecutive elements in bytes. By default, the stride is just SIZEOF(T).

You can use an alternative stride length to access only specific elements of your array. For example if you have an array with XYZ vectors and want to access only the X-values as a block, you could use the pointer to the first X-value and a stride length of SIZEOF(Vector). Or for a Block whose elements are all the same, you can use a stride of 0 and a pointer to a single value.

There is an important difference between a Block and arrays such as BaseArray with respect to the meaning of a const Block: A const block can't be modified itself, so its pointer and length cannot be changed, but the memory to which the block points is still non-const (if #T is a non-const type). So you can modify the memory through a const block. In other words, a[13] = 42; is OK for a const Block<Int> a, but not for a const BaseArray<Int> a. Therefore you have to use Block<const Int> whenever the memory of the block shall be read-only. A typical case is an input parameter to a function such as

static Result<Int32> StringConversion::ToInt32(const Block<const Utf32Char>& str, STRINGCONVERSION flags);

A Block supports the usual array functions which do not modify the length. Also it can be converted to the Array interface. If unsupported functions are invoked then, they will cause a DebugStop and indicate a failure on return.

Template Parameters
TType of elements of the block. If the memory must not be modified through this block, use a const type.
STRIDEDTrue if a stride other than SIZEOF(T) shall be supported.
MOVETrue if the values of this block shall be moved (only useful when used as a parameter type, see MoveBlock).
See also
$ref blocks

Public Types

using Super = ArrayBase< Block< T, STRIDED, MOVE >, T, BlockBase< T, STRIDED >, DefaultCompare >
 
using StrideType = typename std::conditional< GENERIC, Char, T >::type
 
using IsBlock = std::true_type
 
using Iterator = BaseIterator< Block, STRIDED >
 
using ConstIterator = BaseIterator< const Block, STRIDED >
 
- Public Types inherited from Collection< Block< T, STRIDED, MOVE >, T, BlockBase< T, STRIDED > >
using Super = BaseCollection< Block< T, STRIDED, MOVE >, BlockBase< T, STRIDED > >
 
using ValueType = T
 
- Public Types inherited from BaseCollection< Block< T, STRIDED, MOVE >, BlockBase< T, STRIDED > >
using IsCollection = std::true_type
 

Public Member Functions

 Block ()
 
 Block (T *ptr, Int size, Int stride=(STRIDED &&GENERIC) ? -1 :SIZEOF(StrideType))
 
 Block (Iterator start, Int size, Int stride=(STRIDED &&GENERIC) ? -1 :SIZEOF(StrideType))
 
 Block (ConstIterator start, Int size, Int stride=(STRIDED &&GENERIC) ? -1 :SIZEOF(StrideType))
 
template<Int N>
MAXON_IMPLICIT Block (T(&array)[N])
 
MAXON_IMPLICIT Block (const std::initializer_list< typename std::remove_const< T >::type > &list)
 
 Block (const Block &src)=default
 
Blockoperator= (const Block &src)=default
 
Result< void > CopyFrom (const Block &src)=delete
 
 operator ArrayImpl< Block & > ()=delete
 
 operator ArrayImpl< const Block & > () const =delete
 
Int GetStride () const
 
Bool IsStrided () const
 
void Set (T *ptr, Int size, Int stride=(STRIDED &&GENERIC) ? -1 :SIZEOF(StrideType))
 
void Reset ()
 
 operator typename std::conditional< STRIDED, DummyReturnType, Block< T, true, MOVE >>::type () const
 
 operator typename std::conditional< STRIDED||std::is_const< T >::value, const DummyReturnType, Block< const T, true, MOVE >>::type () const
 
 operator typename std::conditional< std::is_const< T >::value, volatile DummyReturnType, const Block< const T, STRIDED, MOVE > & >::type () const
 
 operator typename std::conditional< STRIDED||!MOVE, DummyReturnType &, Block< T, true, false >>::type () const
 
 operator typename std::conditional< STRIDED||!MOVE||std::is_const< T >::value, const DummyReturnType &, Block< const T, true, false >>::type () const
 
 operator typename std::conditional<!MOVE||std::is_const< T >::value, volatile DummyReturnType &, const Block< const T, STRIDED, false > & >::type () const
 
 operator typename std::conditional<!MOVE, const volatile DummyReturnType &, const Block< T, STRIDED, false > & >::type () const
 
 operator typename std::conditional< GENERIC, const volatile DummyReturnType, const Block< typename InheritConst< Generic, T >::type, STRIDED > & >::type () const
 
T * GetFirst () const
 
void SetFirst (T *value)
 
T * GetLast () const
 
T & operator[] (Int index) const
 
Block Slice (Int start) const
 
Block Slice (Int start, Int end) const
 
Bool StartsWith (const Block &prefix) const
 
Iterator Begin () const
 
Iterator End () const
 
Int GetCapacityCount () const
 
template<typename T2 , Bool S2>
Result< void > CopyValuesFrom (const Block< T2, S2 > &other)
 
template<typename COLLECTION >
Result< void > CopyValuesFrom (const COLLECTION &other)
 
Int GetMemorySize () const
 
Int GetBlock (Int index, Block< const T, false > &block) const
 
Int GetBlock (Int index, Block< T, false > &block)
 
Int GetBlock (Int index, StridedBlock< T > &block)
 
Int GetBlock (Int index, StridedBlock< const T > &block) const
 
- Public Member Functions inherited from ArrayBase< Block< T, STRIDED, MOVE >, T, BlockBase< T, STRIDED >, DefaultCompare >
MAXON_ATTRIBUTE_FORCE_INLINE ArrayBase (ARGS &&... args)
 
ArrayImpl< Block< T, STRIDED, MOVE > &> ToArray ()
 
ArrayImpl< const Block< T, STRIDED, MOVE > &> ToArray () const
 
MAXON_ATTRIBUTE_FORCE_INLINE operator ArrayImpl< Block< T, STRIDED, MOVE > &> ()
 
MAXON_ATTRIBUTE_FORCE_INLINE operator ArrayImpl< const Block< T, STRIDED, MOVE > &> () const
 
- Public Member Functions inherited from ArrayBase0< Block< T, STRIDED, MOVE >, T, BlockBase< T, STRIDED >, DefaultCompare >
MAXON_ATTRIBUTE_FORCE_INLINE ArrayBase0 (ARGS &&... args)
 
Bool IsValidIndex (Int index) const
 
Result< void > CheckValidIndex (Int index) const
 
Int FindIndex (typename ByValueParam< T >::type v, Int start) const
 
Int FindLastIndex (typename ByValueParam< T >::type v) const
 
Int FindLastIndex (typename ByValueParam< T >::type v, Int start) const
 
Bool EraseFirst (typename ByValueParam< T >::type v)
 
Int EraseAll (typename ByValueParam< T >::type v)
 
Result< void > AppendAllImpl (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags, Bool overwrite, OverloadRank0)
 
Result< void > InsertAll (Int index, COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags=COLLECTION_RESIZE_FLAGS::ON_GROW_RESERVE_CAPACITY)
 
Result< void > Add (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags=COLLECTION_RESIZE_FLAGS::ON_GROW_RESERVE_CAPACITY)
 
Result< void > SubtractImpl (COLLECTION2 &&other, OverloadRank0)
 
Bool IsEqualImpl (const COLLECTION2 &other, COMPARE &&cmp, OverloadRank0) const
 
UInt GetHashCode () const
 
MAXON_ATTRIBUTE_FORCE_INLINE AutoIterator< Block< T, STRIDED, MOVE > > Slice (Int start)
 
MAXON_ATTRIBUTE_FORCE_INLINE AutoIterator< const Block< T, STRIDED, MOVE > > Slice (Int start) const
 
MAXON_ATTRIBUTE_FORCE_INLINE AutoIterator< Block< T, STRIDED, MOVE > > Slice (Int start, Int end)
 
MAXON_ATTRIBUTE_FORCE_INLINE AutoIterator< const Block< T, STRIDED, MOVE > > Slice (Int start, Int end) const
 
BlockIterator< Block< T, STRIDED, MOVE >, T, false, false > GetBlocks ()
 
BlockIterator< Block< T, STRIDED, MOVE >, T, true, false > GetBlocks () const
 
BlockIterator< Block< T, STRIDED, MOVE >, T, false, true > GetStridedBlocks ()
 
BlockIterator< Block< T, STRIDED, MOVE >, T, true, true > GetStridedBlocks () const
 
- Public Member Functions inherited from Collection< Block< T, STRIDED, MOVE >, T, BlockBase< T, STRIDED > >
MAXON_ATTRIBUTE_FORCE_INLINE Collection (ARGS &&... args)
 
ResultOk< void > VariadicAppend ()
 
Result< void > VariadicAppend (V &&value, VALUES &&... rest)
 
 operator ValueReceiver< const T &> ()
 
 operator ValueReceiver< T &&> ()
 
 operator ValueReceiver< typename std::conditional< std::is_scalar< T >::value, T, DummyParamType &>::type > ()
 
Result< BoolForEach (FN &&callback)
 
H::Iterator Find (typename ByValueParam< T >::type v)
 
H::ConstIterator Find (typename ByValueParam< T >::type v) const
 
Int FindIndex (typename ByValueParam< T >::type v) const
 
MAXON_ATTRIBUTE_FORCE_INLINE Bool Contains (typename ByValueParam< T >::type v) const
 
- Public Member Functions inherited from BaseCollection< Block< T, STRIDED, MOVE >, BlockBase< T, STRIDED > >
MAXON_ATTRIBUTE_FORCE_INLINE BaseCollection (ARGS &&... args)
 
MAXON_ATTRIBUTE_FORCE_INLINE std::enable_if< maxon::IsCollection< COLLECTION2 >::value, Bool >::type operator== (const COLLECTION2 &other) const
 
MAXON_ATTRIBUTE_FORCE_INLINE std::enable_if< maxon::IsCollection< COLLECTION2 >::value, Bool >::type operator!= (const COLLECTION2 &other) const
 
MAXON_ATTRIBUTE_FORCE_INLINE std::enable_if< maxon::IsCollection< COLLECTION2 >::value &&!std::is_same< typename std::decay< COMPARE >::type, EQUALITY >::value, Bool >::type IsEqual (const COLLECTION2 &other, COMPARE &&cmp=COMPARE()) const
 
MAXON_ATTRIBUTE_FORCE_INLINE Result< void > AppendAll (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags=COLLECTION_RESIZE_FLAGS::ON_GROW_RESERVE_CAPACITY)
 
MAXON_ATTRIBUTE_FORCE_INLINE Result< void > CopyFrom (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags=COLLECTION_RESIZE_FLAGS::FIT_TO_SIZE)
 
MAXON_ATTRIBUTE_FORCE_INLINE Result< void > Subtract (COLLECTION2 &&other)
 
MAXON_ATTRIBUTE_FORCE_INLINE Result< void > Intersect (const COLLECTION2 &other)
 
Bool Intersects (const COLLECTION2 &other) const
 
MAXON_ATTRIBUTE_FORCE_INLINE Result< void > CopyFromImpl (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags, OverloadRank0)
 
Result< void > AppendAllImpl (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags, Bool overwrite, OverloadRank0)
 
Result< void > IntersectImpl (COLLECTION2 &&other, OverloadRank0)
 
MAXON_ATTRIBUTE_FORCE_INLINE Bool IsEmpty () const
 
MAXON_ATTRIBUTE_FORCE_INLINE Bool IsPopulated () const
 
String ToString (const FormatStatement *formatStatement) const
 
MAXON_ATTRIBUTE_FORCE_INLINE Bool ContainsAll (COLLECTION2 &&other) const
 
Bool ContainsAllImpl (COLLECTION2 &&other, OverloadRank0) const
 
- Public Member Functions inherited from BlockBase< T, STRIDED >
 BlockBase (T *ptr, Int size, Int stride)
 
Int GetCount () const
 
void SetCount (Int cnt)
 

Static Public Member Functions

static const BlockNullValueRef ()
 
- Static Public Member Functions inherited from Collection< Block< T, STRIDED, MOVE >, T, BlockBase< T, STRIDED > >
static const T & GetMapKey (const T &key)
 

Static Public Attributes

static const Bool GENERIC
 
- Static Public Attributes inherited from ArrayBase0< Block< T, STRIDED, MOVE >, T, BlockBase< T, STRIDED >, DefaultCompare >
static const COLLECTION_KIND KIND
 

Private Member Functions

T * GetPtr (Int index) const
 
ResultRef< T > Insert (Int index)
 
ResultMem Insert (Int index, const Block< const T > &values)
 
ResultRef< T > Append ()
 
ResultMem Erase (Int index, Int count)
 
ResultMem SwapErase (Int index, Int count)
 
ResultMem SetCapacityHint (Int requestedCapacity, COLLECTION_RESIZE_FLAGS resizeFlags=COLLECTION_RESIZE_FLAGS::ON_GROW_RESERVE_CAPACITY)
 
ResultMem Resize (Int count, COLLECTION_RESIZE_FLAGS resizeFlags=COLLECTION_RESIZE_FLAGS::DEFAULT)
 
void Flush ()
 

Friends

template<typename ARRAY >
class ArrayImpl
 

Additional Inherited Members

- Protected Member Functions inherited from BlockBase< T, STRIDED >
Int PrivateGetStride () const
 
- Protected Attributes inherited from BlockBase< T, STRIDED >
T * _ptr
 
Int _size
 
Int _stride
 

Member Typedef Documentation

◆ Super

using Super = ArrayBase<Block<T, STRIDED, MOVE>, T, BlockBase<T, STRIDED>, DefaultCompare>

◆ StrideType

using StrideType = typename std::conditional<GENERIC, Char, T>::type

◆ IsBlock

using IsBlock = std::true_type

◆ Iterator

using Iterator = BaseIterator<Block, STRIDED>

◆ ConstIterator

using ConstIterator = BaseIterator<const Block, STRIDED>

Constructor & Destructor Documentation

◆ Block() [1/7]

Block ( )

Constructs an empty block with a nullptr pointer and zero count.

◆ Block() [2/7]

Block ( T *  ptr,
Int  size,
Int  stride = (STRIDED && GENERIC) ? -1 : SIZEOF(StrideType) 
)

Constructs a block using the given values.

Parameters
[in]ptrPointer to the first element.
[in]sizeNumber of elements.
[in]strideElement stride. If the STRIDED parameter of the Block class is false, this has to be SIZEOF(T).

◆ Block() [3/7]

Block ( Iterator  start,
Int  size,
Int  stride = (STRIDED && GENERIC) ? -1 : SIZEOF(StrideType) 
)

Constructs a block from iterator and size. Equivalent to Block(static_cast<T*>(&*start), size, stride).

◆ Block() [4/7]

Block ( ConstIterator  start,
Int  size,
Int  stride = (STRIDED && GENERIC) ? -1 : SIZEOF(StrideType) 
)

Constructs a block from const iterator and size. Equivalent to Block(static_cast<T*>(&*start), size, stride).

◆ Block() [5/7]

MAXON_IMPLICIT Block ( T(&)  array[N])

Constructs a block for a C++ fixed-size array. The length is automatically set to the full length of the array.

◆ Block() [6/7]

MAXON_IMPLICIT Block ( const std::initializer_list< typename std::remove_const< T >::type > &  list)

◆ Block() [7/7]

Block ( const Block< T, STRIDED, MOVE > &  src)
default

Member Function Documentation

◆ operator=()

Block& operator= ( const Block< T, STRIDED, MOVE > &  src)
default

◆ CopyFrom()

Result<void> CopyFrom ( const Block< T, STRIDED, MOVE > &  src)
delete

◆ operator ArrayImpl< Block & >()

operator ArrayImpl< Block &> ( )
delete

◆ operator ArrayImpl< const Block & >()

operator ArrayImpl< const Block &> ( ) const
delete

◆ GetStride()

Int GetStride ( ) const

Returns the stride of this block. If the STRIDED parameter of the Block class is false, this will always be SIZEOF(T).

Returns
Element stride of this block, i.e., the difference in bytes between two consecutive elements.

◆ IsStrided()

Bool IsStrided ( ) const

Returns true if this block uses a stride other than SIZEOF(T).

Returns
True if the stride differs from the element size.

◆ Set()

void Set ( T *  ptr,
Int  size,
Int  stride = (STRIDED && GENERIC) ? -1 : SIZEOF(StrideType) 
)

Sets this block to new values.

Parameters
[in]ptrPointer to the first element.
[in]sizeNumber of elements.
[in]strideElement stride. If the STRIDED parameter of the Block class is false, this has to be SIZEOF(T).

◆ Reset()

void Reset ( void  )

Resets this block so that is points to nullptr and has zero length.

◆ operator typename std::conditional< STRIDED, DummyReturnType, Block< T, true, MOVE >>::type()

operator typename std::conditional< STRIDED, DummyReturnType, Block< T, true, MOVE >>::type ( ) const

Converts this block to a strided block.

◆ operator typename std::conditional< STRIDED||std::is_const< T >::value, const DummyReturnType, Block< const T, true, MOVE >>::type()

operator typename std::conditional< STRIDED||std::is_const< T >::value, const DummyReturnType, Block< const T, true, MOVE >>::type ( ) const

Converts this block to a strided block with constant elements.

◆ operator typename std::conditional< std::is_const< T >::value, volatile DummyReturnType, const Block< const T, STRIDED, MOVE > & >::type()

operator typename std::conditional< std::is_const< T >::value, volatile DummyReturnType, const Block< const T, STRIDED, MOVE > &>::type ( ) const

Converts this block to a block with constant elements.

◆ operator typename std::conditional< STRIDED||!MOVE, DummyReturnType &, Block< T, true, false >>::type()

operator typename std::conditional< STRIDED||!MOVE, DummyReturnType &, Block< T, true, false >>::type ( ) const

Converts this block to a strided non-movable block.

◆ operator typename std::conditional< STRIDED||!MOVE||std::is_const< T >::value, const DummyReturnType &, Block< const T, true, false >>::type()

operator typename std::conditional< STRIDED||!MOVE||std::is_const< T >::value, const DummyReturnType &, Block< const T, true, false >>::type ( ) const

Converts this block to a strided non-movable block with constant elements.

◆ operator typename std::conditional<!MOVE||std::is_const< T >::value, volatile DummyReturnType &, const Block< const T, STRIDED, false > & >::type()

operator typename std::conditional<!MOVE||std::is_const< T >::value, volatile DummyReturnType &, const Block< const T, STRIDED, false > &>::type ( ) const

Converts this block to a non-movable block with constant elements.

◆ operator typename std::conditional<!MOVE, const volatile DummyReturnType &, const Block< T, STRIDED, false > & >::type()

operator typename std::conditional<!MOVE, const volatile DummyReturnType &, const Block< T, STRIDED, false > &>::type ( ) const

Converts this block to a non-movable block.

◆ operator typename std::conditional< GENERIC, const volatile DummyReturnType, const Block< typename InheritConst< Generic, T >::type, STRIDED > & >::type()

operator typename std::conditional< GENERIC, const volatile DummyReturnType, const Block< typename InheritConst< Generic, T >::type, STRIDED > &>::type ( ) const

Converts this block to a generic block.

◆ GetFirst()

T* GetFirst ( ) const

Returns the first element of this block. For an empty block, this is undefined and might be nullptr or any other value.

Returns
Pointer to the first element of this block (undefined if the block is empty).

◆ SetFirst()

void SetFirst ( T *  value)

Sets the first element of this block, may be nullptr.

Parameters
[in]valuePointer to first element of this block.

◆ GetLast()

T* GetLast ( ) const

Returns the last element of this block. For an empty block, this is undefined and might be nullptr or any other value.

Returns
Pointer to the last element of this block (undefined if the block is empty).

◆ operator[]()

T& operator[] ( Int  index) const

Returns the element at the given index of this block.

Parameters
[in]indexIndex into the block.
Returns
Reference to the value at index.

◆ Slice() [1/2]

Block Slice ( Int  start) const

Returns a sub block of this block with contains the elements from the given start index up to the end of the block.

Parameters
[in]startStart index of the sub block.
Returns
Sub block starting at start.

◆ Slice() [2/2]

Block Slice ( Int  start,
Int  end 
) const

Returns a sub block of this block with contains the elements from the given start index up to but not including the given end index.

Parameters
[in]startStart index of the sub block.
[in]endEnd index of the sub block. The end index is exclusive.
Returns
Sub block with elements from start up to end.

◆ StartsWith()

Bool StartsWith ( const Block< T, STRIDED, MOVE > &  prefix) const

◆ Begin()

Iterator Begin ( ) const

Gets an iterator for the first element.

Returns
Iterator for the first element (equal to End() if the array is empty).

◆ End()

Iterator End ( ) const

Gets an iterator for the end (End() - 1 is the last element if the array is not empty).

Returns
Iterator for the array end (this is behind the last element).

◆ NullValueRef()

static const Block& NullValueRef ( )
static

◆ GetCapacityCount()

Int GetCapacityCount ( ) const

◆ CopyValuesFrom() [1/2]

Result<void> CopyValuesFrom ( const Block< T2, S2 > &  other)

◆ CopyValuesFrom() [2/2]

Result<void> CopyValuesFrom ( const COLLECTION &  other)

◆ GetMemorySize()

Int GetMemorySize ( void  ) const

◆ GetBlock() [1/4]

Int GetBlock ( Int  index,
Block< const T, false > &  block 
) const

◆ GetBlock() [2/4]

Int GetBlock ( Int  index,
Block< T, false > &  block 
)

◆ GetBlock() [3/4]

Int GetBlock ( Int  index,
StridedBlock< T > &  block 
)

◆ GetBlock() [4/4]

Int GetBlock ( Int  index,
StridedBlock< const T > &  block 
) const

◆ GetPtr()

T* GetPtr ( Int  index) const
private

◆ Insert() [1/2]

ResultRef<T> Insert ( Int  index)
private

◆ Insert() [2/2]

ResultMem Insert ( Int  index,
const Block< const T > &  values 
)
private

◆ Append()

ResultRef<T> Append ( )
private

◆ Erase()

ResultMem Erase ( Int  index,
Int  count 
)
private

◆ SwapErase()

ResultMem SwapErase ( Int  index,
Int  count 
)
private

◆ SetCapacityHint()

ResultMem SetCapacityHint ( Int  requestedCapacity,
COLLECTION_RESIZE_FLAGS  resizeFlags = COLLECTION_RESIZE_FLAGS::ON_GROW_RESERVE_CAPACITY 
)
private

◆ Resize()

ResultMem Resize ( Int  count,
COLLECTION_RESIZE_FLAGS  resizeFlags = COLLECTION_RESIZE_FLAGS::DEFAULT 
)
private

◆ Flush()

void Flush ( void  )
private

Friends And Related Function Documentation

◆ ArrayImpl

friend class ArrayImpl
friend

Member Data Documentation

◆ GENERIC

const Bool GENERIC
static