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/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 public:
00041   typedef size_t size_type;
00042   SmallSet() {}
00043 
00044   bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const {
00045     return Vector.empty() && Set.empty();
00046   }
00047 
00048   size_type size() const {
00049     return isSmall() ? Vector.size() : Set.size();
00050   }
00051 
00052   /// count - Return 1 if the element is in the set, 0 otherwise.
00053   size_type count(const T &V) const {
00054     if (isSmall()) {
00055       // Since the collection is small, just do a linear search.
00056       return vfind(V) == Vector.end() ? 0 : 1;
00057     } else {
00058       return Set.count(V);
00059     }
00060   }
00061 
00062   /// insert - Insert an element into the set if it isn't already there.
00063   /// Returns true if the element is inserted (it was not in the set before).
00064   /// The first value of the returned pair is unused and provided for
00065   /// partial compatibility with the standard library self-associative container
00066   /// concept.
00067   // FIXME: Add iterators that abstract over the small and large form, and then
00068   // return those here.
00069   std::pair<NoneType, bool> insert(const T &V) {
00070     if (!isSmall())
00071       return std::make_pair(None, Set.insert(V).second);
00072 
00073     VIterator I = vfind(V);
00074     if (I != Vector.end())    // Don't reinsert if it already exists.
00075       return std::make_pair(None, false);
00076     if (Vector.size() < N) {
00077       Vector.push_back(V);
00078       return std::make_pair(None, true);
00079     }
00080 
00081     // Otherwise, grow from vector to set.
00082     while (!Vector.empty()) {
00083       Set.insert(Vector.back());
00084       Vector.pop_back();
00085     }
00086     Set.insert(V);
00087     return std::make_pair(None, true);
00088   }
00089 
00090   template <typename IterT>
00091   void insert(IterT I, IterT E) {
00092     for (; I != E; ++I)
00093       insert(*I);
00094   }
00095   
00096   bool erase(const T &V) {
00097     if (!isSmall())
00098       return Set.erase(V);
00099     for (mutable_iterator I = Vector.begin(), E = Vector.end(); I != E; ++I)
00100       if (*I == V) {
00101         Vector.erase(I);
00102         return true;
00103       }
00104     return false;
00105   }
00106 
00107   void clear() {
00108     Vector.clear();
00109     Set.clear();
00110   }
00111 private:
00112   bool isSmall() const { return Set.empty(); }
00113 
00114   VIterator vfind(const T &V) const {
00115     for (VIterator I = Vector.begin(), E = Vector.end(); I != E; ++I)
00116       if (*I == V)
00117         return I;
00118     return Vector.end();
00119   }
00120 };
00121 
00122 /// If this set is of pointer values, transparently switch over to using
00123 /// SmallPtrSet for performance.
00124 template <typename PointeeType, unsigned N>
00125 class SmallSet<PointeeType*, N> : public SmallPtrSet<PointeeType*, N> {};
00126 
00127 } // end namespace llvm
00128 
00129 #endif