aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/select/branch.cpp
blob: d8dc58edf200a93513a4f73a1b096a57648caf57 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "branch.h"
#include "visitor.h"
#include <cassert>
#include <ostream>

namespace document::select {

And::And(std::unique_ptr<Node> left, std::unique_ptr<Node> right, const char* name)
    : Branch(name ? name : "and", std::max(left->max_depth(), right->max_depth()) + 1),
      _left(std::move(left)),
      _right(std::move(right))
{
    assert(_left);
    assert(_right);
}


void
And::visit(Visitor &v) const
{
    v.visitAndBranch(*this);
}


void
And::print(std::ostream& out, bool verbose, const std::string& indent) const
{
    if (_parentheses) out << '(';
    _left->print(out, verbose, indent);
    out << " " << _name << " ";
    _right->print(out, verbose, indent);
    if (_parentheses) out << ')';
}

namespace {
    template<typename T>
    ResultList traceAndValue(const T& value, std::ostream& out,
                         const Node& leftNode, const Node& rightNode)
    {
        out << "And - Left branch returned " << leftNode.contains(value) << ".\n";
        out << "And - Right branch returned " << rightNode.contains(value) << ".\n";
        return leftNode.contains(value) && rightNode.contains(value);
    }
}

ResultList
And::trace(const Context& context, std::ostream& out) const
{
    return traceAndValue(context, out, *_left, *_right);
}

Or::Or(std::unique_ptr<Node> left, std::unique_ptr<Node> right, const char* name)
    : Branch(name ? name : "or", std::max(left->max_depth(), right->max_depth()) + 1),
      _left(std::move(left)),
      _right(std::move(right))
{
    assert(_left.get());
    assert(_right.get());
}


void
Or::visit(Visitor &v) const
{
    v.visitOrBranch(*this);
}


void
Or::print(std::ostream& out, bool verbose, const std::string& indent) const
{
    if (_parentheses) out << '(';
    _left->print(out, verbose, indent);
    out << " " << _name << " ";
    _right->print(out, verbose, indent);
    if (_parentheses) out << ')';
}

namespace {
    template<typename T>
    ResultList traceOrValue(const T& value, std::ostream& out,
                         const Node& leftNode, const Node& rightNode)
    {
        out << "Or - Left branch returned " << leftNode.contains(value) << ".\n";
        out << "Or - Right branch returned " << rightNode.contains(value) << ".\n";
        return leftNode.contains(value) || rightNode.contains(value);
    }
}

ResultList
Or::trace(const Context& context, std::ostream& out) const
{
    return traceOrValue(context, out, *_left, *_right);
}

Not::Not(std::unique_ptr<Node> child, const char* name)
    : Branch(name ? name : "not", child->max_depth() + 1),
      _child(std::move(child))
{
    assert(_child.get());
}


void
Not::visit(Visitor &v) const
{
    v.visitNotBranch(*this);
}

void
Not::print(std::ostream& out, bool verbose, const std::string& indent) const
{
    if (_parentheses) out << '(';
    out << _name << " ";
    _child->print(out, verbose, indent);
    if (_parentheses) out << ')';
}

namespace {
    template<typename T>
    ResultList traceNotValue(const T& value, std::ostream& out, const Node& node)
    {
        out << "Not - Child returned " << node.contains(value)
            << ". Returning opposite.\n";
        return !node.contains(value);
    }
}

ResultList
Not::trace(const Context& context, std::ostream& out) const
{
    return traceNotValue(context, out, *_child);
}

}