LLVM API Documentation

DenseSet.h
Go to the documentation of this file.
00001 //===- llvm/ADT/DenseSet.h - Dense probed hash table ------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the DenseSet class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_ADT_DENSESET_H
00015 #define LLVM_ADT_DENSESET_H
00016 
00017 #include "llvm/ADT/DenseMap.h"
00018 
00019 namespace llvm {
00020 
00021 /// DenseSet - This implements a dense probed hash-table based set.
00022 ///
00023 /// FIXME: This is currently implemented directly in terms of DenseMap, this
00024 /// should be optimized later if there is a need.
00025 template<typename ValueT, typename ValueInfoT = DenseMapInfo<ValueT> >
00026 class DenseSet {
00027   typedef DenseMap<ValueT, char, ValueInfoT> MapTy;
00028   MapTy TheMap;
00029 public:
00030   typedef ValueT key_type;
00031   typedef ValueT value_type;
00032   typedef unsigned size_type;
00033 
00034   explicit DenseSet(unsigned NumInitBuckets = 0) : TheMap(NumInitBuckets) {}
00035 
00036   bool empty() const { return TheMap.empty(); }
00037   size_type size() const { return TheMap.size(); }
00038   size_t getMemorySize() const { return TheMap.getMemorySize(); }
00039 
00040   /// Grow the DenseSet so that it has at least Size buckets. Will not shrink
00041   /// the Size of the set.
00042   void resize(size_t Size) { TheMap.resize(Size); }
00043 
00044   void clear() {
00045     TheMap.clear();
00046   }
00047 
00048   /// Return 1 if the specified key is in the set, 0 otherwise.
00049   size_type count(const ValueT &V) const {
00050     return TheMap.count(V);
00051   }
00052 
00053   bool erase(const ValueT &V) {
00054     return TheMap.erase(V);
00055   }
00056 
00057   void swap(DenseSet& RHS) {
00058     TheMap.swap(RHS.TheMap);
00059   }
00060 
00061   // Iterators.
00062 
00063   class Iterator {
00064     typename MapTy::iterator I;
00065     friend class DenseSet;
00066   public:
00067     typedef typename MapTy::iterator::difference_type difference_type;
00068     typedef ValueT value_type;
00069     typedef value_type *pointer;
00070     typedef value_type &reference;
00071     typedef std::forward_iterator_tag iterator_category;
00072 
00073     Iterator(const typename MapTy::iterator &i) : I(i) {}
00074 
00075     ValueT& operator*() { return I->first; }
00076     ValueT* operator->() { return &I->first; }
00077 
00078     Iterator& operator++() { ++I; return *this; }
00079     bool operator==(const Iterator& X) const { return I == X.I; }
00080     bool operator!=(const Iterator& X) const { return I != X.I; }
00081   };
00082 
00083   class ConstIterator {
00084     typename MapTy::const_iterator I;
00085     friend class DenseSet;
00086   public:
00087     typedef typename MapTy::const_iterator::difference_type difference_type;
00088     typedef ValueT value_type;
00089     typedef value_type *pointer;
00090     typedef value_type &reference;
00091     typedef std::forward_iterator_tag iterator_category;
00092 
00093     ConstIterator(const typename MapTy::const_iterator &i) : I(i) {}
00094 
00095     const ValueT& operator*() { return I->first; }
00096     const ValueT* operator->() { return &I->first; }
00097 
00098     ConstIterator& operator++() { ++I; return *this; }
00099     bool operator==(const ConstIterator& X) const { return I == X.I; }
00100     bool operator!=(const ConstIterator& X) const { return I != X.I; }
00101   };
00102 
00103   typedef Iterator      iterator;
00104   typedef ConstIterator const_iterator;
00105 
00106   iterator begin() { return Iterator(TheMap.begin()); }
00107   iterator end() { return Iterator(TheMap.end()); }
00108 
00109   const_iterator begin() const { return ConstIterator(TheMap.begin()); }
00110   const_iterator end() const { return ConstIterator(TheMap.end()); }
00111 
00112   iterator find(const ValueT &V) { return Iterator(TheMap.find(V)); }
00113   void erase(Iterator I) { return TheMap.erase(I.I); }
00114   void erase(ConstIterator CI) { return TheMap.erase(CI.I); }
00115 
00116   std::pair<iterator, bool> insert(const ValueT &V) {
00117     return TheMap.insert(std::make_pair(V, 0));
00118   }
00119   
00120   // Range insertion of values.
00121   template<typename InputIt>
00122   void insert(InputIt I, InputIt E) {
00123     for (; I != E; ++I)
00124       insert(*I);
00125   }
00126 };
00127 
00128 } // end namespace llvm
00129 
00130 #endif