Index: include/llvm/ADT/EquivalenceClasses.h =================================================================== --- include/llvm/ADT/EquivalenceClasses.h +++ include/llvm/ADT/EquivalenceClasses.h @@ -15,6 +15,7 @@ #ifndef LLVM_ADT_EQUIVALENCECLASSES_H #define LLVM_ADT_EQUIVALENCECLASSES_H +#include "llvm/ADT/iterator.h" #include "llvm/Support/DataTypes.h" #include #include @@ -150,10 +151,23 @@ // Only leaders provide anything to iterate over. return member_iterator(I->isLeader() ? &*I : nullptr); } + class leader_iterator; + member_iterator member_begin(leader_iterator I) const { + return member_iterator(&*I); + } member_iterator member_end() const { return member_iterator(nullptr); } + /// Iterate over the leaders (i.e. the equivalence classes). + leader_iterator leader_begin() const { + // Only leaders provide anything to iterate over. + return leader_iterator(TheMapping.begin(), TheMapping.end()); + } + leader_iterator leader_end() const { + return leader_iterator(TheMapping.end(), TheMapping.end()); + } + /// findValue - Return an iterator to the specified value. If it does not /// exist, end() is returned. iterator findValue(const ElemTy &V) const { @@ -276,6 +290,51 @@ return Node != RHS.Node; } }; + + class leader_iterator + : public iterator_facade_base { + typedef iterator_facade_base super; + + iterator Iter; + iterator End; + + void findLeader() { + for (; Iter != End; ++Iter) + if (Iter->isLeader()) + break; + } + + public: + typedef size_t size_type; + typedef typename super::pointer pointer; + typedef typename super::reference reference; + + explicit leader_iterator() {} + explicit leader_iterator(iterator First, iterator Last) + : Iter(First), End(Last) { + // Ensure that Iter is pointing at the first leader. + findLeader(); + } + + leader_iterator &operator++() { + assert(Iter != End && "++'d off the end of the list!"); + ++Iter; + // If this is not a leader, keep going. + findLeader(); + return *this; + } + + reference operator*() const { + assert(Iter != End && "Dereferencing end()!"); + return *Iter; + } + + bool operator==(const leader_iterator &RHS) const { + return Iter == RHS.Iter; + } + }; }; } // End llvm namespace