summaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/queryeval/flow.h
blob: 36c0a259feb4b21f4d87bd7153bbd72c64326a7f (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once
#include <cstddef>

namespace search::queryeval {

// Model how boolean result decisions flow through intermediate nodes
// of different types based on relative estimates for sub-expressions

class AndFlow {
private:
    double _flow;
    size_t _cnt;
public:
    AndFlow(double in = 1.0) noexcept : _flow(in), _cnt(0) {}
    void add(double est) noexcept {
        _flow *= est;
        ++_cnt;
    }
    double flow() const noexcept {
        return _flow;
    }
    double estimate() const noexcept {
        return (_cnt > 0) ? _flow : 0.0;
    }
};

class OrFlow {
private:
    double _flow;
public:
    OrFlow(double in = 1.0) noexcept : _flow(in) {}
    void add(double est) noexcept {
        _flow *= (1.0 - est);
    }
    double flow() const noexcept {
        return _flow;
    }
    double estimate() const noexcept {
        return (1.0 - _flow);
    }
};

class AndNotFlow {
private:
    double _flow;
    size_t _cnt;
public:
    AndNotFlow(double in = 1.0) noexcept : _flow(in), _cnt(0) {}
    void add(double est) noexcept {
        _flow *= (_cnt++ == 0) ? est : (1.0 - est);
    }
    double flow() const noexcept {
        return _flow;
    }
    double estimate() const noexcept {
        return (_cnt > 0) ? _flow : 0.0;
    }
};

}