2022-10-28 19:20:44 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <array>
|
|
|
|
|
|
|
|
namespace Pinetime {
|
|
|
|
namespace Utility {
|
|
|
|
// based on: https://github.com/SHristov92/LinearApproximation/blob/main/Linear.h
|
2023-01-25 04:24:38 -05:00
|
|
|
template <typename Key, typename Value, std::size_t Size>
|
|
|
|
class LinearApproximation {
|
2022-10-28 19:20:44 -04:00
|
|
|
using Point = struct {
|
|
|
|
Key key;
|
|
|
|
Value value;
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
LinearApproximation(const std::array<Point, Size>&& sorted_points) : points {sorted_points} {
|
|
|
|
}
|
|
|
|
|
|
|
|
Value GetValue(Key key) const {
|
|
|
|
if (key <= points[0].key) {
|
|
|
|
return points[0].value;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::size_t i = 1; i < Size; i++) {
|
|
|
|
const auto& p = points[i];
|
|
|
|
const auto& p_prev = points[i - 1];
|
|
|
|
|
|
|
|
if (key < p.key) {
|
|
|
|
return p_prev.value + (key - p_prev.key) * (p.value - p_prev.value) / (p.key - p_prev.key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return points[Size - 1].value;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::array<Point, Size> points;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|