3 stable releases

1.1.1 Nov 6, 2022
1.0.0 Sep 7, 2020

#673 in Data structures

Download history 1/week @ 2023-12-18 3/week @ 2023-12-25 4/week @ 2024-02-05 4/week @ 2024-02-12 31/week @ 2024-02-19 66/week @ 2024-02-26 25/week @ 2024-03-04 16/week @ 2024-03-11 24/week @ 2024-03-18 37/week @ 2024-03-25 93/week @ 2024-04-01

176 downloads per month
Used in 9 crates (2 directly)

GPL-3.0-or-later

20KB
306 lines

deque (double-ended queue) with stable element indices

# use vecdeque_stableix::Deque;
let mut deque = Deque::new();
let pushed_a : i64 = deque.push_back('a');
let pushed_b = deque.push_back('b');
assert_eq!(deque.get(pushed_a), Some(&'a'));
assert_eq!(deque.pop_front(), Some('a'));
assert_eq!(deque[pushed_b], 'b'); // would panic if it had been removed

Index type

You can use a suitable signed integer type, eg. i64, as the element index. It is important that it doesn't overflow --- see below.

You may have to specify the index type explicitly:

# use vecdeque_stableix::Deque;
let mut deque : Deque<_,i64> = Deque::new();
deque.push_front(42);
assert_eq!(deque.front(), Some(&42));

Index stabiliy

This Deque is implemented by adding a front counter to std::vec_deque::VecDeque. The abstraction is rather thin. Methods are provided to access the individual parts. If you use them, you may invalidate your existing indices, so that they no longer point to the same elements.

If this happens, your program will still be memory-safe, but it may function incorrectly.

Methods with a risk of panicking are so noted in the documentation.

Panics, and index overflow

This library will panic if the index type overflows. This can occur if you push more than 2^n values through the queue, where n is the size of your integer type.

If you prefer to wrap, reusing element indices rather than panicing, you can impl Offset for a newtype containing an unsigned type, and perform wrapping arithmetic.

It would be possible to provide non-panicing library entrypoints, which return errors instead. Patches for that welcome.

Changelog

1.1.1

  • Update the README from the crate-level docs.

1.1.0

  • Provide IterMut (from iter_mut).
  • Provide draining IntoIter (via impl IntoIterator).
  • Implement Eq and PartialEq for Deque.
  • Implement FromIterator and Extend for Deque.

1.0.0

  • Initial public release.

Dependencies

~155KB