aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/expression/numericfunctionnode.h
blob: e64525a8cc0779f997cddd529c3ac13fe8b1971b (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
171
172
173
174
175
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "multiargfunctionnode.h"
#include "resultvector.h"

namespace search::expression {

class NumericFunctionNode : public MultiArgFunctionNode
{
public:
    DECLARE_ABSTRACT_EXPRESSIONNODE(NumericFunctionNode);
    NumericFunctionNode();
    NumericFunctionNode(const NumericFunctionNode & rhs);
    NumericFunctionNode & operator = (const NumericFunctionNode & rhs);
    ~NumericFunctionNode();
    void reset() override { _handler.reset(); MultiArgFunctionNode::reset(); }
protected:
    void onPrepare(bool preserveAccurateTypes) override;

    class Handler
    {
    public:
        Handler(const NumericFunctionNode & func) : _function(func) { }
        virtual ~Handler() { }
        virtual void handle(const ResultNode & arg) = 0;
        virtual void handleFirst(const ResultNode & arg) = 0;
    protected:
        const NumericFunctionNode & function() const { return _function; }
    private:
        const NumericFunctionNode & _function;
    };

    template <typename T>
    class VectorHandler : public Handler
    {
    protected:
        VectorHandler(const NumericFunctionNode & func) :
            Handler(func),
            _result(static_cast<T &>(func.updateResult()))
        { }
        void handle(const ResultNode & arg) override;
        void handleFirst(const ResultNode & arg) override;
    private:
        T & _result;
    };

    class VectorIntegerHandler : public VectorHandler<IntegerResultNodeVector>
    {
    private:
        using BaseHandler = VectorHandler<IntegerResultNodeVector>;
    public:
        VectorIntegerHandler(const NumericFunctionNode & func) : BaseHandler(func) { }
    };
    class VectorFloatHandler : public VectorHandler<FloatResultNodeVector>
    {
    private:
        using BaseHandler = VectorHandler<FloatResultNodeVector>;
    public:
        VectorFloatHandler(const NumericFunctionNode & func) : BaseHandler(func) { }
    };
    class VectorStringHandler : public VectorHandler<StringResultNodeVector>
    {
    private:
        using BaseHandler = VectorHandler<StringResultNodeVector>;
    public:
        VectorStringHandler(const NumericFunctionNode & func) : BaseHandler(func) { }
    };
private:
    virtual ResultNode::CP getInitialValue() const = 0;
    virtual ResultNode & flatten(const ResultNodeVector & v, ResultNode & result) const = 0;
    class ScalarIntegerHandler : public Handler
    {
    public:
        ScalarIntegerHandler(const NumericFunctionNode & func) :
            Handler(func),
            _result(static_cast<Int64ResultNode &>(func.updateResult()))
        { }
        void handle(const ResultNode & arg) override;
        void handleFirst(const ResultNode & arg) override { _result.set(arg.getInteger()); }
    protected:
        Int64ResultNode & _result;
    };
    class ScalarFloatHandler : public Handler
    {
    public:
        ScalarFloatHandler(const NumericFunctionNode & func) :
            Handler(func),
            _result(static_cast<FloatResultNode &>(func.updateResult()))
        { }
        void handle(const ResultNode & arg) override;
        void handleFirst(const ResultNode & arg) override { _result.set(arg.getFloat()); }
    protected:
        FloatResultNode & _result;
    };
    class ScalarStringHandler : public Handler
    {
    public:
        ScalarStringHandler(const NumericFunctionNode & func) :
            Handler(func),
            _result(static_cast<StringResultNode &>(func.updateResult()))
        { }
        void handle(const ResultNode & arg) override;
        void handleFirst(const ResultNode & arg) override {
            char buf[32];
            vespalib::ConstBufferRef b = arg.getString(vespalib::BufferRef(buf, sizeof(buf)));
            _result.set(vespalib::stringref(b.c_str(), b.size()));
        }
    protected:
        StringResultNode & _result;
    };
    class ScalarRawHandler : public Handler
    {
    public:
        ScalarRawHandler(const NumericFunctionNode & func) :
            Handler(func),
            _result(static_cast<RawResultNode &>(func.updateResult()))
        { }
        void handle(const ResultNode & arg) override;
        void handleFirst(const ResultNode & arg) override {
            char buf[32];
            vespalib::ConstBufferRef b = arg.getString(vespalib::BufferRef(buf, sizeof(buf)));
            _result.setBuffer(b.data(), b.size());
        }
    protected:
        RawResultNode & _result;
    };
    class FlattenIntegerHandler : public ScalarIntegerHandler
    {
    public:
        FlattenIntegerHandler(const NumericFunctionNode & func) :
            ScalarIntegerHandler(func),
            _initial()
        {
            _initial.set(*func.getInitialValue());
        }
        void handle(const ResultNode & arg) override;
        void handleFirst(const ResultNode & arg) override { handle(arg); }
    private:
        Int64ResultNode _initial;
    };
    class FlattenFloatHandler : public ScalarFloatHandler
    {
    public:
        FlattenFloatHandler(const NumericFunctionNode & func) :
            ScalarFloatHandler(func),
            _initial()
        {
            _initial.set(*func.getInitialValue());
        }
        void handle(const ResultNode & arg) override;
        void handleFirst(const ResultNode & arg) override { handle(arg); }
    private:
        FloatResultNode _initial;
    };
    class FlattenStringHandler : public ScalarStringHandler
    {
    public:
        FlattenStringHandler(const NumericFunctionNode & func) :
            ScalarStringHandler(func),
            _initial()
        {
            _initial.set(*func.getInitialValue());
        }
        void handle(const ResultNode & arg) override;
        void handleFirst(const ResultNode & arg) override { handle(arg); }
    private:
        StringResultNode _initial;
    };

    bool onCalculate(const ExpressionNodeVector & args, ResultNode & result) const override;
    std::unique_ptr<Handler> _handler;
};

}