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

#include "value.h"
#include <vespa/vespalib/util/typify.h>
#include <vespa/vespalib/objects/nbostream.h>

namespace vespalib::eval {

namespace {

struct EmptyView : Value::Index::View {
    void lookup(ConstArrayRef<const string_id*> ) override {}
    bool next_result(ConstArrayRef<string_id*> , size_t &) override { return false; }
};

struct TrivialView : Value::Index::View {
    bool first = false;
    void lookup(ConstArrayRef<const string_id*> ) override { first = true; }
    bool next_result(ConstArrayRef<string_id*> , size_t &idx_out) override {
        if (first) {
            idx_out = 0;
            first = false;
            return true;
        } else {
            return false;
        }
    }
};

struct MySum {
    template <typename CT> static double invoke(TypedCells cells) {
        double res = 0.0;
        for (CT cell: cells.typify<CT>()) {
            res += cell;
        }
        return res;
    }
};

} // <unnamed>

EmptyIndex::EmptyIndex() = default;
EmptyIndex EmptyIndex::_index;

size_t
EmptyIndex::size() const
{
    return 0;
}

std::unique_ptr<Value::Index::View>
EmptyIndex::create_view(ConstArrayRef<size_t>) const
{
    return std::make_unique<EmptyView>();
}

TrivialIndex::TrivialIndex() = default;
TrivialIndex TrivialIndex::_index;

size_t
TrivialIndex::size() const
{
    return 1;
}

std::unique_ptr<Value::Index::View>
TrivialIndex::create_view(ConstArrayRef<size_t>) const
{
    return std::make_unique<TrivialView>();
}

double
Value::as_double() const
{
    return typify_invoke<1,TypifyCellType,MySum>(type().cell_type(), cells());
}

ValueType DoubleValue::_type = ValueType::double_type();

}