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