aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/fef/termfieldmatchdataposition.h
blob: 595d6138bd78644eda681740933d5681b53fab21 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <vespa/searchlib/common/fslimits.h>
#include <cstdint>

namespace search::fef {

class TermFieldMatchDataPositionKey
{
private:
    uint32_t _elementId;
    uint32_t _position;

public:
    TermFieldMatchDataPositionKey()
        : _elementId(0u),
          _position(0u)
    { }

    TermFieldMatchDataPositionKey(uint32_t elementId,
                                  uint32_t position)
        : _elementId(elementId),
          _position(position)
    { }

    uint32_t getElementId() const { return _elementId; }
    uint32_t getPosition() const { return _position; }

    void setElementId(uint32_t elementId) { _elementId = elementId; }
    void setPosition(uint32_t position) { _position = position; }

    bool operator<(const TermFieldMatchDataPositionKey &rhs) const {
        if (_elementId != rhs._elementId) {
            return _elementId < rhs._elementId;
        }
        return _position < rhs._position;
    }

    bool operator==(const TermFieldMatchDataPositionKey &rhs) const {
        return ((_elementId == rhs._elementId) &&
                (_position == rhs._position));
    }
};

class TermFieldMatchDataPosition : public TermFieldMatchDataPositionKey
{
private:
    int32_t _elementWeight;
    uint32_t _elementLen;
    double   _matchExactness; // or possibly _matchWeight

public:
    TermFieldMatchDataPosition()
        : TermFieldMatchDataPositionKey(),
          _elementWeight(1),
          _elementLen(SEARCHLIB_FEF_UNKNOWN_FIELD_LENGTH),
          _matchExactness(1.0)
    { }

    const TermFieldMatchDataPositionKey &key() const {
        return *this;
    }

    /**
     * A comparator for sorting in natural (ascending) order but if
     * positions are equal, sort best exactness first.
     */
    static bool compareWithExactness(const TermFieldMatchDataPosition &a,
                                     const TermFieldMatchDataPosition &b)
    {
        if (a < b) return true;
        if (b < a) return false;
        return a._matchExactness > b._matchExactness;
    }

    TermFieldMatchDataPosition(uint32_t elementId,
                               uint32_t position,
                               int32_t elementWeight,
                               uint32_t elementLen)
        : TermFieldMatchDataPositionKey(elementId, position),
          _elementWeight(elementWeight),
          _elementLen(elementLen),
          _matchExactness(1.0)
    { }

    int32_t getElementWeight() const { return _elementWeight; }
    uint32_t getElementLen() const { return _elementLen; }
    double getMatchExactness() const { return _matchExactness; }

    void setElementWeight(int32_t elementWeight) {
        _elementWeight = elementWeight;
    }
    void setElementLen(uint32_t elementLen) {
        _elementLen = elementLen;
    }
    TermFieldMatchDataPosition& setMatchExactness(double exactness) {
        _matchExactness = exactness;
        return *this;
    }
};

}