📦

Moving Average+ Methods

The MovingAveragePlus class abstracts the implementation of a circular array in order to calculate the arithmetic average of its members. Because it is a circular array, the next input will replace the oldest element (represented by the first index), so it is also possible to call it a FIFO queue. If you are used to the C++ Standard Library, it will be easier to learn the methods since they follow the same pattern of name and logic. If you are looking for calculating a shorter average of the same data array more efficiently, check the new feature, partial(WIP).

📌Table of Contents

🛠Constructor


📝Syntax

// Passing only size as an argument
MovingAveragePlus<class TypeOfArray> yourAverageName(size_t size);
// Or, also, passing a value to initialize the array
-Deprecated- MovingAveragePlus<class TypeOfArray> yourAverageName(size_t size, TypeOfArray initialize);
  1. Passing only size as an argument. The class will create an array with the size passed as an argument and, by default, all positions will initialize with 0.
  1. -Deprecated- Passing size and initializer value as argument.

🔮Example

// This will create an MovingAverage of size 10 and unsigned type
MovingAveragePlus<unsigned int> intAverage(10);

⏱Complexity

If the option "1." is used, the complexity will be constant (O(1)). -Deprecated- Although, if using the option "2.", complexity will be linear (O(n)) in the array size passed as an argument.

🔗Get Result and Access elements


📥Push


Inserts a new element in the internal array and returns a pointer. Methods that return pointers allow chained calls.

MovingAveragePlus<unsigned int> intAverage(10);

unsigned int x = 5, y = 10;
unsigned int result, first, last;

result = intAverage.push(x).push(y).get();
first = intAverage.push(56).front();
last = intAverage.push(8).back();

📝Syntax

MovingAveragePlus<TypeOfArray> &push(TypeOfArray input)

🔮Example

// This will create an MovingAverage of size 5 and unsigned type
// Array: 0 0 0 0 0
MovingAveragePlus<unsigned int> intAverage(5);

// Array: 6
intAverage.push(6);

// Array: 1 6
// Array: 2 1 6
// Array: 3 
// Array: 4 3 2 1 6
for(int i = 1; i < 5; i++){
	intAverage.push(i);
}

// Array: 21 1 2 3 4
intAverage.push(21);

⏱Complexity

Constant (O(1)).

📤Get


Returns the current moving average value.

📝Syntax

TypeOfArray get()

// Passing the number of points you want to calculate the average as an parameter is an alias to ge_by_brute function.
//⚠Pay attention to how get_by_brute is significantly slower than the pure get function.
//If you are looking for a more efficient solution, search for the partial average concept in this library.
TypeOfArray get(size_t n_points) { return get_by_brute(n_points); }

🔮Example

// This will create an MovingAverage of size 4 and int type
MovingAveragePlus<int> intAverage(4);

// Array: 8 0 0 0
intAverage.push(8);
// Array: 8 8 0 0
intAverage.push(8);
// Array: 4 8 8 0
intAverage.push(4);
// Array: 4 4 8 8
intAverage.push(4);

// Returns the average
// Value: (8 + 8 + 4 + 4) / 4 = 6
intAverage.get();

// Value: (8 + 8) / 2 = 8
intAverage.get(2);

⏱Complexity

Constant (O(1)).

📤Get By Brute


Returns the average of the N last added data points requested. If the number of requested points exceed the array size, it will return the average of points already added. ⚠Pay attention to how get_by_brute is significantly slower than the pure get function. If you are looking for a more efficient solution, search for the partial average concept in this library.

📝Syntax

TypeOfArray get_by_brute(size_t n_points)

🔮Example

// This will create an MovingAverage of size 4 and int type
// Array: 0 0 0 0
MovingAveragePlus<int> intAverage(4);

// Array: 8 0 0 0
intAverage.push(8);
// Array: 8 8 0 0
intAverage.push(8);
// Array: 4 8 8 0
intAverage.push(4);
// Array: 4 4 8 8
intAverage.push(4);

// Returns the average
// Value: (8 + 8 + 4 + 4) / 4 = 6
intAverage.get_by_brute(4);

// Value: (8 + 8) / 2 = 8
intAverage.get_by_brute(2);

⏱Complexity

Linear (O(n)) in the data points requested.

➡️Front


Returns the first element of the array (the most recent element added).

📝Syntax

TypeOfArray front()

🔮Example

// This will create an MovingAverage of size 4 and int type
MovingAveragePlus<int> intAverage(4);

// Will return 0
intAverage.front();

// Array: 8 0 0 0
intAverage.push(8);

// Will return 8
intAverage.front();

⏱Complexity

Constant (O(1)).

⬅️Back


Returns the last element of the array (the oldest element added).

📝Syntax

TypeOfArray back()

🔮Example

// This will create an MovingAverage of size 4 and int type
MovingAveragePlus<int> intAverage(4);

// Will return 0
intAverage.back();

// Array: 8 0 0 0
intAverage.push(8);
// Array: 7 8 0 0
intAverage.push(7);
// Array: 12 7 8 0
intAverage.push(12);
// Array: 1 12 7 8
intAverage.push(1);

// Will return 8
intAverage.back();

⏱Complexity

Constant (O(1)).

📍Operator[]


Returns the requested element at the relative position of the array (the first index is aways the most recent element added). If the requested index exceed the array size, it will return a NULL pointer.

📝Syntax

TypeOfArray operator[](int index)

🔮Example

// This will create an MovingAverage of size 4 and int type
MovingAveragePlus<int> intAverage(4);

// Array[]: 1 0 0 0
intAverage.push(1);
// Array[]: 2 1 0 0
intAverage.push(2);
// Array[]: 3 2 1 0
intAverage.push(3);
// Array[]: 4 3 2 1
intAverage.push(4);

// Return 4
intAverage[0];

⏱Complexity

Constant (O(1)).

📍AtIndex


Returns the requested element at the absolute position of the array. If the requested index exceed the array size, it will return a NULL pointer.

📝Syntax

TypeOfArray atIndex(size_t index)

🔮Example

// This will create an MovingAverage of size 4 and int type
MovingAveragePlus<int> intAverage(4);

// Array.atIndex(): 1 0 0 0
intAverage.push(1);
// Array.atIndex(): 1 2 0 0
intAverage.push(2);
// Array.atIndex(): 1 2 3 0
intAverage.push(3);
// Array.atIndex(): 1 2 3 4
intAverage.push(4)

// return 1
intAverage.atIndex(0);

⏱Complexity

Constant (O(1)).

📏Size


Returns the size of the array.

📝Syntax

size_t size()

🔮Example

MovingAveragePlus<unsigned int> intAverage(10);
MovingAveragePlus<unsigned int> anotherAverage(4);

// Will return 10
intAverage.size()

// Will return 4
anotherAverage.size()

⏱Complexity

Constant (O(1)).

🔏Modify Array


📐Resize


Changes the array size. Methods that return pointers allow chained calls.

MovingAveragePlus<unsigned int> intAverage(10);

unsigned int x = 5, y = 10;
unsigned int result, first, last;

result = intAverage.push(x).push(y).get();
first = intAverage.push(56).front();
last = intAverage.push(8).back();

📝Syntax

MovingAveragePlus<TypeOfArray> &resize(size_t new_size)

🔮Example

MovingAveragePlus<unsigned int> intAverage(3);

// 3 2 1
intAverage.push(1).push(2).push(3);

// 3 2 1 0 0
intAverage.resize(5);

// 4 3 2 1 0
intAverage.push(4);

⏱Complexity

Linear (O(n)) in the array size.

🧹Clear


Clears the average back to 0, including its initial size. Methods that return pointers allow chained calls.

MovingAveragePlus<unsigned int> intAverage(10);

unsigned int x = 5, y = 10;
unsigned int result, first, last;

result = intAverage.push(x).push(y).get();
first = intAverage.push(56).front();
last = intAverage.push(8).back();

📝Syntax

MovingAveragePlus<TypeOfArray> &clear()

🔮Example

MovingAveragePlus<unsigned int> intAverage(5);

// 4 3 2 1 0
intAverage.push(1).push(2).push(3).push(4)

// will return 0
intAverage.clear().get()

⏱Complexity

Linear (O(n)) in the array size.

🌊Fill


Fills and overwrites the array with the received element. Methods that return pointers allow chained calls.

MovingAveragePlus<unsigned int> intAverage(10);

unsigned int x = 5, y = 10;
unsigned int result, first, last;

result = intAverage.push(x).push(y).get();
first = intAverage.push(56).front();
last = intAverage.push(8).back();

📝Syntax

MovingAveragePlus<TypeOfArray> &fill(TypeOfArray fill_value)

🔮Example

// 0 0 0 0 0
MovingAveragePlus<unsigned int> intAverage(5);

// 4 3 2 1 0
intAverage.push(1).push(2).push(3).push(4)

// 10 10 10 10 10
intAverage.fill(10)

⏱Complexity

Linear (O(n)) in the array size.