* Cleanups

* operator* now returns a reference to the current SCC, instead of a possibly
  null pointer


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@8261 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2003-08-31 19:34:27 +00:00
parent 0c0023b754
commit b55cae23cb
2 changed files with 28 additions and 42 deletions

View File

@ -16,17 +16,16 @@
#define SUPPORT_TARJANSCCITERATOR_H #define SUPPORT_TARJANSCCITERATOR_H
#include "Support/GraphTraits.h" #include "Support/GraphTraits.h"
#include "Support/Debug.h"
#include "Support/iterator" #include "Support/iterator"
#include <vector> #include <vector>
#include <map> #include <map>
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
// class SCC : A simple representation of an SCC in a generic Graph. // class SCC - A simple representation of an SCC in a generic Graph.
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
template<class GraphT, class GT = GraphTraits<GraphT> > template<class GraphT, class GT = GraphTraits<GraphT> >
struct SCC: public std::vector<typename GT::NodeType*> { struct SCC : public std::vector<typename GT::NodeType*> {
typedef typename GT::NodeType NodeType; typedef typename GT::NodeType NodeType;
typedef typename GT::ChildIteratorType ChildItTy; typedef typename GT::ChildIteratorType ChildItTy;
@ -43,7 +42,7 @@ struct SCC: public std::vector<typename GT::NodeType*> {
bool HasLoop() const { bool HasLoop() const {
if (size() > 1) return true; if (size() > 1) return true;
NodeType* N = front(); NodeType* N = front();
for (ChildItTy CI=GT::child_begin(N), CE=GT::child_end(N); CI != CE; ++CI) for (ChildItTy CI = GT::child_begin(N), CE=GT::child_end(N); CI != CE; ++CI)
if (*CI == N) if (*CI == N)
return true; return true;
return false; return false;
@ -56,8 +55,7 @@ struct SCC: public std::vector<typename GT::NodeType*> {
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
template<class GraphT, class GT = GraphTraits<GraphT> > template<class GraphT, class GT = GraphTraits<GraphT> >
class TarjanSCC_iterator : public forward_iterator<SCC<GraphT, GT>, ptrdiff_t> class TarjanSCC_iterator : public forward_iterator<SCC<GraphT, GT>, ptrdiff_t> {
{
typedef SCC<GraphT, GT> SccTy; typedef SCC<GraphT, GT> SccTy;
typedef forward_iterator<SccTy, ptrdiff_t> super; typedef forward_iterator<SccTy, ptrdiff_t> super;
typedef typename super::reference reference; typedef typename super::reference reference;
@ -185,32 +183,27 @@ public:
_Self tmp = *this; ++*this; return tmp; _Self tmp = *this; ++*this; return tmp;
} }
// Retrieve a pointer to the current SCC. Returns NULL when done. // Retrieve a reference to the current SCC
inline const SccTy* operator*() const { inline const SccTy &operator*() const {
assert(!CurrentSCC.empty() || VisitStack.empty()); assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
return CurrentSCC.empty()? NULL : &CurrentSCC; return CurrentSCC;
} }
inline SccTy* operator*() { inline SccTy &operator*() {
assert(!CurrentSCC.empty() || VisitStack.empty()); assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
return CurrentSCC.empty()? NULL : &CurrentSCC; return CurrentSCC;
} }
}; };
// Global constructor for the Tarjan SCC iterator. Use *I == NULL or I.fini() // Global constructor for the Tarjan SCC iterator.
// to test termination efficiently, instead of I == the "end" iterator.
template <class T> template <class T>
TarjanSCC_iterator<T> tarj_begin(T G) TarjanSCC_iterator<T> tarj_begin(T G) {
{
return TarjanSCC_iterator<T>::begin(G); return TarjanSCC_iterator<T>::begin(G);
} }
template <class T> template <class T>
TarjanSCC_iterator<T> tarj_end(T G) TarjanSCC_iterator<T> tarj_end(T G) {
{
return TarjanSCC_iterator<T>::end(G); return TarjanSCC_iterator<T>::end(G);
} }
//===----------------------------------------------------------------------===//
#endif #endif

View File

@ -16,17 +16,16 @@
#define SUPPORT_TARJANSCCITERATOR_H #define SUPPORT_TARJANSCCITERATOR_H
#include "Support/GraphTraits.h" #include "Support/GraphTraits.h"
#include "Support/Debug.h"
#include "Support/iterator" #include "Support/iterator"
#include <vector> #include <vector>
#include <map> #include <map>
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
// class SCC : A simple representation of an SCC in a generic Graph. // class SCC - A simple representation of an SCC in a generic Graph.
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
template<class GraphT, class GT = GraphTraits<GraphT> > template<class GraphT, class GT = GraphTraits<GraphT> >
struct SCC: public std::vector<typename GT::NodeType*> { struct SCC : public std::vector<typename GT::NodeType*> {
typedef typename GT::NodeType NodeType; typedef typename GT::NodeType NodeType;
typedef typename GT::ChildIteratorType ChildItTy; typedef typename GT::ChildIteratorType ChildItTy;
@ -43,7 +42,7 @@ struct SCC: public std::vector<typename GT::NodeType*> {
bool HasLoop() const { bool HasLoop() const {
if (size() > 1) return true; if (size() > 1) return true;
NodeType* N = front(); NodeType* N = front();
for (ChildItTy CI=GT::child_begin(N), CE=GT::child_end(N); CI != CE; ++CI) for (ChildItTy CI = GT::child_begin(N), CE=GT::child_end(N); CI != CE; ++CI)
if (*CI == N) if (*CI == N)
return true; return true;
return false; return false;
@ -56,8 +55,7 @@ struct SCC: public std::vector<typename GT::NodeType*> {
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
template<class GraphT, class GT = GraphTraits<GraphT> > template<class GraphT, class GT = GraphTraits<GraphT> >
class TarjanSCC_iterator : public forward_iterator<SCC<GraphT, GT>, ptrdiff_t> class TarjanSCC_iterator : public forward_iterator<SCC<GraphT, GT>, ptrdiff_t> {
{
typedef SCC<GraphT, GT> SccTy; typedef SCC<GraphT, GT> SccTy;
typedef forward_iterator<SccTy, ptrdiff_t> super; typedef forward_iterator<SccTy, ptrdiff_t> super;
typedef typename super::reference reference; typedef typename super::reference reference;
@ -185,32 +183,27 @@ public:
_Self tmp = *this; ++*this; return tmp; _Self tmp = *this; ++*this; return tmp;
} }
// Retrieve a pointer to the current SCC. Returns NULL when done. // Retrieve a reference to the current SCC
inline const SccTy* operator*() const { inline const SccTy &operator*() const {
assert(!CurrentSCC.empty() || VisitStack.empty()); assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
return CurrentSCC.empty()? NULL : &CurrentSCC; return CurrentSCC;
} }
inline SccTy* operator*() { inline SccTy &operator*() {
assert(!CurrentSCC.empty() || VisitStack.empty()); assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
return CurrentSCC.empty()? NULL : &CurrentSCC; return CurrentSCC;
} }
}; };
// Global constructor for the Tarjan SCC iterator. Use *I == NULL or I.fini() // Global constructor for the Tarjan SCC iterator.
// to test termination efficiently, instead of I == the "end" iterator.
template <class T> template <class T>
TarjanSCC_iterator<T> tarj_begin(T G) TarjanSCC_iterator<T> tarj_begin(T G) {
{
return TarjanSCC_iterator<T>::begin(G); return TarjanSCC_iterator<T>::begin(G);
} }
template <class T> template <class T>
TarjanSCC_iterator<T> tarj_end(T G) TarjanSCC_iterator<T> tarj_end(T G) {
{
return TarjanSCC_iterator<T>::end(G); return TarjanSCC_iterator<T>::end(G);
} }
//===----------------------------------------------------------------------===//
#endif #endif