aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/expression/simple_interpolate.cpp
blob: a3f8d55dec10567c33e85fa56e2c1956e276a3e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "simple_interpolate.h"

namespace search::expression {

double
simple_interpolate(const std::vector<double>& v, double lookup) noexcept
{
    if (v.empty() || lookup < v[0]) {
        return 0;
    }
    for (size_t i = 1; i < v.size(); ++i) {
        if (lookup < v[i]) {
            double total = v[i] - v[i - 1];
            double above = lookup - v[i - 1];
            double result = i - 1;
            result += (above / total);
            return result;
        }
    }
    return v.size() - 1;
}

}