简体   繁体   中英

How to create a past-the-end iterator?

I have created a binary tree structure to store a bounded volume hierarchy, to make it easier to use (and safer) I created two iterators to complement it: breadth-first and depth-first.

The breadth-first iterator is essentially a wrapper for the underlying QList. But I am stuck on the depth-first iterator (bidirectional only), I can handle the actual iteration around the tree, I just do not how to create a past-the-end iterator.

I can't just use the QList::end() because there is no guarantee the lowest-level rightmost node is also the rightmost node of the whole tree. I'm reluctant to make a 'fake' BVH node that can tested for because it will involve a large code change (and probably overhead) to have the various node management mechanisms ignore the fake node, and disable a lot of the tree building automation (for example the parent of the fake node will have to be told it is a leaf). But if this is the only way - then it is the only way.

Having looked briefly at qlist.h, it appears that you won't be able to use the same end() for both iteration types. But that's OK--you can use a null pointer or a static dummy or other techniques to make an end() iterator for your second iteration method. I don't see why this would have to impact a huge amount of other code (most of which should just refer to end() without knowing its implementation details).

Can you not just use null or something like this as end? This is at least what I would expect eg for a linked list structure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM