简体   繁体   English

是否执行本地排序的集合或优先级队列?

[英]Implementation of a locally ordered set or priority queue?

I have a rather large set of objects that represent numbers and I want to select such numbers according to a custom ordering. 我有一组代表数字的对象,我想根据自定义顺序选择这些数字。 This ordering includes several criteria such as the type of their representation (some numbers are represented by an interval), their integrality and ultimatively their value. 此排序包括几个条件,例如它们的表示类型(一些数字由一个间隔表示),它们的完整性以及最终的值。 These numbers are shared throughout the programs (shared pointers) and there is nothing I can do about this. 这些数字在程序中共享(共享指针),对此我无能为力。

However, the elements properties can change at any time such that the order changes while I can't notify the container about this. 但是,elements属性可以随时更改,以便顺序更改,而我无法将此通知容器。 For example, some operations require a refinement of a number that is represented by an interval and during this refinement, the exact value can be found. 例如,某些操作需要细化以间隔表示的数字,并且在此细化期间,可以找到确切的值。 Thereby, the number changes from the interval representation to a rational number, possibly even an integer. 因此,数字从间隔表示变为有理数,甚至可能是整数。 This change, due to the shared instance, immediately propagates to the number in the container and breaks the ordering (and I don't even know which number changed). 由于共享实例的原因,此更改会立即传播到容器中的数字并中断排序(我什至不知道更改了哪个数字)。 This totally breaks std::set . 这完全破坏了std::set

So what I'd like to have is a container that tries to be sorted, but does not rely on this. 因此,我想拥有一个试图进行排序但不依赖于此的容器。 Whenever an operation detects an incorrect ordering, this ordering should be corrected locally. 每当操作检测到错误的排序时,都应在本地更正此排序。 For example insert would insert the element (using binary search) and always check if the ordering of the current element (wrt the neighbors) is correct. 例如, insert会插入元素(使用二进制搜索),并始终检查当前元素的顺序(是否破坏了邻居)是否正确。

I'd be willing to accept that "give me the smallest element" would then be only "give me a small element" and that find or remove would have linear complexity: I only need front , insert and remove_front to be particularly efficient. 我愿意接受“给我最小的元素”只是“给我一个小元素”,而findremove将具有线性复杂性:我只需要frontinsertremove_front才特别有效。

Is there any implementation that does something like this? 有没有实现这样的实现的东西? How would you implement this? 您将如何实施?

If you are looking for an algorithm in the standard library, you should take a look at: 如果要在标准库中查找算法,则应查看以下内容:

std::make_heap
std::pop_heap
std::push_heap

In <algorithm> . <algorithm> They might fit your need, and even if they don't I'm quite sure you will find what you are looking for in some kind of heap structure. 它们可能满足您的需求,即使他们不满足,我也可以肯定您会在某种堆结构中找到所需的内容。 Which one will probably depend on how your code is structured, and how often you expect a value to change etc. 哪一个可能取决于代码的结构以及期望值更改的频率等。

In short: 简而言之:

A heap is a data structure in which it is fast to find and extract the smallest (or largest) element. 堆是一种数据结构,可以在其中快速查找和提取最小(或最大)元素。 It is also for most heaps possible to create restructure the heap in linear time or better. 对于大多数堆来说,也可以在线性时间或更短的时间内创建重组结构。 You could start out from this page on Wikipedia if you want to learn more about heaps. 如果要了解有关堆的更多信息,可以从Wikipedia上的此页面开始。

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

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