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 Add a given element to the set
58 * @param e Element added
60 void add(const Element &e) { _set.insert(e); }
62 * @brief remove a given element from the set
64 * @param e Element removed
66 void remove(const Element &e) { _set.erase(e); }
68 * @brief Get size of the set
70 * @return The size of the set
72 uint32_t size() const { return static_cast<uint32_t>(_set.size()); }
74 * @brief Get whether the set is empty
76 * @return Whether the set is empty
78 bool empty() const { return _set.empty(); }
80 * @brief Get whether a given element exists in the set
82 * @param e A given element
84 * @return Whether a given element exists in the set
86 bool contains(const Element &e) const { return _set.find(e) != _set.end(); }
88 * @brief Get first element of the set
90 * @return first element of the set
92 const Element &getOnlyElement() const
94 assert(_set.size() == 1u);
100 * @brief operator overloading function for `|`
102 * @return A set with two sets combined
104 Set<Element> operator|(const Set<Element> &other) const // Union
114 * @brief operator overloading function for `&`
116 * @return A set of elements that overlap in two sets
118 Set<Element> operator&(const Set<Element> &other) const // Intersect
131 * @brief operator overloading function for `-`
133 * @return A set of subtracted from another set
135 Set<Element> operator-(const Set<Element> &other) const // Minus
147 * @brief begin() of const_iterator for this class
149 * @return The first iterator of the set
151 typename std::unordered_set<Element>::const_iterator begin() const { return _set.begin(); }
153 * @brief end() of const_iterator for this class
155 * @return The last iterator of the set
157 typename std::unordered_set<Element>::const_iterator end() const { return _set.end(); }
160 std::unordered_set<Element> _set;
166 #endif // __ONERT_UTIL_SET_H__