DenseSet.h revision 93ee2fd7da6100eafcc510cfdeb71f2bfdd15429
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
19namespace 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.
25template<typename ValueT, typename ValueInfoT = DenseMapInfo<ValueT> >
26class DenseSet {
27  typedef DenseMap<ValueT, char, ValueInfoT> MapTy;
28  MapTy TheMap;
29public:
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  bool insert(const ValueT &V) {
45    return TheMap.insert(std::make_pair(V, 0));
46  }
47
48  void erase(const ValueT &V) {
49    TheMap.erase(V);
50  }
51
52  DenseSet &operator=(const DenseSet &RHS) {
53    TheMap = RHS.TheMap;
54    return *this;
55  }
56
57  // Iterators.
58
59  class Iterator {
60    typename MapTy::iterator I;
61  public:
62    Iterator(const typename MapTy::iterator &i) : I(i) {}
63
64    ValueT& operator*() { return I->first; }
65    ValueT* operator->() { return &I->first; }
66
67    Iterator& operator++() { ++I; return *this; };
68    bool operator==(const Iterator& X) const { return I == X.I; }
69    bool operator!=(const Iterator& X) const { return I != X.I; }
70  };
71
72  class ConstIterator {
73    typename MapTy::const_iterator I;
74  public:
75    ConstIterator(const typename MapTy::const_iterator &i) : I(i) {}
76
77    const ValueT& operator*() { return I->first; }
78    const ValueT* operator->() { return &I->first; }
79
80    ConstIterator& operator++() { ++I; return *this; };
81    bool operator==(const ConstIterator& X) const { return I == X.I; }
82    bool operator!=(const ConstIterator& X) const { return I != X.I; }
83  };
84
85  typedef Iterator      iterator;
86  typedef ConstIterator const_iterator;
87
88  iterator begin() { return Iterator(TheMap.begin()); }
89  iterator end() { return Iterator(TheMap.end()); }
90
91  const_iterator begin() const { return ConstIterator(TheMap.begin()); }
92  const_iterator end() const { return ConstIterator(TheMap.end()); }
93};
94
95} // end namespace llvm
96
97#endif
98