summaryrefslogtreecommitdiffstats
path: root/eval/src/vespa/eval/tensor/dense/dense_tensor_view.h
blob: 11ed9639cc67b077a4f6f57769f1858f28d8f36e (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <vespa/eval/tensor/tensor.h>
#include <vespa/eval/tensor/types.h>
#include <vespa/eval/eval/value_type.h>
#include "dense_tensor_cells_iterator.h"

namespace vespalib::tensor {

class DenseTensor;

/**
 * A view to a dense tensor where all dimensions are indexed.
 * Tensor cells are stored in an underlying array according to the order of the dimensions.
 */
class DenseTensorView : public Tensor
{
public:
    using Cells = std::vector<double>;
    using CellsRef = ConstArrayRef<double>;
    using CellsIterator = DenseTensorCellsIterator;
    using Address = std::vector<eval::ValueType::Dimension::size_type>;

private:
    const eval::ValueType &_typeRef;
    Tensor::UP reduce_all(join_fun_t op, const std::vector<vespalib::string> &dimensions) const;
protected:
    CellsRef _cellsRef;

    void initCellsRef(CellsRef cells_in) {
        _cellsRef = cells_in;
    }

public:
    explicit DenseTensorView(const DenseTensor &rhs);
    DenseTensorView(const eval::ValueType &type_in, CellsRef cells_in)
        : _typeRef(type_in),
          _cellsRef(cells_in)
    {}
    DenseTensorView(const eval::ValueType &type_in)
            : _typeRef(type_in),
              _cellsRef()
    {}
    const eval::ValueType &fast_type() const { return _typeRef; }
    const CellsRef &cellsRef() const { return _cellsRef; }
    bool operator==(const DenseTensorView &rhs) const;
    CellsIterator cellsIterator() const { return CellsIterator(_typeRef, _cellsRef); }

    const eval::ValueType &type() const override;
    double as_double() const override;
    Tensor::UP apply(const CellFunction &func) const override;
    Tensor::UP join(join_fun_t function, const Tensor &arg) const override;
    Tensor::UP reduce(join_fun_t op, const std::vector<vespalib::string> &dimensions) const override;
    std::unique_ptr<Tensor> modify(join_fun_t op, const CellValues &cellValues) const override;
    std::unique_ptr<Tensor> add(const Tensor &arg) const override;
    std::unique_ptr<Tensor> remove(const CellValues &) const override;
    bool equals(const Tensor &arg) const override;
    Tensor::UP clone() const override;
    eval::TensorSpec toSpec() const override;
    void accept(TensorVisitor &visitor) const override;
};

}