2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @brief This file contains onert::util::Set class
20 * @ingroup COM_AI_RUNTIME
23 #ifndef __ONERT_UTIL_SET_H__
24 #define __ONERT_UTIL_SET_H__
27 #include <unordered_set>
35 * @brief Class for set of custom element
36 & @tparam Element Key type of Set
38 template <typename Element> class Set
42 * @brief Construct default Set object.
46 * @brief Construct Set object by copy semantics.
48 Set(const Set<Element> &) = default;
50 * @brief Construct move Set object by move semantics.
52 Set(Set<Element> &&) = default;
56 * @brief copy assignment operator
58 Set<Element> &operator=(const Set<Element> &) = default;
60 * @brief move assignment operator
62 Set<Element> &operator=(Set<Element> &&) = default;
66 * @brief Add a given element to the set
68 * @param e Element added
70 void add(const Element &e) { _set.insert(e); }
72 * @brief remove a given element from the set
74 * @param e Element removed
76 void remove(const Element &e) { _set.erase(e); }
78 * @brief Get size of the set
80 * @return The size of the set
82 uint32_t size() const { return static_cast<uint32_t>(_set.size()); }
84 * @brief Get whether the set is empty
86 * @return Whether the set is empty
88 bool empty() const { return _set.empty(); }
90 * @brief Get whether a given element exists in the set
92 * @param e A given element
94 * @return Whether a given element exists in the set
96 bool contains(const Element &e) const { return _set.find(e) != _set.end(); }
98 * @brief Get first element of the set
100 * @return first element of the set
102 const Element &getOnlyElement() const
104 assert(_set.size() == 1u);
105 return *_set.begin();
110 * @brief operator overloading function for `|`
112 * @return A set with two sets combined
114 Set<Element> operator|(const Set<Element> &other) const // Union
117 for (auto &&e : other)
124 * @brief operator overloading function for `&`
126 * @return A set of elements that overlap in two sets
128 Set<Element> operator&(const Set<Element> &other) const // Intersect
131 for (auto &&e : other)
141 * @brief operator overloading function for `-`
143 * @return A set of subtracted from another set
145 Set<Element> operator-(const Set<Element> &other) const // Minus
148 for (auto &&e : other)
157 * @brief begin() of const_iterator for this class
159 * @return The first iterator of the set
161 typename std::unordered_set<Element>::const_iterator begin() const { return _set.begin(); }
163 * @brief end() of const_iterator for this class
165 * @return The last iterator of the set
167 typename std::unordered_set<Element>::const_iterator end() const { return _set.end(); }
170 std::unordered_set<Element> _set;
176 #endif // __ONERT_UTIL_SET_H__