X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=include%2Fllvm%2FADT%2FSmallSet.h;h=cd117f59ba76ee963781ecdbbcf978a4c53590f8;hb=ac39a035351a20928e087617e412aa6ce510181f;hp=dd1081e3889ed177f18c05dbeca4812939478c5b;hpb=182907645c9c2da22dba78255f0c8541f5d9f50d;p=oota-llvm.git diff --git a/include/llvm/ADT/SmallSet.h b/include/llvm/ADT/SmallSet.h index dd1081e3889..cd117f59ba7 100644 --- a/include/llvm/ADT/SmallSet.h +++ b/include/llvm/ADT/SmallSet.h @@ -2,8 +2,8 @@ // // The LLVM Compiler Infrastructure // -// This file was developed by Chris Lattner and is distributed under -// the University of Illinois Open Source License. See LICENSE.TXT for details. +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // @@ -15,68 +15,103 @@ #define LLVM_ADT_SMALLSET_H #include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/SmallPtrSet.h" +#include namespace llvm { /// SmallSet - This maintains a set of unique values, optimizing for the case /// when the set is small (less than N). In this case, the set can be -/// maintained with no mallocs. +/// maintained with no mallocs. If the set gets large, we expand to using an +/// std::set to maintain reasonable lookup times. /// -/// Note that this set does not guarantee that the elements in the set will be -/// ordered. -template +/// Note that this set does not provide a way to iterate over members in the +/// set. +template > class SmallSet { + /// Use a SmallVector to hold the elements here (even though it will never + /// reach its 'large' stage) to avoid calling the default ctors of elements + /// we will never use. SmallVector Vector; + std::set Set; + typedef typename SmallVector::const_iterator VIterator; typedef typename SmallVector::iterator mutable_iterator; public: SmallSet() {} - // Support iteration. - typedef typename SmallVector::const_iterator iterator; - typedef typename SmallVector::const_iterator const_iterator; - - iterator begin() const { return Vector.begin(); } - iterator end() const { return Vector.end(); } - - bool empty() const { return Vector.empty(); } - unsigned size() const { return Vector.size(); } - - iterator find(const T &V) const { - for (iterator I = begin(), E = end(); I != E; ++I) - if (*I == V) - return I; - return end(); + bool empty() const { return Vector.empty() && Set.empty(); } + unsigned size() const { + return isSmall() ? Vector.size() : Set.size(); } - + /// count - Return true if the element is in the set. - unsigned count(const T &V) const { - // Since the collection is small, just do a linear search. - return find(V) != end(); + bool count(const T &V) const { + if (isSmall()) { + // Since the collection is small, just do a linear search. + return vfind(V) != Vector.end(); + } else { + return Set.count(V); + } } - + /// insert - Insert an element into the set if it isn't already there. - std::pair insert(const T &V) { - iterator I = find(V); - if (I == end()) // Don't reinsert if it already exists. - return std::make_pair(I, false); - Vector.push_back(V); - return std::make_pair(end()-1, true); + bool insert(const T &V) { + if (!isSmall()) + return Set.insert(V).second; + + VIterator I = vfind(V); + if (I != Vector.end()) // Don't reinsert if it already exists. + return false; + if (Vector.size() < N) { + Vector.push_back(V); + return true; + } + + // Otherwise, grow from vector to set. + while (!Vector.empty()) { + Set.insert(Vector.back()); + Vector.pop_back(); + } + Set.insert(V); + return true; + } + + template + void insert(IterT I, IterT E) { + for (; I != E; ++I) + insert(*I); } - void erase(const T &V) { + bool erase(const T &V) { + if (!isSmall()) + return Set.erase(V); for (mutable_iterator I = Vector.begin(), E = Vector.end(); I != E; ++I) if (*I == V) { Vector.erase(I); - return; + return true; } + return false; } - + void clear() { Vector.clear(); + Set.clear(); + } +private: + bool isSmall() const { return Set.empty(); } + + VIterator vfind(const T &V) const { + for (VIterator I = Vector.begin(), E = Vector.end(); I != E; ++I) + if (*I == V) + return I; + return Vector.end(); } - }; +/// If this set is of pointer values, transparently switch over to using +/// SmallPtrSet for performance. +template +class SmallSet : public SmallPtrSet {}; } // end namespace llvm