4#ifndef APPROXIMATING_FUNCTION_H_
5#define APPROXIMATING_FUNCTION_H_
9template<
typename X,
typename Y>
10const Y MapT(
const X& x,
11 const X& x1,
const X& x2,
12 const Y& y1,
const Y& y2) {
13 Y return_val =
static_cast<Y
>((x - x1) * (y2 - y1) / (x2 - x1) + y1);
17template <
typename KeyT,
typename ValT>
19 InterpData(
const KeyT& k,
const ValT& v) : key(k), val(v) {}
24template <
typename KeyT,
typename ValT>
25inline void SelectInterpPoints(
const KeyT& k,
28 int* dest_lower_bound,
29 int* dest_upper_bound) {
31 *dest_lower_bound = *dest_upper_bound = -1;
34 if (k < array[0].key) {
35 *dest_lower_bound = *dest_upper_bound = 0;
39 for (
int i = 0; i < n - 1; ++i) {
43 if (curr.key <= k && k <= next.key) {
44 *dest_lower_bound = i;
45 *dest_upper_bound = i+1;
49 *dest_lower_bound = n - 1;
50 *dest_upper_bound = n - 1;
53template <
typename KeyT,
typename ValT>
62 SelectInterpPoints<KeyT, ValT>(k, array, n, &low_idx, &high_idx);
64 if (low_idx == high_idx) {
65 return array[low_idx].val;
72 return MapT<KeyT, ValT>(k, curr->key, next->key, curr->val, next->val);