92interp1D(RAIterator
const &begin, RAIterator
const &end, Key
const &keyVal,
93 ExtractKey
const &extractKey, ExtractValue
const &extractValue,
94 CompareKey
const &comp = std::less<Key>())
98 using category =
typename std::iterator_traits<RAIterator>::iterator_category;
99 static_assert(std::is_same_v<category, std::bidirectional_iterator_tag> ||
100 std::is_same_v<category, std::random_access_iterator_tag>,
101 "Iterators must be (at least) bidirectional");
104 if(std::distance(begin, end) < 1)
105 throw std::runtime_error(
106 "Interp1D: I need at least 2 points to interpolate!");
111 for(
auto dis = std::distance(a, b); dis > 1;)
113 RAIterator c = std::next(a, dis / 2);
114 if(comp(keyVal, extractKey(*c)))
118 dis = std::distance(a, b);
127 const auto valueLeft = extractValue(*a);
128 const Key keyLeft = extractKey(*a);
129 const auto valueRight = extractValue(*b);
130 const Key keyRight = extractKey(*b);
131 const auto len = keyRight - keyLeft;
133 const auto coeffRight = (keyVal - keyLeft) / len;
134 const auto coeffLeft = 1.0 - coeffRight;
135 return valueLeft * coeffLeft + valueRight * coeffRight;
auto interp1D(RAIterator const &begin, RAIterator const &end, Key const &keyVal, ExtractKey const &extractKey, ExtractValue const &extractValue, CompareKey const &comp=std::less< Key >())
Definition interp1D.hpp:92