aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/expression/integerresultnode.h
blob: d145714a5f20aa4c0c996b1c89b96c957da2db9d (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "numericresultnode.h"
#include <vespa/vespalib/util/sort.h>
#include <limits>
#include <type_traits>

namespace search::expression {

class BucketResultNode;

class IntegerResultNode : public NumericResultNode
{
public:
    DECLARE_ABSTRACT_RESULTNODE(IntegerResultNode);

    virtual const BucketResultNode& getNullBucket() const override;
};

template <typename T>
class IntegerResultNodeT : public IntegerResultNode
{
public:
    IntegerResultNodeT(int64_t v=0) : _value(v) { }
    size_t hash() const override { return _value; }
    int onCmp(const Identifiable & b) const override {
        T bv(static_cast<const IntegerResultNodeT &>(b)._value);
        return (_value < bv) ? -1 : (_value > bv) ? 1 : 0;
    }
    void add(const ResultNode & b) override { _value = uint64_t(_value) + uint64_t(b.getInteger()); }
    void negate() override { _value = - _value; }
    void multiply(const ResultNode & b) override {
        if constexpr (std::is_same_v<T, bool>) {
         _value = (_value && (b.getInteger() != 0));
       } else {
         _value *= b.getInteger();
       }
    }
    void divide(const ResultNode & b) override {
        int64_t val = b.getInteger();
        _value = (val == 0) ? 0 : (_value / val);
    }
    void modulo(const ResultNode & b) override {
        int64_t val = b.getInteger();
        _value = (val == 0) ? 0 : (_value % val);
    }
    void min(const ResultNode & b) override {
        int64_t t(b.getInteger());
        if (t < _value) { _value = t; }
    }
    void max(const ResultNode & b) override {
        int64_t t(b.getInteger());
        if (t > _value) { _value = t; }
    }
    void set(const ResultNode & rhs) override { _value = rhs.getInteger(); }
    void andOp(const ResultNode & b) { _value &= b.getInteger(); }
    void  orOp(const ResultNode & b) { _value |= b.getInteger(); }
    void xorOp(const ResultNode & b) { _value ^= b.getInteger(); }
    int64_t get()      const { return _value; }
    void set(int64_t value)  { _value = value; }
    IntegerResultNode & operator ++() { _value++; return *this; }
    IntegerResultNode & operator +=(int64_t v) { _value += v; return *this; }

protected:
    void setValue(const T &value) { _value = value; }
    T getValue() const { return _value; }
private:
    int cmpMem(const void * a, const void *b) const override {
        const T & ai(*static_cast<const T *>(a));
        const T & bi(*static_cast<const T *>(b));
        return ai < bi ? -1 : ai == bi ? 0 : 1;
    }
    void create(void * buf)  const override { (void) buf; }
    void destroy(void * buf) const override { (void) buf; }
    void decode(const void * buf)  override { _value = *static_cast<const T *>(buf); }
    void encode(void * buf) const  override { *static_cast<T *>(buf) = _value; }
    void swap(void * buf)          override { std::swap(*static_cast<T *>(buf), _value); }
    size_t hash(const void * buf) const override { return *static_cast<const T *>(buf); }
    uint64_t  radixAsc(const void * buf) const override {
        return vespalib::convertForSort<T,  true>::convert(*static_cast<const T *>(buf));
    }
    uint64_t radixDesc(const void * buf) const override {
        return vespalib::convertForSort<T, false>::convert(*static_cast<const T *>(buf));
    }
    size_t onGetRawByteSize() const override { return sizeof(_value); }
    void setMin() override { _value = std::numeric_limits<T>::min(); }
    void setMax() override { _value = std::numeric_limits<T>::max(); }
    vespalib::Serializer & onSerialize(vespalib::Serializer & os) const override { return os << _value; }
    vespalib::Deserializer & onDeserialize(vespalib::Deserializer & is) override { return is >> _value; }
    void visitMembers(vespalib::ObjectVisitor &visitor) const override { visit(visitor, "value", _value); }
    int64_t onGetInteger(size_t index) const override { (void) index; return _value; }
    double onGetFloat(size_t index)    const override { (void) index; return _value; }
    T _value;
};

class BoolResultNode : public IntegerResultNodeT<bool>
{
private:
    using Base = IntegerResultNodeT<bool>;
public:
    DECLARE_RESULTNODE(BoolResultNode);
    BoolResultNode(bool v=false) : Base(v) { }
    bool getBool() const { return getValue(); }
private:
    ConstBufferRef onGetString(size_t index, BufferRef buf) const override;
};

class Int8ResultNode : public IntegerResultNodeT<int8_t>
{
private:
    using Base = IntegerResultNodeT<int8_t>;
public:
    DECLARE_RESULTNODE(Int8ResultNode);
    Int8ResultNode(int8_t v=0) : Base(v) { }
private:
    ConstBufferRef onGetString(size_t index, BufferRef buf) const override;
};

class Int16ResultNode : public IntegerResultNodeT<int16_t>
{
private:
    using Base = IntegerResultNodeT<int16_t>;
public:
    DECLARE_RESULTNODE(Int16ResultNode);
    Int16ResultNode(int16_t v=0) : Base(v) { }
private:
    ConstBufferRef onGetString(size_t index, BufferRef buf) const override;
};

class Int32ResultNode : public IntegerResultNodeT<int32_t>
{
private:
    using Base = IntegerResultNodeT<int32_t>;
public:
    DECLARE_RESULTNODE(Int32ResultNode);
    Int32ResultNode(int32_t v=0) : Base(v) { }
private:
    ConstBufferRef onGetString(size_t index, BufferRef buf) const override;
};

class Int64ResultNode : public IntegerResultNodeT<int64_t>
{
private:
    using Base = IntegerResultNodeT<int64_t>;
public:
    DECLARE_RESULTNODE(Int64ResultNode);
    Int64ResultNode(int64_t v=0) : Base(v) { }
private:
    ConstBufferRef onGetString(size_t index, BufferRef buf) const override;
};

}