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

 Table (TYPE(*function)(TYPE))
 
 Table (void(*function)(TYPE, TYPE), TYPE arg)
 
 Table (void(*function)(TYPE x, Result &y))
 
 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

TYPE items [CAPACITY]
 
unsigned int count
 

Detailed Description

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

Definition at line 2429 of file klang.h.

Member Typedef Documentation

◆ Array

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

Definition at line 2430 of file klang.h.

◆ Result

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

Definition at line 2431 of file klang.h.

Constructor & Destructor Documentation

◆ Table() [1/4]

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

Definition at line 2436 of file klang.h.

2436 {
2437 for (int x = 0; x < SIZE; x++)
2438 add(function(x));
2439 }

◆ Table() [2/4]

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

Definition at line 2442 of file klang.h.

2442 {
2443 Array::count = SIZE;
2444 for (int x = 0; x < SIZE; x++)
2445 Array::items[x] = function(x, arg);
2446 }
TYPE items[CAPACITY]
Definition klang.h:239

◆ Table() [3/4]

template<typename TYPE , int SIZE>
klang::Table< TYPE, SIZE >::Table ( void(* function )(TYPE x, Result &y))
inline

Definition at line 2449 of file klang.h.

2449 {
2450 Array::count = SIZE;
2451 Result y(Array::items, 0);
2452 for (int x = 0; x < SIZE; x++) {
2453 function((TYPE)x, y);
2454 y.sum += Array::items[x];
2455 y++;
2456 }
2457 }
Result< TYPE > Result
Definition klang.h:2431

◆ Table() [4/4]

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

Definition at line 2459 of file klang.h.

2459 {
2460 for (TYPE value : values)
2461 add(value);
2462 }

Member Function Documentation

◆ add() [1/2]

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

Definition at line 255 of file klang.h.

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

◆ add() [2/2]

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

Definition at line 249 of file klang.h.

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

◆ capacity()

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

Definition at line 243 of file klang.h.

243{ return CAPACITY; }

◆ clear()

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

Definition at line 262 of file klang.h.

262{ count = 0; }

◆ max()

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

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

◆ mean()

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

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()

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

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

◆ operator[]() [1/3]

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

Definition at line 300 of file klang.h.

300{ return items[index]; }

◆ operator[]() [2/3]

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

Definition at line 303 of file klang.h.

303{ return items[index]; }

◆ operator[]() [3/3]

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

Definition at line 2465 of file klang.h.

2465 {
2466 if (index < 0) return Array::items[0];
2467 else if (index >= (SIZE - 1)) return Array::items[SIZE - 1];
2468 else {
2469 const float x = std::floor(index);
2470 const int i = int(x);
2471 const float dx = index - x;
2472 const float dy = Array::items[i + 1] - Array::items[i];
2473 return Array::items[i] + dx * dy;
2474 }
2475 }

◆ rms()

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

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()

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

Definition at line 246 of file klang.h.

246{ return count; }

Member Data Documentation

◆ count

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

Definition at line 240 of file klang.h.

◆ items

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

Definition at line 239 of file klang.h.