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