LLVM API Documentation

SmallSet.h
Go to the documentation of this file.
00001 //===- llvm/ADT/SmallSet.h - 'Normally small' sets --------------*- 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 SmallSet class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_ADT_SMALLSET_H
00015 #define LLVM_ADT_SMALLSET_H
00016 
00017 #include "llvm/ADT/SmallPtrSet.h"
00018 #include "llvm/ADT/SmallVector.h"
00019 #include <set>
00020 
00021 namespace llvm {
00022 
00023 /// SmallSet - This maintains a set of unique values, optimizing for the case
00024 /// when the set is small (less than N).  In this case, the set can be
00025 /// maintained with no mallocs.  If the set gets large, we expand to using an
00026 /// std::set to maintain reasonable lookup times.
00027 ///
00028 /// Note that this set does not provide a way to iterate over members in the
00029 /// set.
00030 template <typename T, unsigned N,  typename C = std::less<T> >
00031 class SmallSet {
00032   /// Use a SmallVector to hold the elements here (even though it will never
00033   /// reach its 'large' stage) to avoid calling the default ctors of elements
00034   /// we will never use.
00035   SmallVector<T, N> Vector;
00036   std::set<T, C> Set;
00037   typedef typename SmallVector<T, N>::const_iterator VIterator;
00038   typedef typename SmallVector<T, N>::iterator mutable_iterator;
00039 public:
00040   typedef size_t size_type;
00041   SmallSet() {}
00042 
00043   bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const {
00044     return Vector.empty() && Set.empty();
00045   }
00046 
00047   size_type size() const {
00048     return isSmall() ? Vector.size() : Set.size();
00049   }
00050 
00051   /// count - Return 1 if the element is in the set, 0 otherwise.
00052   size_type count(const T &V) const {
00053     if (isSmall()) {
00054       // Since the collection is small, just do a linear search.
00055       return vfind(V) == Vector.end() ? 0 : 1;
00056     } else {
00057       return Set.count(V);
00058     }
00059   }
00060 
00061   /// insert - Insert an element into the set if it isn't already there.
00062   /// Returns true if the element is inserted (it was not in the set before).
00063   bool insert(const T &V) {
00064     if (!isSmall())
00065       return Set.insert(V).second;
00066 
00067     VIterator I = vfind(V);
00068     if (I != Vector.end())    // Don't reinsert if it already exists.
00069       return false;
00070     if (Vector.size() < N) {
00071       Vector.push_back(V);
00072       return true;
00073     }
00074 
00075     // Otherwise, grow from vector to set.
00076     while (!Vector.empty()) {
00077       Set.insert(Vector.back());
00078       Vector.pop_back();
00079     }
00080     Set.insert(V);
00081     return true;
00082   }
00083 
00084   template <typename IterT>
00085   void insert(IterT I, IterT E) {
00086     for (; I != E; ++I)
00087       insert(*I);
00088   }
00089   
00090   bool erase(const T &V) {
00091     if (!isSmall())
00092       return Set.erase(V);
00093     for (mutable_iterator I = Vector.begin(), E = Vector.end(); I != E; ++I)
00094       if (*I == V) {
00095         Vector.erase(I);
00096         return true;
00097       }
00098     return false;
00099   }
00100 
00101   void clear() {
00102     Vector.clear();
00103     Set.clear();
00104   }
00105 private:
00106   bool isSmall() const { return Set.empty(); }
00107 
00108   VIterator vfind(const T &V) const {
00109     for (VIterator I = Vector.begin(), E = Vector.end(); I != E; ++I)
00110       if (*I == V)
00111         return I;
00112     return Vector.end();
00113   }
00114 };
00115 
00116 /// If this set is of pointer values, transparently switch over to using
00117 /// SmallPtrSet for performance.
00118 template <typename PointeeType, unsigned N>
00119 class SmallSet<PointeeType*, N> : public SmallPtrSet<PointeeType*, N> {};
00120 
00121 } // end namespace llvm
00122 
00123 #endif