From 9963c99275b1841c1775ea55d8475e18d1a4ddb3 Mon Sep 17 00:00:00 2001 From: BuildKite Date: Fri, 3 Jan 2025 00:50:40 +0100 Subject: [PATCH] Helper to get last element from posting list --- rs/index/src/posting_list/mod.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/rs/index/src/posting_list/mod.rs b/rs/index/src/posting_list/mod.rs index 61007acc..269e6109 100644 --- a/rs/index/src/posting_list/mod.rs +++ b/rs/index/src/posting_list/mod.rs @@ -31,6 +31,9 @@ impl<'a> PostingList<'a> { let mut elem_count = 0; let elem_size_in_bytes = size_of::(); for slice in &slices { + if slice.is_empty() { + return Err(anyhow!("Posting list slice should not be empty")); + } if slice.len() % elem_size_in_bytes != 0 { return Err(anyhow!( "Invalid slice length {}: should be multiple of u64's size in bytes", @@ -53,6 +56,23 @@ impl<'a> PostingList<'a> { current_index: 0, } } + + pub fn last(&self) -> Option { + if self.slices.is_empty() { + return None; + } + + // Get the last slice (guaranteed to exist and be non-empty at this point) + let last_slice = self.slices.last().unwrap(); + + // Calculate the index of the last u64 in the slice + let last_index = last_slice.len() - size_of::(); + + // Extract and return the last u64 + Some(u64::from_le_bytes( + last_slice[last_index..].try_into().unwrap(), + )) + } } impl<'a> Iterator for PostingListIterator<'a> {