From 299918ad4813ded5eb717c0c4898eb67205d880b Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Mon, 10 Feb 2014 14:17:42 +0000 Subject: Make succ_iterator a real random access iterator and clean up a couple of users. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@201088 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/CFG.h | 52 +++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 17 deletions(-) (limited to 'include') diff --git a/include/llvm/Support/CFG.h b/include/llvm/Support/CFG.h index a9427793ac..c0733ca859 100644 --- a/include/llvm/Support/CFG.h +++ b/include/llvm/Support/CFG.h @@ -101,23 +101,45 @@ inline const_pred_iterator pred_end(const BasicBlock *BB) { //===----------------------------------------------------------------------===// template // Successor Iterator -class SuccIterator : public std::iterator { +class SuccIterator : public std::iterator { + typedef std::iterator + super; + +public: + typedef typename super::pointer pointer; + typedef typename super::reference reference; + +private: const Term_ Term; unsigned idx; - typedef std::iterator super; typedef SuccIterator Self; inline bool index_is_valid(int idx) { return idx >= 0 && (unsigned) idx < Term->getNumSuccessors(); } -public: - typedef typename super::pointer pointer; - typedef typename super::reference reference; - // TODO: This can be random access iterator, only operator[] missing. + /// \brief Proxy object to allow write access in operator[] + class SuccessorProxy { + Self it; + + public: + explicit SuccessorProxy(const Self &it) : it(it) {} + + SuccessorProxy &operator=(SuccessorProxy r) { + *this = reference(r); + return *this; + } + + SuccessorProxy &operator=(reference r) { + it.Term->setSuccessor(it.idx, r); + return *this; + } + operator reference() const { return *it; } + }; + +public: explicit inline SuccIterator(Term_ T) : Term(T), idx(0) {// begin iterator } inline SuccIterator(Term_ T, bool) // end iterator @@ -206,15 +228,11 @@ public: return distance; } - // This works for read access, however write access is difficult as changes - // to Term are only possible with Term->setSuccessor(idx). Pointers that can - // be modified are not available. - // - // inline pointer operator[](int offset) { - // Self tmp = *this; - // tmp += offset; - // return tmp.operator*(); - // } + inline SuccessorProxy operator[](int offset) { + Self tmp = *this; + tmp += offset; + return SuccessorProxy(tmp); + } /// Get the source BB of this iterator. inline BB_ *getSource() { -- cgit v1.2.3