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