LLVM  3.7.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 class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_ADT_DENSESET_H
15 #define LLVM_ADT_DENSESET_H
16 
17 #include "llvm/ADT/DenseMap.h"
18 
19 namespace llvm {
20 
21 namespace detail {
22 struct DenseSetEmpty {};
23 
24 // Use the empty base class trick so we can create a DenseMap where the buckets
25 // contain only a single item.
26 template <typename KeyT> class DenseSetPair : public DenseSetEmpty {
27  KeyT key;
28 
29 public:
30  KeyT &getFirst() { return key; }
31  const KeyT &getFirst() const { return key; }
32  DenseSetEmpty &getSecond() { return *this; }
33  const DenseSetEmpty &getSecond() const { return *this; }
34 };
35 }
36 
37 /// DenseSet - This implements a dense probed hash-table based set.
38 template<typename ValueT, typename ValueInfoT = DenseMapInfo<ValueT> >
39 class DenseSet {
40  typedef DenseMap<ValueT, detail::DenseSetEmpty, ValueInfoT,
42  static_assert(sizeof(typename MapTy::value_type) == sizeof(ValueT),
43  "DenseMap buckets unexpectedly large!");
44  MapTy TheMap;
45 public:
46  typedef ValueT key_type;
47  typedef ValueT value_type;
48  typedef unsigned size_type;
49 
50  explicit DenseSet(unsigned NumInitBuckets = 0) : TheMap(NumInitBuckets) {}
51 
52  bool empty() const { return TheMap.empty(); }
53  size_type size() const { return TheMap.size(); }
54  size_t getMemorySize() const { return TheMap.getMemorySize(); }
55 
56  /// Grow the DenseSet so that it has at least Size buckets. Will not shrink
57  /// the Size of the set.
58  void resize(size_t Size) { TheMap.resize(Size); }
59 
60  void clear() {
61  TheMap.clear();
62  }
63 
64  /// Return 1 if the specified key is in the set, 0 otherwise.
65  size_type count(const ValueT &V) const {
66  return TheMap.count(V);
67  }
68 
69  bool erase(const ValueT &V) {
70  return TheMap.erase(V);
71  }
72 
73  void swap(DenseSet& RHS) {
74  TheMap.swap(RHS.TheMap);
75  }
76 
77  // Iterators.
78 
79  class Iterator {
80  typename MapTy::iterator I;
81  friend class DenseSet;
82  public:
84  typedef ValueT value_type;
85  typedef value_type *pointer;
87  typedef std::forward_iterator_tag iterator_category;
88 
89  Iterator(const typename MapTy::iterator &i) : I(i) {}
90 
91  ValueT &operator*() { return I->getFirst(); }
92  ValueT *operator->() { return &I->getFirst(); }
93 
94  Iterator& operator++() { ++I; return *this; }
95  bool operator==(const Iterator& X) const { return I == X.I; }
96  bool operator!=(const Iterator& X) const { return I != X.I; }
97  };
98 
99  class ConstIterator {
100  typename MapTy::const_iterator I;
101  friend class DenseSet;
102  public:
104  typedef ValueT value_type;
105  typedef value_type *pointer;
107  typedef std::forward_iterator_tag iterator_category;
108 
109  ConstIterator(const typename MapTy::const_iterator &i) : I(i) {}
110 
111  const ValueT &operator*() { return I->getFirst(); }
112  const ValueT *operator->() { return &I->getFirst(); }
113 
114  ConstIterator& operator++() { ++I; return *this; }
115  bool operator==(const ConstIterator& X) const { return I == X.I; }
116  bool operator!=(const ConstIterator& X) const { return I != X.I; }
117  };
118 
119  typedef Iterator iterator;
120  typedef ConstIterator const_iterator;
121 
122  iterator begin() { return Iterator(TheMap.begin()); }
123  iterator end() { return Iterator(TheMap.end()); }
124 
125  const_iterator begin() const { return ConstIterator(TheMap.begin()); }
126  const_iterator end() const { return ConstIterator(TheMap.end()); }
127 
128  iterator find(const ValueT &V) { return Iterator(TheMap.find(V)); }
129 
130  /// Alternative version of find() which allows a different, and possibly less
131  /// expensive, key type.
132  /// The DenseMapInfo is responsible for supplying methods
133  /// getHashValue(LookupKeyT) and isEqual(LookupKeyT, KeyT) for each key type
134  /// used.
135  template <class LookupKeyT>
136  iterator find_as(const LookupKeyT &Val) {
137  return Iterator(TheMap.find_as(Val));
138  }
139  template <class LookupKeyT>
140  const_iterator find_as(const LookupKeyT &Val) const {
141  return ConstIterator(TheMap.find_as(Val));
142  }
143 
144  void erase(Iterator I) { return TheMap.erase(I.I); }
145  void erase(ConstIterator CI) { return TheMap.erase(CI.I); }
146 
147  std::pair<iterator, bool> insert(const ValueT &V) {
149  return TheMap.insert(std::make_pair(V, Empty));
150  }
151 
152  // Range insertion of values.
153  template<typename InputIt>
154  void insert(InputIt I, InputIt E) {
155  for (; I != E; ++I)
156  insert(*I);
157  }
158 };
159 
160 } // end namespace llvm
161 
162 #endif
bool operator!=(const Iterator &X) const
Definition: DenseSet.h:96
std::forward_iterator_tag iterator_category
Definition: DenseSet.h:107
MapTy::iterator::difference_type difference_type
Definition: DenseSet.h:83
value_type & reference
Definition: DenseSet.h:86
Iterator(const typename MapTy::iterator &i)
Definition: DenseSet.h:89
const DenseSetEmpty & getSecond() const
Definition: DenseSet.h:33
size_type size() const
Definition: DenseSet.h:53
DenseSet - This implements a dense probed hash-table based set.
Definition: DenseSet.h:39
ConstIterator(const typename MapTy::const_iterator &i)
Definition: DenseSet.h:109
ValueT key_type
Definition: DenseSet.h:46
const ValueT & operator*()
Definition: DenseSet.h:111
ValueT & operator*()
Definition: DenseSet.h:91
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:169
size_t getMemorySize() const
Return the approximate size (in bytes) of the actual map.
Definition: DenseMap.h:532
bool erase(const ValueT &V)
Definition: DenseSet.h:69
size_t getMemorySize() const
Definition: DenseSet.h:54
Iterator iterator
Definition: DenseSet.h:119
bool empty() const
Definition: DenseSet.h:52
ConstIterator const_iterator
Definition: DenseSet.h:120
ConstIterator & operator++()
Definition: DenseSet.h:114
DenseSetEmpty & getSecond()
Definition: DenseSet.h:32
bool operator!=(const ConstIterator &X) const
Definition: DenseSet.h:116
void erase(Iterator I)
Definition: DenseSet.h:144
bool erase(const KeyT &Val)
Definition: DenseMap.h:206
std::forward_iterator_tag iterator_category
Definition: DenseSet.h:87
iterator find(const ValueT &V)
Definition: DenseSet.h:128
ValueT value_type
Definition: DenseSet.h:47
size_type count(const ValueT &V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition: DenseSet.h:65
value_type * pointer
Definition: DenseSet.h:85
const_iterator find_as(const LookupKeyT &Val) const
Definition: DenseSet.h:140
unsigned size_type
Definition: DenseSet.h:48
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
const_iterator end() const
Definition: DenseSet.h:126
const ValueT * operator->()
Definition: DenseSet.h:112
void resize(size_type Size)
Grow the densemap so that it has at least Size buckets. Does not shrink.
Definition: DenseMap.h:85
void erase(ConstIterator CI)
Definition: DenseSet.h:145
void insert(InputIt I, InputIt E)
Definition: DenseSet.h:154
DenseSet(unsigned NumInitBuckets=0)
Definition: DenseSet.h:50
iterator begin()
Definition: DenseSet.h:122
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:147
const KeyT & getFirst() const
Definition: DenseSet.h:31
bool operator==(const ConstIterator &X) const
Definition: DenseSet.h:115
size_type count(const KeyT &Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:119
Iterator & operator++()
Definition: DenseSet.h:94
iterator find_as(const LookupKeyT &Val)
Alternative version of find() which allows a different, and possibly less expensive, key type.
Definition: DenseSet.h:136
unsigned size() const
Definition: DenseMap.h:82
void resize(size_t Size)
Grow the DenseSet so that it has at least Size buckets.
Definition: DenseSet.h:58
iterator begin()
Definition: DenseMap.h:64
const_iterator begin() const
Definition: DenseSet.h:125
#define I(x, y, z)
Definition: MD5.cpp:54
iterator end()
Definition: DenseMap.h:68
MapTy::const_iterator::difference_type difference_type
Definition: DenseSet.h:103
iterator find(const KeyT &Val)
Definition: DenseMap.h:124
void swap(DenseMap &RHS)
Definition: DenseMap.h:578
void clear()
Definition: DenseSet.h:60
bool operator==(const Iterator &X) const
Definition: DenseSet.h:95
iterator find_as(const LookupKeyT &Val)
Alternate version of find() which allows a different, and possibly less expensive, key type.
Definition: DenseMap.h:143
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: DenseMap.h:79
void swap(DenseSet &RHS)
Definition: DenseSet.h:73
iterator end()
Definition: DenseSet.h:123
ValueT * operator->()
Definition: DenseSet.h:92
ptrdiff_t difference_type
Definition: DenseMap.h:993