pub struct DequeBTreeMap<K, V> { /* private fields */ }
Expand description

Double-ended queue with Map feature.

DequeBTreeMap is a data structure that combines the functionality of a double-ended queue (Deque) and a map. It allows you to insert and remove key-value pairs from either end of the queue in a constant time, and provides map-like access to the values by their keys.

The implementation of DequeBTreeMap uses a BTreeMap to store the entries, and a VecDeque to store the indices in the order they were added to the map. This allows DequeBTreeMap to provide efficient O(log n) insertion, removal, and access operations. It also implements many common traits, such as Default, PartialEq, PartialOrd, Clone, and Debug.

DequeBTreeMap provides several methods for inserting and removing key-value pairs. The insert() method inserts a key-value pair into the map, and returns the old value if the key was already present. The push_back() and push_front() methods insert a key-value pair at the back or front of the queue, respectively, and return the old value if the key was already present.

DequeBTreeMap also provides the entry() method, which returns an Entry enum that represents either a vacant or occupied entry in the map. This can be used to insert or update values in the map while also managing the indices in the queue.

In addition, DequeBTreeMap provides methods for accessing and iterating over the entries in the map. The get() and get_mut() methods allow you to retrieve a reference to the value associated with a key, and the iter() and into_iter() methods return iterators over the entries in the map. DequeBTreeMap also implements the Index and IndexMut traits, which allow you to access and modify the values in the map using index syntax (e.g., map[key]).

Overall, DequeBTreeMap is a useful data structure for situations where you need to maintain the insertion order of the entries while also providing efficient access to the values by their keys.

One potential limitation of DequeBTreeMap is that it is not optimized for processing large batches of data with many duplicates. This is because the insert() method has a worst-case time complexity of O(n), where n is the number of entries in the map. This means that if you try to insert a large number of duplicate keys into the map, the performance may degrade significantly.

Additionally, DequeBTreeMap uses a BTreeMap internally, which means that the keys must implement the Ord trait. This means that the keys must have a total order and must be comparable using the <, >, <=, and >= operators. This may not always be desirable, depending on the types of keys you need to use with DequeBTreeMap.

Overall, while DequeBTreeMap is a useful data structure in many cases, it is important to consider its performance and limitations when deciding whether to use it in your own code.

When the element is present, the maximum time complexity is O(n). So it is not suitable for processing large batches of data with too many duplicates.

Here are some examples of using DequeBTreeMap in Rust code:

let mut map: DequeBTreeMap<String, i32> = DequeBTreeMap::new();

map.push_back("foo".to_string(), 42);

map.push_front("bar".to_string(), -1);

let old_value = map.insert("baz".to_string(), 123);

let value = map.get("baz");

for (key, value) in map.iter() {
println!("{}: {}", key, value);
}

The above content and some comments in the code are written by ChatGPT.

Implementations§

source§

impl<K, V> DequeBTreeMap<K, V>

source

pub fn new() -> Self

source

pub fn with_capacity(capacity: usize) -> Self

source§

impl<K, V> DequeBTreeMap<K, V>where K: Clone + Ord,

source

pub fn insert(&mut self, key: K, value: V) -> Option<V>

Inserts a key-value pair into the map.

If the map did not have this key present, None is returned.

If the map did have this key present, the value is updated, and the old value is returned. The key is not updated, though; this matters for types that can be == without being identical.

source

pub fn push_back(&mut self, key: K, value: V) -> Option<V>

source

pub fn push_front(&mut self, key: K, value: V) -> Option<V>

source

pub fn entry(&mut self, key: K) -> Entry<'_, K, V>where K: Ord,

source

pub fn shrink_to_fit(&mut self)

source

pub fn capacity(&mut self) -> usize

source§

impl<K, V> DequeBTreeMap<K, V>

source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for at least additional more elements to be inserted in the given VecDeque. The collection may reserve more space to avoid frequent reallocations.

source

pub fn clear(&mut self)

source

pub fn remove(&mut self, k: &K) -> Option<V>where K: Ord,

source

pub fn get<Q>(&self, k: &Q) -> Option<&V>where K: Borrow<Q> + Ord, Q: Ord + ?Sized,

source

pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>where K: Borrow<Q> + Ord, Q: Ord + ?Sized,

source

pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>where K: Borrow<Q> + Ord, Q: Ord + ?Sized,

source

pub fn iter(&self) -> Iter<'_, K, V>

source

pub fn len(&self) -> usize

source

pub fn is_empty(&self) -> bool

source

pub fn contains_key<Q>(&self, k: &Q) -> boolwhere K: Borrow<Q> + Ord, Q: Ord + ?Sized,

source

pub fn front(&self) -> Option<(&K, &V)>where K: Ord,

source

pub fn pop_front(&mut self) -> Option<(K, V)>where K: Ord,

source

pub fn back(&self) -> Option<(&K, &V)>where K: Ord,

source

pub fn pop_back(&mut self) -> Option<(K, V)>where K: Ord,

Trait Implementations§

source§

impl<K: Clone, V: Clone> Clone for DequeBTreeMap<K, V>

source§

fn clone(&self) -> DequeBTreeMap<K, V>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<K: Debug, V: Debug> Debug for DequeBTreeMap<K, V>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<K, V> Default for DequeBTreeMap<K, V>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<'de, K, V> Deserialize<'de> for DequeBTreeMap<K, V>where K: Deserialize<'de> + Ord + Clone, V: Deserialize<'de>,

Requires crate feature "serde"

source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<'a, K, V> Extend<(&'a K, &'a V)> for DequeBTreeMap<K, V>where K: Ord + Copy, V: Copy,

source§

fn extend<T>(&mut self, iter: T)where T: IntoIterator<Item = (&'a K, &'a V)>,

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<K, V> Extend<(K, V)> for DequeBTreeMap<K, V>where K: Ord + Clone,

source§

fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<K, V, const N: usize> From<[(K, V); N]> for DequeBTreeMap<K, V>where K: Ord + Clone,

source§

fn from(items: [(K, V); N]) -> Self

Converts to this type from the input type.
source§

impl<K, V> FromIterator<(K, V)> for DequeBTreeMap<K, V>where K: Ord + Clone,

source§

fn from_iter<T>(iter: T) -> Selfwhere T: IntoIterator<Item = (K, V)>,

Creates a value from an iterator. Read more
source§

impl<'a, K, Q, V> Index<&'a Q> for DequeBTreeMap<K, V>where K: Borrow<Q> + Ord, Q: Ord,

§

type Output = V

The returned type after indexing.
source§

fn index(&self, key: &'a Q) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl<K: Ord, V> Index<usize> for DequeBTreeMap<K, V>

§

type Output = V

The returned type after indexing.
source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl<K: Ord, V> IndexMut<usize> for DequeBTreeMap<K, V>

source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
source§

impl<'de, K, V, E> IntoDeserializer<'de, E> for DequeBTreeMap<K, V>where K: IntoDeserializer<'de, E> + Ord, V: IntoDeserializer<'de, E>, E: Error,

§

type Deserializer = MapDeserializer<'de, <DequeBTreeMap<K, V> as IntoIterator>::IntoIter, E>

The type of the deserializer being converted into.
source§

fn into_deserializer(self) -> Self::Deserializer

Convert this value into a deserializer.
source§

impl<'a, K: Ord, V> IntoIterator for &'a DequeBTreeMap<K, V>

§

type Item = (&'a K, &'a V)

The type of the elements being iterated over.
§

type IntoIter = Iter<'a, K, V>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<K, V> IntoIterator for DequeBTreeMap<K, V>where K: Ord,

§

type Item = (K, V)

The type of the elements being iterated over.
§

type IntoIter = IntoIter<K, V>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<K: Ord, V: Ord> Ord for DequeBTreeMap<K, V>

source§

fn cmp(&self, other: &DequeBTreeMap<K, V>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl<K: PartialEq, V: PartialEq> PartialEq for DequeBTreeMap<K, V>

source§

fn eq(&self, other: &DequeBTreeMap<K, V>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<K: PartialOrd, V: PartialOrd> PartialOrd for DequeBTreeMap<K, V>

source§

fn partial_cmp(&self, other: &DequeBTreeMap<K, V>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<K, V> Serialize for DequeBTreeMap<K, V>where K: Serialize + Ord, V: Serialize,

source§

fn serialize<T>(&self, serializer: T) -> Result<T::Ok, T::Error>where T: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<K: Eq, V: Eq> Eq for DequeBTreeMap<K, V>

source§

impl<K, V> StructuralEq for DequeBTreeMap<K, V>

source§

impl<K, V> StructuralPartialEq for DequeBTreeMap<K, V>

Auto Trait Implementations§

§

impl<K, V> RefUnwindSafe for DequeBTreeMap<K, V>where K: RefUnwindSafe, V: RefUnwindSafe,

§

impl<K, V> Send for DequeBTreeMap<K, V>where K: Send, V: Send,

§

impl<K, V> Sync for DequeBTreeMap<K, V>where K: Sync, V: Sync,

§

impl<K, V> Unpin for DequeBTreeMap<K, V>where K: Unpin,

§

impl<K, V> UnwindSafe for DequeBTreeMap<K, V>where K: UnwindSafe + RefUnwindSafe, V: RefUnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,