xref: /aosp_15_r20/external/ComputeLibrary/tests/SimpleTensorAccessor.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2019 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #ifndef ARM_COMPUTE_TEST_SIMPLE_TENSOR_ACCESSOR_H
25 #define ARM_COMPUTE_TEST_SIMPLE_TENSOR_ACCESSOR_H
26 
27 #include "SimpleTensor.h"
28 #include "tests/IAccessor.h"
29 
30 namespace arm_compute
31 {
32 namespace test
33 {
34 /** Accessor implementation for @ref SimpleTensor objects. */
35 template <typename T>
36 class SimpleTensorAccessor : public IAccessor
37 {
38 public:
39     /** Create an accessor for the given @p tensor.
40      *
41      * @param[in, out] tensor To be accessed tensor.
42      */
43     SimpleTensorAccessor(SimpleTensor<T> &tensor);
44 
45     /** Prevent instances of this class from being copy constructed */
46     SimpleTensorAccessor(const SimpleTensorAccessor &) = delete;
47     /** Prevent instances of this class from being copied */
48     SimpleTensorAccessor &operator=(const SimpleTensorAccessor &) = delete;
49     /** Allow instances of this class to be move constructed */
50     SimpleTensorAccessor(SimpleTensorAccessor &&) = default;
51     /** Allow instances of this class to be moved */
52     SimpleTensorAccessor &operator=(SimpleTensorAccessor &&) = default;
53 
54     /** Get the tensor data.
55      *
56      * @return a constant pointer to the tensor data.
57      */
58     const void *data() const;
59     /** Get the tensor data.
60      *
61      * @return a pointer to the tensor data.
62      */
63     void *data();
64 
65     // Inherited methods overridden:
66     TensorShape      shape() const override;
67     size_t           element_size() const override;
68     size_t           size() const override;
69     Format           format() const override;
70     DataLayout       data_layout() const override;
71     DataType         data_type() const override;
72     int              num_channels() const override;
73     int              num_elements() const override;
74     PaddingSize      padding() const override;
75     QuantizationInfo quantization_info() const override;
76     const void *operator()(const Coordinates &coord) const override;
77     void *operator()(const Coordinates &coord) override;
78 
79 private:
80     SimpleTensor<T> &_tensor;
81 };
82 
83 template <typename T>
SimpleTensorAccessor(SimpleTensor<T> & tensor)84 inline SimpleTensorAccessor<T>::SimpleTensorAccessor(SimpleTensor<T> &tensor)
85     : _tensor{ tensor }
86 {
87 }
88 
89 template <typename T>
shape()90 inline TensorShape SimpleTensorAccessor<T>::shape() const
91 {
92     return _tensor.shape();
93 }
94 
95 template <typename T>
element_size()96 inline size_t SimpleTensorAccessor<T>::element_size() const
97 {
98     return _tensor.element_size();
99 }
100 
101 template <typename T>
size()102 inline size_t SimpleTensorAccessor<T>::size() const
103 {
104     return _tensor.num_elements() * _tensor.element_size();
105 }
106 
107 template <typename T>
format()108 inline Format SimpleTensorAccessor<T>::format() const
109 {
110     return _tensor.format();
111 }
112 
113 template <typename T>
data_layout()114 inline DataLayout SimpleTensorAccessor<T>::data_layout() const
115 {
116     return _tensor.data_layout();
117 }
118 
119 template <typename T>
data_type()120 inline DataType SimpleTensorAccessor<T>::data_type() const
121 {
122     return _tensor.data_type();
123 }
124 
125 template <typename T>
num_channels()126 inline int SimpleTensorAccessor<T>::num_channels() const
127 {
128     return _tensor.num_channels();
129 }
130 
131 template <typename T>
num_elements()132 inline int SimpleTensorAccessor<T>::num_elements() const
133 {
134     return _tensor.num_elements();
135 }
136 
137 template <typename T>
padding()138 inline PaddingSize SimpleTensorAccessor<T>::padding() const
139 {
140     return _tensor.padding();
141 }
142 
143 template <typename T>
quantization_info()144 inline QuantizationInfo SimpleTensorAccessor<T>::quantization_info() const
145 {
146     return _tensor.quantization_info();
147 }
148 
149 template <typename T>
data()150 inline const void *SimpleTensorAccessor<T>::data() const
151 {
152     return _tensor.data();
153 }
154 
155 template <typename T>
data()156 inline void *SimpleTensorAccessor<T>::data()
157 {
158     return _tensor.data();
159 }
160 
161 template <typename T>
operator()162 inline const void *SimpleTensorAccessor<T>::operator()(const Coordinates &coord) const
163 {
164     return _tensor(coord);
165 }
166 
167 template <typename T>
operator()168 inline void *SimpleTensorAccessor<T>::operator()(const Coordinates &coord)
169 {
170     return _tensor(coord);
171 }
172 } // namespace test
173 } // namespace arm_compute
174 #endif /* ARM_COMPUTE_TEST_SIMPLE_TENSOR_ACCESSOR_H */
175