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"
21 /// DenseSet - This implements a dense probed hash-table based set.
23 /// FIXME: This is currently implemented directly in terms of DenseMap, this
24 /// should be optimized later if there is a need.
25 template<typename ValueT, typename ValueInfoT = DenseMapInfo<ValueT> >
27 typedef DenseMap<ValueT, char, ValueInfoT> MapTy;
30 typedef ValueT key_type;
31 typedef ValueT value_type;
33 DenseSet(const DenseSet &Other) : TheMap(Other.TheMap) {}
34 explicit DenseSet(unsigned NumInitBuckets = 0) : TheMap(NumInitBuckets) {}
36 bool empty() const { return TheMap.empty(); }
37 unsigned size() const { return TheMap.size(); }
38 size_t getMemorySize() const { return TheMap.getMemorySize(); }
40 /// Grow the DenseSet so that it has at least Size buckets. Will not shrink
41 /// the Size of the set.
42 void resize(size_t Size) { TheMap.resize(Size); }
48 bool count(const ValueT &V) const {
49 return TheMap.count(V);
52 bool erase(const ValueT &V) {
53 return TheMap.erase(V);
56 void swap(DenseSet& RHS) {
57 TheMap.swap(RHS.TheMap);
60 DenseSet &operator=(const DenseSet &RHS) {
68 typename MapTy::iterator I;
69 friend class DenseSet;
71 typedef typename MapTy::iterator::difference_type difference_type;
72 typedef ValueT value_type;
73 typedef value_type *pointer;
74 typedef value_type &reference;
75 typedef std::forward_iterator_tag iterator_category;
77 Iterator(const typename MapTy::iterator &i) : I(i) {}
79 ValueT& operator*() { return I->first; }
80 ValueT* operator->() { return &I->first; }
82 Iterator& operator++() { ++I; return *this; }
83 bool operator==(const Iterator& X) const { return I == X.I; }
84 bool operator!=(const Iterator& X) const { return I != X.I; }
88 typename MapTy::const_iterator I;
89 friend class DenseSet;
91 typedef typename MapTy::const_iterator::difference_type difference_type;
92 typedef ValueT value_type;
93 typedef value_type *pointer;
94 typedef value_type &reference;
95 typedef std::forward_iterator_tag iterator_category;
97 ConstIterator(const typename MapTy::const_iterator &i) : I(i) {}
99 const ValueT& operator*() { return I->first; }
100 const ValueT* operator->() { return &I->first; }
102 ConstIterator& operator++() { ++I; return *this; }
103 bool operator==(const ConstIterator& X) const { return I == X.I; }
104 bool operator!=(const ConstIterator& X) const { return I != X.I; }
107 typedef Iterator iterator;
108 typedef ConstIterator const_iterator;
110 iterator begin() { return Iterator(TheMap.begin()); }
111 iterator end() { return Iterator(TheMap.end()); }
113 const_iterator begin() const { return ConstIterator(TheMap.begin()); }
114 const_iterator end() const { return ConstIterator(TheMap.end()); }
116 iterator find(const ValueT &V) { return Iterator(TheMap.find(V)); }
117 void erase(Iterator I) { return TheMap.erase(I.I); }
118 void erase(ConstIterator CI) { return TheMap.erase(CI.I); }
120 std::pair<iterator, bool> insert(const ValueT &V) {
121 return TheMap.insert(std::make_pair(V, 0));
124 // Range insertion of values.
125 template<typename InputIt>
126 void insert(InputIt I, InputIt E) {
132 } // end namespace llvm