Apache Arrow (C++)
A columnar in-memory analytics layer designed to accelerate big data.
tensor.h
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 
18 #ifndef ARROW_TENSOR_H
19 #define ARROW_TENSOR_H
20 
21 #include <cstdint>
22 #include <memory>
23 #include <string>
24 #include <vector>
25 
26 #include "arrow/buffer.h"
27 #include "arrow/type.h"
28 #include "arrow/util/macros.h"
29 #include "arrow/util/visibility.h"
30 
31 namespace arrow {
32 
33 static inline bool is_tensor_supported(Type::type type_id) {
34  switch (type_id) {
35  case Type::UINT8:
36  case Type::INT8:
37  case Type::UINT16:
38  case Type::INT16:
39  case Type::UINT32:
40  case Type::INT32:
41  case Type::UINT64:
42  case Type::INT64:
43  case Type::HALF_FLOAT:
44  case Type::FLOAT:
45  case Type::DOUBLE:
46  return true;
47  default:
48  break;
49  }
50  return false;
51 }
52 
53 class ARROW_EXPORT Tensor {
54  public:
55  virtual ~Tensor() = default;
56 
58  Tensor(const std::shared_ptr<DataType>& type, const std::shared_ptr<Buffer>& data,
59  const std::vector<int64_t>& shape);
60 
62  Tensor(const std::shared_ptr<DataType>& type, const std::shared_ptr<Buffer>& data,
63  const std::vector<int64_t>& shape, const std::vector<int64_t>& strides);
64 
66  Tensor(const std::shared_ptr<DataType>& type, const std::shared_ptr<Buffer>& data,
67  const std::vector<int64_t>& shape, const std::vector<int64_t>& strides,
68  const std::vector<std::string>& dim_names);
69 
70  std::shared_ptr<DataType> type() const { return type_; }
71  std::shared_ptr<Buffer> data() const { return data_; }
72 
73  const uint8_t* raw_data() const { return data_->data(); }
74  uint8_t* raw_data() { return data_->mutable_data(); }
75 
76  const std::vector<int64_t>& shape() const { return shape_; }
77  const std::vector<int64_t>& strides() const { return strides_; }
78 
79  int ndim() const { return static_cast<int>(shape_.size()); }
80 
81  const std::string& dim_name(int i) const;
82 
84  int64_t size() const;
85 
87  bool is_mutable() const { return data_->is_mutable(); }
88 
90  bool is_contiguous() const;
91 
93  bool is_row_major() const;
94 
96  bool is_column_major() const;
97 
98  Type::type type_id() const;
99 
100  bool Equals(const Tensor& other) const;
101 
102  protected:
103  Tensor() {}
104 
105  std::shared_ptr<DataType> type_;
106  std::shared_ptr<Buffer> data_;
107  std::vector<int64_t> shape_;
108  std::vector<int64_t> strides_;
109 
111  std::vector<std::string> dim_names_;
112 
113  private:
115 };
116 
117 } // namespace arrow
118 
119 #endif // ARROW_TENSOR_H
int ndim() const
Definition: tensor.h:79
std::shared_ptr< DataType > type_
Definition: tensor.h:105
bool is_mutable() const
Return true if the underlying data buffer is mutable.
Definition: tensor.h:87
type
Definition: type.h:45
Unsigned 16-bit little-endian integer.
Definition: type.h:59
Unsigned 32-bit little-endian integer.
Definition: type.h:65
std::shared_ptr< DataType > type() const
Definition: tensor.h:70
Unsigned 8-bit little-endian integer.
Definition: type.h:53
Signed 8-bit little-endian integer.
Definition: type.h:56
Definition: tensor.h:53
std::vector< std::string > dim_names_
These names are optional.
Definition: tensor.h:111
Signed 32-bit little-endian integer.
Definition: type.h:68
bool is_contiguous(PyObject *array)
4-byte floating point value
Definition: type.h:80
const std::vector< int64_t > & strides() const
Definition: tensor.h:77
Signed 16-bit little-endian integer.
Definition: type.h:62
std::vector< int64_t > shape_
Definition: tensor.h:107
Signed 64-bit little-endian integer.
Definition: type.h:74
const std::vector< int64_t > & shape() const
Definition: tensor.h:76
std::vector< int64_t > strides_
Definition: tensor.h:108
Top-level namespace for Apache Arrow C++ API.
Definition: allocator.h:29
8-byte floating point value
Definition: type.h:83
std::shared_ptr< Buffer > data_
Definition: tensor.h:106
Unsigned 64-bit little-endian integer.
Definition: type.h:71
std::shared_ptr< Buffer > data() const
Definition: tensor.h:71
Tensor()
Definition: tensor.h:103
const uint8_t * raw_data() const
Definition: tensor.h:73
2-byte floating point value
Definition: type.h:77
#define ARROW_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:23
uint8_t * raw_data()
Definition: tensor.h:74