ie_layers_property.hpp
1 // Copyright (C) 2018 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>
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  /**
51  * @brief allows access up-to capacity size
52  * @param index
53  * @return
54  */
55  T &at(int index) {
56  if (index >= N) {
57  THROW_IE_EXCEPTION << "Property index is out of bounds(" << index << "/" << N;
58  }
59  return _axises[index];
60  }
61 
62  const T &operator[](size_t index) const {
63  if (index >= N ||!_allocated[index]) {
64  THROW_IE_EXCEPTION << "Property index ("<< index <<")is out of bounds";
65  }
66  return _axises[index];
67  }
68 
69  T &operator[](size_t index) {
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  PropertyVector &operator=(const PropertyVector &src) {
77  if (this != &src) {
78  _length = src.size();
79  for (size_t i = 0; i < N; i++) {
80  _allocated[i] = src._allocated[i];
81  if (_allocated[i]) {
82  _axises[i] = src[i];
83  }
84  }
85  }
86  return *this;
87  }
88 
89  bool operator==(const PropertyVector& src) const {
90  if (this == &src) return true;
91  if (_length != src.size()) return false;
92  for (size_t i = 0; i < N; i++)
93  if ((_allocated[i] != src._allocated[i]) ||
94  (_allocated[i] && _axises[i] != src._axises[i])) return false;
95  return true;
96  }
97 
98  size_t size() const {
99  return _length;
100  }
101 
102  void insert(size_t axis, const T &val) {
103  if (axis < N) {
104  if (!_allocated[axis]) {
105  _allocated[axis] = true;
106  _length++;
107  }
108  _axises[axis] = val;
109  } else {
110  THROW_IE_EXCEPTION << "Layer Property insertion at(axis) should be in [0,"<< N<< ")";
111  }
112  }
113 
114  void remove(size_t axis) {
115  if (axis < N && _allocated[axis]) {
116  _allocated[axis] = false;
117  _length--;
118  }
119  }
120 
121  void clear() {
122  for (int i = 0; i != N; i++) {
123  _allocated[i] = 0;
124  }
125  _length = 0u;
126  }
127 
128  bool exist(size_t axis) const {
129  return (axis < N && _allocated[axis]);
130  }
131 };
132 
133 } // namespace InferenceEngine
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:22
Definition: ie_argmax_layer.hpp:11
Definition: ie_layers_property.hpp:24
T & at(int index)
allows access up-to capacity size
Definition: ie_layers_property.hpp:55