简体   繁体   English

使用可更改元素确定优先级队列的最佳ADT(C ++)

[英]Determining the best ADT for a priority queue with changeable elements (C++)

First post here and I'm a beginner - hope I'm making myself useful... 第一篇文章,我是初学者 - 希望我自己有用......

I'm trying to find and understand the ADT/concept that does the job I'm after. 我正在努力寻找和理解完成我所追求的工作的ADT /概念。 I'm guessing it's already out there. 我猜它已经在那里了。

I have an array/list/tree (container to be decided) of objects each of which has a count associated with how much it hasn't been used over iterations of a process. 我有一个对象的数组/列表/树(容器待定),每个对象的计数都与过程迭代中使用的数量相关联。 As iterations proceed the count for each object accumulates by 1. The idea is that sooner or later I'm going to need the memory that any unused objects are using so I'll delete them to make space for an object not in RAM (which will have an initial count of '0') - But, if it turns out that I use an object that is still in memory it's count is reset to '0', and I pat myself on the back for not having had to access the disk for its contents. 随着迭代的进行,每个对象的计数累加1.想法是迟早我将需要任何未使用的对象正在使用的内存,所以我将删除它们以为不在RAM中的对象腾出空间(其中将初始计数为'0') - 但是,如果事实证明我使用的是仍然在内存中的对象,那么它的计数将被重置为'0',并且我自己拍了拍,因为没有必要访问磁盘的内容。

A cache? 缓存?

The main process loop would have something similar to the following in it: 主进程循环将具有类似于以下内容的内容:

if (object needs to be added && (totalNumberOfObjects > someConstant))
    object with highest count deleted from RAM and the (heap??)
    newObject added with a count of '0'

if (an object already in RAM is accessed by the process)
    accessedObject count is set to '0'

for (All objects in RAM) 
    count++

I could bash about for a (long and buggy time) and build my own mess, but I thought it'd be interesting to learn the most efficient way from word go. 我可以抨击一个(长时间和错误的时间)并构建我自己的混乱,但我认为从单词去学习最有效的方法会很有趣。

Something like a heap? 像堆一样的东西?

You could use a heap for this, but I think it would be overkill. 你可以使用堆来做这件事,但我认为这样会有点过分。 It sounds like you're not going to have a lot of different values for the counts, and you'll have a lot of objects with each count. 听起来你不会有很多不同的计数值,每个计数你都会有很多对象。 If that's true, then you only need thread the objects onto a list of objects with the same count. 如果这是真的,那么您只需要将对象线程化到具有相同计数的对象列表中。 These lists are themselves arranged in a dequeue (or 'deque' as C++ insists on calling it). 这些列表本身以dequeue(或者deque'排列,因为C ++坚持调用它)。

The key here is that you need to increment the count of all objects, and presumably you want that to be O(1) if possible, rather than O(N). 这里的关键是你需要增加所有对象的数量,并且如果可能的话,你可能希望它是O(1),而不是O(N)。 And it is possible: the key is that each list's header contains also the difference of its count from the next smaller count. 并且有可能:关键是每个列表的标题还包含其计数与下一个较小计数的差异。 The header of the list with the smallest count contains a delta from 0, which is the smallest count. 具有最小计数的列表的标题包含从0开始的增量,这是最小的计数。 To increment the count of all objects, you only have to increase this single number by one. 要增加所有对象的计数,您只需将此单个数字增加一个。

To set an object's count to 0, you remove the object from its list (which means you always need to refer to objects by their list iterator, or you need to implement your own intrusive linked list), and either (1) add it to the bottom list, if that list has a count of 0, or (2) create a new bottom list with a count of 0 containing only that object. 要将对象的计数设置为0,可以从列表中删除对象(这意味着您始终需要通过列表迭代器引用对象,或者您需要实现自己的侵入链表),并且(1)将其添加到底部列表,如果该列表的计数为0,或(2)创建一个计数为0的新底部列表,仅包含该对象。

The procedure for creating a new object is the same, except that you don't have to unlink it from its current list. 创建新对象的过程是相同的,除了您不必将其与当前列表取消链接。

To evict an object from memory, you choose the object at the head of the top list (which is the list with the largest count). 要从内存中逐出对象,可以选择顶部列表头部的对象(具有最大计数的列表)。 If that list becomes empty, you pop it off the dequeue. 如果该列表变空,则将其从出列名中弹出。 If you need more memory, you can repeat this operation. 如果需要更多内存,可以重复此操作。

So all operations, including "increment all counts", are O(1). 所以所有操作,包括“递增所有计数”,都是O(1)。 Unfortunately, the storage overhead is two pointers per object, plus two pointers and an integer per unique count (at worst, this is the same as the number of objects, but presumably in practice it's much less). 不幸的是,存储开销是每个对象两个指针,加上两个指针和每个唯一计数的整数(最坏的情况是,这与对象的数量相同,但可能在实践中它要少得多)。 Since it's hard to imagine any other algorithm which uses less than one pointer plus a count for each object, this is probably not even a space-time tradeoff; 由于很难想象任何其他算法使用少于一个指针加上每个对象的计数,这可能甚至不是时空权衡; the additional space requirements are minimal. 额外的空间要求是最小的。

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

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