LLVM  4.0.0
DenseSet.h
Go to the documentation of this file.
1 //===- llvm/ADT/DenseSet.h - Dense probed hash table ------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the DenseSet and SmallDenseSet classes.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_ADT_DENSESET_H
15 #define LLVM_ADT_DENSESET_H
16 
17 #include "llvm/ADT/DenseMap.h"
18 #include <initializer_list>
19 
20 namespace llvm {
21 
22 namespace detail {
23 struct DenseSetEmpty {};
24 
25 // Use the empty base class trick so we can create a DenseMap where the buckets
26 // contain only a single item.
27 template <typename KeyT> class DenseSetPair : public DenseSetEmpty {
28  KeyT key;
29 
30 public:
31  KeyT &getFirst() { return key; }
32  const KeyT &getFirst() const { return key; }
33  DenseSetEmpty &getSecond() { return *this; }
34  const DenseSetEmpty &getSecond() const { return *this; }
35 };
36 
37 /// Base class for DenseSet and DenseSmallSet.
38 ///
39 /// MapTy should be either
40 ///
41 /// DenseMap<ValueT, detail::DenseSetEmpty, ValueInfoT,
42 /// detail::DenseSetPair<ValueT>>
43 ///
44 /// or the equivalent SmallDenseMap type. ValueInfoT must implement the
45 /// DenseMapInfo "concept".
46 template <typename ValueT, typename MapTy, typename ValueInfoT>
47 class DenseSetImpl {
48  static_assert(sizeof(typename MapTy::value_type) == sizeof(ValueT),
49  "DenseMap buckets unexpectedly large!");
50  MapTy TheMap;
51 
52 public:
53  typedef ValueT key_type;
54  typedef ValueT value_type;
55  typedef unsigned size_type;
56 
57  explicit DenseSetImpl(unsigned InitialReserve = 0) : TheMap(InitialReserve) {}
58 
59  DenseSetImpl(std::initializer_list<ValueT> Elems)
60  : DenseSetImpl(Elems.size()) {
61  insert(Elems.begin(), Elems.end());
62  }
63 
64  bool empty() const { return TheMap.empty(); }
65  size_type size() const { return TheMap.size(); }
66  size_t getMemorySize() const { return TheMap.getMemorySize(); }
67 
68  /// Grow the DenseSet so that it has at least Size buckets. Will not shrink
69  /// the Size of the set.
70  void resize(size_t Size) { TheMap.resize(Size); }
71 
72  /// Grow the DenseSet so that it can contain at least \p NumEntries items
73  /// before resizing again.
74  void reserve(size_t Size) { TheMap.reserve(Size); }
75 
76  void clear() {
77  TheMap.clear();
78  }
79 
80  /// Return 1 if the specified key is in the set, 0 otherwise.
81  size_type count(const ValueT &V) const {
82  return TheMap.count(V);
83  }
84 
85  bool erase(const ValueT &V) {
86  return TheMap.erase(V);
87  }
88 
89  void swap(DenseSetImpl &RHS) { TheMap.swap(RHS.TheMap); }
90 
91  // Iterators.
92 
93  class Iterator {
94  typename MapTy::iterator I;
95  friend class DenseSetImpl;
96 
97  public:
98  typedef typename MapTy::iterator::difference_type difference_type;
99  typedef ValueT value_type;
100  typedef value_type *pointer;
102  typedef std::forward_iterator_tag iterator_category;
103 
104  Iterator(const typename MapTy::iterator &i) : I(i) {}
105 
106  ValueT &operator*() { return I->getFirst(); }
107  ValueT *operator->() { return &I->getFirst(); }
108 
109  Iterator& operator++() { ++I; return *this; }
110  Iterator operator++(int) { auto T = *this; ++I; return T; }
111  bool operator==(const Iterator& X) const { return I == X.I; }
112  bool operator!=(const Iterator& X) const { return I != X.I; }
113  };
114 
116  typename MapTy::const_iterator I;
117  friend class DenseSet;
118 
119  public:
120  typedef typename MapTy::const_iterator::difference_type difference_type;
122  typedef value_type *pointer;
124  typedef std::forward_iterator_tag iterator_category;
125 
126  ConstIterator(const typename MapTy::const_iterator &i) : I(i) {}
127 
128  const ValueT &operator*() { return I->getFirst(); }
129  const ValueT *operator->() { return &I->getFirst(); }
130 
131  ConstIterator& operator++() { ++I; return *this; }
132  ConstIterator operator++(int) { auto T = *this; ++I; return T; }
133  bool operator==(const ConstIterator& X) const { return I == X.I; }
134  bool operator!=(const ConstIterator& X) const { return I != X.I; }
135  };
136 
137  typedef Iterator iterator;
138  typedef ConstIterator const_iterator;
139 
140  iterator begin() { return Iterator(TheMap.begin()); }
141  iterator end() { return Iterator(TheMap.end()); }
142 
143  const_iterator begin() const { return ConstIterator(TheMap.begin()); }
144  const_iterator end() const { return ConstIterator(TheMap.end()); }
145 
146  iterator find(const ValueT &V) { return Iterator(TheMap.find(V)); }
147  const_iterator find(const ValueT &V) const {
148  return ConstIterator(TheMap.find(V));
149  }
150 
151  /// Alternative version of find() which allows a different, and possibly less
152  /// expensive, key type.
153  /// The DenseMapInfo is responsible for supplying methods
154  /// getHashValue(LookupKeyT) and isEqual(LookupKeyT, KeyT) for each key type
155  /// used.
156  template <class LookupKeyT>
157  iterator find_as(const LookupKeyT &Val) {
158  return Iterator(TheMap.find_as(Val));
159  }
160  template <class LookupKeyT>
161  const_iterator find_as(const LookupKeyT &Val) const {
162  return ConstIterator(TheMap.find_as(Val));
163  }
164 
165  void erase(Iterator I) { return TheMap.erase(I.I); }
166  void erase(ConstIterator CI) { return TheMap.erase(CI.I); }
167 
168  std::pair<iterator, bool> insert(const ValueT &V) {
170  return TheMap.try_emplace(V, Empty);
171  }
172 
173  std::pair<iterator, bool> insert(ValueT &&V) {
175  return TheMap.try_emplace(std::move(V), Empty);
176  }
177 
178  /// Alternative version of insert that uses a different (and possibly less
179  /// expensive) key type.
180  template <typename LookupKeyT>
181  std::pair<iterator, bool> insert_as(const ValueT &V,
182  const LookupKeyT &LookupKey) {
183  return TheMap.insert_as({V, detail::DenseSetEmpty()}, LookupKey);
184  }
185  template <typename LookupKeyT>
186  std::pair<iterator, bool> insert_as(ValueT &&V, const LookupKeyT &LookupKey) {
187  return TheMap.insert_as({std::move(V), detail::DenseSetEmpty()}, LookupKey);
188  }
189 
190  // Range insertion of values.
191  template<typename InputIt>
192  void insert(InputIt I, InputIt E) {
193  for (; I != E; ++I)
194  insert(*I);
195  }
196 };
197 
198 } // namespace detail
199 
200 /// Implements a dense probed hash-table based set.
201 template <typename ValueT, typename ValueInfoT = DenseMapInfo<ValueT>>
203  ValueT, DenseMap<ValueT, detail::DenseSetEmpty, ValueInfoT,
204  detail::DenseSetPair<ValueT>>,
205  ValueInfoT> {
206  using BaseT =
207  detail::DenseSetImpl<ValueT,
208  DenseMap<ValueT, detail::DenseSetEmpty, ValueInfoT,
210  ValueInfoT>;
211 
212 public:
213  using BaseT::BaseT;
214 };
215 
216 /// Implements a dense probed hash-table based set with some number of buckets
217 /// stored inline.
218 template <typename ValueT, unsigned InlineBuckets = 4,
219  typename ValueInfoT = DenseMapInfo<ValueT>>
221  : public detail::DenseSetImpl<
222  ValueT, SmallDenseMap<ValueT, detail::DenseSetEmpty, InlineBuckets,
223  ValueInfoT, detail::DenseSetPair<ValueT>>,
224  ValueInfoT> {
225  using BaseT = detail::DenseSetImpl<
227  ValueInfoT, detail::DenseSetPair<ValueT>>,
228  ValueInfoT>;
229 
230 public:
231  using BaseT::BaseT;
232 };
233 
234 } // end namespace llvm
235 
236 #endif
size_t getMemorySize() const
Definition: DenseSet.h:66
size_t i
const DenseSetEmpty & getSecond() const
Definition: DenseSet.h:34
iterator find(const ValueT &V)
Definition: DenseSet.h:146
Implements a dense probed hash-table based set.
Definition: DenseSet.h:202
const_iterator find_as(const LookupKeyT &Val) const
Definition: DenseSet.h:161
bool erase(const ValueT &V)
Definition: DenseSet.h:85
std::pair< iterator, bool > insert_as(ValueT &&V, const LookupKeyT &LookupKey)
Definition: DenseSet.h:186
bool empty() const
Definition: DenseSet.h:64
std::forward_iterator_tag iterator_category
Definition: DenseSet.h:124
ConstIterator const_iterator
Definition: DenseSet.h:138
#define T
DenseSetEmpty & getSecond()
Definition: DenseSet.h:33
MapTy::iterator::difference_type difference_type
Definition: DenseSet.h:98
Iterator(const typename MapTy::iterator &i)
Definition: DenseSet.h:104
bool operator==(const Iterator &X) const
Definition: DenseSet.h:111
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
std::pair< iterator, bool > insert_as(const ValueT &V, const LookupKeyT &LookupKey)
Alternative version of insert that uses a different (and possibly less expensive) key type...
Definition: DenseSet.h:181
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:168
iterator find_as(const LookupKeyT &Val)
Alternative version of find() which allows a different, and possibly less expensive, key type.
Definition: DenseSet.h:157
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
std::pair< iterator, bool > insert(ValueT &&V)
Definition: DenseSet.h:173
void erase(Iterator I)
Definition: DenseSet.h:165
void insert(InputIt I, InputIt E)
Definition: DenseSet.h:192
void swap(DenseSetImpl &RHS)
Definition: DenseSet.h:89
bool operator!=(const ConstIterator &X) const
Definition: DenseSet.h:134
std::forward_iterator_tag iterator_category
Definition: DenseSet.h:102
const_iterator end() const
Definition: DenseSet.h:144
MapTy::const_iterator::difference_type difference_type
Definition: DenseSet.h:120
DenseSetImpl(std::initializer_list< ValueT > Elems)
Definition: DenseSet.h:59
DenseSetImpl(unsigned InitialReserve=0)
Definition: DenseSet.h:57
const_iterator find(const ValueT &V) const
Definition: DenseSet.h:147
const KeyT & getFirst() const
Definition: DenseSet.h:32
const_iterator begin() const
Definition: DenseSet.h:143
Implements a dense probed hash-table based set with some number of buckets stored inline...
Definition: DenseSet.h:220
void erase(ConstIterator CI)
Definition: DenseSet.h:166
void resize(size_t Size)
Grow the DenseSet so that it has at least Size buckets.
Definition: DenseSet.h:70
ConstIterator(const typename MapTy::const_iterator &i)
Definition: DenseSet.h:126
bool operator!=(const Iterator &X) const
Definition: DenseSet.h:112
size_type count(const ValueT &V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition: DenseSet.h:81
#define I(x, y, z)
Definition: MD5.cpp:54
void reserve(size_t Size)
Grow the DenseSet so that it can contain at least NumEntries items before resizing again...
Definition: DenseSet.h:74
bool operator==(const ConstIterator &X) const
Definition: DenseSet.h:133
size_type size() const
Definition: DenseSet.h:65
Base class for DenseSet and DenseSmallSet.
Definition: DenseSet.h:47