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

Lookup table object. More...

#include <klang.h>

+ Inheritance diagram for klang::Table< TYPE, SIZE >:

Public Types

typedef Array< TYPE, SIZE > Array
 
typedef Result< TYPE > Result
 

Public Member Functions

template<typename FUNCTION , std::enable_if_t< std::is_invocable_r_v< TYPE, FUNCTION &, TYPE >||std::is_invocable_v< FUNCTION &, TYPE, Result & >, int > = 0>
 Table (FUNCTION function)
 Construct and fill a table from a value-returning or Result-writing callable.
 
 Table (TYPE(*function)(TYPE, TYPE), TYPE arg)
 
 Table (std::initializer_list< TYPE > values)
 
TYPE operator[] (float index)
 
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.
 

Static Public Member Functions

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

Public Attributes

unsigned int count
 
TYPE items [CAPACITY]
 

Detailed Description

template<typename TYPE, int SIZE>
struct klang::Table< TYPE, SIZE >

Definition at line 4548 of file klang.h.

Member Typedef Documentation

◆ Array

template<typename TYPE , int SIZE>
Array<TYPE, SIZE> klang::Table< TYPE, SIZE >::Array

Definition at line 4549 of file klang.h.

◆ Result

template<typename TYPE , int SIZE>
Result<TYPE> klang::Table< TYPE, SIZE >::Result

Definition at line 4550 of file klang.h.

Constructor & Destructor Documentation

◆ Table() [1/3]

template<typename TYPE , int SIZE>
template<typename FUNCTION , std::enable_if_t< std::is_invocable_r_v< TYPE, FUNCTION &, TYPE >||std::is_invocable_v< FUNCTION &, TYPE, Result & >, int > = 0>
klang::Table< TYPE, SIZE >::Table ( FUNCTION function)
inline

Definition at line 4558 of file klang.h.

4558 {
4559 Array::count = SIZE;
4560 Result result(Array::items, 0);
4561 for (int x = 0; x < SIZE; x++)
4562 if constexpr (std::is_invocable_r_v<TYPE, FUNCTION&, TYPE>)
4563 Array::items[x] = function((TYPE)x);
4564 else {
4565 function((TYPE)x, result);
4566 result.sum += Array::items[x];
4567 if (x + 1 < SIZE) result++;
4568 }
4569 }
TYPE items[CAPACITY]
Definition klang.h:309
Result< TYPE > Result
Definition klang.h:4550

◆ Table() [2/3]

template<typename TYPE , int SIZE>
klang::Table< TYPE, SIZE >::Table ( TYPE(* function )(TYPE, TYPE),
TYPE arg )
inline

Definition at line 4572 of file klang.h.

4572 {
4573 Array::count = SIZE;
4574 for (int x = 0; x < SIZE; x++)
4575 Array::items[x] = function(x, arg);
4576 }

◆ Table() [3/3]

template<typename TYPE , int SIZE>
klang::Table< TYPE, SIZE >::Table ( std::initializer_list< TYPE > values)
inline

Definition at line 4578 of file klang.h.

4578 {
4579 for (TYPE value : values)
4580 add(value);
4581 }

Member Function Documentation

◆ add() [1/2]

TYPE * klang::Array< TYPE, CAPACITY >::add ( )
inlineinherited

Definition at line 324 of file klang.h.

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

◆ add() [2/2]

void klang::Array< TYPE, CAPACITY >::add ( const TYPE & item)
inlineinherited

Definition at line 318 of file klang.h.

318 {
319 if (count < CAPACITY)
320 items[count++] = item;
321 }

◆ capacity()

static int klang::Array< TYPE, CAPACITY >::capacity ( )
inlinestaticinherited

Definition at line 312 of file klang.h.

312{ return CAPACITY; }

◆ clear()

void klang::Array< TYPE, CAPACITY >::clear ( )
inlineinherited

Definition at line 331 of file klang.h.

331{ count = 0; }

◆ max()

float klang::Array< TYPE, CAPACITY >::max ( ) const
inlineinherited

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
Definition klang.h:334

◆ mean()

float klang::Array< TYPE, CAPACITY >::mean ( ) const
inlineinherited

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 }

◆ normalise()

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

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
Definition klang.h:343
float rms() const
Definition klang.h:351

◆ operator[]() [1/3]

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

Definition at line 369 of file klang.h.

369{ return items[index]; }

◆ operator[]() [2/3]

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

Definition at line 372 of file klang.h.

372{ return items[index]; }

◆ operator[]() [3/3]

template<typename TYPE , int SIZE>
TYPE klang::Table< TYPE, SIZE >::operator[] ( float index)
inline

Definition at line 4584 of file klang.h.

4584 {
4585 if (index < 0) return Array::items[0];
4586 else if (index >= (SIZE - 1)) return Array::items[SIZE - 1];
4587 else {
4588 const float x = std::floor(index);
4589 const int i = int(x);
4590 const float dx = index - x;
4591 const float dy = Array::items[i + 1] - Array::items[i];
4592 return Array::items[i] + dx * dy;
4593 }
4594 }

◆ rms()

float klang::Array< TYPE, CAPACITY >::rms ( ) const
inlineinherited

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 }

◆ size()

unsigned int klang::Array< TYPE, CAPACITY >::size ( ) const
inlineinherited

Definition at line 315 of file klang.h.

315{ return count; }

Member Data Documentation

◆ count

unsigned int klang::Array< TYPE, CAPACITY >::count
inherited

Definition at line 308 of file klang.h.

◆ items

TYPE klang::Array< TYPE, CAPACITY >::items[CAPACITY]
inherited

Definition at line 309 of file klang.h.