/// The number of elements in the [`ZeroMap2dBorrowed`] pubfn len(&self) -> usize { self.values.zvl_len()
}
/// Whether the [`ZeroMap2dBorrowed`] is empty pubfn is_empty(&self) -> bool { self.values.zvl_len() == 0
}
}
impl<'a, K0, K1, V> ZeroMap2dBorrowed<'a, K0, K1, V> where
K0: ZeroMapKV<'a> + Ord,
K1: ZeroMapKV<'a> + Ord,
V: ZeroMapKV<'a>,
K0: ?Sized,
K1: ?Sized,
V: ?Sized,
{ /// Get the value associated with `key0` and `key1`, if it exists. /// /// This is able to return values that live longer than the map itself /// since they borrow directly from the backing buffer. This is the /// primary advantage of using [`ZeroMap2dBorrowed`](super::ZeroMap2dBorrowed) over [`ZeroMap2d`](super::ZeroMap2d). /// /// ```rust /// use zerovec::ZeroMap2d; /// /// let mut map = ZeroMap2d::new(); /// map.insert(&1, "one", "foo"); /// map.insert(&2, "one", "bar"); /// map.insert(&2, "two", "baz"); /// /// let borrowed = map.as_borrowed(); /// assert_eq!(borrowed.get_2d(&1, "one"), Some("foo")); /// assert_eq!(borrowed.get_2d(&1, "two"), None); /// assert_eq!(borrowed.get_2d(&2, "one"), Some("bar")); /// assert_eq!(borrowed.get_2d(&2, "two"), Some("baz")); /// assert_eq!(borrowed.get_2d(&3, "three"), None); /// ``` pubfn get_2d(&self, key0: &K0, key1: &K1) -> Option<&'a V::GetType> { self.get0(key0)?.get1(key1)
}
}
impl<'a, K0, K1, V> ZeroMap2dBorrowed<'a, K0, K1, V> where
K0: ZeroMapKV<'a> + Ord,
K1: ZeroMapKV<'a>,
V: ZeroMapKV<'a>,
K0: ?Sized,
K1: ?Sized,
V: ?Sized,
{ /// Gets a cursor for `key0`. If `None`, then `key0` is not in the map. If `Some`, /// then `key0` is in the map, and `key1` can be queried. /// /// ```rust /// use zerovec::ZeroMap2d; /// /// let mut map = ZeroMap2d::new(); /// map.insert(&1, "one", "foo"); /// map.insert(&2, "two", "bar"); /// let borrowed = map.as_borrowed(); /// assert!(matches!(borrowed.get0(&1), Some(_))); /// assert!(matches!(borrowed.get0(&3), None)); /// ``` #[inline] pubfn get0<'l>(&'l self, key0: &K0) -> Option<ZeroMap2dCursor<'a, 'a, K0, K1, V>> { let key0_index = self.keys0.zvl_binary_search(key0).ok()?;
Some(ZeroMap2dCursor::from_borrowed(self, key0_index))
}
/// Binary search the map for `key0`, returning a cursor. /// /// ```rust /// use zerovec::ZeroMap2d; /// /// let mut map = ZeroMap2d::new(); /// map.insert(&1, "one", "foo"); /// map.insert(&2, "two", "bar"); /// let borrowed = map.as_borrowed(); /// assert!(matches!(borrowed.get0_by(|probe| probe.cmp(&1)), Some(_))); /// assert!(matches!(borrowed.get0_by(|probe| probe.cmp(&3)), None)); /// ``` pubfn get0_by<'l>(
&'l self,
predicate: impl FnMut(&K0) -> Ordering,
) -> Option<ZeroMap2dCursor<'a, 'a, K0, K1, V>> { let key0_index = self.keys0.zvl_binary_search_by(predicate).ok()?;
Some(ZeroMap2dCursor::from_borrowed(self, key0_index))
}
/// Returns whether `key0` is contained in this map /// /// ```rust /// use zerovec::ZeroMap2d; /// /// let mut map = ZeroMap2d::new(); /// map.insert(&1, "one", "foo"); /// map.insert(&2, "two", "bar"); /// let borrowed = map.as_borrowed(); /// assert!(borrowed.contains_key0(&1)); /// assert!(!borrowed.contains_key0(&3)); /// ``` pubfn contains_key0(&self, key0: &K0) -> bool { self.keys0.zvl_binary_search(key0).is_ok()
}
}
impl<'a, K0, K1, V> ZeroMap2dBorrowed<'a, K0, K1, V> where
K0: ZeroMapKV<'a> + Ord,
K1: ZeroMapKV<'a> + Ord,
V: ZeroMapKV<'a>,
V: Copy,
K0: ?Sized,
K1: ?Sized,
{ /// For cases when `V` is fixed-size, obtain a direct copy of `V` instead of `V::ULE` pubfn get_copied_2d(&self, key0: &K0, key1: &K1) -> Option<V> { self.get0(key0)?.get1_copied(key1)
}
}
// We can't use the default PartialEq because ZeroMap2d is invariant // so otherwise rustc will not automatically allow you to compare ZeroMaps // with different lifetimes impl<'a, 'b, K0, K1, V> PartialEq<ZeroMap2dBorrowed<'b, K0, K1, V>> for ZeroMap2dBorrowed<'a, K0, K1, V> where
K0: for<'c> ZeroMapKV<'c> + ?Sized,
K1: for<'c> ZeroMapKV<'c> + ?Sized,
V: for<'c> ZeroMapKV<'c> + ?Sized,
<K0 as ZeroMapKV<'a>>::Slice: PartialEq<<K0 as ZeroMapKV<'b>>::Slice>,
<K1 as ZeroMapKV<'a>>::Slice: PartialEq<<K1 as ZeroMapKV<'b>>::Slice>,
<V as ZeroMapKV<'a>>::Slice: PartialEq<<V as ZeroMapKV<'b>>::Slice>,
{ fn eq(&self, other: &ZeroMap2dBorrowed<'b, K0, K1, V>) -> bool { self.keys0.eq(other.keys0)
&& self.joiner.eq(other.joiner)
&& self.keys1.eq(other.keys1)
&& self.values.eq(other.values)
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.