diff options
| author | Chandler Carruth <chandlerc@gmail.com> | 2014-04-17 09:07:50 +0000 |
|---|---|---|
| committer | Chandler Carruth <chandlerc@gmail.com> | 2014-04-17 09:07:50 +0000 |
| commit | 4c1b05f822f025d4807f364e07b6a4e5696049bf (patch) | |
| tree | ffac0b4d218c893d9dc4bd54bec7c32e16378f93 /llvm/include | |
| parent | de4077a5d3268a4df41b418e21040fe3683b68d1 (diff) | |
| download | bcm5719-llvm-4c1b05f822f025d4807f364e07b6a4e5696049bf.tar.gz bcm5719-llvm-4c1b05f822f025d4807f364e07b6a4e5696049bf.zip | |
Make the User::value_op_iterator a random access iterator. I had written
this code ages ago and lost track of it. Seems worth doing though --
this thing can get called from places that would benefit from knowing
that std::distance is O(1). Also add a very fledgeling unittest for
Users and make sure various aspects of this seem to work reasonably.
llvm-svn: 206453
Diffstat (limited to 'llvm/include')
| -rw-r--r-- | llvm/include/llvm/IR/User.h | 51 |
1 files changed, 39 insertions, 12 deletions
diff --git a/llvm/include/llvm/IR/User.h b/llvm/include/llvm/IR/User.h index abe68221df7..66442e82df0 100644 --- a/llvm/include/llvm/IR/User.h +++ b/llvm/include/llvm/IR/User.h @@ -129,11 +129,12 @@ public: /// Convenience iterator for directly iterating over the Values in the /// OperandList - class value_op_iterator : public std::iterator<std::forward_iterator_tag, - Value*> { + class value_op_iterator + : public std::iterator<std::random_access_iterator_tag, Value *, + ptrdiff_t, Value *, Value *> { op_iterator OI; public: - explicit value_op_iterator(Use *U) : OI(U) {} + explicit value_op_iterator(Use *U = nullptr) : OI(U) {} bool operator==(const value_op_iterator &x) const { return OI == x.OI; @@ -142,21 +143,47 @@ public: return !operator==(x); } - /// Iterator traversal: forward iteration only - value_op_iterator &operator++() { // Preincrement - ++OI; + value_op_iterator &operator+=(ptrdiff_t n) { + OI += n; return *this; } - value_op_iterator operator++(int) { // Postincrement - value_op_iterator tmp = *this; ++*this; return tmp; + value_op_iterator &operator-=(ptrdiff_t n) { + OI -= n; + return *this; } - - /// Retrieve a pointer to the current Value. - Value *operator*() const { - return *OI; + value_op_iterator operator+(ptrdiff_t n) const { + return value_op_iterator(OI + n); + } + friend value_op_iterator operator+(ptrdiff_t n, + const value_op_iterator &i) { + return i + n; + } + value_op_iterator operator-(ptrdiff_t n) const { + return value_op_iterator(OI - n); + } + ptrdiff_t operator-(const value_op_iterator &RHS) const { + return OI - RHS.OI; + } + bool operator<(const value_op_iterator &RHS) const { return OI < RHS.OI; } + bool operator>(const value_op_iterator &RHS) const { return OI > RHS.OI; } + bool operator<=(const value_op_iterator &RHS) const { return OI <= RHS.OI; } + bool operator>=(const value_op_iterator &RHS) const { return OI >= RHS.OI; } + value_op_iterator &operator++() { return *this += 1; } + value_op_iterator &operator--() { return *this -= 1; } + value_op_iterator operator++(int) { + value_op_iterator tmp = *this; + ++*this; + return tmp; + } + value_op_iterator operator--(int) { + value_op_iterator tmp = *this; + --*this; + return tmp; } + Value *operator*() const { return *OI; } Value *operator->() const { return operator*(); } + Value *operator[](ptrdiff_t n) const { return *(*this + n); } }; inline value_op_iterator value_op_begin() { |

