update lookup table example
This commit is contained in:
102
Chapter11/compile_time/util/inc/signal.hpp
Normal file
102
Chapter11/compile_time/util/inc/signal.hpp
Normal file
@@ -0,0 +1,102 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
struct signal : public std::array<T, N>
|
||||
{
|
||||
|
||||
constexpr signal() {}
|
||||
|
||||
constexpr signal(T begin, T end)
|
||||
{
|
||||
static_assert(N > 1, "N must be bigger than 1");
|
||||
float step = (end - begin) / (N - 1);
|
||||
|
||||
for (std::size_t i = 0; i < N; i++)
|
||||
{
|
||||
this->at(i) = begin + i * step;
|
||||
}
|
||||
}
|
||||
|
||||
constexpr signal(const std::array<T, N> &x, auto fun)
|
||||
{
|
||||
for (std::size_t i = 0; i < N; i++)
|
||||
{
|
||||
this->at(i) = fun(x.at(i));
|
||||
}
|
||||
}
|
||||
|
||||
constexpr signal(const signal &sig, auto fun)
|
||||
{
|
||||
for (std::size_t i = 0; i < N; i++)
|
||||
{
|
||||
this->at(i) = fun(sig.at(i));
|
||||
}
|
||||
}
|
||||
|
||||
constexpr signal operator+(const T &t) const
|
||||
{
|
||||
return signal(*this, [&](T elem)
|
||||
{ return elem + t; });
|
||||
};
|
||||
|
||||
constexpr signal operator-(const T &t) const
|
||||
{
|
||||
return signal(*this, [&](T elem)
|
||||
{ return elem - t; });
|
||||
};
|
||||
|
||||
constexpr signal operator*(const T &t) const
|
||||
{
|
||||
return signal(*this, [&](T elem)
|
||||
{ return elem * t; });
|
||||
};
|
||||
|
||||
constexpr signal operator/(const T &t) const
|
||||
{
|
||||
return signal(*this, [&](T elem)
|
||||
{ return elem / t; });
|
||||
};
|
||||
|
||||
constexpr signal operator+(const signal &sig) const
|
||||
{
|
||||
signal ret;
|
||||
for (std::size_t i = 0; i < N; i++)
|
||||
{
|
||||
ret.at(i) = this->at(i) + sig.at(i);
|
||||
}
|
||||
return ret;
|
||||
};
|
||||
|
||||
constexpr signal operator-(const signal &sig) const
|
||||
{
|
||||
signal ret;
|
||||
for (std::size_t i = 0; i < N; i++)
|
||||
{
|
||||
ret.at(i) = this->at(i) - sig.at(i);
|
||||
}
|
||||
return ret;
|
||||
};
|
||||
|
||||
friend constexpr signal operator+(const T &t, const signal &sig)
|
||||
{
|
||||
return sig + t;
|
||||
}
|
||||
|
||||
friend constexpr signal operator*(const T &t, const signal &sig)
|
||||
{
|
||||
return sig * t;
|
||||
}
|
||||
|
||||
friend constexpr signal operator/(const T &t, const signal &sig)
|
||||
{
|
||||
signal ret;
|
||||
for (std::size_t i = 0; i < N; i++)
|
||||
{
|
||||
ret.at(i) = t / sig.at(i);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
33
Chapter11/compile_time/util/inc/units.hpp
Normal file
33
Chapter11/compile_time/util/inc/units.hpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
namespace units
|
||||
{
|
||||
template<typename T, typename ST>
|
||||
class unit {
|
||||
private:
|
||||
T val_;
|
||||
public:
|
||||
explicit unit(T val) : val_(val){}
|
||||
[[nodiscard]] T get() const {return val_;}
|
||||
[[nodiscard]] T get_mili() const {return 1e3 * val_;}
|
||||
|
||||
constexpr T operator/(const unit& second) const{
|
||||
return val_ / second.get();
|
||||
}
|
||||
|
||||
constexpr unit operator*(const T& second) const{
|
||||
return unit(val_ * second);
|
||||
}
|
||||
|
||||
constexpr unit operator/(const T& second) const{
|
||||
return unit(val_ / second);
|
||||
}
|
||||
};
|
||||
|
||||
using voltage = unit<float, struct the_voltage>;
|
||||
using resistance = unit<float, struct the_resistance>;
|
||||
|
||||
voltage operator""_V(long double volts);
|
||||
|
||||
resistance operator""_Ohm(long double ohms);
|
||||
};
|
||||
11
Chapter11/compile_time/util/src/units.cpp
Normal file
11
Chapter11/compile_time/util/src/units.cpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#include <units.hpp>
|
||||
|
||||
namespace units {
|
||||
voltage operator""_V(long double volts) {
|
||||
return voltage(static_cast<float>(volts));
|
||||
}
|
||||
|
||||
resistance operator""_Ohm(long double ohms) {
|
||||
return resistance(static_cast<float>(ohms));
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user