SyTen
syten::DynArray< Type > Class Template Reference

"Light" version of std::vector that supports malloc() allocation. More...

#include <dynarray.h>

+ Inheritance diagram for syten::DynArray< Type >:
+ Collaboration diagram for syten::DynArray< Type >:

Public Types

enum  Allocation : char { Allocation::Dynamic, Allocation::Eternal }
 Specificiation whether we should use l_malloc (for dynamic allocation) or le_malloc (for allocation that is not freed again effectively. More...
 
using value_type = Type
 Typedef for the contained type. More...
 

Public Member Functions

MemoryUsage::MemorySize allocSize () const
 Returns the size in bytes allocated by this array. More...
 
Type & at (Size i)
 Element access. More...
 
Type const & at (Size i) const
 Element access. More...
 
Type * begin ()
 Pointer to start of array. More...
 
const Type * begin () const
 Pointer to start of const array. More...
 
const Type * cbegin () const
 Pointer to start of const array. More...
 
const Type * cend () const
 Pointer one past the end of const array. More...
 
 DynArray (Size length_, Type const initialiser=0, Allocation alloc_=Allocation::Dynamic)
 Ctor with value-initialisation. More...
 
 DynArray (Size length_, Init do_init, Allocation alloc_=Allocation::Dynamic)
 Ctor with option to turn off constructor calling. More...
 
 DynArray (DynArray const &other)
 Copy ctor. More...
 
 DynArray (DynArray &&other)
 Move ctor. More...
 
 DynArray (Type *ptr_, Size length_, Allocation alloc_=Allocation::Dynamic)
 Pointer/Length ctor, use if you know what you're doing. More...
 
 DynArray ()=default
 Default ctor. More...
 
Type * end ()
 Pointer one past the end of array. More...
 
const Type * end () const
 Pointer one past the end of const array. More...
 
DynArray< Type > eternalised () const
 Returns a copy of *this with the internal array allocated on the eternal heap. More...
 
template<typename Archive >
void load (Archive &ar, const unsigned int in_version)
 Boost serialisation: loading. More...
 
DynArrayoperator= (DynArray const &other)
 Copy assignment operator. More...
 
DynArrayoperator= (DynArray &&other)
 Move assignment operator. More...
 
Type & operator[] (Size i)
 Element access. More...
 
Type const & operator[] (Size i) const
 Element access. More...
 
template<typename Archive >
void save (Archive &ar, const unsigned int) const
 Boost serialisation: saving. More...
 
Size size () const
 Number of contained elements. More...
 
 ~DynArray ()
 Dtor. More...
 

Static Public Attributes

static constexpr Size alignment = 64
 alignment restriction More...
 
static constexpr unsigned int version = 3
 Boost serialisation version. More...
 

Private Member Functions

void free ()
 Free allocated memory. More...
 
void init (Init do_init=Init::Yes)
 Acquire memory based on the current value of length. More...
 
void initNew ()
 Acquire memory and construct elements of Type on it. More...
 
void initTrivial ()
 Acquire memory only without construction of objects. More...
 

Private Attributes

Allocation alloc = Allocation::Dynamic
 Whether we used dynamic or eternal allocation. More...
 
Type * array = nullptr
 Pointer to array with our data. More...
 
Size length = 0
 Length of that array. More...
 

Detailed Description

template<typename Type>
class syten::DynArray< Type >

"Light" version of std::vector that supports malloc() allocation.

A call std::vector<std::complex<double>>(2000) will allocate space for 2000 complex numbers and then default-construct 2000 complex numbers in that space (essentially set them all to zero). However, that is perfectly wasted time if, in the next step, we’ll overwrite all 2000 of these numbers by some other values (as happens in e.g. matrix-matrix multiplication).

DynArray supports a l_malloc() based initialisation method where memory is only allocated but not initialised. This is only reasonable for types T that have std::is_trivial<T> true – and for std::complex<T>, as a complex type is just two base types next to each other (since a reinterpret_cast<T(&)[2]>(z)[0/1] has to give the real/imaginary part).

To make use of this allocation method, pass Init::No as the second argument to the constructor.

Template Parameters
Typethe contained type

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