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 DenseSet(const DenseSet &Other) : TheMap(Other.TheMap) {}
31 explicit DenseSet(unsigned NumInitBuckets = 0) : TheMap(NumInitBuckets) {}
33 bool empty() const { return TheMap.empty(); }
34 unsigned size() const { return TheMap.size(); }
35 size_t getMemorySize() const { return TheMap.getMemorySize(); }
37 /// Grow the denseset so that it has at least Size buckets. Does not shrink
38 void resize(size_t Size) { TheMap.resize(Size); }
44 bool count(const ValueT &V) const {
45 return TheMap.count(V);
48 bool erase(const ValueT &V) {
49 return TheMap.erase(V);
52 void swap(DenseSet& RHS) {
53 TheMap.swap(RHS.TheMap);
56 DenseSet &operator=(const DenseSet &RHS) {
64 typename MapTy::iterator I;
65 friend class DenseSet;
67 typedef typename MapTy::iterator::difference_type difference_type;
68 typedef ValueT value_type;
69 typedef value_type *pointer;
70 typedef value_type &reference;
71 typedef std::forward_iterator_tag iterator_category;
73 Iterator(const typename MapTy::iterator &i) : I(i) {}
75 ValueT& operator*() { return I->first; }
76 ValueT* operator->() { return &I->first; }
78 Iterator& operator++() { ++I; return *this; }
79 bool operator==(const Iterator& X) const { return I == X.I; }
80 bool operator!=(const Iterator& X) const { return I != X.I; }
84 typename MapTy::const_iterator I;
85 friend class DenseSet;
87 typedef typename MapTy::const_iterator::difference_type difference_type;
88 typedef ValueT value_type;
89 typedef value_type *pointer;
90 typedef value_type &reference;
91 typedef std::forward_iterator_tag iterator_category;
93 ConstIterator(const typename MapTy::const_iterator &i) : I(i) {}
95 const ValueT& operator*() { return I->first; }
96 const ValueT* operator->() { return &I->first; }
98 ConstIterator& operator++() { ++I; return *this; }
99 bool operator==(const ConstIterator& X) const { return I == X.I; }
100 bool operator!=(const ConstIterator& X) const { return I != X.I; }
103 typedef Iterator iterator;
104 typedef ConstIterator const_iterator;
106 iterator begin() { return Iterator(TheMap.begin()); }
107 iterator end() { return Iterator(TheMap.end()); }
109 const_iterator begin() const { return ConstIterator(TheMap.begin()); }
110 const_iterator end() const { return ConstIterator(TheMap.end()); }
112 iterator find(const ValueT &V) { return Iterator(TheMap.find(V)); }
113 void erase(Iterator I) { return TheMap.erase(I.I); }
114 void erase(ConstIterator CI) { return TheMap.erase(CI.I); }
116 std::pair<iterator, bool> insert(const ValueT &V) {
117 return TheMap.insert(std::make_pair(V, 0));
120 // Range insertion of values.
121 template<typename InputIt>
122 void insert(InputIt I, InputIt E) {
128 } // end namespace llvm