89f55caa95475c64c054bc1f9cd7bd49af4cfc0e
[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   DenseSet(const DenseSet &Other) : TheMap(Other.TheMap) {}
31   explicit DenseSet(unsigned NumInitBuckets = 64) : TheMap(NumInitBuckets) {}
32
33   bool empty() const { return TheMap.empty(); }
34   unsigned size() const { return TheMap.size(); }
35
36   void clear() {
37     TheMap.clear();
38   }
39
40   bool count(const ValueT &V) const {
41     return TheMap.count(V);
42   }
43
44   void erase(const ValueT &V) {
45     TheMap.erase(V);
46   }
47
48   DenseSet &operator=(const DenseSet &RHS) {
49     TheMap = RHS.TheMap;
50     return *this;
51   }
52
53   // Iterators.
54
55   class Iterator {
56     typename MapTy::iterator I;
57   public:
58     Iterator(const typename MapTy::iterator &i) : I(i) {}
59
60     ValueT& operator*() { return I->first; }
61     ValueT* operator->() { return &I->first; }
62
63     Iterator& operator++() { ++I; return *this; }
64     bool operator==(const Iterator& X) const { return I == X.I; }
65     bool operator!=(const Iterator& X) const { return I != X.I; }
66   };
67
68   class ConstIterator {
69     typename MapTy::const_iterator I;
70   public:
71     ConstIterator(const typename MapTy::const_iterator &i) : I(i) {}
72
73     const ValueT& operator*() { return I->first; }
74     const ValueT* operator->() { return &I->first; }
75
76     ConstIterator& operator++() { ++I; return *this; }
77     bool operator==(const ConstIterator& X) const { return I == X.I; }
78     bool operator!=(const ConstIterator& X) const { return I != X.I; }
79   };
80
81   typedef Iterator      iterator;
82   typedef ConstIterator const_iterator;
83
84   iterator begin() { return Iterator(TheMap.begin()); }
85   iterator end() { return Iterator(TheMap.end()); }
86
87   const_iterator begin() const { return ConstIterator(TheMap.begin()); }
88   const_iterator end() const { return ConstIterator(TheMap.end()); }
89
90   std::pair<iterator, bool> insert(const ValueT &V) {
91     return TheMap.insert(std::make_pair(V, 0));
92   }
93   
94   // Range insertion of values.
95   template<typename InputIt>
96   void insert(InputIt I, InputIt E) {
97     for (; I != E; ++I)
98       insert(*I);
99   }
100 };
101
102 } // end namespace llvm
103
104 #endif