Klang C++ 0.7.15
Language Reference (draft)
Loading...
Searching...
No Matches
klang::Array< TYPE, CAPACITY >

Variable-sized array, pre-allocated to a max. capacity. More...

#include <klang.h>

Public Member Functions

unsigned int size () const
 The current number of items in the array.
 
void add (const TYPE &item)
 Add the specified item to the end of the array.
 
TYPE * add ()
 Add a blank item to the end of the array, returning a pointer that allows the item to be modified.
 
void clear ()
 Clear the array contents. Only resets the item count, without wiping memory.
 
float max () const
 Find the maximum value in the array.
 
float mean () const
 Find the mean average of values in the array.
 
float rms () const
 Find the root mean squared (RMS) average of values in the array.
 
void normalise (float target=1.f, int mode=Peak)
 Normalises values in the array to the specified target, based on peak, mean, or RMS value;.
 
TYPE & operator[] (int index)
 Returns a reference to the array item at the given index.
 
const TYPE & operator[] (int index) const
 Returns a read-only reference to the array item at the given index.
 
 Array (std::initializer_list< TYPE > init_list)
 Construct an array given the specified values.
 
 Array ()=default
 Construct an empty array.
 

Static Public Member Functions

static int capacity ()
 The maximum capacity of the array.
 

Public Attributes

unsigned int count = 0
 
TYPE items [CAPACITY]
 

Detailed Description

template<typename TYPE, int CAPACITY>
struct klang::Array< TYPE, CAPACITY >

Definition at line 307 of file klang.h.

Constructor & Destructor Documentation

◆ Array() [1/2]

template<typename TYPE , int CAPACITY>
klang::Array< TYPE, CAPACITY >::Array ( std::initializer_list< TYPE > init_list)
inline

Definition at line 375 of file klang.h.

375 {
376 count = std::min(static_cast<unsigned int>(init_list.size()), static_cast<unsigned int>(CAPACITY));
377 std::copy_n(init_list.begin(), count, items);
378 }
TYPE items[CAPACITY]
Definition klang.h:309
unsigned int count
Definition klang.h:308

References klang::Array< TYPE, CAPACITY >::count, and klang::Array< TYPE, CAPACITY >::items.

◆ Array() [2/2]

template<typename TYPE , int CAPACITY>
klang::Array< TYPE, CAPACITY >::Array ( )
default

Member Function Documentation

◆ add() [1/2]

template<typename TYPE , int CAPACITY>
TYPE * klang::Array< TYPE, CAPACITY >::add ( )
inline

Definition at line 324 of file klang.h.

324 {
325 if (count < CAPACITY)
326 return &items[count++];
327 return nullptr;
328 }

References klang::Array< TYPE, CAPACITY >::count, and klang::Array< TYPE, CAPACITY >::items.

◆ add() [2/2]

template<typename TYPE , int CAPACITY>
void klang::Array< TYPE, CAPACITY >::add ( const TYPE & item)
inline

◆ capacity()

template<typename TYPE , int CAPACITY>
static int klang::Array< TYPE, CAPACITY >::capacity ( )
inlinestatic

Definition at line 312 of file klang.h.

312{ return CAPACITY; }

◆ clear()

template<typename TYPE , int CAPACITY>
void klang::Array< TYPE, CAPACITY >::clear ( )
inline

Definition at line 331 of file klang.h.

331{ count = 0; }

References klang::Array< TYPE, CAPACITY >::count.

Referenced by klang::Generic::Graph< SIZE >::clear().

◆ max()

template<typename TYPE , int CAPACITY>
float klang::Array< TYPE, CAPACITY >::max ( ) const
inline

Definition at line 334 of file klang.h.

334 {
335 float max = 0.f;
336 for (unsigned int i = 0; i < count; i++)
337 if (abs(items[i]) > max)
338 max = items[i];
339 return max;
340 }
float max() const
Find the maximum value in the array.
Definition klang.h:334

References klang::Array< TYPE, CAPACITY >::count, klang::Array< TYPE, CAPACITY >::items, and klang::Array< TYPE, CAPACITY >::max().

Referenced by klang::Array< TYPE, CAPACITY >::max(), and klang::Array< TYPE, CAPACITY >::normalise().

◆ mean()

template<typename TYPE , int CAPACITY>
float klang::Array< TYPE, CAPACITY >::mean ( ) const
inline

Definition at line 343 of file klang.h.

343 {
344 float sum = 0.f;
345 for (unsigned int i = 0; i < count; i++)
346 sum += abs(items[i]);
347 return sum / count;
348 }

References klang::Array< TYPE, CAPACITY >::count, and klang::Array< TYPE, CAPACITY >::items.

Referenced by klang::Array< TYPE, CAPACITY >::normalise().

◆ normalise()

template<typename TYPE , int CAPACITY>
void klang::Array< TYPE, CAPACITY >::normalise ( float target = 1.f,
int mode = Peak )
inline

Definition at line 359 of file klang.h.

359 {
360 if (!count) return;
361
362 const float current = (mode == Peak) ? max() : (mode == Mean) ? mean() : rms();
363 const float scale = current == 0.f ? 0.f : target / current;
364 for (int i = 0; i < count; i++)
365 items[i] *= scale;
366 }
@ Peak
Definition klang.h:149
@ Mean
Definition klang.h:149
float mean() const
Find the mean average of values in the array.
Definition klang.h:343
float rms() const
Find the root mean squared (RMS) average of values in the array.
Definition klang.h:351

References klang::Array< TYPE, CAPACITY >::count, klang::Array< TYPE, CAPACITY >::items, klang::Array< TYPE, CAPACITY >::max(), klang::Mean, klang::Array< TYPE, CAPACITY >::mean(), klang::Peak, and klang::Array< TYPE, CAPACITY >::rms().

◆ operator[]() [1/2]

template<typename TYPE , int CAPACITY>
TYPE & klang::Array< TYPE, CAPACITY >::operator[] ( int index)
inline

Definition at line 369 of file klang.h.

369{ return items[index]; }

References klang::Array< TYPE, CAPACITY >::items.

◆ operator[]() [2/2]

template<typename TYPE , int CAPACITY>
const TYPE & klang::Array< TYPE, CAPACITY >::operator[] ( int index) const
inline

Definition at line 372 of file klang.h.

372{ return items[index]; }

References klang::Array< TYPE, CAPACITY >::items.

◆ rms()

template<typename TYPE , int CAPACITY>
float klang::Array< TYPE, CAPACITY >::rms ( ) const
inline

Definition at line 351 of file klang.h.

351 {
352 float sum = 0.f;
353 for (unsigned int i = 0; i < count; i++)
354 sum += items[i] * items[i];
355 return sqrt(sum / count);
356 }

References klang::Array< TYPE, CAPACITY >::count, and klang::Array< TYPE, CAPACITY >::items.

Referenced by klang::Array< TYPE, CAPACITY >::normalise().

◆ size()

template<typename TYPE , int CAPACITY>
unsigned int klang::Array< TYPE, CAPACITY >::size ( ) const
inline

Definition at line 315 of file klang.h.

315{ return count; }

References klang::Array< TYPE, CAPACITY >::count.

Referenced by klang::Menu(), and klang::Menu().

Member Data Documentation

◆ count

◆ items