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!");
47 typedef ValueT key_type;
48 typedef ValueT value_type;
49 typedef unsigned size_type;
51 explicit DenseSet(unsigned NumInitBuckets = 0) : TheMap(NumInitBuckets) {}
53 bool empty() const { return TheMap.empty(); }
54 size_type size() const { return TheMap.size(); }
55 size_t getMemorySize() const { return TheMap.getMemorySize(); }
57 /// Grow the DenseSet so that it has at least Size buckets. Will not shrink
58 /// the Size of the set.
59 void resize(size_t Size) { TheMap.resize(Size); }
65 /// Return 1 if the specified key is in the set, 0 otherwise.
66 size_type count(const ValueT &V) const {
67 return TheMap.count(V);
70 bool erase(const ValueT &V) {
71 return TheMap.erase(V);
74 void swap(DenseSet& RHS) {
75 TheMap.swap(RHS.TheMap);
81 typename MapTy::iterator I;
82 friend class DenseSet;
85 typedef typename MapTy::iterator::difference_type difference_type;
86 typedef ValueT value_type;
87 typedef value_type *pointer;
88 typedef value_type &reference;
89 typedef std::forward_iterator_tag iterator_category;
91 Iterator(const typename MapTy::iterator &i) : I(i) {}
93 ValueT &operator*() { return I->getFirst(); }
94 ValueT *operator->() { return &I->getFirst(); }
96 Iterator& operator++() { ++I; return *this; }
97 bool operator==(const Iterator& X) const { return I == X.I; }
98 bool operator!=(const Iterator& X) const { return I != X.I; }
101 class ConstIterator {
102 typename MapTy::const_iterator I;
103 friend class DenseSet;
106 typedef typename MapTy::const_iterator::difference_type difference_type;
107 typedef ValueT value_type;
108 typedef value_type *pointer;
109 typedef value_type &reference;
110 typedef std::forward_iterator_tag iterator_category;
112 ConstIterator(const typename MapTy::const_iterator &i) : I(i) {}
114 const ValueT &operator*() { return I->getFirst(); }
115 const ValueT *operator->() { return &I->getFirst(); }
117 ConstIterator& operator++() { ++I; return *this; }
118 bool operator==(const ConstIterator& X) const { return I == X.I; }
119 bool operator!=(const ConstIterator& X) const { return I != X.I; }
122 typedef Iterator iterator;
123 typedef ConstIterator const_iterator;
125 iterator begin() { return Iterator(TheMap.begin()); }
126 iterator end() { return Iterator(TheMap.end()); }
128 const_iterator begin() const { return ConstIterator(TheMap.begin()); }
129 const_iterator end() const { return ConstIterator(TheMap.end()); }
131 iterator find(const ValueT &V) { return Iterator(TheMap.find(V)); }
133 /// Alternative version of find() which allows a different, and possibly less
134 /// expensive, key type.
135 /// The DenseMapInfo is responsible for supplying methods
136 /// getHashValue(LookupKeyT) and isEqual(LookupKeyT, KeyT) for each key type
138 template <class LookupKeyT>
139 iterator find_as(const LookupKeyT &Val) {
140 return Iterator(TheMap.find_as(Val));
142 template <class LookupKeyT>
143 const_iterator find_as(const LookupKeyT &Val) const {
144 return ConstIterator(TheMap.find_as(Val));
147 void erase(Iterator I) { return TheMap.erase(I.I); }
148 void erase(ConstIterator CI) { return TheMap.erase(CI.I); }
150 std::pair<iterator, bool> insert(const ValueT &V) {
151 detail::DenseSetEmpty Empty;
152 return TheMap.insert(std::make_pair(V, Empty));
155 // Range insertion of values.
156 template<typename InputIt>
157 void insert(InputIt I, InputIt E) {
163 } // end namespace llvm