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

#include "value_builder_factory.h"

namespace vespalib::eval {

namespace {

struct CopyValue {
    template <typename CT>
    static std::unique_ptr<Value> invoke(const Value &value,
                                         const ValueType &type,
                                         const ValueBuilderFactory &factory)
    {
        size_t num_mapped = type.count_mapped_dimensions();
        size_t dense_size = type.dense_subspace_size();
        const auto & idx = value.index();
        auto input_cells = value.cells().typify<CT>();
        auto builder = factory.create_value_builder<CT>(type, num_mapped, dense_size, idx.size());
        std::vector<string_id> addr(num_mapped);
        if (num_mapped == 0) {
            assert(idx.size() == 1);
            auto array_ref = builder->add_subspace(addr);
            for (size_t i = 0; i < dense_size; ++i) {
                array_ref[i] = input_cells[i];
            }
        } else {
            auto view = idx.create_view({});
            view->lookup({});
            std::vector<string_id*> addr_fetch;
            addr_fetch.reserve(num_mapped);
            for (auto & label : addr) {
                addr_fetch.push_back(&label);
            }
            size_t subspace_idx;
            while (view->next_result(addr_fetch, subspace_idx)) {
                auto array_ref = builder->add_subspace(addr);
                for (size_t i = 0; i < dense_size; ++i) {
                    array_ref[i] = input_cells[(dense_size * subspace_idx) + i];
                }
            }
        }
        return builder->build(std::move(builder));
    }
};

} // namespace <unnamed>

std::unique_ptr<Value>
ValueBuilderFactory::copy(const Value &value) const
{
    const auto & type = value.type();
    return typify_invoke<1,TypifyCellType,CopyValue>(type.cell_type(),
                                                     value, type, *this);
}

}