ADT: Add a drop_back() helper to ArrayRef
[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   typedef unsigned size_type;\r
33
34   explicit DenseSet(unsigned NumInitBuckets = 0) : TheMap(NumInitBuckets) {}
35
36   bool empty() const { return TheMap.empty(); }
37   size_type 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   /// Return 1 if the specified key is in the set, 0 otherwise.\r
49   size_type count(const ValueT &V) const {
50     return TheMap.count(V);
51   }
52
53   bool erase(const ValueT &V) {
54     return TheMap.erase(V);
55   }
56
57   void swap(DenseSet& RHS) {
58     TheMap.swap(RHS.TheMap);
59   }
60
61   // Iterators.
62
63   class Iterator {
64     typename MapTy::iterator I;
65     friend class DenseSet;
66   public:
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;
72
73     Iterator(const typename MapTy::iterator &i) : I(i) {}
74
75     ValueT& operator*() { return I->first; }
76     ValueT* operator->() { return &I->first; }
77
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; }
81   };
82
83   class ConstIterator {
84     typename MapTy::const_iterator I;
85     friend class DenseSet;
86   public:
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;
92
93     ConstIterator(const typename MapTy::const_iterator &i) : I(i) {}
94
95     const ValueT& operator*() { return I->first; }
96     const ValueT* operator->() { return &I->first; }
97
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; }
101   };
102
103   typedef Iterator      iterator;
104   typedef ConstIterator const_iterator;
105
106   iterator begin() { return Iterator(TheMap.begin()); }
107   iterator end() { return Iterator(TheMap.end()); }
108
109   const_iterator begin() const { return ConstIterator(TheMap.begin()); }
110   const_iterator end() const { return ConstIterator(TheMap.end()); }
111
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); }
115
116   std::pair<iterator, bool> insert(const ValueT &V) {
117     return TheMap.insert(std::make_pair(V, 0));
118   }
119   
120   // Range insertion of values.
121   template<typename InputIt>
122   void insert(InputIt I, InputIt E) {
123     for (; I != E; ++I)
124       insert(*I);
125   }
126 };
127
128 } // end namespace llvm
129
130 #endif