Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / runtime / onert / core / include / util / Index.h
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef __ONERT_UTIL_INDEX_H__
18 #define __ONERT_UTIL_INDEX_H__
19
20 #include <functional>
21 #include <limits>
22 #include <stdint.h>
23 #include <string>
24
25 namespace onert
26 {
27 namespace util
28 {
29
30 /**
31  * @brief A wrapper class for unsigned integral Index
32  *        NOTE : Max value of the underlying type is used as the invalid value
33  *
34  * @tparam T Underlying type. Must be unsigned integral type otherwise its behavior is undefined.
35  * @tparam DummyTag Dummy type to distinguish types with a same underlying type. Using an opaque
36  * type is recommended.
37  */
38 template <typename T, typename DummyTag> class Index
39 {
40 private:
41   static const T UNDEFINED = std::numeric_limits<T>::max();
42
43 public:
44   /**
45    * @brief Construct a new Index object
46    */
47   explicit Index(void) : _index{UNDEFINED} {}
48   /**
49    * @brief Construct a new Index object with a value in the underlying type
50    *
51    * @param o Value in the underlying type
52    */
53   explicit Index(const T o) : _index{o} {}
54   /**
55    * @brief Copy Constructor
56    *
57    * @param o Object to be copied
58    */
59   Index(const Index &o) = default;
60
61   /**
62    * @brief Assign a value in the underlying time
63    *
64    * @param o Value in the underlying type
65    * @return Index& Reference of this pointer
66    */
67   Index &operator=(const T o)
68   {
69     _index = o;
70     return *this;
71   }
72
73   /**
74    * @brief Copy assignment operator
75    *
76    * @param o Object to be copied
77    * @return Index& Reference of this pointer
78    */
79   Index &operator=(const Index &o) = default;
80
81   /**
82    * @brief Equality operator
83    *
84    * @param o The other value in the underlying type to compare
85    * @return true if underlying value is the same, false otherwise
86    */
87   bool operator==(T o) const { return _index == o; }
88   /**
89    * @brief Equality operator
90    *
91    * @param o The other object to compare
92    * @return true if underlying value is the same, false otherwise
93    */
94   bool operator==(const Index &o) const { return _index == o._index; }
95   /**
96    * @brief Inquality operator
97    *
98    * @param o The other value in the underlying type to compare
99    * @return true if underlying value is different, false otherwise
100    */
101   bool operator!=(T o) const { return !(*this == o); }
102   /**
103    * @brief Inquality operator
104    *
105    * @param o The other object to compare
106    * @return true if underlying value is different, false otherwise
107    */
108   bool operator!=(const Index &o) const { return !(*this == o); }
109
110   /**
111    * @brief Post increment operator
112    *
113    * @return Index Index before increment
114    */
115   Index operator++(int)
116   {
117     Index temp = *this;
118     _index++;
119     return temp;
120   }
121
122   /**
123    * @brief Check whether the value is valid or not
124    *
125    * @return true if valid, false otherwise
126    */
127   bool valid() const { return _index != UNDEFINED; }
128   /**
129    * @brief Check whether the value is undefined
130    *
131    * @return true if undefined, false otherwise
132    */
133   bool undefined() const { return _index == UNDEFINED; }
134   /**
135    * @brief Return underlying value
136    *
137    * @return T Underlying value
138    */
139   T value() const { return _index; }
140
141   friend std::ostream &operator<<(std::ostream &o, const Index &t)
142   {
143     if (t.undefined())
144       return o << std::string("undefined");
145     else
146       return o << t.value();
147   }
148
149 private:
150   T _index;
151 };
152
153 } // namespace util
154 } // namespace onert
155
156 namespace std
157 {
158
159 template <typename T, typename Tag> struct hash<::onert::util::Index<T, Tag>>
160 {
161   size_t operator()(const ::onert::util::Index<T, Tag> &index) const noexcept
162   {
163     return hash<T>()(index.value());
164   }
165 };
166
167 } // namespace std
168
169 #endif // __ONERT_UTIL_INDEX_H__