简体   繁体   中英

How to implement an addressable FIFO queue?

I'm currently looking for a data structure with all O(1) operations

  • insert(K, V): Insert a value at the end of the queue.
  • remove_key(K): Remove the value from the queue corresponding to the provided key.
  • remove_head(): Remove the value from the front of the queue (the oldest one).

The only reasonably easy to implement thing I can think of is using a doubly linked list as the primary data structure, and keeping pointers to the list nodes in a hash table, which would get the desired asymptotic behavior, however this might not be the most efficient option in actual runtime.

I found "addressable priority queues" in the literature, but they are rather complicated (and maybe even more expensive) data structures, so I was wondering if someone has a better suggestion. It seems no one implemented something like this for Rust so far, which is why I'm hoping it doesn't get too complicated.

I would use a pub struct VecDeque<T> and use pop_front() instead of remove_head() .

See the doc: VecDeque

这里我用Python实现了一个Addressable Binary Heap ,没有第三方依赖。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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