1 //===- llvm/ADT/DenseSet.h - Dense probed hash table ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the DenseSet class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_DENSESET_H
15 #define LLVM_ADT_DENSESET_H
17 #include "llvm/ADT/DenseMap.h"
22 struct DenseSetEmpty {};
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 {
30 KeyT &getFirst() { return key; }
31 const KeyT &getFirst() const { return key; }
32 DenseSetEmpty &getSecond() { return *this; }
33 const DenseSetEmpty &getSecond() const { return *this; }
37 /// DenseSet - This implements a dense probed hash-table based set.
38 template<typename ValueT, typename ValueInfoT = DenseMapInfo<ValueT> >
40 typedef DenseMap<ValueT, detail::DenseSetEmpty, ValueInfoT,
41 detail::DenseSetPair<ValueT>> MapTy;
42 static_assert(sizeof(typename MapTy::value_type) == sizeof(ValueT),
43 "DenseMap buckets unexpectedly large!");
46 typedef ValueT key_type;
47 typedef ValueT value_type;
48 typedef unsigned size_type;
50 explicit DenseSet(unsigned NumInitBuckets = 0) : TheMap(NumInitBuckets) {}
52 bool empty() const { return TheMap.empty(); }
53 size_type size() const { return TheMap.size(); }
54 size_t getMemorySize() const { return TheMap.getMemorySize(); }
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); }
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);
69 bool erase(const ValueT &V) {
70 return TheMap.erase(V);
73 void swap(DenseSet& RHS) {
74 TheMap.swap(RHS.TheMap);
80 typename MapTy::iterator I;
81 friend class DenseSet;
83 typedef typename MapTy::iterator::difference_type difference_type;
84 typedef ValueT value_type;
85 typedef value_type *pointer;
86 typedef value_type &reference;
87 typedef std::forward_iterator_tag iterator_category;
89 Iterator(const typename MapTy::iterator &i) : I(i) {}
91 ValueT &operator*() { return I->getFirst(); }
92 ValueT *operator->() { return &I->getFirst(); }
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; }
100 typename MapTy::const_iterator I;
101 friend class DenseSet;
103 typedef typename MapTy::const_iterator::difference_type difference_type;
104 typedef ValueT value_type;
105 typedef value_type *pointer;
106 typedef value_type &reference;
107 typedef std::forward_iterator_tag iterator_category;
109 ConstIterator(const typename MapTy::const_iterator &i) : I(i) {}
111 const ValueT &operator*() { return I->getFirst(); }
112 const ValueT *operator->() { return &I->getFirst(); }
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; }
119 typedef Iterator iterator;
120 typedef ConstIterator const_iterator;
122 iterator begin() { return Iterator(TheMap.begin()); }
123 iterator end() { return Iterator(TheMap.end()); }
125 const_iterator begin() const { return ConstIterator(TheMap.begin()); }
126 const_iterator end() const { return ConstIterator(TheMap.end()); }
128 iterator find(const ValueT &V) { return Iterator(TheMap.find(V)); }
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
135 template <class LookupKeyT>
136 iterator find_as(const LookupKeyT &Val) {
137 return Iterator(TheMap.find_as(Val));
139 template <class LookupKeyT>
140 const_iterator find_as(const LookupKeyT &Val) const {
141 return ConstIterator(TheMap.find_as(Val));
144 void erase(Iterator I) { return TheMap.erase(I.I); }
145 void erase(ConstIterator CI) { return TheMap.erase(CI.I); }
147 std::pair<iterator, bool> insert(const ValueT &V) {
148 detail::DenseSetEmpty Empty;
149 return TheMap.insert(std::make_pair(V, Empty));
152 // Range insertion of values.
153 template<typename InputIt>
154 void insert(InputIt I, InputIt E) {
160 } // end namespace llvm