summaryrefslogtreecommitdiffstats
path: root/eval/src/tests/ann/nns.h
blob: 2e6666309bdb2ad7fcb90837122be8ef1b8a217e (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
58
59
60
// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once
#include <vespa/vespalib/util/arrayref.h>
#include "doc_vector_access.h"
#include "nns-l2.h"
#include <memory>

struct NnsHit {
    uint32_t docid;
    double sqDistance;
    NnsHit(uint32_t di, double sqD)
        : docid(di), sqDistance(sqD) {}
};
struct NnsHitComparatorLessDistance {
    bool operator() (const NnsHit &lhs, const NnsHit& rhs) const {
        if (lhs.sqDistance > rhs.sqDistance) return false;
        if (lhs.sqDistance < rhs.sqDistance) return true;
        return (lhs.docid > rhs.docid);
    }
};
struct NnsHitComparatorGreaterDistance {
    bool operator() (const NnsHit &lhs, const NnsHit& rhs) const {
        if (lhs.sqDistance < rhs.sqDistance) return false;
        if (lhs.sqDistance > rhs.sqDistance) return true;
        return (lhs.docid > rhs.docid);
    }
};
struct NnsHitComparatorLessDocid {
    bool operator() (const NnsHit &lhs, const NnsHit& rhs) const {
        return (lhs.docid < rhs.docid);
    }
};

template <typename FltType = float>
class NNS
{
public:
    NNS(uint32_t numDims, const DocVectorAccess<FltType> &dva)
      : _numDims(numDims), _dva(dva)
    {}

    virtual void addDoc(uint32_t docid) = 0;
    virtual void removeDoc(uint32_t docid) = 0;

    using Vector = vespalib::ConstArrayRef<FltType>;
    virtual std::vector<NnsHit> topK(uint32_t k, Vector vector, uint32_t search_k) = 0;
    virtual ~NNS() {}
protected:
    uint32_t _numDims;
    const DocVectorAccess<FltType> &_dva;
};

extern
std::unique_ptr<NNS<float>>
make_annoy_nns(uint32_t numDims, const DocVectorAccess<float> &dva);

extern
std::unique_ptr<NNS<float>>
make_rplsh_nns(uint32_t numDims, const DocVectorAccess<float> &dva);