SHOGUN  6.1.3
List of all members | Public Member Functions | Protected Attributes | Friends
DynArray< T > Class Template Reference

Detailed Description

template<class T>
class shogun::DynArray< T >

Template Dynamic array class that creates an array that can be used like a list or an array.

It grows and shrinks dynamically, while elements can be accessed via index. It is performance tuned for simple types like float etc. and for hi-level objects only stores pointers, which are not automagically SG_REF'd/deleted.

Definition at line 32 of file DynArray.h.

Public Member Functions

 DynArray (int32_t p_resize_granularity=128, bool tracable=true)
 
 DynArray (T *p_array, int32_t p_array_size, bool p_free_array, bool p_copy_array, bool tracable=true)
 
 DynArray (const T *p_array, int32_t p_array_size, bool tracable=true)
 
virtual ~DynArray ()
 
int32_t set_granularity (int32_t g)
 
int32_t get_array_size () const
 
int32_t get_num_elements () const
 
get_element (int32_t index) const
 
get_last_element () const
 
T * get_element_ptr (int32_t index)
 
get_element_safe (int32_t index) const
 
bool set_element (T element, int32_t index)
 
bool insert_element (T element, int32_t index)
 
bool append_element (T element)
 
void push_back (T element)
 
void pop_back ()
 
back () const
 
int32_t find_element (T element) const
 
bool delete_element (int32_t idx)
 
bool resize_array (int32_t n, bool exact_resize=false)
 
T * get_array () const
 
void set_array (T *p_array, int32_t p_num_elements, int32_t p_array_size, bool p_free_array, bool p_copy_array)
 
void set_array (const T *p_array, int32_t p_num_elements, int32_t p_array_size)
 
void clear_array (T value)
 
void reset (T value)
 
void shuffle ()
 
void shuffle (CRandom *rand)
 
void set_const (const T &const_element)
 
operator[] (int32_t index) const
 
DynArray< T > & operator= (DynArray< T > &orig)
 
virtual const char * get_name () const
 

Protected Attributes

int32_t resize_granularity
 
T * array
 
int32_t num_elements
 
int32_t current_num_elements
 
bool use_sg_mallocs
 
bool free_array
 

Friends

template<class U >
class CDynamicArray
 
class CDynamicObjectArray
 
class CCommUlongStringKernel
 

Constructor & Destructor Documentation

◆ DynArray() [1/3]

DynArray ( int32_t  p_resize_granularity = 128,
bool  tracable = true 
)

constructor

Parameters
p_resize_granularityresize granularity
tracable

Definition at line 44 of file DynArray.h.

◆ DynArray() [2/3]

DynArray ( T *  p_array,
int32_t  p_array_size,
bool  p_free_array,
bool  p_copy_array,
bool  tracable = true 
)

constructor

Parameters
p_arrayanother array
p_array_sizearray's size
p_free_arrayif array must be freed
p_copy_arrayif array must be copied
tracable

Definition at line 67 of file DynArray.h.

◆ DynArray() [3/3]

DynArray ( const T *  p_array,
int32_t  p_array_size,
bool  tracable = true 
)

constructor

Parameters
p_arrayanother array
p_array_sizearray's size
tracable

Definition at line 83 of file DynArray.h.

◆ ~DynArray()

virtual ~DynArray ( )
virtual

destructor

Definition at line 94 of file DynArray.h.

Member Function Documentation

◆ append_element()

bool append_element ( element)

append array element to the end of array

Parameters
elementelement to append
Returns
if setting was successful

Definition at line 244 of file DynArray.h.

◆ back()

T back ( ) const

STD VECTOR compatible. Return array element at the end of array.

Returns
element at the end of array

Definition at line 278 of file DynArray.h.

◆ clear_array()

void clear_array ( value)

clear the array (with e.g. zeros)

Definition at line 431 of file DynArray.h.

◆ delete_element()

bool delete_element ( int32_t  idx)

delete array element at idx (does not call SG_FREE() or the like)

Parameters
idxindex
Returns
if deleting was successful

Definition at line 315 of file DynArray.h.

◆ find_element()

int32_t find_element ( element) const

find first occurence of array element and return its index or -1 if not available

Parameters
elementelement to search for
Returns
index of element or -1

Definition at line 292 of file DynArray.h.

◆ get_array()

T* get_array ( ) const

get the array call get_array just before messing with it DO NOT call any [],resize/delete functions after get_array(), the pointer may become invalid !

Returns
the array

Definition at line 372 of file DynArray.h.

◆ get_array_size()

int32_t get_array_size ( ) const

get array size (including granularity buffer)

Returns
total array size (including granularity buffer)

Definition at line 121 of file DynArray.h.

◆ get_element()

T get_element ( int32_t  index) const

get array element at index

(does NOT do bounds checking)

Parameters
indexindex
Returns
array element at index

Definition at line 142 of file DynArray.h.

◆ get_element_ptr()

T* get_element_ptr ( int32_t  index)

get array element at index as pointer

(does NOT do bounds checking)

Parameters
indexindex
Returns
array element at index

Definition at line 163 of file DynArray.h.

◆ get_element_safe()

T get_element_safe ( int32_t  index) const

get array element at index

(does bounds checking)

Parameters
indexindex
Returns
array element at index

Definition at line 175 of file DynArray.h.

◆ get_last_element()

T get_last_element ( ) const

gets last array element

Returns
array element at last index

Definition at line 151 of file DynArray.h.

◆ get_name()

virtual const char* get_name ( ) const
virtual
Returns
object name

Definition at line 511 of file DynArray.h.

◆ get_num_elements()

int32_t get_num_elements ( ) const

get number of elements

Returns
number of elements

Definition at line 130 of file DynArray.h.

◆ insert_element()

bool insert_element ( element,
int32_t  index 
)

insert array element at index

Parameters
elementelement to insert
indexindex
Returns
if setting was successful

Definition at line 223 of file DynArray.h.

◆ operator=()

DynArray<T>& operator= ( DynArray< T > &  orig)

operator overload for array assignment. Left array is resized if needed.

Parameters
origoriginal array
Returns
new array

Definition at line 488 of file DynArray.h.

◆ operator[]()

T operator[] ( int32_t  index) const

operator overload for array read only access use set_element() for write access (will also make the array dynamically grow)

DOES NOT DO ANY BOUNDS CHECKING

Parameters
indexindex
Returns
element at index

Definition at line 477 of file DynArray.h.

◆ pop_back()

void pop_back ( )

STD VECTOR compatible. Delete array element at the end of array.

Definition at line 265 of file DynArray.h.

◆ push_back()

void push_back ( element)

STD VECTOR compatible. Append array element to the end of array.

Parameters
elementelement to append

Definition at line 254 of file DynArray.h.

◆ reset()

void reset ( value)

resets the array (as if it was just created), keeps granularity

Definition at line 441 of file DynArray.h.

◆ resize_array()

bool resize_array ( int32_t  n,
bool  exact_resize = false 
)

resize the array

Parameters
nnew size
exact_resizeresize exactly to size n
Returns
if resizing was successful

Definition at line 340 of file DynArray.h.

◆ set_array() [1/2]

void set_array ( T *  p_array,
int32_t  p_num_elements,
int32_t  p_array_size,
bool  p_free_array,
bool  p_copy_array 
)

set the array pointer and free previously allocated memory

Parameters
p_arraynew array
p_num_elementslast element index + 1
p_array_sizenumber of elements in array
p_free_arrayif array must be freed
p_copy_arrayif array must be copied

Definition at line 385 of file DynArray.h.

◆ set_array() [2/2]

void set_array ( const T *  p_array,
int32_t  p_num_elements,
int32_t  p_array_size 
)

set the array pointer and free previously allocated memory

Parameters
p_arraynew array
p_num_elementslast element index + 1
p_array_sizenumber of elements in array

Definition at line 413 of file DynArray.h.

◆ set_const()

void set_const ( const T &  const_element)

set array with a constant

Definition at line 462 of file DynArray.h.

◆ set_element()

bool set_element ( element,
int32_t  index 
)

set array element at index

Parameters
elementelement to set
indexindex
Returns
if setting was successful

Definition at line 191 of file DynArray.h.

◆ set_granularity()

int32_t set_granularity ( int32_t  g)

set the resize granularity

Parameters
gnew granularity
Returns
what has been set (minimum is 128)

Definition at line 110 of file DynArray.h.

◆ shuffle() [1/2]

void shuffle ( )

randomizes the array (not thread safe!)

Definition at line 448 of file DynArray.h.

◆ shuffle() [2/2]

void shuffle ( CRandom rand)

randomizes the array with external random state

Definition at line 455 of file DynArray.h.

Friends And Related Function Documentation

◆ CCommUlongStringKernel

friend class CCommUlongStringKernel
friend

Definition at line 36 of file DynArray.h.

◆ CDynamicArray

friend class CDynamicArray
friend

Definition at line 34 of file DynArray.h.

◆ CDynamicObjectArray

friend class CDynamicObjectArray
friend

Definition at line 35 of file DynArray.h.

Member Data Documentation

◆ array

T* array
protected

memory for dynamic array

Definition at line 518 of file DynArray.h.

◆ current_num_elements

int32_t current_num_elements
protected

the number of currently used elements

Definition at line 524 of file DynArray.h.

◆ free_array

bool free_array
protected

if array must be freed

Definition at line 530 of file DynArray.h.

◆ num_elements

int32_t num_elements
protected

the number of potentially used elements in array

Definition at line 521 of file DynArray.h.

◆ resize_granularity

int32_t resize_granularity
protected

shrink/grow step size

Definition at line 515 of file DynArray.h.

◆ use_sg_mallocs

bool use_sg_mallocs
protected

whether SG_MALLOC or just malloc etc shall be used

Definition at line 527 of file DynArray.h.


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

SHOGUN Machine Learning Toolbox - Documentation