CoreComponents 3.0.0
A Modern C++ Toolkit
Loading...
Searching...
No Matches
Array< T > Class Template Reference

An array of items within a contiguous chunk of memory. More...

#include <cc/Array>

Public Types

using Item = T
 Item type.
 

Standard Iterators

using value_type = Item
 Item value type.
 
using size_type = long
 Type of the container capacity.
 
using iterator = ArrayIterator<Array, 1>
 Value iterator.
 
using const_iterator = ArrayIterator<const Array, 1>
 Readonly value iterator.
 
using reverse_iterator = ArrayIterator<Array, -1>
 Reverse value iterator.
 
using const_reverse_iterator = ArrayIterator<const Array, -1>
 Readonly reverse value iterator.
 
iterator begin ()
 Return iterator pointing to the first item (if any)
 
iterator end ()
 Return iterator pointing behind the last item
 
const_iterator begin () const
 Return readonly iterator pointing to the first item (if any)
 
const_iterator cbegin () const
 Return readonly iterator pointing to the first item (if any)
 
const_iterator end () const
 Return readonly iterator pointing behind the last item
 
const_iterator cend () const
 Return readonly iterator pointing behind the last item
 
reverse_iterator rbegin ()
 Return reverse iterator pointing to the last item (if any)
 
reverse_iterator rend ()
 Return reverse iterator pointing before the first item
 
const_reverse_iterator rbegin () const
 Return readonly reverse iterator pointing to the last item (if any)
 
const_reverse_iterator crbegin () const
 Return readonly reverse iterator pointing to the last item (if any)
 
const_reverse_iterator rend () const
 Return reverse iterator pointing before the first item
 
const_reverse_iterator crend () const
 Return reverse iterator pointing before the first item
 

Construction and Assignment

static Array allocate (long n)
 Create a array of n items.
 
 Array ()
 Create an empty array.
 
 Array (Dim< 1 > dim)
 Create a array of dim[0] items.
 
 Array (const Array &other)=default
 Construct a copy of other.
 
 Array (Array &&b)
 Construct from right-side b.
 
 Array (std::initializer_list< T > items)
 Construct with initial items.
 
 Array (std::initializer_list< Array > arrays)
 Concatenate arrays into a single new array.
 
 Array (const Item *src, long count)
 Create a copy of the low-level array src of count items.
 
 Array (const List< Array > &parts)
 Join several arrays into one.
 
 Array (const List< Array > &parts, Item sep)
 Join several arrays into one using sep as interposing separator.
 
Arrayoperator= (const Array &)=default
 Default assignment operator.
 
Arrayoperator= (Array &&b)
 Assign right-side b.
 

Type Compatibility

static Array wrap (void *data, long count)
 Create a new array wrapped around data containing count items.
 
const Itemitems () const
 Get pointer to internal memory buffer.
 
Itemitems ()
 Get pointer to internal memory buffer.
 
const char * chars () const
 Low-level access to the underlying characters.
 
char * chars ()
 Low-level access to the underlying characters.
 
const uint8_tbytes () const
 Low-level access to the underlying bytes.
 
uint8_tbytes ()
 Low-level access to the underlying bytes.
 
const uint32_twords () const
 Low-level access to the underlying memory words.
 
uint32_twords ()
 Low-level access to the underlying memory words.
 
template<class OtherItem >
 operator OtherItem * ()
 Explicit conversion to low-level pointer.
 
template<class OtherItem >
 operator const OtherItem * () const
 Explicit conversion to constant low-level pointer.
 
 operator void * ()
 Implicit conversion to low-level data pointer.
 
 operator const void * () const
 Implicit conversion to constant low-level data pointer.
 
void wrapAround (void *data, long count)
 Wrap this array around the low-level memory vector data of count items.
 

Item Access

long count () const
 Size of this array in number of items.
 
long size () const
 Size of this array in number of items.
 
bool has (long i) const
 Check if i is a valid index.
 
 operator bool () const
 Check if this array is non-empty.
 
long operator+ () const
 Size of this array in number of items.
 
Itemat (long i)
 Get a reference to the item at i.
 
const Itemat (long i) const
 Get a constant reference to the item at i.
 
template<class U >
long itemCount () const
 Get number of items of type U.
 
template<class U >
U & item (long j)
 Return a reference to an object of type U at object index j
 
template<class U >
const U & item (long j) const
 Return a constant reference to an object of type U at object index j
 
std::uint8_t & byteAt (long j)
 Convenience function for item<std::uint8_t>()
 
const std::uint8_t & byteAt (long j) const
 Convenience function for item<std::uint8_t>()
 
std::uint32_t & wordAt (long j)
 Convenience function for item<std::uint32_t>()
 
const std::uint32_t & wordAt (long j) const
 Convenience function for item<std::uint32_t>()
 
Itemoperator[] (long i)
 Get a reference to the item at i.
 
const Itemoperator[] (long i) const
 Get a constant reference to the item at i.
 
Item operator() (long i) const
 Get value at index if 0 <= i && i < count(), otherwise return fallback.
 
Arrayoperator() (long i, const T &value)
 Set value at index if 0 <= i && i < count(), otherwise do nothing.
 
const Itemfirst () const
 Get constant reference to first item.
 
const Itemlast () const
 Get constant reference to last item.
 
bool startsWith (const Array &head) const
 Check if head equals the start of this array.
 
bool endsWith (const Array &tail) const
 Check if tail equals the end of this array.
 

Global Operations

long count (const Item &b) const
 Count the number of occurences of item b.
 
bool find (const Item &b, InOut< long > i0=None{}) const
 Find item b.
 
void replace (const Item &b, const Item &s)
 Replace all occurance of b by s.
 
Array copy () const
 Create a deep copy of this array.
 
Array copy (long i0, long i1) const
 Create a copy of range [i0, i1)
 
void copyRangeTo (long i0, long i1, Out< Array > dst) const
 Copy the items in range [i0, i1] to array dst.
 
void copyRangeToOffset (long i0, long i1, Out< Array > dst, long j0) const
 Copy the items in range [i0, i1] to array dst starting at item dst[j0].
 
void copyTo (Out< Array > dst) const
 Copy contents of this array to array dst.
 
void copyToOffset (Out< Array > dst, long j0) const
 Copy contents of this array to array dst starting at item dst[j0].
 
void copyToRange (Out< Array > dst, long j0, long j1) const
 Copy contents of this array to array dst in the range dst[j0]..dst[j1-1].
 
template<class F >
void applyTo (InOut< Array > dst) const
 Apply the contents of this array to array dst.
 
void fill (const Item &b)
 Set all items to b
 
void resize (long n)
 Resize this array to n items preserving the contents (new items are uninitialized)
 
void truncate (long n)
 Reduce the length of this array to n items without freeing memory.
 
void deplete ()
 Truncate to zero length and free all previously stored items.
 
Array select (long i0, long i1)
 Return a selection of range [i0, i1)
 
ArrayselectAs (long i0, long i1, Out< Array > target)
 Return a selection of range [i0, i1) in target
 
Array selectHead (long n)
 Select the first n items or less (if n > count())
 
Array selectTail (long n)
 Select the last n items or less (if n > count())
 
long offset () const
 Offset of this array within its parent array if this array is a selection (0 otherwise)
 
Array parent ()
 Get parent array if this array is a selection.
 
void shift (long n)
 Truncate |n| items from the front (n > 0) or from the back (n < 0)
 
void reverse ()
 Reverse the order of items in this array.
 
Array reversed () const
 Get a copy of this array with order of items reversed.
 

Comparism Operators

bool operator== (const Array &other) const
 Equality operator.
 
std::strong_ordering operator<=> (const Array &other) const
 Ordering operator.
 

Detailed Description

template<class T>
class cc::Array< T >

An array of items within a contiguous chunk of memory.

Template Parameters
TItem type

Member Typedef Documentation

◆ Item

template<class T >
using Item = T

Item type.

◆ value_type

template<class T >
using value_type = Item

Item value type.

◆ size_type

template<class T >
using size_type = long

Type of the container capacity.

◆ iterator

template<class T >
using iterator = ArrayIterator<Array, 1>

Value iterator.

◆ const_iterator

template<class T >
using const_iterator = ArrayIterator<const Array, 1>

Readonly value iterator.

◆ reverse_iterator

template<class T >
using reverse_iterator = ArrayIterator<Array, -1>

Reverse value iterator.

◆ const_reverse_iterator

template<class T >
using const_reverse_iterator = ArrayIterator<const Array, -1>

Readonly reverse value iterator.

Constructor & Destructor Documentation

◆ Array() [1/9]

template<class T >
Array ( )

Create an empty array.

◆ Array() [2/9]

template<class T >
Array ( Dim< 1 > dim)

Create a array of dim[0] items.

Note
For performance reasons the items are not initialized if the item type is a fundamental type (e.g. char).

◆ Array() [3/9]

template<class T >
Array ( const Array< T > & other)
default

Construct a copy of other.

◆ Array() [4/9]

template<class T >
Array ( Array< T > && b)

Construct from right-side b.

◆ Array() [5/9]

template<class T >
Array ( std::initializer_list< T > items)

Construct with initial items.

◆ Array() [6/9]

template<class T >
Array ( std::initializer_list< Array< T > > arrays)

Concatenate arrays into a single new array.

◆ Array() [7/9]

template<class T >
Array ( const Item * src,
long count )
explicit

Create a copy of the low-level array src of count items.

◆ Array() [8/9]

template<class T >
Array ( const List< Array< T > > & parts)
explicit

Join several arrays into one.

◆ Array() [9/9]

template<class T >
Array ( const List< Array< T > > & parts,
Item sep )
explicit

Join several arrays into one using sep as interposing separator.

Member Function Documentation

◆ allocate()

template<class T >
static Array allocate ( long n)
static

Create a array of n items.

Note
For performance reasons the items are not initialized if the item type is a fundamental type (e.g. char).

◆ operator=() [1/2]

template<class T >
Array & operator= ( const Array< T > & )
default

Default assignment operator.

◆ operator=() [2/2]

template<class T >
Array & operator= ( Array< T > && b)

Assign right-side b.

◆ count() [1/2]

template<class T >
long count ( ) const

Size of this array in number of items.

◆ size()

template<class T >
long size ( ) const

Size of this array in number of items.

◆ has()

template<class T >
bool has ( long i) const

Check if i is a valid index.

◆ operator bool()

template<class T >
operator bool ( ) const
explicit

Check if this array is non-empty.

◆ operator+()

template<class T >
long operator+ ( ) const

Size of this array in number of items.

◆ at() [1/2]

template<class T >
Item & at ( long i)

Get a reference to the item at i.

◆ at() [2/2]

template<class T >
const Item & at ( long i) const

Get a constant reference to the item at i.

◆ itemCount()

template<class T >
template<class U >
long itemCount ( ) const

Get number of items of type U.

◆ item() [1/2]

template<class T >
template<class U >
U & item ( long j)

Return a reference to an object of type U at object index j

◆ item() [2/2]

template<class T >
template<class U >
const U & item ( long j) const

Return a constant reference to an object of type U at object index j

◆ byteAt() [1/2]

template<class T >
std::uint8_t & byteAt ( long j)

Convenience function for item<std::uint8_t>()

◆ byteAt() [2/2]

template<class T >
const std::uint8_t & byteAt ( long j) const

Convenience function for item<std::uint8_t>()

◆ wordAt() [1/2]

template<class T >
std::uint32_t & wordAt ( long j)

Convenience function for item<std::uint32_t>()

◆ wordAt() [2/2]

template<class T >
const std::uint32_t & wordAt ( long j) const

Convenience function for item<std::uint32_t>()

◆ operator[]() [1/2]

template<class T >
Item & operator[] ( long i)

Get a reference to the item at i.

◆ operator[]() [2/2]

template<class T >
const Item & operator[] ( long i) const

Get a constant reference to the item at i.

◆ operator()() [1/2]

template<class T >
Item operator() ( long i) const

Get value at index if 0 <= i && i < count(), otherwise return fallback.

◆ operator()() [2/2]

template<class T >
Array & operator() ( long i,
const T & value )

Set value at index if 0 <= i && i < count(), otherwise do nothing.

◆ first()

template<class T >
const Item & first ( ) const

Get constant reference to first item.

◆ last()

template<class T >
const Item & last ( ) const

Get constant reference to last item.

◆ startsWith()

template<class T >
bool startsWith ( const Array< T > & head) const

Check if head equals the start of this array.

◆ endsWith()

template<class T >
bool endsWith ( const Array< T > & tail) const

Check if tail equals the end of this array.

◆ count() [2/2]

template<class T >
long count ( const Item & b) const

Count the number of occurences of item b.

◆ find()

template<class T >
bool find ( const Item & b,
InOut< long > i0 = None{} ) const

Find item b.

Parameters
bThe item to search for
i0Provides the starting position and returns the final position
Returns
True if item b was found, false otherwise

◆ replace()

template<class T >
void replace ( const Item & b,
const Item & s )

Replace all occurance of b by s.

◆ copy() [1/2]

template<class T >
Array copy ( ) const

Create a deep copy of this array.

◆ copy() [2/2]

template<class T >
Array copy ( long i0,
long i1 ) const

Create a copy of range [i0, i1)

◆ copyRangeTo()

template<class T >
void copyRangeTo ( long i0,
long i1,
Out< Array< T > > dst ) const

Copy the items in range [i0, i1] to array dst.

◆ copyRangeToOffset()

template<class T >
void copyRangeToOffset ( long i0,
long i1,
Out< Array< T > > dst,
long j0 ) const

Copy the items in range [i0, i1] to array dst starting at item dst[j0].

◆ copyTo()

template<class T >
void copyTo ( Out< Array< T > > dst) const

Copy contents of this array to array dst.

◆ copyToOffset()

template<class T >
void copyToOffset ( Out< Array< T > > dst,
long j0 ) const

Copy contents of this array to array dst starting at item dst[j0].

◆ copyToRange()

template<class T >
void copyToRange ( Out< Array< T > > dst,
long j0,
long j1 ) const

Copy contents of this array to array dst in the range dst[j0]..dst[j1-1].

Note
j1 is only the upper limit. A fewer number of bytes might be written.

◆ applyTo()

template<class T >
template<class F >
void applyTo ( InOut< Array< T > > dst) const

Apply the contents of this array to array dst.

Template Parameters
FApplication policy (e.g. Xor)

◆ fill()

template<class T >
void fill ( const Item & b)

Set all items to b

◆ resize()

template<class T >
void resize ( long n)

Resize this array to n items preserving the contents (new items are uninitialized)

◆ truncate()

template<class T >
void truncate ( long n)

Reduce the length of this array to n items without freeing memory.

◆ deplete()

template<class T >
void deplete ( )

Truncate to zero length and free all previously stored items.

◆ select()

template<class T >
Array select ( long i0,
long i1 )

Return a selection of range [i0, i1)

◆ selectAs()

template<class T >
Array & selectAs ( long i0,
long i1,
Out< Array< T > > target )

Return a selection of range [i0, i1) in target

Returns
Reference to this array

◆ selectHead()

template<class T >
Array selectHead ( long n)

Select the first n items or less (if n > count())

◆ selectTail()

template<class T >
Array selectTail ( long n)

Select the last n items or less (if n > count())

◆ offset()

template<class T >
long offset ( ) const

Offset of this array within its parent array if this array is a selection (0 otherwise)

◆ parent()

template<class T >
Array parent ( )

Get parent array if this array is a selection.

◆ shift()

template<class T >
void shift ( long n)

Truncate |n| items from the front (n > 0) or from the back (n < 0)

◆ reverse()

template<class T >
void reverse ( )

Reverse the order of items in this array.

◆ reversed()

template<class T >
Array reversed ( ) const

Get a copy of this array with order of items reversed.

◆ items() [1/2]

template<class T >
const Item * items ( ) const

Get pointer to internal memory buffer.

◆ items() [2/2]

template<class T >
Item * items ( )

Get pointer to internal memory buffer.

◆ chars() [1/2]

template<class T >
const char * chars ( ) const

Low-level access to the underlying characters.

◆ chars() [2/2]

template<class T >
char * chars ( )

Low-level access to the underlying characters.

◆ bytes() [1/2]

template<class T >
const uint8_t * bytes ( ) const

Low-level access to the underlying bytes.

◆ bytes() [2/2]

template<class T >
uint8_t * bytes ( )

Low-level access to the underlying bytes.

◆ words() [1/2]

template<class T >
const uint32_t * words ( ) const

Low-level access to the underlying memory words.

◆ words() [2/2]

template<class T >
uint32_t * words ( )

Low-level access to the underlying memory words.

◆ operator OtherItem *()

template<class T >
template<class OtherItem >
operator OtherItem * ( )
explicit

Explicit conversion to low-level pointer.

◆ operator const OtherItem *()

template<class T >
template<class OtherItem >
operator const OtherItem * ( ) const
explicit

Explicit conversion to constant low-level pointer.

◆ operator void *()

template<class T >
operator void * ( )

Implicit conversion to low-level data pointer.

◆ operator const void *()

template<class T >
operator const void * ( ) const

Implicit conversion to constant low-level data pointer.

◆ wrapAround()

template<class T >
void wrapAround ( void * data,
long count )

Wrap this array around the low-level memory vector data of count items.

Note
This only works if this array is empty or has been used for wrapping before.

◆ wrap()

template<class T >
static Array wrap ( void * data,
long count )
static

Create a new array wrapped around data containing count items.

Note
Referencing the data array at the same time with different Array or Item types is not supported in C++ (see "pointer aliasing").

◆ begin() [1/2]

template<class T >
iterator begin ( )

Return iterator pointing to the first item (if any)

◆ end() [1/2]

template<class T >
iterator end ( )

Return iterator pointing behind the last item

◆ begin() [2/2]

template<class T >
const_iterator begin ( ) const

Return readonly iterator pointing to the first item (if any)

◆ cbegin()

template<class T >
const_iterator cbegin ( ) const

Return readonly iterator pointing to the first item (if any)

◆ end() [2/2]

template<class T >
const_iterator end ( ) const

Return readonly iterator pointing behind the last item

◆ cend()

template<class T >
const_iterator cend ( ) const

Return readonly iterator pointing behind the last item

◆ rbegin() [1/2]

template<class T >
reverse_iterator rbegin ( )

Return reverse iterator pointing to the last item (if any)

◆ rend() [1/2]

template<class T >
reverse_iterator rend ( )

Return reverse iterator pointing before the first item

◆ rbegin() [2/2]

template<class T >
const_reverse_iterator rbegin ( ) const

Return readonly reverse iterator pointing to the last item (if any)

◆ crbegin()

template<class T >
const_reverse_iterator crbegin ( ) const

Return readonly reverse iterator pointing to the last item (if any)

◆ rend() [2/2]

template<class T >
const_reverse_iterator rend ( ) const

Return reverse iterator pointing before the first item

◆ crend()

template<class T >
const_reverse_iterator crend ( ) const

Return reverse iterator pointing before the first item

◆ operator==()

template<class T >
bool operator== ( const Array< T > & other) const

Equality operator.

◆ operator<=>()

template<class T >
std::strong_ordering operator<=> ( const Array< T > & other) const

Ordering operator.