简体   繁体   English

具有随机访问元素删除的类似队列的数据结构

[英]Queue-like data structure with random access element removal

Is there a data structure like a queue which also supports removal of elements at arbitrary points? 是否有像队列一样的数据结构,它也支持在任意点删除元素? Enqueueing and dequeueing occur most frequently, but mid-queue element removal must be similar in speed terms since there may be periods where that is the most common operation. 排队和出列最频繁发生,但中间队列元素移除在速度方面必须相似,因为可能存在最常见操作的时段。 Consistency of performance is more important than absolute speed. 性能的一致性比绝对速度更重要。 Time is more important than memory. 时间比记忆更重要。 Queue length is small, under 1,000 elements at absolute peak load.In case it's not obvious I'll state it explicitly: random insertion is not required. 队列长度很小,在绝对峰值负载下低于1,000个元素。如果不明显,我将明确说明:不需要随机插入。

Have tagged C++ since that is my implementation language, but I'm not using (and don't want to use) any STL or Boost. 标记了C ++,因为那是我的实现语言,但我没有使用(也不想使用)任何STL或Boost。 Pure C or C++ only (I will convert C solutions to a C++ class.) 仅限纯C或C ++(我将C解决方案转换为C ++类。)

Edit: I think what I want is a kind of dictionary that also has a queue interface (or a queue that also has a dictionary interface) so that I can do things like this: 编辑:我想我想要的是一种字典,它也有一个队列接口(或一个也有字典接口的队列),这样我就可以这样做:

Container.enqueue(myObjPtr1);
MyObj *myObjPtr2 = Container.dequeue();
Container.remove(myObjPtr3);

I think that double-link list is exactly what you want (assuming you do not want a priority queue): 我认为双链接列表正是您想要的(假设您不需要优先级队列):

  1. Easy and fast adding elements to both ends 轻松快速地向两端添加元素
  2. Easy and fast removal of elements from anywhere 从任何地方轻松快速地移除元素

You can use std::list container, but (in your case) it is difficult to remove an element from the middle of the list if you only have a pointer (or reference) to the element (wrapped in STL's list element), but you do not have an iterator. 您可以使用std::list容器,但是(在您的情况下)如果您只有元素的指针(或引用)(包含在STL的列表元素中),则很难从列表中间删除元素,但是你没有迭代器。 If using iterators (eg storing them) is not an option - then implementing a double linked list (even with element counter) should be pretty easy. 如果使用迭代器(例如存储它们)不是一个选项 - 那么实现双链表(即使使用元素计数器)应该非常简单。 If you implement your own list - you can directly operate on pointers to elements (each of them contains pointers to both of its neighbours). 如果你实现自己的列表 - 你可以直接操作指向元素的指针(每个元素包含指向它的两个邻居的指针)。 If you do not want to use Boost or STL this is probably the best option (and the simplest), and you have control of everything (you can even write your own block allocator for list elements to speed up things). 如果您不想使用Boost或STL,这可能是最好的选择(也是最简单的选项),并且您可以控制所有内容(您甚至可以为列表元素编写自己的块分配器以加快速度)。

One option is to use an order statistic tree , an augmented tree structure that supports O(log n) random access to each element, along with O(log n) insertion and deletion at arbitrary points. 一种选择是使用订单统计树 ,一种增强树结构,支持对每个元素的O(log n)随机访问,以及在任意点处的O(log n)插入和删除。 Internally, the order statistic tree is implemented as a balanced binary search treewith extra information associated with it. 在内部,订单统计树被实现为平衡二进制搜索树,其具有与之关联的额外信息。 As a result, lookups are a slower than in a standard dynamic array, but the insertions are much faster. 因此,查找比标准动态数组慢,但插入速度要快得多。

Hope this helps! 希望这可以帮助!

You can use a combination of a linked list and a hash table. 您可以使用链表和哈希表的组合。 In java it is called a LinkedHashSet . 在java中,它被称为LinkedHashSet

The idea is simple, have a linked list of elements, and also maintain a hash map of (key,nodes) , where node is a pointer to the relevant node in the linked list, and key is the key representing this node. 这个想法很简单,有链接的元素列表,并且还维护(key,nodes)hash map ,其中node是指向链表中相关节点的指针, key是表示该节点的键。

Note that the basic implementation is a set , and some extra work will be needed to make this data structure allow dupes. 请注意,基本实现是一个set ,并且需要一些额外的工作来使这个数据结构允许欺骗。

This data structure allows you both O(1) head/tail access, and both O(1) access to any element in the list. 此数据结构允许您进行O(1)头/尾访问,并允许O(1)访问列表中的任何元素。 [all on average armotorized] [平均所有机动车]

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

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