aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/attribute/distance_metric_utils.cpp
blob: db6c0432374b79806e210a75d2ed13b7b4df3d03 (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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "distance_metric_utils.h"
#include <vespa/vespalib/util/exceptions.h>

namespace search::attribute {

namespace {

const vespalib::string euclidean = "euclidean";
const vespalib::string angular = "angular";
const vespalib::string geodegrees = "geodegrees";
const vespalib::string innerproduct = "innerproduct";
const vespalib::string prenormalized_angular = "prenormalized_angular";
const vespalib::string dotproduct = "dotproduct";
const vespalib::string hamming = "hamming";

}

vespalib::string
DistanceMetricUtils::to_string(DistanceMetric metric)
{
    switch (metric) {
        case DistanceMetric::Euclidean: return euclidean;
        case DistanceMetric::Angular: return angular;
        case DistanceMetric::GeoDegrees: return geodegrees;
        case DistanceMetric::InnerProduct: return innerproduct;
        case DistanceMetric::Hamming: return hamming;
        case DistanceMetric::PrenormalizedAngular: return prenormalized_angular;
        case DistanceMetric::Dotproduct: return dotproduct;
    }
    throw vespalib::IllegalArgumentException("Unknown distance metric " + std::to_string(static_cast<int>(metric)));
}

DistanceMetric
DistanceMetricUtils::to_distance_metric(const vespalib::string& metric)
{
    if (metric == euclidean) {
        return DistanceMetric::Euclidean;
    } else if (metric == angular) {
        return DistanceMetric::Angular;
    } else if (metric == geodegrees) {
        return DistanceMetric::GeoDegrees;
    } else if (metric == innerproduct) {
        return DistanceMetric::InnerProduct;
    } else if (metric == prenormalized_angular) {
        return DistanceMetric::PrenormalizedAngular;
    } else if (metric == dotproduct) {
        return DistanceMetric::Dotproduct;
    } else if (metric == hamming) {
        return DistanceMetric::Hamming;
    } else {
        throw vespalib::IllegalStateException("Unknown distance metric '" + metric + "'");
    }
}

}