SmallSet.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/ADT/SmallSet.h - 'Normally small' sets --------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the SmallSet class.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_ADT_SMALLSET_H
  18. #define LLVM_ADT_SMALLSET_H
  19. #include "llvm/ADT/None.h"
  20. #include "llvm/ADT/SmallPtrSet.h"
  21. #include "llvm/ADT/SmallVector.h"
  22. #include "llvm/ADT/iterator.h"
  23. #include "llvm/Support/Compiler.h"
  24. #include "llvm/Support/type_traits.h"
  25. #include <cstddef>
  26. #include <functional>
  27. #include <set>
  28. #include <type_traits>
  29. #include <utility>
  30. namespace llvm {
  31. /// SmallSetIterator - This class implements a const_iterator for SmallSet by
  32. /// delegating to the underlying SmallVector or Set iterators.
  33. template <typename T, unsigned N, typename C>
  34. class SmallSetIterator
  35. : public iterator_facade_base<SmallSetIterator<T, N, C>,
  36. std::forward_iterator_tag, T> {
  37. private:
  38. using SetIterTy = typename std::set<T, C>::const_iterator;
  39. using VecIterTy = typename SmallVector<T, N>::const_iterator;
  40. using SelfTy = SmallSetIterator<T, N, C>;
  41. /// Iterators to the parts of the SmallSet containing the data. They are set
  42. /// depending on isSmall.
  43. union {
  44. SetIterTy SetIter;
  45. VecIterTy VecIter;
  46. };
  47. bool isSmall;
  48. public:
  49. SmallSetIterator(SetIterTy SetIter) : SetIter(SetIter), isSmall(false) {}
  50. SmallSetIterator(VecIterTy VecIter) : VecIter(VecIter), isSmall(true) {}
  51. // Spell out destructor, copy/move constructor and assignment operators for
  52. // MSVC STL, where set<T>::const_iterator is not trivially copy constructible.
  53. ~SmallSetIterator() {
  54. if (isSmall)
  55. VecIter.~VecIterTy();
  56. else
  57. SetIter.~SetIterTy();
  58. }
  59. SmallSetIterator(const SmallSetIterator &Other) : isSmall(Other.isSmall) {
  60. if (isSmall)
  61. VecIter = Other.VecIter;
  62. else
  63. // Use placement new, to make sure SetIter is properly constructed, even
  64. // if it is not trivially copy-able (e.g. in MSVC).
  65. new (&SetIter) SetIterTy(Other.SetIter);
  66. }
  67. SmallSetIterator(SmallSetIterator &&Other) : isSmall(Other.isSmall) {
  68. if (isSmall)
  69. VecIter = std::move(Other.VecIter);
  70. else
  71. // Use placement new, to make sure SetIter is properly constructed, even
  72. // if it is not trivially copy-able (e.g. in MSVC).
  73. new (&SetIter) SetIterTy(std::move(Other.SetIter));
  74. }
  75. SmallSetIterator& operator=(const SmallSetIterator& Other) {
  76. // Call destructor for SetIter, so it gets properly destroyed if it is
  77. // not trivially destructible in case we are setting VecIter.
  78. if (!isSmall)
  79. SetIter.~SetIterTy();
  80. isSmall = Other.isSmall;
  81. if (isSmall)
  82. VecIter = Other.VecIter;
  83. else
  84. new (&SetIter) SetIterTy(Other.SetIter);
  85. return *this;
  86. }
  87. SmallSetIterator& operator=(SmallSetIterator&& Other) {
  88. // Call destructor for SetIter, so it gets properly destroyed if it is
  89. // not trivially destructible in case we are setting VecIter.
  90. if (!isSmall)
  91. SetIter.~SetIterTy();
  92. isSmall = Other.isSmall;
  93. if (isSmall)
  94. VecIter = std::move(Other.VecIter);
  95. else
  96. new (&SetIter) SetIterTy(std::move(Other.SetIter));
  97. return *this;
  98. }
  99. bool operator==(const SmallSetIterator &RHS) const {
  100. if (isSmall != RHS.isSmall)
  101. return false;
  102. if (isSmall)
  103. return VecIter == RHS.VecIter;
  104. return SetIter == RHS.SetIter;
  105. }
  106. SmallSetIterator &operator++() { // Preincrement
  107. if (isSmall)
  108. VecIter++;
  109. else
  110. SetIter++;
  111. return *this;
  112. }
  113. const T &operator*() const { return isSmall ? *VecIter : *SetIter; }
  114. };
  115. /// SmallSet - This maintains a set of unique values, optimizing for the case
  116. /// when the set is small (less than N). In this case, the set can be
  117. /// maintained with no mallocs. If the set gets large, we expand to using an
  118. /// std::set to maintain reasonable lookup times.
  119. template <typename T, unsigned N, typename C = std::less<T>>
  120. class SmallSet {
  121. /// Use a SmallVector to hold the elements here (even though it will never
  122. /// reach its 'large' stage) to avoid calling the default ctors of elements
  123. /// we will never use.
  124. SmallVector<T, N> Vector;
  125. std::set<T, C> Set;
  126. using VIterator = typename SmallVector<T, N>::const_iterator;
  127. using mutable_iterator = typename SmallVector<T, N>::iterator;
  128. // In small mode SmallPtrSet uses linear search for the elements, so it is
  129. // not a good idea to choose this value too high. You may consider using a
  130. // DenseSet<> instead if you expect many elements in the set.
  131. static_assert(N <= 32, "N should be small");
  132. public:
  133. using size_type = size_t;
  134. using const_iterator = SmallSetIterator<T, N, C>;
  135. SmallSet() = default;
  136. LLVM_NODISCARD bool empty() const {
  137. return Vector.empty() && Set.empty();
  138. }
  139. size_type size() const {
  140. return isSmall() ? Vector.size() : Set.size();
  141. }
  142. /// count - Return 1 if the element is in the set, 0 otherwise.
  143. size_type count(const T &V) const {
  144. if (isSmall()) {
  145. // Since the collection is small, just do a linear search.
  146. return vfind(V) == Vector.end() ? 0 : 1;
  147. } else {
  148. return Set.count(V);
  149. }
  150. }
  151. /// insert - Insert an element into the set if it isn't already there.
  152. /// Returns true if the element is inserted (it was not in the set before).
  153. /// The first value of the returned pair is unused and provided for
  154. /// partial compatibility with the standard library self-associative container
  155. /// concept.
  156. // FIXME: Add iterators that abstract over the small and large form, and then
  157. // return those here.
  158. std::pair<NoneType, bool> insert(const T &V) {
  159. if (!isSmall())
  160. return std::make_pair(None, Set.insert(V).second);
  161. VIterator I = vfind(V);
  162. if (I != Vector.end()) // Don't reinsert if it already exists.
  163. return std::make_pair(None, false);
  164. if (Vector.size() < N) {
  165. Vector.push_back(V);
  166. return std::make_pair(None, true);
  167. }
  168. // Otherwise, grow from vector to set.
  169. while (!Vector.empty()) {
  170. Set.insert(Vector.back());
  171. Vector.pop_back();
  172. }
  173. Set.insert(V);
  174. return std::make_pair(None, true);
  175. }
  176. template <typename IterT>
  177. void insert(IterT I, IterT E) {
  178. for (; I != E; ++I)
  179. insert(*I);
  180. }
  181. bool erase(const T &V) {
  182. if (!isSmall())
  183. return Set.erase(V);
  184. for (mutable_iterator I = Vector.begin(), E = Vector.end(); I != E; ++I)
  185. if (*I == V) {
  186. Vector.erase(I);
  187. return true;
  188. }
  189. return false;
  190. }
  191. void clear() {
  192. Vector.clear();
  193. Set.clear();
  194. }
  195. const_iterator begin() const {
  196. if (isSmall())
  197. return {Vector.begin()};
  198. return {Set.begin()};
  199. }
  200. const_iterator end() const {
  201. if (isSmall())
  202. return {Vector.end()};
  203. return {Set.end()};
  204. }
  205. /// Check if the SmallSet contains the given element.
  206. bool contains(const T &V) const {
  207. if (isSmall())
  208. return vfind(V) != Vector.end();
  209. return Set.find(V) != Set.end();
  210. }
  211. private:
  212. bool isSmall() const { return Set.empty(); }
  213. VIterator vfind(const T &V) const {
  214. for (VIterator I = Vector.begin(), E = Vector.end(); I != E; ++I)
  215. if (*I == V)
  216. return I;
  217. return Vector.end();
  218. }
  219. };
  220. /// If this set is of pointer values, transparently switch over to using
  221. /// SmallPtrSet for performance.
  222. template <typename PointeeType, unsigned N>
  223. class SmallSet<PointeeType*, N> : public SmallPtrSet<PointeeType*, N> {};
  224. /// Equality comparison for SmallSet.
  225. ///
  226. /// Iterates over elements of LHS confirming that each element is also a member
  227. /// of RHS, and that RHS contains no additional values.
  228. /// Equivalent to N calls to RHS.count.
  229. /// For small-set mode amortized complexity is O(N^2)
  230. /// For large-set mode amortized complexity is linear, worst case is O(N^2) (if
  231. /// every hash collides).
  232. template <typename T, unsigned LN, unsigned RN, typename C>
  233. bool operator==(const SmallSet<T, LN, C> &LHS, const SmallSet<T, RN, C> &RHS) {
  234. if (LHS.size() != RHS.size())
  235. return false;
  236. // All elements in LHS must also be in RHS
  237. return all_of(LHS, [&RHS](const T &E) { return RHS.count(E); });
  238. }
  239. /// Inequality comparison for SmallSet.
  240. ///
  241. /// Equivalent to !(LHS == RHS). See operator== for performance notes.
  242. template <typename T, unsigned LN, unsigned RN, typename C>
  243. bool operator!=(const SmallSet<T, LN, C> &LHS, const SmallSet<T, RN, C> &RHS) {
  244. return !(LHS == RHS);
  245. }
  246. } // end namespace llvm
  247. #endif // LLVM_ADT_SMALLSET_H
  248. #ifdef __GNUC__
  249. #pragma GCC diagnostic pop
  250. #endif