是否有任何技术原因的std :: LOWER_BOUND不是专用于红黑树迭代器? [英] Is there any technical reason why std::lower_bound is not specialized for red-black tree iterators?

查看:484
本文介绍了是否有任何技术原因的std :: LOWER_BOUND不是专用于红黑树迭代器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直认为的std :: LOWER_BOUND()运行在对数时间,如果我通过了一双红黑树迭代器(集::迭代器图:迭代)给它。我不得不把自己烧得两次注意到的std :: LOWER_BOUND()在O(n)的运行时间在这种情况下,至少用libstdc ++实现。据我所知,标准不具有红黑树迭代器的概念; 的std :: LOWER_BOUND()会把这作为一个双向迭代器和事先他们线性时间。 我还没有看到任何理由实施不能创建一个特定于实现迭代器标签红黑树迭代器并调用一个专门的 LOWER_BOUND()如果在迭代器传递恰巧是红黑树的迭代器。

I have always assumed that std::lower_bound() runs in logarithmic time if I pass a pair of red-black tree iterators (set::iterator or map::iterator) to it. I had to burn myself twice to notice that std::lower_bound() runs in O(n) time in this case, at least with the libstdc++ implementation. I understand that the standard doesn't have the concept of red-black tree iterators; std::lower_bound() will treat them as bidirectional iterators and advance them in linear time. I still don't see any reason why the implementation couldn't create an implementation specific iterator tag for red-black tree iterators and call a specialized lower_bound() if the passed in iterators happen to be red-black tree iterators.

有任何技术原因的std :: LOWER_BOUND()不是专用于红黑树的迭代器?

Is there any technical reason why std::lower_bound() is not specialized for red-black tree iterators?

更新:我知道的查找该会员的功能,但它是如此不可的地步(在模板code我可能无法访问为仅在容器的一部分,容器或工作。)

UPDATE: Yes, I am aware of the find member functions but it is so not the point. (In a templated code I may not have access to the container or work only on a part of container.)

之后的赏金已经过期:我觉得迈赫达德的和Yakk的回答最有说服力的。我不能太之间做出选择;我给的赏金迈赫达德并接受Yakk的答案。

After the bounty has expired: I find Mehrdad's and Yakk's answers the most convincing. I couldn't decide between the too; I am giving the bounty to Mehrdad and accepting Yakk's answer.

推荐答案

不存在技术原因,这可能无法实现。

There is no technical reason why this could not be implemented.

要证明,我将勾勒出一个方法来实现这一点。

To demonstrate, I will sketch out a way to implement this.

我们添加一个新的迭代器种类, SkipableIterator 。这是 BiDirectionalIterator 的子类型和超类型 RandomAccessIterator的

We add a new Iterator category, SkipableIterator. It is a subtype of BiDirectionalIterator and a supertype of RandomAccessIterator.

SkipableIterator 的担保这之间的函数 做这一点,在的std ::在是可见的作品。

SkipableIterators guarantee that the function between done in a context where std::between is visible works.

template<typeanme SkipableIterator>
SkipableIterator between( SkipableIterator begin, SkipableIterator end )

返回介于的迭代器开始结束 。它返回结束当且仅当 ++开始==结束结束是之后开始)。

between returns an iterator between begin and end. It returns end if and only if ++begin == end (end is right after begin).

从概念上讲,应有效地找到一个元素大约一半的位置开始结束,但我们应该谨慎,让一个随机跳跃列表或一个平衡的红黑树双方的工作。

Conceptually, between should efficiently find an element "about half way between" begin and end, but we should be careful to allow a randomized skip list or a balanced red black tree to both work.

随机访问迭代器有之间的一个非常简单的实现的 - 收益率(开始+((最终开始)+1)/ 2;

添加一个新的标签也很容易。推导使得现有的code工作做好,只要他们正确使用标记的调度(并没有明确的专业),但有一个小的关注破损这里。我们可以有的标签版本,其中 iterator_category_2 的iterator_category 的细化(或soemthing少哈克),或者我们可以使用完全不同的机制来谈谈skipable迭代器(一个独立的迭代器特性?)。

Adding a new tag is also easy. Derivation makes existing code work well so long as they properly used tag dispatching (and did not explicitly specialize), but there is a small concern of breakage here. We could have "tag versions" where iterator_category_2 is a refinement of iterator_category (or soemthing less hacky), or we could use a completely different mechanism to talk about skipable iterators (an independent iterator trait?).

一旦我们有这个能力,我们可以写一个快速有序搜索算法对地图 / 设置工作。它也将努力像的QList 跳跃列表容器。这可能是即使是相同的实现作为随机存取版本!

Once we have this ability, we can write a fast ordered searching algorithms that works on map/set and multi. It would also work on a skip list container like QList. It might be even the same implementation as the random access version!

这篇关于是否有任何技术原因的std :: LOWER_BOUND不是专用于红黑树迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆