DenseSet.h revision ea33c8fed681d103f159e7e80591437c726576ec
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  void insert(const ValueT &V) {
45    TheMap[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  };
70
71  class ConstIterator {
72    typename MapTy::const_iterator I;
73  public:
74    ConstIterator(const typename MapTy::const_iterator &i) : I(i) {}
75
76    const ValueT& operator*() { return I->first; }
77    const ValueT* operator->() { return &I->first; }
78
79    ConstIterator& operator++() { ++I; return *this; };
80    bool operator==(const ConstIterator& X) const { return I == X.I; }
81  };
82
83  typedef Iterator      iterator;
84  typedef ConstIterator const_iterator;
85
86  iterator begin() { return Iterator(TheMap.begin()); }
87  iterator end() { return Iterator(TheMap.end()); }
88
89  const_iterator begin() const { return ConstIterator(TheMap.begin()); }
90  const_iterator end() const { return ConstIterator(TheMap.end()); }
91};
92
93} // end namespace llvm
94
95#endif
96