DenseSet.h revision e3955df639ff9aff990f628ef6a219ff5efdbc81
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 erase(const ValueT &V) { 45 return TheMap.erase(V); 46 } 47 48 void swap(DenseSet& RHS) { 49 TheMap.swap(RHS.TheMap); 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 friend class DenseSet; 62 public: 63 typedef typename MapTy::iterator::difference_type difference_type; 64 typedef ValueT value_type; 65 typedef value_type *pointer; 66 typedef value_type &reference; 67 typedef std::forward_iterator_tag iterator_category; 68 69 Iterator(const typename MapTy::iterator &i) : I(i) {} 70 71 ValueT& operator*() { return I->first; } 72 ValueT* operator->() { return &I->first; } 73 74 Iterator& operator++() { ++I; return *this; } 75 bool operator==(const Iterator& X) const { return I == X.I; } 76 bool operator!=(const Iterator& X) const { return I != X.I; } 77 }; 78 79 class ConstIterator { 80 typename MapTy::const_iterator I; 81 friend class DenseSet; 82 public: 83 typedef typename MapTy::const_iterator::difference_type difference_type; 84 typedef ValueT value_type; 85 typedef value_type *pointer; 86 typedef value_type &reference; 87 typedef std::forward_iterator_tag iterator_category; 88 89 ConstIterator(const typename MapTy::const_iterator &i) : I(i) {} 90 91 const ValueT& operator*() { return I->first; } 92 const ValueT* operator->() { return &I->first; } 93 94 ConstIterator& operator++() { ++I; return *this; } 95 bool operator==(const ConstIterator& X) const { return I == X.I; } 96 bool operator!=(const ConstIterator& X) const { return I != X.I; } 97 }; 98 99 typedef Iterator iterator; 100 typedef ConstIterator const_iterator; 101 102 iterator begin() { return Iterator(TheMap.begin()); } 103 iterator end() { return Iterator(TheMap.end()); } 104 105 const_iterator begin() const { return ConstIterator(TheMap.begin()); } 106 const_iterator end() const { return ConstIterator(TheMap.end()); } 107 108 iterator find(const ValueT &V) { return Iterator(TheMap.find(V)); } 109 void erase(Iterator I) { return TheMap.erase(I.I); } 110 void erase(ConstIterator CI) { return TheMap.erase(CI.I); } 111 112 std::pair<iterator, bool> insert(const ValueT &V) { 113 return TheMap.insert(std::make_pair(V, 0)); 114 } 115 116 // Range insertion of values. 117 template<typename InputIt> 118 void insert(InputIt I, InputIt E) { 119 for (; I != E; ++I) 120 insert(*I); 121 } 122}; 123 124} // end namespace llvm 125 126#endif 127