Publishing 2020.1 content
[platform/upstream/dldt.git] / inference-engine / include / ie_layers_property.hpp
1 // Copyright (C) 2018-2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 /**
6  * @brief a header file for describing property style structure used by CNNLayers
7  * 
8  * @file
9  */
10 #pragma once
11
12 #include <vector>
13
14 namespace InferenceEngine {
15
16 constexpr const int MAX_DIMS_NUMBER = 12;
17
18 enum eDIMS_AXIS : uint8_t { X_AXIS = 0, Y_AXIS, Z_AXIS };
19
20 template <class T, int N = MAX_DIMS_NUMBER>
21 class PropertyVector {
22     T _axises[N] = {};
23     bool _allocated[N] = {};
24     size_t _length = 0;
25
26 public:
27     PropertyVector() = default;
28
29     PropertyVector(size_t len, T val) {
30         if (len > N) {
31             THROW_IE_EXCEPTION << "Property size exceeed limit of: " << N;
32         }
33         for (size_t i = 0; i < len; i++) {
34             _axises[i] = val;
35             _allocated[i] = true;
36         }
37         _length = len;
38     }
39
40     explicit PropertyVector(const std::vector<T>& values) {
41         size_t i = 0;
42         for (const auto val : values) {
43             insert(i++, val);
44         }
45     }
46
47     PropertyVector(std::initializer_list<int> init_list) {
48         size_t i = 0;
49         for (const auto val : init_list) {
50             insert(i++, val);
51         }
52     }
53
54     /**
55      * @brief allows access up-to capacity size
56      *
57      * @param index
58      * @return
59      */
60     T& at(int index) {
61         if (index >= N) {
62             THROW_IE_EXCEPTION << "Property index is out of bounds (" << index << "/" << N;
63         }
64         return _axises[index];
65     }
66
67     const T& operator[](size_t index) const {
68         if (index >= N || !_allocated[index]) {
69             THROW_IE_EXCEPTION << "Property index (" << index << ") is out of bounds";
70         }
71         return _axises[index];
72     }
73
74     T& operator[](size_t index) {
75         if (index >= N || !_allocated[index]) {
76             THROW_IE_EXCEPTION << "Property index (" << index << ") is out of bounds";
77         }
78         return _axises[index];
79     }
80
81     PropertyVector& operator=(const PropertyVector& src) {
82         if (this != &src) {
83             _length = src.size();
84             for (size_t i = 0; i < N; i++) {
85                 _allocated[i] = src._allocated[i];
86                 if (_allocated[i]) {
87                     _axises[i] = src[i];
88                 }
89             }
90         }
91         return *this;
92     }
93
94     bool operator==(const PropertyVector& src) const {
95         if (this == &src) return true;
96         if (_length != src.size()) return false;
97         for (size_t i = 0; i < N; i++)
98             if ((_allocated[i] != src._allocated[i]) || (_allocated[i] && _axises[i] != src._axises[i])) return false;
99         return true;
100     }
101
102     size_t size() const {
103         return _length;
104     }
105
106     void insert(size_t axis, const T& val) {
107         if (axis < N) {
108             if (!_allocated[axis]) {
109                 _allocated[axis] = true;
110                 _length++;
111             }
112             _axises[axis] = val;
113         } else {
114             THROW_IE_EXCEPTION << "Layer Property insertion at(axis) should be in [0," << N << ")";
115         }
116     }
117
118     void remove(size_t axis) {
119         if (axis < N && _allocated[axis]) {
120             _allocated[axis] = false;
121             _length--;
122         }
123     }
124
125     void clear() {
126         for (int i = 0; i != N; i++) {
127             _allocated[i] = 0;
128         }
129         _length = 0u;
130     }
131
132     bool exist(size_t axis) const {
133         return (axis < N && _allocated[axis]);
134     }
135 };
136
137 }  // namespace InferenceEngine