ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Index.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
18
19#include <stdexcept>
20#include <algorithm>
21
22namespace nncc
23{
24namespace core
25{
26namespace ADT
27{
28namespace tensor
29{
30
31Index::Index(std::initializer_list<uint32_t> &&l) : _indices{l}
32{
33 // DO NOTHING
34}
35
36uint32_t Index::rank(void) const { return _indices.size(); }
38{
39 _indices.resize(size);
40 return *this;
41}
42
43Index &Index::fill(uint32_t index)
44{
45 std::fill(_indices.begin(), _indices.end(), index);
46 return (*this);
47}
48
49uint32_t &Index::at(uint32_t axis) { return _indices.at(axis); }
50uint32_t Index::at(uint32_t axis) const { return _indices.at(axis); }
51
52Index operator+(const Index &lhs, const Index &rhs)
53{
54 if (lhs.rank() != rhs.rank())
55 throw std::runtime_error("Two tensors should have same rank");
56
57 Index ret;
58 ret.resize(lhs.rank());
59 for (uint32_t axis = 0; axis < lhs.rank(); axis++)
60 {
61 ret.at(axis) = lhs.at(axis) + rhs.at(axis);
62 }
63 return ret;
64}
65
66bool operator==(const Index &lhs, const Index &rhs)
67{
68 if (lhs.rank() != rhs.rank())
69 return false;
70 for (uint32_t axis = 0; axis < lhs.rank(); axis++)
71 {
72 if (lhs.at(axis) != rhs.at(axis))
73 return false;
74 }
75 return true;
76}
77
78} // namespace tensor
79} // namespace ADT
80} // namespace core
81} // namespace nncc
Index & resize(uint32_t size)
Definition Index.cpp:37
uint32_t & at(uint32_t axis)
Definition Index.cpp:49
Index & fill(uint32_t index)
Definition Index.cpp:43
uint32_t rank(void) const
Definition Index.cpp:36
bool operator==(const Index &lhs, const Index &rhs)
Definition Index.cpp:66
Index operator+(const Index &lhs, const Index &rhs)
Definition Index.cpp:52
int32_t size[5]
Definition Slice.cpp:35