llvm.org GIT mirror llvm / 5106140
Extend SuccIterator Implement most of the missing methods to make SuccIterator random access. operator[] is still missing. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@93363 91177308-0d34-0410-b5e6-96231b3b80d8 Tobias Grosser 10 years ago
1 changed file(s) with 62 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
9292 public:
9393 typedef SuccIterator _Self;
9494 typedef typename super::pointer pointer;
95 // TODO: This can be random access iterator, need operator+ and stuff tho
95 // TODO: This can be random access iterator, only operator[] missing.
9696
9797 inline SuccIterator(Term_ T) : Term(T), idx(0) { // begin iterator
9898 assert(T && "getTerminator returned null!");
108108 return *this;
109109 }
110110
111 inline bool index_is_valid (int idx) {
112 return idx >= 0 && (unsigned) idx < Term->getNumSuccessors();
113 }
114
111115 /// getSuccessorIndex - This is used to interface between code that wants to
112116 /// operate on terminator instructions directly.
113117 unsigned getSuccessorIndex() const { return idx; }
119123 inline pointer operator->() const { return operator*(); }
120124
121125 inline _Self& operator++() { ++idx; return *this; } // Preincrement
126
122127 inline _Self operator++(int) { // Postincrement
123128 _Self tmp = *this; ++*this; return tmp;
124129 }
127132 inline _Self operator--(int) { // Postdecrement
128133 _Self tmp = *this; --*this; return tmp;
129134 }
135
136 inline bool operator<(const _Self& x) const {
137 assert(Term == x.Term && "Cannot compare iterators of different blocks!");
138 return idx < x.idx;
139 }
140
141 inline bool operator<=(const _Self& x) const {
142 assert(Term == x.Term && "Cannot compare iterators of different blocks!");
143 return idx <= x.idx;
144 }
145 inline bool operator>=(const _Self& x) const {
146 assert(Term == x.Term && "Cannot compare iterators of different blocks!");
147 return idx >= x.idx;
148 }
149
150 inline bool operator>(const _Self& x) const {
151 return idx > x.idx;
152 assert(Term == x.Term && "Cannot compare iterators of different blocks!");
153 }
154
155 inline _Self& operator+=(int Right) {
156 unsigned new_idx = idx + Right;
157 assert(index_is_valid(new_idx) && "Iterator index out of bound");
158 idx = new_idx;
159 return *this;
160 }
161
162 inline _Self operator+(int Right) {
163 _Self tmp = *this;
164 tmp += Right;
165 return tmp;
166 }
167
168 inline _Self& operator-=(int Right) {
169 return operator+=(-Right);
170 }
171
172 inline _Self operator-(int Right) {
173 return operator+(-Right);
174 }
175
176 inline int operator-(const _Self& x) {
177 assert(Term == x.Term && "Cannot work on iterators of different blocks!");
178 int distance = idx - x.idx;
179 return distance;
180 }
181
182 // This works for read access, however write access is difficult as changes
183 // to Term are only possible with Term->setSuccessor(idx). Pointers that can
184 // be modified are not available.
185 //
186 // inline pointer operator[](int offset) {
187 // _Self tmp = *this;
188 // tmp += offset;
189 // return tmp.operator*();
190 // }
130191 };
131192
132193 typedef SuccIterator succ_iterator;