created retentive class family

This commit is contained in:
Lauchmelder 2021-12-16 15:38:52 +01:00
parent 4d59b8a53d
commit 75df9ad96d
4 changed files with 254 additions and 71 deletions

View file

@ -1,8 +1,6 @@
#pragma once
#include <vector>
#include <array>
#include <functional>
#include "RetentiveEntity.hpp"
/**
* @brief An array that remembers its past.
@ -24,7 +22,7 @@ template<
typename Type,
unsigned int AttentionSpan,
typename std::enable_if_t<(AttentionSpan > 0), bool> = true>
class RetentiveArray
class RetentiveArray : public RetentiveEntity<std::vector<Type>, AttentionSpan>
{
public:
/**
@ -104,77 +102,14 @@ public:
// Do nothing
}
/**
* @brief Get the array from `index` generations ago
*
* @param index Amount of generations to go backwards in time
* @return The array from before `index` generations
*/
std::vector<Type>& operator[](size_t index)
{
return data[index];
}
/**
* @brief Get the most up-to-date array
*
* @return The array with generation 0
*/
std::vector<Type>& Current()
{
return data[0];
}
/**
* @brief Evolve the data in the array
*
* Simply calls the member function pointer. The called function is then
* supposed to perform whatever is needed to compute the new array contents.
*
* The data is swapped BEFORE calling this function, so the evolution function should
* modify the data in the Current() vector (index 0)
*/
void Evolve()
{
// Evolve the array
Evolve(rule);
}
/**
* @brief Evolve the data in the array
*
* Simply calls the provided function pointer. The called function is then
* supposed to perform whatever is needed to compute the new array contents.
*
* The data is swapped BEFORE calling this function, so the evolution function should
* modify the data in the Current() vector (index 0)
*
* @param rule A function that evolves the data in the array
*/
void Evolve(std::function<void(void)> rule)
private:
void CycleGenerations() override
{
// Cycle the array contents, so that the previous "current" array is now the 2nd.
// The 2nd becomes the 3rd etc, and the former last array becomes the new current
for (int n = 1; n <= AttentionSpan; n++)
{
data[0].swap(data[1]);
data[0].swap(data[n]);
}
rule();
}
/**
* @brief Set a general rule for evolving the data
*
* @param rule The new evolution rule
*/
void SetEvolutionRule(std::function<void(void)> rule)
{
this->rule = rule;
}
private:
std::array<std::vector<Type>, AttentionSpan + 1> data;
std::function<void(void)> rule = std::bind([]() {}); // Do nothing by default
};