From 4f9fe856a25ab29345b90e7725509e9ee38a37be Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:19:41 +0200 Subject: Adding upstream version 1.69.0+dfsg1. Signed-off-by: Daniel Baumann --- vendor/zerovec/src/map2d/cursor.rs | 70 +++++++++++++++++++++++++++++++++----- 1 file changed, 61 insertions(+), 9 deletions(-) (limited to 'vendor/zerovec/src/map2d/cursor.rs') diff --git a/vendor/zerovec/src/map2d/cursor.rs b/vendor/zerovec/src/map2d/cursor.rs index 0654ee794..4802187be 100644 --- a/vendor/zerovec/src/map2d/cursor.rs +++ b/vendor/zerovec/src/map2d/cursor.rs @@ -92,7 +92,9 @@ where self.keys0.zvl_get(self.key0_index).unwrap() } - /// Borrow an ordered iterator over keys1 for a particular key0. + /// Borrow an ordered iterator over keys1 and values for a particular key0. + /// + /// To get the values as copy types, see [`Self::iter1_copied`]. /// /// For an example, see [`ZeroMap2d::iter0()`]. pub fn iter1( @@ -150,6 +152,64 @@ where } } +impl<'l, 'a, K0, K1, V> ZeroMap2dCursor<'l, 'a, K0, K1, V> +where + K0: ZeroMapKV<'a>, + K1: ZeroMapKV<'a>, + V: ZeroMapKV<'a>, + K0: ?Sized, + K1: ?Sized, + V: Copy, +{ + /// Borrow an ordered iterator over keys1 and values for a particular key0. + /// + /// The values are returned as copy types. + /// + /// # Examples + /// + /// ``` + /// use zerovec::ZeroMap2d; + /// + /// let zm2d: ZeroMap2d = [ + /// ("a", 0u8, 1usize), + /// ("b", 1u8, 1000usize), + /// ("b", 2u8, 2000usize), + /// ] + /// .into_iter() + /// .collect(); + /// + /// let mut total_value = 0; + /// + /// for cursor in zm2d.iter0() { + /// for (_, value) in cursor.iter1_copied() { + /// total_value += value; + /// } + /// } + /// + /// assert_eq!(total_value, 3001); + /// ``` + pub fn iter1_copied( + &self, + ) -> impl Iterator>::GetType, V)> + '_ { + let range = self.get_range(); + #[allow(clippy::unwrap_used)] // `self.get_range()` returns a valid range + range.map(move |idx| { + ( + self.keys1.zvl_get(idx).unwrap(), + self.get1_copied_at(idx).unwrap(), + ) + }) + } + + fn get1_copied_at(&self, index: usize) -> Option { + let ule = self.values.zvl_get(index)?; + let mut result = Option::::None; + V::Container::zvl_get_as_t(ule, |v| result.replace(*v)); + #[allow(clippy::unwrap_used)] // `zvl_get_as_t` guarantees that the callback is invoked + Some(result.unwrap()) + } +} + impl<'l, 'a, K0, K1, V> ZeroMap2dCursor<'l, 'a, K0, K1, V> where K0: ZeroMapKV<'a>, @@ -253,14 +313,6 @@ where let key1_index = self.get_key1_index_by(predicate)?; self.get1_copied_at(key1_index) } - - fn get1_copied_at(&self, index: usize) -> Option { - let ule = self.values.zvl_get(index)?; - let mut result = Option::::None; - V::Container::zvl_get_as_t(ule, |v| result.replace(*v)); - #[allow(clippy::unwrap_used)] // `zvl_get_as_t` guarantees that the callback is invoked - Some(result.unwrap()) - } } // We can't use the default PartialEq because ZeroMap2d is invariant -- cgit v1.2.3