ee4062d25c503bd4c5e0ba20917c19beaf230e12
[platform/core/ml/nnfw.git] / runtime / onert / core / include / util / Set.h
1 /*
2  * Copyright (c) 2019 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 /**
18  * @file     Set.h
19  * @brief    This file contains onert::util::Set class
20  * @ingroup  COM_AI_RUNTIME
21  */
22
23 #ifndef __ONERT_UTIL_SET_H__
24 #define __ONERT_UTIL_SET_H__
25
26 #include <cassert>
27 #include <unordered_set>
28
29 namespace onert
30 {
31 namespace util
32 {
33
34 /**
35  * @brief Class for set of custom element
36  & @tparam Element  Key type of Set
37  */
38 template <typename Element> class Set
39 {
40 public:
41   /**
42    * @brief Construct default Set object.
43    */
44   Set() = default;
45   /**
46    * @brief Construct Set object by copy semantics.
47    */
48   Set(const Set<Element> &) = default;
49   /**
50    * @brief Construct move Set object by move semantics.
51    */
52   Set(Set<Element> &&) = default;
53
54 public:
55   /**
56    * @brief Add a given element to the set
57    *
58    * @param e Element added
59    */
60   void add(const Element &e) { _set.insert(e); }
61   /**
62    * @brief remove a given element from the set
63    *
64    * @param e Element removed
65    */
66   void remove(const Element &e) { _set.erase(e); }
67   /**
68    * @brief Get size of the set
69    *
70    * @return The size of the set
71    */
72   uint32_t size() const { return static_cast<uint32_t>(_set.size()); }
73   /**
74    * @brief Get whether the set is empty
75    *
76    * @return Whether the set is empty
77    */
78   bool empty() const { return _set.empty(); }
79   /**
80    * @brief Get whether a given element exists in the set
81    *
82    * @param e A given element
83    *
84    * @return Whether a given element exists in the set
85    */
86   bool contains(const Element &e) const { return _set.find(e) != _set.end(); }
87   /**
88    * @brief Get first element of the set
89    *
90    * @return first element of the set
91    */
92   const Element &getOnlyElement() const
93   {
94     assert(_set.size() == 1u);
95     return *_set.begin();
96   }
97
98 public:
99   /**
100    * @brief operator overloading function for `|`
101    *
102    * @return A set with two sets combined
103    */
104   Set<Element> operator|(const Set<Element> &other) const // Union
105   {
106     auto ret = *this;
107     for (auto e : other)
108     {
109       ret.add(e);
110     }
111     return ret;
112   }
113   /**
114    * @brief operator overloading function for `&`
115    *
116    * @return A set of elements that overlap in two sets
117    */
118   Set<Element> operator&(const Set<Element> &other) const // Intersect
119   {
120     Set<Element> ret;
121     for (auto e : other)
122     {
123       if (contains(e))
124       {
125         ret.add(e);
126       }
127     }
128     return ret;
129   }
130   /**
131    * @brief operator overloading function for `-`
132    *
133    * @return A set of subtracted from another set
134    */
135   Set<Element> operator-(const Set<Element> &other) const // Minus
136   {
137     auto ret = *this;
138     for (auto e : other)
139     {
140       ret.remove(e);
141     }
142     return ret;
143   }
144
145 public:
146   /**
147    * @brief begin() of const_iterator for this class
148    *
149    * @return The first iterator of the set
150    */
151   typename std::unordered_set<Element>::const_iterator begin() const { return _set.begin(); }
152   /**
153    * @brief end() of const_iterator for this class
154    *
155    * @return The last iterator of the set
156    */
157   typename std::unordered_set<Element>::const_iterator end() const { return _set.end(); }
158
159 private:
160   std::unordered_set<Element> _set;
161 };
162
163 } // namespace util
164 } // namespace onert
165
166 #endif // __ONERT_UTIL_SET_H__