66namespace Math
77{
88
9- template <typename T> struct SegmentedFunction
9+ template <typename T, class CRTP > struct SegmentedFunction
1010{
1111 struct Stop
1212 {
@@ -26,11 +26,41 @@ template <typename T> struct SegmentedFunction
2626 stops(std::move(stops))
2727 {}
2828
29- template <class Self >
30- [[nodiscard]] Self operator *(this const Self &, double );
29+ [[nodiscard]] friend CRTP operator *(const CRTP &self,
30+ double value)
31+ {
32+ auto res = self;
33+ for (auto &stop : res.stops ) stop.value = stop.value * value;
34+ return res;
35+ }
36+
37+ [[nodiscard]] friend CRTP operator +(const CRTP &self,
38+ const CRTP &other)
39+ {
40+ CRTP res;
41+ auto &stops = self.stops ;
3142
32- template <class Self >
33- [[nodiscard]] Self operator +(this const Self &, const Self &);
43+ for (auto it0 = stops.begin (), it1 = other.stops .begin ();
44+ it0 != stops.end () || it1 != other.stops .end ();) {
45+ if (it1 == other.stops .end () || it0->pos < it1->pos ) {
46+ res.stops .emplace_back (it0->pos ,
47+ it0->value + other (it0->pos ));
48+ ++it0;
49+ }
50+ else if (it0 == stops.end () || it1->pos < it0->pos ) {
51+ res.stops .emplace_back (it1->pos ,
52+ it1->value + self (it1->pos ));
53+ ++it1;
54+ }
55+ else {
56+ res.stops .emplace_back (it0->pos ,
57+ it0->value + it1->value );
58+ ++it1;
59+ ++it0;
60+ }
61+ }
62+ return res;
63+ }
3464
3565 [[nodiscard]] bool operator ==(
3666 const SegmentedFunction &other) const = default ;
0 commit comments