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 | c95ce87c237796ffba0d53aa4d9927cb380cd7ce (patch) | |
tree | 2c95ab46274256862de8ef63f64dd94e750cee1a /include | |
parent | a30ccb064b8e0d67c26e087b191561ee2e06aac9 (diff) | |
download | llvm-c95ce87c237796ffba0d53aa4d9927cb380cd7ce.tar.gz llvm-c95ce87c237796ffba0d53aa4d9927cb380cd7ce.tar.bz2 llvm-c95ce87c237796ffba0d53aa4d9927cb380cd7ce.tar.xz |
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.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@206453 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include')
-rw-r--r-- | include/llvm/IR/User.h | 51 |
1 files changed, 39 insertions, 12 deletions
diff --git a/include/llvm/IR/User.h b/include/llvm/IR/User.h index abe68221df..66442e82df 100644 --- a/include/llvm/IR/User.h +++ b/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() { |