diff options
Diffstat (limited to 'src/ptr_weak_key_hash_map.rs')
-rw-r--r-- | src/ptr_weak_key_hash_map.rs | 91 |
1 files changed, 81 insertions, 10 deletions
diff --git a/src/ptr_weak_key_hash_map.rs b/src/ptr_weak_key_hash_map.rs index cd48809..2cd5591 100644 --- a/src/ptr_weak_key_hash_map.rs +++ b/src/ptr_weak_key_hash_map.rs @@ -1,10 +1,6 @@ //! A hash map where the keys are held by weak pointers and compared by pointer. -use std::collections::hash_map::RandomState; -use std::fmt::{self, Debug}; -use std::hash::BuildHasher; -use std::iter::FromIterator; -use std::ops::{Deref, Index, IndexMut}; +use crate::compat::*; use super::by_ptr::*; use super::traits::*; @@ -17,11 +13,15 @@ impl <K: WeakElement, V> PtrWeakKeyHashMap<K, V, RandomState> where K::Strong: Deref { /// Creates an empty `PtrWeakKeyHashMap`. + /// + /// *O*(1) time pub fn new() -> Self { PtrWeakKeyHashMap(base::WeakKeyHashMap::new()) } /// Creates an empty `PtrWeakKeyHashMap` with the given capacity. + /// + /// *O*(*n*) time pub fn with_capacity(capacity: usize) -> Self { PtrWeakKeyHashMap(base::WeakKeyHashMap::with_capacity(capacity)) } @@ -31,41 +31,57 @@ impl <K: WeakElement, V, S: BuildHasher> PtrWeakKeyHashMap<K, V, S> where K::Strong: Deref { /// Creates an empty `PtrWeakKeyHashMap` with the given capacity and hasher. + /// + /// *O*(*n*) time pub fn with_hasher(hash_builder: S) -> Self { PtrWeakKeyHashMap(base::WeakKeyHashMap::with_hasher(hash_builder)) } /// Creates an empty `PtrWeakKeyHashMap` with the given capacity and hasher. + /// + /// *O*(*n*) time pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self { PtrWeakKeyHashMap(base::WeakKeyHashMap::with_capacity_and_hasher(capacity, hash_builder)) } /// Returns a reference to the map's `BuildHasher`. + /// + /// *O*(1) time pub fn hasher(&self) -> &S { self.0.hasher() } /// Returns the number of elements the map can hold without reallocating. + /// + /// *O*(1) time pub fn capacity(&self) -> usize { self.0.capacity() } /// Removes all mappings whose keys have expired. + /// + /// *O*(*n*) time pub fn remove_expired(&mut self) { self.0.remove_expired() } /// Reserves room for additional elements. + /// + /// *O*(*n*) time pub fn reserve(&mut self, additional_capacity: usize) { self.0.reserve(additional_capacity) } /// Shrinks the capacity to the minimum allowed to hold the current number of elements. + /// + /// *O*(*n*) time pub fn shrink_to_fit(&mut self) { self.0.shrink_to_fit() } /// Returns an over-approximation of the number of elements. + /// + /// *O*(1) time pub fn len(&self) -> usize { self.0.len() } @@ -74,6 +90,8 @@ impl <K: WeakElement, V, S: BuildHasher> PtrWeakKeyHashMap<K, V, S> /// /// This could answer `false` for an empty map whose keys have /// expired but have yet to be collected. + /// + /// *O*(1) time pub fn is_empty(&self) -> bool { self.len() == 0 } @@ -81,42 +99,58 @@ impl <K: WeakElement, V, S: BuildHasher> PtrWeakKeyHashMap<K, V, S> /// The proportion of buckets that are used. /// /// This is an over-approximation because of expired keys. + /// + /// *O*(1) time pub fn load_factor(&self) -> f32 { self.0.load_factor() } /// Gets the requested entry. + /// + /// expected *O*(1) time; worst-case *O*(*p*) time pub fn entry(&mut self, key: K::Strong) -> Entry<ByPtr<K>, V> { self.0.entry(key) } /// Removes all associations from the map. + /// + /// *O*(*n*) time pub fn clear(&mut self) { self.0.clear() } /// Returns a reference to the value corresponding to the key. + /// + /// expected *O*(1) time; worst-case *O*(*p*) time pub fn get(&self, key: &K::Strong) -> Option<&V> { self.0.get(&(key.deref() as *const _)) } /// Returns true if the map contains the specified key. + /// + /// expected *O*(1) time; worst-case *O*(*p*) time pub fn contains_key(&self, key:&K::Strong) -> bool { self.0.contains_key(&(key.deref() as *const _)) } /// Returns a mutable reference to the value corresponding to the key. + /// + /// expected *O*(1) time; worst-case *O*(*p*) time pub fn get_mut(&mut self, key: &K::Strong) -> Option<&mut V> { self.0.get_mut(&(key.deref() as *const _)) } /// Unconditionally inserts the value, returning the old value if already present. Does not /// replace the key. + /// + /// expected *O*(1) time; worst-case *O*(*p*) time pub fn insert(&mut self, key: K::Strong, value: V) -> Option<V> { self.0.insert(key, value) } /// Removes the entry with the given key, if it exists, and returns the value. + /// + /// expected *O*(1) time; worst-case *O*(*p*) time pub fn remove(&mut self, key: &K::Strong) -> Option<V> { self.0.remove(&(key.deref() as *const _)) } @@ -124,6 +158,8 @@ impl <K: WeakElement, V, S: BuildHasher> PtrWeakKeyHashMap<K, V, S> /// Removes all mappings not satisfying the given predicate. /// /// Also removes any expired mappings. + /// + /// *O*(*n*) time pub fn retain<F>(&mut self, f: F) where F: FnMut(K::Strong, &mut V) -> bool { @@ -134,6 +170,10 @@ impl <K: WeakElement, V, S: BuildHasher> PtrWeakKeyHashMap<K, V, S> /// /// In particular, all the keys of self must be in other and the values must compare true with /// value_equal. + /// + /// expected *O*(*n*) time; worst-case *O*(*nq*) time (where *n* is + /// `self.capacity()` and *q* is the length of the probe sequences + /// in `other`) pub fn submap_with<F, S1, V1>(&self, other: &PtrWeakKeyHashMap<K, V1, S1>, value_equal: F) -> bool where F: FnMut(&V, &V1) -> bool, S1: BuildHasher @@ -142,6 +182,10 @@ impl <K: WeakElement, V, S: BuildHasher> PtrWeakKeyHashMap<K, V, S> } /// Is self a submap of other? + /// + /// expected *O*(*n*) time; worst-case *O*(*nq*) time (where *n* is + /// `self.capacity()` and *q* is the length of the probe sequences + /// in `other`) pub fn is_submap<V1, S1>(&self, other: &PtrWeakKeyHashMap<K, V1, S1>) -> bool where V: PartialEq<V1>, S1: BuildHasher @@ -150,6 +194,10 @@ impl <K: WeakElement, V, S: BuildHasher> PtrWeakKeyHashMap<K, V, S> } /// Are the keys of self a subset of the keys of other? + /// + /// expected *O*(*n*) time; worst-case *O*(*nq*) time (where *n* is + /// `self.capacity()` and *q* is the length of the probe sequences + /// in `other`) pub fn domain_is_subset<V1, S1>(&self, other: &PtrWeakKeyHashMap<K, V1, S1>) -> bool where S1: BuildHasher { @@ -161,31 +209,43 @@ impl<K: WeakElement, V, S> PtrWeakKeyHashMap<K, V, S> where K::Strong: Deref { /// Gets an iterator over the keys and values. + /// + /// *O*(1) time pub fn iter(&self) -> Iter<ByPtr<K>, V> { self.0.iter() } /// Gets an iterator over the keys. + /// + /// *O*(1) time pub fn keys(&self) -> Keys<ByPtr<K>, V> { self.0.keys() } /// Gets an iterator over the values. + /// + /// *O*(1) time pub fn values(&self) -> Values<ByPtr<K>, V> { self.0.values() } /// Gets an iterator over the keys and mutable values. + /// + /// *O*(1) time pub fn iter_mut(&mut self) -> IterMut<ByPtr<K>, V> { self.0.iter_mut() } /// Gets an iterator over the mutable values. + /// + /// *O*(1) time pub fn values_mut(&mut self) -> ValuesMut<ByPtr<K>, V> { self.0.values_mut() } /// Gets a draining iterator, which removes all the values but retains the storage. + /// + /// *O*(1) time (and *O*(*n*) time to dispose of the result) pub fn drain(&mut self) -> Drain<ByPtr<K>, V> { self.0.drain() } @@ -283,6 +343,9 @@ impl<K: WeakElement, V, S> IntoIterator for PtrWeakKeyHashMap<K, V, S> { type Item = (K::Strong, V); type IntoIter = IntoIter<ByPtr<K>, V>; + /// Creates an owning iterator from `self`. + /// + /// *O*(1) time (and *O*(*n*) time to dispose of the result) fn into_iter(self) -> Self::IntoIter { self.0.into_iter() } @@ -292,6 +355,9 @@ impl<'a, K: WeakElement, V, S> IntoIterator for &'a PtrWeakKeyHashMap<K, V, S> { type Item = (K::Strong, &'a V); type IntoIter = Iter<'a, ByPtr<K>, V>; + /// Creates a borrowing iterator from `self`. + /// + /// *O*(1) time fn into_iter(self) -> Self::IntoIter { (&self.0).into_iter() } @@ -301,17 +367,22 @@ impl<'a, K: WeakElement, V, S> IntoIterator for &'a mut PtrWeakKeyHashMap<K, V, type Item = (K::Strong, &'a mut V); type IntoIter = IterMut<'a, ByPtr<K>, V>; + /// Creates a borrowing iterator from `self`. + /// + /// *O*(1) time fn into_iter(self) -> Self::IntoIter { (&mut self.0).into_iter() } } #[cfg(test)] -mod test -{ - use crate::PtrWeakKeyHashMap; - use crate::weak_key_hash_map::Entry; - use std::rc::{Rc, Weak}; +mod test { + use crate::compat::{ + eprintln, + rc::{Rc, Weak}, + Vec, + }; + use super::{Entry, PtrWeakKeyHashMap}; // fn show_me(weakmap: &PtrWeakKeyHashMap<Weak<u32>, f32>) { // for (key, _) in weakmap { |