Index: llvm/trunk/unittests/ADT/SmallSetTest.cpp =================================================================== --- llvm/trunk/unittests/ADT/SmallSetTest.cpp (revision 336804) +++ llvm/trunk/unittests/ADT/SmallSetTest.cpp (revision 336805) @@ -1,70 +1,125 @@ //===- llvm/unittest/ADT/SmallSetTest.cpp ------------------------------===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // // SmallSet unit tests. // //===----------------------------------------------------------------------===// #include "llvm/ADT/SmallSet.h" #include "gtest/gtest.h" +#include using namespace llvm; TEST(SmallSetTest, Insert) { SmallSet s1; for (int i = 0; i < 4; i++) s1.insert(i); for (int i = 0; i < 4; i++) s1.insert(i); EXPECT_EQ(4u, s1.size()); for (int i = 0; i < 4; i++) EXPECT_EQ(1u, s1.count(i)); EXPECT_EQ(0u, s1.count(4)); } TEST(SmallSetTest, Grow) { SmallSet s1; for (int i = 0; i < 8; i++) s1.insert(i); EXPECT_EQ(8u, s1.size()); for (int i = 0; i < 8; i++) EXPECT_EQ(1u, s1.count(i)); EXPECT_EQ(0u, s1.count(8)); } TEST(SmallSetTest, Erase) { SmallSet s1; for (int i = 0; i < 8; i++) s1.insert(i); EXPECT_EQ(8u, s1.size()); // Remove elements one by one and check if all other elements are still there. for (int i = 0; i < 8; i++) { EXPECT_EQ(1u, s1.count(i)); EXPECT_TRUE(s1.erase(i)); EXPECT_EQ(0u, s1.count(i)); EXPECT_EQ(8u - i - 1, s1.size()); for (int j = i + 1; j < 8; j++) EXPECT_EQ(1u, s1.count(j)); } EXPECT_EQ(0u, s1.count(8)); } + +TEST(SmallSetTest, IteratorInt) { + SmallSet s1; + + // Test the 'small' case. + for (int i = 0; i < 3; i++) + s1.insert(i); + + std::vector V(s1.begin(), s1.end()); + // Make sure the elements are in the expected order. + std::sort(V.begin(), V.end()); + for (int i = 0; i < 3; i++) + EXPECT_EQ(i, V[i]); + + // Test the 'big' case by adding a few more elements to switch to std::set + // internally. + for (int i = 3; i < 6; i++) + s1.insert(i); + + V.assign(s1.begin(), s1.end()); + // Make sure the elements are in the expected order. + std::sort(V.begin(), V.end()); + for (int i = 0; i < 6; i++) + EXPECT_EQ(i, V[i]); +} + +TEST(SmallSetTest, IteratorString) { + // Test SmallSetIterator for SmallSet with a type with non-trivial + // ctors/dtors. + SmallSet s1; + + s1.insert("str 1"); + s1.insert("str 2"); + s1.insert("str 1"); + + std::vector V(s1.begin(), s1.end()); + std::sort(V.begin(), V.end()); + EXPECT_EQ(2u, s1.size()); + EXPECT_EQ("str 1", V[0]); + EXPECT_EQ("str 2", V[1]); + + s1.insert("str 4"); + s1.insert("str 0"); + s1.insert("str 4"); + + V.assign(s1.begin(), s1.end()); + // Make sure the elements are in the expected order. + std::sort(V.begin(), V.end()); + EXPECT_EQ(4u, s1.size()); + EXPECT_EQ("str 0", V[0]); + EXPECT_EQ("str 1", V[1]); + EXPECT_EQ("str 2", V[2]); + EXPECT_EQ("str 4", V[3]); +} Index: llvm/trunk/include/llvm/ADT/SmallSet.h =================================================================== --- llvm/trunk/include/llvm/ADT/SmallSet.h (revision 336804) +++ llvm/trunk/include/llvm/ADT/SmallSet.h (revision 336805) @@ -1,142 +1,209 @@ //===- llvm/ADT/SmallSet.h - 'Normally small' sets --------------*- C++ -*-===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // // This file defines the SmallSet class. // //===----------------------------------------------------------------------===// #ifndef LLVM_ADT_SMALLSET_H #define LLVM_ADT_SMALLSET_H #include "llvm/ADT/None.h" #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/iterator.h" #include "llvm/Support/Compiler.h" +#include "llvm/Support/type_traits.h" #include #include #include +#include #include namespace llvm { +/// SmallSetIterator - This class implements a const_iterator for SmallSet by +/// delegating to the underlying SmallVector or Set iterators. +template +class SmallSetIterator + : public iterator_facade_base, + std::forward_iterator_tag, T> { +private: + using SetIterTy = typename std::set::const_iterator; + using VecIterTy = typename SmallVector::const_iterator; + using SelfTy = SmallSetIterator; + + /// Iterators to the parts of the SmallSet containing the data. They are set + /// depending on isSmall. + union { + SetIterTy SetIter; + VecIterTy VecIter; + }; + + bool isSmall; + +public: + SmallSetIterator(SetIterTy SetIter) : SetIter(SetIter), isSmall(false) { + // Use static_assert here, as the SmallSetIterator type is incomplete in the + // class scope. + static_assert(std::is_trivially_destructible() && + llvm::is_trivially_copy_constructible() && + llvm::is_trivially_move_constructible(), + "SelfTy needs to by trivially constructible and destructible"); + } + + SmallSetIterator(VecIterTy VecIter) : VecIter(VecIter), isSmall(true) {} + + bool operator==(const SmallSetIterator &RHS) const { + if (isSmall != RHS.isSmall) + return false; + if (isSmall) + return VecIter == RHS.VecIter; + return SetIter == RHS.SetIter; + } + + SmallSetIterator &operator++() { // Preincrement + if (isSmall) + VecIter++; + else + SetIter++; + return *this; + } + + const T &operator*() const { return isSmall ? *VecIter : *SetIter; } +}; + /// 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. If the set gets large, we expand to using an /// std::set to maintain reasonable lookup times. /// /// 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; using VIterator = typename SmallVector::const_iterator; using mutable_iterator = typename SmallVector::iterator; // In small mode SmallPtrSet uses linear search for the elements, so it is // not a good idea to choose this value too high. You may consider using a // DenseSet<> instead if you expect many elements in the set. static_assert(N <= 32, "N should be small"); public: using size_type = size_t; + using const_iterator = SmallSetIterator; SmallSet() = default; LLVM_NODISCARD bool empty() const { return Vector.empty() && Set.empty(); } size_type size() const { return isSmall() ? Vector.size() : Set.size(); } /// count - Return 1 if the element is in the set, 0 otherwise. size_type count(const T &V) const { if (isSmall()) { // Since the collection is small, just do a linear search. return vfind(V) == Vector.end() ? 0 : 1; } else { return Set.count(V); } } /// insert - Insert an element into the set if it isn't already there. /// Returns true if the element is inserted (it was not in the set before). /// The first value of the returned pair is unused and provided for /// partial compatibility with the standard library self-associative container /// concept. // FIXME: Add iterators that abstract over the small and large form, and then // return those here. std::pair insert(const T &V) { if (!isSmall()) return std::make_pair(None, Set.insert(V).second); VIterator I = vfind(V); if (I != Vector.end()) // Don't reinsert if it already exists. return std::make_pair(None, false); if (Vector.size() < N) { Vector.push_back(V); return std::make_pair(None, true); } // Otherwise, grow from vector to set. while (!Vector.empty()) { Set.insert(Vector.back()); Vector.pop_back(); } Set.insert(V); return std::make_pair(None, true); } template void insert(IterT I, IterT E) { for (; I != E; ++I) insert(*I); } 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 true; } return false; } void clear() { Vector.clear(); Set.clear(); } + const_iterator begin() const { + if (isSmall()) + return {Vector.begin()}; + return {Set.begin()}; + } + + const_iterator end() const { + if (isSmall()) + return {Vector.end()}; + return {Set.end()}; + } + 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 #endif // LLVM_ADT_SMALLSET_H