Imported Upstream version 1.25.0
[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 copy assignment operator
57    */
58   Set<Element> &operator=(const Set<Element> &) = default;
59   /**
60    * @brief move assignment operator
61    */
62   Set<Element> &operator=(Set<Element> &&) = default;
63
64 public:
65   /**
66    * @brief Add a given element to the set
67    *
68    * @param e Element added
69    */
70   void add(const Element &e) { _set.insert(e); }
71   /**
72    * @brief remove a given element from the set
73    *
74    * @param e Element removed
75    */
76   void remove(const Element &e) { _set.erase(e); }
77   /**
78    * @brief Get size of the set
79    *
80    * @return The size of the set
81    */
82   uint32_t size() const { return static_cast<uint32_t>(_set.size()); }
83   /**
84    * @brief Get whether the set is empty
85    *
86    * @return Whether the set is empty
87    */
88   bool empty() const { return _set.empty(); }
89   /**
90    * @brief Get whether a given element exists in the set
91    *
92    * @param e A given element
93    *
94    * @return Whether a given element exists in the set
95    */
96   bool contains(const Element &e) const { return _set.find(e) != _set.end(); }
97   /**
98    * @brief Get first element of the set
99    *
100    * @return first element of the set
101    */
102   const Element &getOnlyElement() const
103   {
104     assert(_set.size() == 1u);
105     return *_set.begin();
106   }
107
108 public:
109   /**
110    * @brief operator overloading function for `|`
111    *
112    * @return A set with two sets combined
113    */
114   Set<Element> operator|(const Set<Element> &other) const // Union
115   {
116     auto ret = *this;
117     for (auto &&e : other)
118     {
119       ret.add(e);
120     }
121     return ret;
122   }
123   /**
124    * @brief operator overloading function for `&`
125    *
126    * @return A set of elements that overlap in two sets
127    */
128   Set<Element> operator&(const Set<Element> &other) const // Intersect
129   {
130     Set<Element> ret;
131     for (auto &&e : other)
132     {
133       if (contains(e))
134       {
135         ret.add(e);
136       }
137     }
138     return ret;
139   }
140   /**
141    * @brief operator overloading function for `-`
142    *
143    * @return A set of subtracted from another set
144    */
145   Set<Element> operator-(const Set<Element> &other) const // Minus
146   {
147     auto ret = *this;
148     for (auto &&e : other)
149     {
150       ret.remove(e);
151     }
152     return ret;
153   }
154
155 public:
156   /**
157    * @brief begin() of const_iterator for this class
158    *
159    * @return The first iterator of the set
160    */
161   typename std::unordered_set<Element>::const_iterator begin() const { return _set.begin(); }
162   /**
163    * @brief end() of const_iterator for this class
164    *
165    * @return The last iterator of the set
166    */
167   typename std::unordered_set<Element>::const_iterator end() const { return _set.end(); }
168
169 private:
170   std::unordered_set<Element> _set;
171 };
172
173 } // namespace util
174 } // namespace onert
175
176 #endif // __ONERT_UTIL_SET_H__