简体   繁体   English

如何创建一个过去的迭代器?

[英]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. 广度优先迭代器本质上是底层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. 我不能只使用QList::end()因为不能保证最低级别的最右边节点也是整个树的最右边节点。 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). 我不愿意制作一个可以测试的'假'BVH节点,因为它会涉及大量的代码更改(可能是开销),让各种节点管理机制忽略假节点,并禁用很多树构建自动化(例如,假节点的父节点必须被告知它是叶子)。 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. 简要地看了一下qlist.h,看来你将无法对两种迭代类型使用相同的end()。 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. 但是没关系 - 你可以使用空指针或静态虚拟或其他技术为你的第二个迭代方法创建一个end()迭代器。 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). 我不明白为什么这会影响大量的其他代码(大多数代码应该只是引用end()而不知道它的实现细节)。

Can you not just use null or something like this as end? 你能不能只使用null或类似的东西作为结束? This is at least what I would expect eg for a linked list structure. 这至少是我期望的,例如链表结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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