aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/update/tensor_modify_update.h
blob: d63034ebdaf37b4417c083fd85a58612a4a00804 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "tensor_update.h"
#include "valueupdate.h"
#include <optional>

namespace vespalib::eval { struct Value; }

namespace document {

class TensorDataType;
class TensorFieldValue;

/*
 * An update for a subset of the cells in a tensor.
 *
 * The operand is represented as a tensor field value containing a
 * mapped (aka sparse) tensor.
 */
class TensorModifyUpdate final : public ValueUpdate, public TensorUpdate {
public:
    /** Declare all types of tensor modify updates. */
    enum class Operation { // Operation to be applied to matching tensor cells
        REPLACE  = 0,
        ADD      = 1,
        MULTIPLY = 2,
        MAX_NUM_OPERATIONS = 3
    };
private:
    Operation _operation;
    std::unique_ptr<const TensorDataType> _tensorType;
    std::unique_ptr<TensorFieldValue> _tensor;
    // When this is set, non-existing cells are created in the input tensor before applying the update.
    std::optional<double> _default_cell_value;

    friend ValueUpdate;
    TensorModifyUpdate();
    ACCEPT_UPDATE_VISITOR;
public:
    TensorModifyUpdate(Operation operation, std::unique_ptr<TensorFieldValue> tensor);
    TensorModifyUpdate(Operation operation, std::unique_ptr<TensorFieldValue> tensor, double default_cell_value);
    TensorModifyUpdate(const TensorModifyUpdate &rhs) = delete;
    TensorModifyUpdate &operator=(const TensorModifyUpdate &rhs) = delete;
    ~TensorModifyUpdate() override;

    bool operator==(const ValueUpdate &other) const override;
    Operation getOperation() const { return _operation; }
    const TensorFieldValue &getTensor() const { return *_tensor; }
    const std::optional<double>& get_default_cell_value() const { return _default_cell_value; }
    void checkCompatibility(const Field &field) const override;
    std::unique_ptr<vespalib::eval::Value> applyTo(const vespalib::eval::Value &tensor) const;
    std::unique_ptr<Value> apply_to(const Value &tensor,
                                    const ValueBuilderFactory &factory) const override;
    bool applyTo(FieldValue &value) const override;
    void printXml(XmlOutputStream &xos) const override;
    void print(std::ostream &out, bool verbose, const std::string &indent) const override;
    void deserialize(const DocumentTypeRepo &repo, const DataType &type, nbostream &stream) override;
};

}