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