| 236 | // Very basic iterator, that permits to walk linked list in backwards direction |
| 237 | // and remove elements along the way. |
| 238 | class reverse_iterator |
| 239 | { |
| 240 | private: |
| 241 | Stack<Object, Capacity>& stack; |
| 242 | Stack<Entry*> entries; |
| 243 | Entry* current_entry; |
| 244 | FB_SIZE_T elem; |
| 245 | |
| 246 | public: |
| 247 | explicit reverse_iterator(Stack<Object, Capacity>& s) |
| 248 | : stack(s), entries(s.getPool()), elem(0) |
| 249 | { |
| 250 | current_entry = s.stk; |
| 251 | if (current_entry) { |
| 252 | while (Entry *next = current_entry->next) { |
| 253 | entries.push(current_entry); |
| 254 | current_entry = next; |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | bool hasData() const |
| 260 | { |
| 261 | return current_entry; |
| 262 | } |
| 263 | |
| 264 | reverse_iterator& operator++() |
| 265 | { |
| 266 | fb_assert(current_entry); |
| 267 | elem++; |
| 268 | |
| 269 | if (elem >= current_entry->getCount()) { |
| 270 | elem = 0; |
| 271 | if (entries.hasData()) |
| 272 | current_entry = entries.pop(); |
| 273 | else |
| 274 | current_entry = NULL; |
| 275 | } |
| 276 | |
| 277 | return *this; |
| 278 | } |
| 279 | |
| 280 | Object object() const |
| 281 | { |
| 282 | fb_assert(current_entry); |
| 283 | return current_entry->getObject(elem); |
| 284 | } |
| 285 | |
| 286 | void remove() { |
| 287 | fb_assert(current_entry); |
| 288 | current_entry->remove(elem); |
| 289 | |
| 290 | if (elem >= current_entry->getCount()) { |
| 291 | if (elem) { |
| 292 | // Simple case - just advance pointer |
| 293 | elem = 0; |
| 294 | if (entries.hasData()) |
| 295 | current_entry = entries.pop(); |