Evo C++ Library v0.5.1
Public Types | Public Member Functions | Protected Attributes | List of all members
Array< T, TSize > Class Template Reference

#include <evo/array.h>

Detailed Description

template<class T, class TSize = SizeT>
class evo::Array< T, TSize >

Dynamic array container with similar interface to List.

Template Parameters
TItem type to use
TSizeSize type to use for size/index values (must be unsigned integer) – default: SizeT
Features

C++11:

Iterators
Constructors
Read Access
Modifiers
Advanced
Example
#include <evo/array.h>
#include <evo/io.h>
using namespace evo;
static Console& c = con();
int main() {
// Create an array and fill with 0's
Array<int> array;
array.resize(5);
array.fill(0);
// Set some values
for (int i = 0; i < 5; ++i)
array[i] = i;
// Iterate and print items
for (Array<int>::Iter iter(array); iter; ++iter)
c.out << "Value: " << *iter << NL;
return 0;
}

Output:

Item: 1
Item: 2
Item: 3
Item: 4
Item: 5

Public Types

typedef void EvoContainerType
 Identify Evo container type. More...
 
typedef T Item
 Item type (same as Value) More...
 
typedef IteratorRa< ThisType >::Const Iter
 Iterator (const) - IteratorRa. More...
 
typedef IteratorRa< ThisTypeIterM
 Iterator (mutable) - IteratorRa. More...
 
typedef Size Key
 Key type (item index) More...
 
typedef TSize Size
 List size integer type. More...
 
typedef Array< T, TSize > ThisType
 This array type. More...
 
typedef T Value
 Value type (same as Item) More...
 

Public Member Functions

 Array ()
 Default constructor sets as null. More...
 
 Array (const ValEmpty &val)
 Constructor sets as empty but not null. More...
 
 Array (const ThisType &src)
 Copy constructor. More...
 
 Array (const T *data, Size size)
 Copy constructor. More...
 
 Array (std::initializer_list< T > init)
 Sequence constructor (C++11). More...
 
 Array (ThisType &&src)
 Move constructor (C++11). More...
 
 ~Array ()
 Destructor to free used memory. More...
 
ThisTypeadd (const Item &item)
 Append new item. More...
 
ThisTypeaddnew (Size size=1)
 Append new items. More...
 
ThisTypeadvResize (Size size)
 Advanced: Resize while preserving existing data, POD items not initialized (modifier). More...
 
const T & advRing (Key index) const
 Advanced: Get ring-buffer item at position (const). More...
 
T & advRing (Key index)
 Advanced: Get ring-buffer item at position (mutable). More...
 
const ThisTypeasconst () const
 Explicitly use a const reference to this. More...
 
IterM begin ()
 Get iterator at first item (mutable). More...
 
Iter begin () const
 Get iterator at first item (const). More...
 
Iter cbegin () const
 Get iterator at first item (const). More...
 
Iter cend () const
 Get iterator at end (const). More...
 
ThisTypeclear ()
 Clear by removing all items. More...
 
int compare (const ThisType &data) const
 Comparison. More...
 
const T * data () const
 Get data pointer (const). More...
 
T * data ()
 Get data pointer (mutable). More...
 
bool empty () const
 Get whether empty. More...
 
IterM end ()
 Get iterator at end. More...
 
Iter end () const
 Get iterator at end. More...
 
bool ends (const T &item) const
 Check if ends with given item. More...
 
bool ends (const T *items, Size size) const
 Check if ends with given items. More...
 
ThisTypefill (const T &item, Key index=0, Size size=ALL)
 Fill using item (modifier). More...
 
const T * first () const
 Get first item (const). More...
 
T * first ()
 Get first item (mutable). More...
 
ulong hash (ulong seed=0) const
 Get data hash value for whole array. More...
 
Key iend (Size offset=0) const
 Get index from last item using offset. More...
 
Size insert (Key index, const Item &item)
 Insert new item. More...
 
Size insertnew (Key index, Size size=1)
 Insert new items. More...
 
const T & item (Key index) const
 Get item at position (const). More...
 
T & item (Key index)
 Get item at position (mutable). More...
 
const T * last () const
 Get last item (const). More...
 
T * last ()
 Get last item (mutable). More...
 
bool null () const
 Get whether null. More...
 
bool operator!= (const ThisType &data) const
 Inequality operator. More...
 
ThisTypeoperator= (ThisType &&src)
 Move assignment operator (C++11). More...
 
ThisTypeoperator= (const ThisType &src)
 Assignment operator. More...
 
ThisTypeoperator= (const ValNull &)
 Assignment operator to set as null and empty. More...
 
ThisTypeoperator= (const ValEmpty &)
 Assignment operator to set as empty but not null. More...
 
bool operator== (const ThisType &data) const
 Equality operator. More...
 
const T & operator[] (Key index) const
 Get item at position (const). More...
 
T & operator[] (Key index)
 Get item at position (mutable). More...
 
Size remove (Key index, Size size=1)
 Remove items. More...
 
ThisTyperesize (Size size)
 Resize while preserving existing data (modifier). More...
 
ThisTyperesize2 (Size size)
 Resize as power of 2 while preserving existing data (modifier). More...
 
const T & ring (Key index) const
 Get ring-buffer item at position (const). More...
 
T & ring (Key index)
 Get ring-buffer item at position (mutable). More...
 
ThisTypeset ()
 Set as null and empty. More...
 
ThisTypeset (const ThisType &src)
 Set as copy of another array. More...
 
ThisTypeset (const T *data, Size size)
 Set as copy using data pointer. More...
 
ThisTypesetempty ()
 Set as empty but not null. More...
 
bool shared () const
 Get whether shared (false). More...
 
Size size () const
 Get size. More...
 
bool starts (const T &item) const
 Check if starts with given item. More...
 
bool starts (const T *items, Size size) const
 Check if starts with given items. More...
 
void swap (ThisType &array)
 Swap with another array. More...
 
ThisTypeunshare ()
 Make data unique – no-op. More...
 

Protected Attributes

T * data_
 
Size size_
 

Member Typedef Documentation

◆ EvoContainerType

typedef void EvoContainerType

Identify Evo container type.

◆ Item

typedef T Item

Item type (same as Value)

◆ Iter

typedef IteratorRa<ThisType>::Const Iter

Iterator (const) - IteratorRa.

◆ IterM

Iterator (mutable) - IteratorRa.

◆ Key

typedef Size Key

Key type (item index)

◆ Size

typedef TSize Size

List size integer type.

◆ ThisType

typedef Array<T,TSize> ThisType

This array type.

◆ Value

typedef T Value

Value type (same as Item)

Constructor & Destructor Documentation

◆ Array() [1/6]

Array ( )
inline

Default constructor sets as null.

◆ Array() [2/6]

Array ( const ValEmpty val)
inlineexplicit

Constructor sets as empty but not null.

Parameters
valvEMPTY

◆ Array() [3/6]

Array ( const ThisType src)
inline

Copy constructor.

Parameters
srcData to copy

◆ Array() [4/6]

Array ( const T *  data,
Size  size 
)
inline

Copy constructor.

Parameters
dataData to copy
sizeData size as item count

◆ ~Array()

~Array ( )
inline

Destructor to free used memory.

◆ Array() [5/6]

Array ( std::initializer_list< T >  init)
inline

Sequence constructor (C++11).

Parameters
initInitializer list, passed as comma-separated values in braces { }

◆ Array() [6/6]

Array ( ThisType &&  src)
inline

Move constructor (C++11).

Parameters
srcSource to move

Member Function Documentation

◆ add()

ThisType& add ( const Item item)
inline

Append new item.

Parameters
itemItem to append
Returns
This

◆ addnew()

ThisType& addnew ( Size  size = 1)
inline

Append new items.

Parameters
sizeSize as item count to append
Returns
This

◆ advResize()

ThisType& advResize ( Size  size)
inline

Advanced: Resize while preserving existing data, POD items not initialized (modifier).

  • This is a slightly modified version of resize():
    • No difference on non POD item types
    • For POD item type: New items created here will not be initialized/zeroed
    • This gives a slight performance increase in some cases but will leave uninitialized garbage data for POD items
    • In most cases resize() is preferred since it's safer and the performance difference is usually negligible
  • This adds/removes items as needed until given size is reached
Parameters
sizeNew size as item count
Returns
This

◆ advRing() [1/2]

const T& advRing ( Key  index) const
inline

Advanced: Get ring-buffer item at position (const).

  • This is optimized for speed using bit operations instead of modulus operator
  • Must be resized with resize2() so size is always a power of 2
  • Caution: Results are undefined if size is not a power of 2
Parameters
indexItem index, scaled down if out of bounds
Returns
Given item as read-only (const)

◆ advRing() [2/2]

T& advRing ( Key  index)
inline

Advanced: Get ring-buffer item at position (mutable).

  • Must be resized with resize2() so size is always a power of 2
  • Caution: Results are undefined if size is not a power of 2
Parameters
indexItem index
Returns
Given item

◆ asconst()

const ThisType& asconst ( ) const
inline

Explicitly use a const reference to this.

  • This is useful to force using this as const without casting
Returns
This

◆ begin() [1/2]

IterM begin ( )
inline

Get iterator at first item (mutable).

  • This allows compatibility with range-based for loops and other libraries, otherwise use container Iter directly
  • cbegin() is more efficient, since this effectively calls unshare() to make items mutable
Returns
Iterator at first item, or at end position if empty
See also
IterM, end(), cbegin(), cend()

◆ begin() [2/2]

Iter begin ( ) const
inline

Get iterator at first item (const).

  • This allows compatibility with range-based for loops and other libraries, otherwise use container Iter directly
Returns
Iterator at first item, or at end position if empty
See also
end() const, cbegin()

◆ cbegin()

Iter cbegin ( ) const
inline

Get iterator at first item (const).

  • This allows compatibility with range-based for loops and other libraries, otherwise use container Iter directly
Returns
Iterator at first item, or at end position if empty
See also
Iter, cend(), begin(), end()

◆ cend()

Iter cend ( ) const
inline

Get iterator at end (const).

  • This allows compatibility with range-based for loops and other libraries, otherwise use container Iter directly
  • This really just creates an empty iterator
Returns
Iterator at end position
See also
Iter, cbegin(), begin(), end()

◆ clear()

ThisType& clear ( )
inline

Clear by removing all items.

  • Does not set as null – null status is unchanged
Returns
This

◆ compare()

int compare ( const ThisType data) const
inline

Comparison.

Parameters
dataData to compare to
Returns
Result (<0 if this is less, 0 if equal, >0 if this is greater)

◆ data() [1/2]

const T* data ( ) const
inline

Get data pointer (const).

  • Caution: Modifying the size of the array will invalidate returned pointer
Returns
Data pointer as read-only, NULL/invalid if empty

◆ data() [2/2]

T* data ( )
inline

Get data pointer (mutable).

  • Caution: Modifying the size of the array will invalidate returned pointer
Returns
Data pointer (mutable), NULL/invalid if empty

◆ empty()

bool empty ( ) const
inline

Get whether empty.

Returns
Whether empty

◆ end() [1/2]

IterM end ( )
inline

Get iterator at end.

  • This allows compatibility with range-based for loops and other libraries, otherwise use container Iter directly
  • This really just creates an empty iterator
Returns
Iterator at end position
See also
IterM, begin(), cbegin(), cend()

◆ end() [2/2]

Iter end ( ) const
inline

Get iterator at end.

  • This allows compatibility with range-based for loops and other libraries, otherwise use container Iter directly
  • This really just creates an empty iterator
Returns
Iterator at end position
See also
begin() const, cend()

◆ ends() [1/2]

bool ends ( const T &  item) const
inline

Check if ends with given item.

  • Uses item operator==() for comparisons
Parameters
itemItem to check
Returns
Whether ends with item

◆ ends() [2/2]

bool ends ( const T *  items,
Size  size 
) const
inline

Check if ends with given items.

  • Uses item operator==() for comparisons
Parameters
itemsItems to check
sizeItem count to check
Returns
Whether ends with items

◆ fill()

ThisType& fill ( const T &  item,
Key  index = 0,
Size  size = ALL 
)
inline

Fill using item (modifier).

  • Resizes to fill new items if needed
Parameters
itemItem to fill with
indexStart index, END to start at end and append
sizeSize to fill as item count from index, ALL for all items from index, 0 to do nothing

◆ first() [1/2]

const T* first ( ) const
inline

Get first item (const).

Returns
First item pointer, NULL if empty

◆ first() [2/2]

T* first ( )
inline

Get first item (mutable).

Returns
First item pointer, NULL if empty

◆ hash()

ulong hash ( ulong  seed = 0) const
inline

Get data hash value for whole array.

Parameters
seedSeed value for hashing multiple values, 0 if none
Returns
Hash value

◆ iend()

Key iend ( Size  offset = 0) const
inline

Get index from last item using offset.

  • This simplifies math when computing an index from last item
  • This uses the formula: iend = size - 1 - offset
Parameters
offsetOffset from end, 0 for last item, 1 for second-last, etc
Returns
Resulting index, END if offset out of bounds

◆ insert()

Size insert ( Key  index,
const Item item 
)
inline

Insert new item.

Parameters
indexInsert index, END to append
itemItem to insert
Returns
Inserted index

◆ insertnew()

Size insertnew ( Key  index,
Size  size = 1 
)
inline

Insert new items.

Parameters
indexInsert index, END to append
sizeSize as item count to insert
Returns
Inserted index

◆ item() [1/2]

const T& item ( Key  index) const
inline

Get item at position (const).

  • Caution: Results are undefined if index is out of bounds
Parameters
indexItem index
Returns
Given item as read-only (const)

◆ item() [2/2]

T& item ( Key  index)
inline

Get item at position (mutable).

  • Caution: Results are undefined if index is out of bounds
Parameters
indexItem index
Returns
Given item (mutable)

◆ last() [1/2]

const T* last ( ) const
inline

Get last item (const).

Returns
Last item pointer, NULL if empty

◆ last() [2/2]

T* last ( )
inline

Get last item (mutable).

Returns
Last item pointer, NULL if empty

◆ null()

bool null ( ) const
inline

Get whether null.

  • Always empty when null
Returns
Whether null

◆ operator!=()

bool operator!= ( const ThisType data) const
inline

Inequality operator.

Parameters
dataData to compare to
Returns
Whether inequal

◆ operator=() [1/4]

ThisType& operator= ( ThisType &&  src)
inline

Move assignment operator (C++11).

Parameters
srcSource to move
Returns
This

◆ operator=() [2/4]

ThisType& operator= ( const ThisType src)
inline

Assignment operator.

Parameters
srcData to copy
Returns
This

◆ operator=() [3/4]

ThisType& operator= ( const ValNull )
inline

Assignment operator to set as null and empty.

Example:

array = vNULL;
Returns
This

◆ operator=() [4/4]

ThisType& operator= ( const ValEmpty )
inline

Assignment operator to set as empty but not null.

Example:

array = vEMPTY;
Returns
This

◆ operator==()

bool operator== ( const ThisType data) const
inline

Equality operator.

Parameters
dataData to compare to
Returns
Whether equal

◆ operator[]() [1/2]

const T& operator[] ( Key  index) const
inline

Get item at position (const).

  • Caution: Results are undefined if index is out of bounds
Parameters
indexItem index
Returns
Given item as read-only (const)

◆ operator[]() [2/2]

T& operator[] ( Key  index)
inline

Get item at position (mutable).

  • Caution: Results are undefined if index is out of bounds
Parameters
indexItem index
Returns
Given item (mutable)

◆ remove()

Size remove ( Key  index,
Size  size = 1 
)
inline

Remove items.

Parameters
indexRemove index
sizeRemove size, ALL for all items from index
Returns
Number of items removed

◆ resize()

ThisType& resize ( Size  size)
inline

Resize while preserving existing data (modifier).

  • This adds/removes items as needed until given size is reached
  • Advanced: See advResize() for best performance in certain POD cases
Parameters
sizeNew size as item count
Returns
This

◆ resize2()

ThisType& resize2 ( Size  size)
inline

Resize as power of 2 while preserving existing data (modifier).

  • This makes sure size is always a power of 2, or 0 if empty
  • If desired size isn't a power of 2, this increases it to the next power of 2
  • Especially useful for ring buffers using ring() and advRing()
Parameters
sizeNew size as item count
Returns
This

◆ ring() [1/2]

const T& ring ( Key  index) const
inline

Get ring-buffer item at position (const).

Parameters
indexItem index, scaled down if out of bounds
Returns
Given item as read-only (const)

◆ ring() [2/2]

T& ring ( Key  index)
inline

Get ring-buffer item at position (mutable).

Parameters
indexItem index, scaled down if out of bounds
Returns
Given item (mutable)

◆ set() [1/3]

ThisType& set ( )
inline

Set as null and empty.

Returns
This

◆ set() [2/3]

ThisType& set ( const ThisType src)
inline

Set as copy of another array.

Parameters
srcData to copy
Returns
This

◆ set() [3/3]

ThisType& set ( const T *  data,
Size  size 
)
inline

Set as copy using data pointer.

Parameters
dataData to copy
sizeData size as item count
Returns
This

◆ setempty()

ThisType& setempty ( )
inline

Set as empty but not null.

Returns
This

◆ shared()

bool shared ( ) const
inline

Get whether shared (false).

  • This doesn't support sharing so always returns false
Returns
Whether shared (always false)

◆ size()

Size size ( ) const
inline

Get size.

Returns
Size as item count

◆ starts() [1/2]

bool starts ( const T &  item) const
inline

Check if starts with given item.

  • Uses item operator==() for comparisons
Parameters
itemItem to check
Returns
Whether starts with item

◆ starts() [2/2]

bool starts ( const T *  items,
Size  size 
) const
inline

Check if starts with given items.

  • Uses item operator==() for comparisons
Parameters
itemsItems to check
sizeSize as item count to check
Returns
Whether starts with items

◆ swap()

void swap ( ThisType array)
inline

Swap with another array.

  • This swaps internal state directly so is faster than moving items
Parameters
arrayArray to swap with

◆ unshare()

ThisType& unshare ( )
inline

Make data unique – no-op.

  • Array doesn't support sharing so this is a no-op
Returns
This

Member Data Documentation

◆ data_

T* data_
protected

◆ size_

Size size_
protected

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