[C++] Use 'nullptr'.
[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   explicit DenseSet(unsigned NumInitBuckets = 0) : TheMap(NumInitBuckets) {}
34
35   bool empty() const { return TheMap.empty(); }
36   unsigned size() const { return TheMap.size(); }
37   size_t getMemorySize() const { return TheMap.getMemorySize(); }
38
39   /// Grow the DenseSet so that it has at least Size buckets. Will not shrink
40   /// the Size of the set.
41   void resize(size_t Size) { TheMap.resize(Size); }
42
43   void clear() {
44     TheMap.clear();
45   }
46
47   bool count(const ValueT &V) const {
48     return TheMap.count(V);
49   }
50
51   bool erase(const ValueT &V) {
52     return TheMap.erase(V);
53   }
54
55   void swap(DenseSet& RHS) {
56     TheMap.swap(RHS.TheMap);
57   }
58
59   // Iterators.
60
61   class Iterator {
62     typename MapTy::iterator I;
63     friend class DenseSet;
64   public:
65     typedef typename MapTy::iterator::difference_type difference_type;
66     typedef ValueT value_type;
67     typedef value_type *pointer;
68     typedef value_type &reference;
69     typedef std::forward_iterator_tag iterator_category;
70
71     Iterator(const typename MapTy::iterator &i) : I(i) {}
72
73     ValueT& operator*() { return I->first; }
74     ValueT* operator->() { return &I->first; }
75
76     Iterator& operator++() { ++I; return *this; }
77     bool operator==(const Iterator& X) const { return I == X.I; }
78     bool operator!=(const Iterator& X) const { return I != X.I; }
79   };
80
81   class ConstIterator {
82     typename MapTy::const_iterator I;
83     friend class DenseSet;
84   public:
85     typedef typename MapTy::const_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;
90
91     ConstIterator(const typename MapTy::const_iterator &i) : I(i) {}
92
93     const ValueT& operator*() { return I->first; }
94     const ValueT* operator->() { return &I->first; }
95
96     ConstIterator& operator++() { ++I; return *this; }
97     bool operator==(const ConstIterator& X) const { return I == X.I; }
98     bool operator!=(const ConstIterator& X) const { return I != X.I; }
99   };
100
101   typedef Iterator      iterator;
102   typedef ConstIterator const_iterator;
103
104   iterator begin() { return Iterator(TheMap.begin()); }
105   iterator end() { return Iterator(TheMap.end()); }
106
107   const_iterator begin() const { return ConstIterator(TheMap.begin()); }
108   const_iterator end() const { return ConstIterator(TheMap.end()); }
109
110   iterator find(const ValueT &V) { return Iterator(TheMap.find(V)); }
111   void erase(Iterator I) { return TheMap.erase(I.I); }
112   void erase(ConstIterator CI) { return TheMap.erase(CI.I); }
113
114   std::pair<iterator, bool> insert(const ValueT &V) {
115     return TheMap.insert(std::make_pair(V, 0));
116   }
117   
118   // Range insertion of values.
119   template<typename InputIt>
120   void insert(InputIt I, InputIt E) {
121     for (; I != E; ++I)
122       insert(*I);
123   }
124 };
125
126 } // end namespace llvm
127
128 #endif