aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/select/resultlist.cpp
blob: 78ea717ade8a5b617ad65aa7e2288b511c1c4637 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "resultlist.h"
#include <bitset>
#include <ostream>

namespace document::select {

ResultList::ResultList() = default;
ResultList::ResultList(ResultList &&) noexcept = default;
ResultList & ResultList::operator = (ResultList &&) noexcept = default;
ResultList::~ResultList() = default;

ResultList::ResultList(const Result& result) {
    add(VariableMap(), result);
}

void
ResultList::add(VariableMap variables, const Result& result)
{
    _results.emplace_back(std::move(variables), &result);
}

void
ResultList::print(std::ostream& out, bool, const std::string&) const
{
    out << "ResultList(";
    for (uint32_t i = 0; i < _results.size(); i++) {
        if (!_results[i].first.empty()) {
            out << _results[i].first.toString() << " => ";
        }
        out << _results[i].second->toString() << " ";
    }
    out << ")";
}

namespace {

const Result &
combineResultsLocal(const ResultList::Results & results)
{
    if (results.empty()) {
        return Result::False;
    }

    bool foundFalse = false;

    for (const auto & it : results) {
        if (*it.second == Result::True) {
            return Result::True;
        } else if (*it.second == Result::False) {
            foundFalse = true;
        }
    }

    return (foundFalse) ? Result::False : Result::Invalid;
}

}

const Result&
ResultList::combineResults() const {
    return combineResultsLocal(_results);
}

bool
ResultList::combineVariables(VariableMap& combination, const VariableMap& a, const VariableMap& b)
{
    // First, verify that all variables are overlapping
    for (const auto & ovar : a) {
        auto found(b.find(ovar.first));

        if (found != b.end()) {
            if (!(found->second == ovar.second)) {
                return false;
            }
        }
    }

    for (const auto & ivar : b) {
        auto found(a.find(ivar.first));
        if (found != a.end()) {
            if (!(found->second == ivar.second)) {
                return false;
            }
        }
    }
    // Ok, variables are overlapping. Add all variables from input to output.
    for (const auto & var : a) {
        combination[var.first] = var.second;
    }
    for (const auto & var : b) {
        combination[var.first] = var.second;
    }

    return true;
}

ResultList
ResultList::operator&&(const ResultList& other) const
{
    ResultList results;

    std::bitset<3> resultForNoVariables;
    for (const auto & it : _results) {
        for (const auto & it2 : other._results) {
            VariableMap vars;
            if ( combineVariables(vars, it.first, it2.first) ) {
                const Result & result = *it.second && *it2.second;
                if (vars.empty()) {
                    resultForNoVariables.set(result.toEnum());
                } else {
                    results.add(std::move(vars), result);
                }
            }
        }
    }
    for (uint32_t i(0); i < resultForNoVariables.size(); i++) {
        if (resultForNoVariables[i]) {
            results.add(VariableMap(), Result::fromEnum(i));
        }
    }

    return results;
}

ResultList
ResultList::operator||(const ResultList& other) const
{
    ResultList results;

    std::bitset<3> resultForNoVariables;
    for (const auto & it : _results) {
        for (const auto & it2 : other._results) {
            VariableMap vars;
            if (combineVariables(vars, it.first, it2.first)) {
                const Result & result = *it.second || *it2.second;
                if (vars.empty()) {
                    resultForNoVariables.set(result.toEnum());
                } else {
                    results.add(std::move(vars), result);
                }
            }
        }
    }
    for (uint32_t i(0); i < resultForNoVariables.size(); i++) {
        if (resultForNoVariables[i]) {
            results.add(VariableMap(), Result::fromEnum(i));
        }
    }

    return results;
}

ResultList
ResultList::operator!() && {
    ResultList result;

    for (auto & it : _results) {
        result.add(std::move(it.first), !*it.second);
    }

    return result;
}

bool ResultList::operator==(const ResultList& other) const {
    return (combineResultsLocal(_results) == combineResultsLocal(other._results));
}

}