Klang C++
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

TYPE items [CAPACITY]
 
unsigned int count = 0
 

Detailed Description

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

Definition at line 238 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 306 of file klang.h.

306 {
307 count = std::min(static_cast<unsigned int>(init_list.size()), static_cast<unsigned int>(CAPACITY));
308 std::copy_n(init_list.begin(), count, items);
309 }
TYPE items[CAPACITY]
Definition klang.h:239
unsigned int count
Definition klang.h:240

◆ 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 255 of file klang.h.

255 {
256 if (count < CAPACITY)
257 return &items[count++];
258 return nullptr;
259 }

◆ add() [2/2]

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

Definition at line 249 of file klang.h.

249 {
250 if(count < CAPACITY)
251 items[count++] = item;
252 }

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

◆ capacity()

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

Definition at line 243 of file klang.h.

243{ return CAPACITY; }

◆ clear()

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

Definition at line 262 of file klang.h.

262{ count = 0; }

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

◆ max()

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

Definition at line 265 of file klang.h.

265 {
266 float max = 0.f;
267 for (unsigned int i = 0; i < count; i++)
268 if (abs(items[i]) > max)
269 max = items[i];
270 return max;
271 }
float max() const
Find the maximum value in the array.
Definition klang.h:265

◆ mean()

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

Definition at line 274 of file klang.h.

274 {
275 float sum = 0.f;
276 for (unsigned int i = 0; i < count; i++)
277 sum += abs(items[i]);
278 return sum / count;
279 }

◆ normalise()

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

Definition at line 290 of file klang.h.

290 {
291 if (!count) return;
292
293 const float current = (mode == Peak) ? max() : (mode == Mean) ? mean() : rms();
294 const float scale = current == 0.f ? 0.f : target / current;
295 for (int i = 0; i < count; i++)
296 items[i] *= scale;
297 }
@ Peak
Definition klang.h:83
@ Mean
Definition klang.h:83
float mean() const
Find the mean average of values in the array.
Definition klang.h:274
float rms() const
Find the root mean squared (RMS) average of values in the array.
Definition klang.h:282

◆ operator[]() [1/2]

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

Definition at line 300 of file klang.h.

300{ return items[index]; }

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

Referenced by klang::Synth::onControl(), and klang::Synth::onPreset().

◆ operator[]() [2/2]

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

Definition at line 303 of file klang.h.

303{ return items[index]; }

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

Referenced by klang::Controls::operator=(), and klang::Presets::operator=().

◆ rms()

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

Definition at line 282 of file klang.h.

282 {
283 float sum = 0.f;
284 for (unsigned int i = 0; i < count; i++)
285 sum += items[i] * items[i];
286 return sqrt(sum / count);
287 }

◆ size()

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

Definition at line 246 of file klang.h.

246{ return count; }

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

Member Data Documentation

◆ count

template<typename TYPE , int CAPACITY>
unsigned int klang::Array< TYPE, CAPACITY >::count = 0

◆ items

template<typename TYPE , int CAPACITY>
TYPE klang::Array< TYPE, CAPACITY >::items[CAPACITY]