blob: 84d76e71434a8042e3573a79b3a5413e42f758d1 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 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_TENSOR_H__
25#define __ARM_COMPUTE_TEST_TENSOR_H__
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
29
30namespace arm_compute
31{
32namespace test
33{
34namespace validation
35{
36template <typename T>
37class Tensor
38{
39public:
40 Tensor()
41 : _shape(), _dt(DataType::UNKNOWN), _fixed_point_position(0), _ptr(nullptr), _ptr_const(nullptr) {};
42
43 Tensor(TensorShape shape, DataType dt, int fixed_point_position, T *ptr)
44 : _shape(shape), _dt(dt), _fixed_point_position(fixed_point_position), _ptr(ptr), _ptr_const(nullptr) {};
45
46 Tensor(TensorShape shape, DataType dt, int fixed_point_position, const T *ptr)
47 : _shape(shape), _dt(dt), _fixed_point_position(fixed_point_position), _ptr(nullptr), _ptr_const(ptr) {};
48
49 Tensor(const Tensor &tensor) = delete;
50 Tensor &operator=(const Tensor &) = delete;
51 Tensor(Tensor &&) = default;
52 Tensor &operator=(Tensor &&) = default;
53
54 ~Tensor() = default;
55
56 T &operator[](size_t offset)
57 {
Moritz Pflanzere3bc5c92017-08-02 11:38:42 +010058 ARM_COMPUTE_ERROR_ON(_ptr == nullptr);
59
Anthony Barbier6ff3b192017-09-04 18:44:23 +010060 return _ptr[offset];
61 }
62
63 const T &operator[](size_t offset) const
64 {
Moritz Pflanzere3bc5c92017-08-02 11:38:42 +010065 const T *ptr = (_ptr_const != nullptr) ? _ptr_const : _ptr;
66
67 ARM_COMPUTE_ERROR_ON(ptr == nullptr);
68
Giorgio Arenafc2817d2017-06-27 17:26:37 +010069 return ptr[offset]; // NOLINT
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070 }
71
72 int num_elements() const
73 {
74 return std::accumulate(_shape.cbegin(), _shape.cend(), 1, std::multiplies<int>());
75 }
76
77 TensorShape shape() const
78 {
79 return _shape;
80 }
81
82 DataType data_type() const
83 {
84 return _dt;
85 }
86
87 int fixed_point_position() const
88 {
89 return _fixed_point_position;
90 }
91
92 const T *data() const
93 {
Moritz Pflanzere3bc5c92017-08-02 11:38:42 +010094 return (_ptr_const != nullptr) ? _ptr_const : _ptr;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095 }
Moritz Pflanzere3bc5c92017-08-02 11:38:42 +010096
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097 T *data()
98 {
99 return _ptr;
100 }
101
Moritz Pflanzere3bc5c92017-08-02 11:38:42 +0100102 const T *data_const() const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103 {
104 return _ptr_const;
105 }
106
107private:
108 TensorShape _shape;
109 DataType _dt;
110 int _fixed_point_position;
111 T *_ptr;
112 const T *_ptr_const;
113};
114} // namespace validation
115} // test
116} // arm_compute
117
118#endif /* __ARM_COMPUTE_TEST_TENSOR_H__ */