Imported Upstream version 1.18.0
[platform/core/ml/nnfw.git] / runtime / libs / ndarray / include / ndarray / Shape.h
1 /*
2  * Copyright (c) 2019 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
17 #ifndef _NDARRAY_SHAPE_H_
18 #define _NDARRAY_SHAPE_H_
19
20 #include "Common.h"
21
22 #include <array>
23 #include <cassert>
24 #include <cstddef>
25
26 namespace ndarray
27 {
28
29 class Shape
30 {
31 public:
32   //_dims{} here and later since array does not have std::initializer_list ctor
33   // and aggregate initialization is not allowed here
34   explicit Shape(size_t rank) noexcept : _dims{}, _rank(rank)
35   {
36     std::fill(_dims.begin(), _dims.end(), 0);
37   }
38
39   Shape(std::initializer_list<size_t> list) noexcept : _dims{}, _rank(list.size())
40   {
41     std::copy(list.begin(), list.end(), _dims.begin());
42   }
43
44   size_t dim(int i) const noexcept { return _dims.at(i); }
45
46   size_t &dim(int i) noexcept { return _dims.at(i); }
47
48   size_t element_count() const noexcept
49   {
50     uint32_t res = 1;
51     for (size_t i = 0; i < rank(); ++i)
52       res *= dim(i);
53     assert(res <= 0xffffffff);
54     return res;
55   }
56
57   size_t rank() const noexcept { return _rank; }
58
59 private:
60   std::array<size_t, NDARRAY_MAX_DIMENSION_COUNT> _dims;
61   size_t _rank;
62 };
63
64 } // namespace ndarray
65
66 #endif //_NDARRAY_SHAPE_H_