Hopscotch Queues

A hopscotch::Queue is a earliest-in-earliest-out (FIFO) queue where each item in the queue has:

In addition to supporting the ordinary push, pop, and get methods of a FIFO queue, a hopscotch queue also supports the methods after and before (and their respective _mut variants):

rust pub fn after(&self, index: u64, tags: &[K]) -> Option<Item<K, T>> { ... } pub fn before(&self, index: u64, tags: &[K]) -> Option<Item<K, T>> { ... }

These methods find next item in the queue whose tag is equal to any of a given set of tags.

These methods are the real benefit of using a hopscotch queue over another data structure. Their asymptotic time complexity is:

When might I use this?

This queue performs well when:

The push and pop operations are slower by a considerable constant factor than their equivalents for VecDeque<(K, T)>, but for this price, you gain the ability to skip and hop between sets of tags in almost-constant time.

One use-case for such a structure is implementing a bounded event buffer where clients interested in a particular subset of events repeatedly query for the next event matching their desired subset. This scenario plays to the strengths of a hopscotch queue, because each query can be performed very quickly, regardless of the contents or size of the buffer.

If this is similar to your needs, this data structure might be for you!

When should I not use this?