aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/queryeval/termwise_helper.h
blob: b8526621751cb59c0d1f46c120d8de14ff37b9ad (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "searchiterator.h"
#include <vespa/searchlib/common/bitvector.h>

namespace search::queryeval {

/**
* Helper methods for doing termwise evaluation.
**/
class TermwiseHelper {
public:
    template<typename IT>
    static BitVector::UP andChildren(BitVector::UP result, IT from, IT to, uint32_t begin_id);
    template<typename IT>
    static void andChildren(BitVector & result, IT from, IT to, uint32_t begin_id);
    template<typename IT>
    static BitVector::UP andChildren(IT from, IT to, uint32_t begin_id);

    template<typename IT>
    static BitVector::UP orChildren(BitVector::UP result, IT from, IT to, uint32_t begin_id);
    template<typename IT>
    static void orChildren(BitVector & result, IT from, IT to, uint32_t begin_id);
    template<typename IT>
    static BitVector::UP orChildren(IT from, IT to, uint32_t begin_id);
private:
    template<typename IT>
    static BitVector::UP andIterators(BitVector::UP result, IT begin, IT end, uint32_t begin_id, bool select_bitvector);
    template<typename IT>
    static void andIterators(BitVector & result, IT begin, IT end, uint32_t begin_id, bool select_bitvector);
    template<typename IT>
    static BitVector::UP orIterators(BitVector::UP result, IT begin, IT end, uint32_t begin_id, bool select_bitvector);
    template<typename IT>
    static void orIterators(BitVector & result, IT begin, IT end, uint32_t begin_id, bool select_bitvector);
};

template<typename IT>
BitVector::UP
TermwiseHelper::andChildren(BitVector::UP result, IT from, IT to, uint32_t begin_id) {
    return andIterators(andIterators(std::move(result), from, to, begin_id, true), from, to, begin_id, false);
}

template<typename IT>
void
TermwiseHelper::andChildren(BitVector & result, IT from, IT to, uint32_t begin_id) {
    andIterators(result, from, to, begin_id, true);
    andIterators(result, from, to, begin_id, false);
}

template<typename IT>
BitVector::UP
TermwiseHelper::andChildren(IT from, IT to, uint32_t begin_id) {
    return andChildren(BitVector::UP(), from, to, begin_id);
}

template<typename IT>
BitVector::UP
TermwiseHelper::orChildren(BitVector::UP result, IT from, IT to, uint32_t begin_id) {
    return orIterators(orIterators(std::move(result), from, to, begin_id, true),
                       from, to, begin_id, false);
}

template<typename IT>
void
TermwiseHelper::orChildren(BitVector & result, IT from, IT to, uint32_t begin_id) {
    orIterators(result, from, to, begin_id, true);
    orIterators(result, from, to, begin_id, false);
}

template<typename IT>
BitVector::UP
TermwiseHelper::orChildren(IT from, IT to, uint32_t begin_id) {
    return orChildren(BitVector::UP(), from, to, begin_id);
}

template<typename IT>
BitVector::UP
TermwiseHelper::andIterators(BitVector::UP result, IT begin, IT end, uint32_t begin_id, bool select_bitvector) {
    for (IT it(begin); it != end; ++it) {
        auto & child = *it;
        if (child->isBitVector() == select_bitvector) {
            if (!result) {
                result = child->get_hits(begin_id);
            } else {
                child->and_hits_into(*result, begin_id);
            }
        }
    }
    return result;
}

template<typename IT>
void
TermwiseHelper::andIterators(BitVector & result, IT begin, IT end, uint32_t begin_id, bool select_bitvector) {
    for (IT it(begin); it != end; ++it) {
        auto & child = *it;
        if (child->isBitVector() == select_bitvector) {
            child->and_hits_into(result, begin_id);
        }
    }
}

template<typename IT>
BitVector::UP
TermwiseHelper::orIterators(BitVector::UP result, IT begin, IT end, uint32_t begin_id, bool select_bitvector) {
    for (IT it(begin); it != end; ++it) {
        auto & child = *it;
        if (child->isBitVector() == select_bitvector) {
            if (!result) {
                result = child->get_hits(begin_id);
            } else {
                child->or_hits_into(*result, begin_id);
            }
        }
    }
    return result;
}

template<typename IT>
void
TermwiseHelper::orIterators(BitVector & result, IT begin, IT end, uint32_t begin_id, bool select_bitvector) {
    for (IT it(begin); it != end; ++it) {
        auto & child = *it;
        if (child->isBitVector() == select_bitvector) {
            child->or_hits_into(result, begin_id);
        }
    }
}

}