Revert r199791.
[oota-llvm.git] / include / llvm / ADT / DenseSet.h
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 /// DenseSet - This implements a dense probed hash-table based set.
22 ///
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> >
26 class DenseSet {
27   typedef DenseMap<ValueT, char, ValueInfoT> MapTy;
28   MapTy TheMap;
29 public:
30   typedef ValueT key_type;
31   typedef ValueT value_type;
32
33   DenseSet(const DenseSet &Other) : TheMap(Other.TheMap) {}
34   explicit DenseSet(unsigned NumInitBuckets = 0) : TheMap(NumInitBuckets) {}
35
36   bool empty() const { return TheMap.empty(); }
37   unsigned size() const { return TheMap.size(); }
38   size_t getMemorySize() const { return TheMap.getMemorySize(); }
39
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); }
43
44   void clear() {
45     TheMap.clear();
46   }
47
48   bool count(const ValueT &V) const {
49     return TheMap.count(V);
50   }
51
52   bool erase(const ValueT &V) {
53     return TheMap.erase(V);
54   }
55
56   void swap(DenseSet& RHS) {
57     TheMap.swap(RHS.TheMap);
58   }
59
60   DenseSet &operator=(const DenseSet &RHS) {
61     TheMap = RHS.TheMap;
62     return *this;
63   }
64
65   // Iterators.
66
67   class Iterator {
68     typename MapTy::iterator I;
69     friend class DenseSet;
70   public:
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;
76
77     Iterator(const typename MapTy::iterator &i) : I(i) {}
78
79     ValueT& operator*() { return I->first; }
80     ValueT* operator->() { return &I->first; }
81
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; }
85   };
86
87   class ConstIterator {
88     typename MapTy::const_iterator I;
89     friend class DenseSet;
90   public:
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;
96
97     ConstIterator(const typename MapTy::const_iterator &i) : I(i) {}
98
99     const ValueT& operator*() { return I->first; }
100     const ValueT* operator->() { return &I->first; }
101
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; }
105   };
106
107   typedef Iterator      iterator;
108   typedef ConstIterator const_iterator;
109
110   iterator begin() { return Iterator(TheMap.begin()); }
111   iterator end() { return Iterator(TheMap.end()); }
112
113   const_iterator begin() const { return ConstIterator(TheMap.begin()); }
114   const_iterator end() const { return ConstIterator(TheMap.end()); }
115
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); }
119
120   std::pair<iterator, bool> insert(const ValueT &V) {
121     return TheMap.insert(std::make_pair(V, 0));
122   }
123   
124   // Range insertion of values.
125   template<typename InputIt>
126   void insert(InputIt I, InputIt E) {
127     for (; I != E; ++I)
128       insert(*I);
129   }
130 };
131
132 } // end namespace llvm
133
134 #endif