From 0ef1d797255811f5f8db09b85d2d143eae9ab658 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Fri, 11 Aug 2023 06:40:37 +1000 Subject: [PATCH] refine Chat::Dialogue.post_find() --- libs/chat/src/dialogue.rs | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/libs/chat/src/dialogue.rs b/libs/chat/src/dialogue.rs index 2270dc3a8..f2dfd01a0 100644 --- a/libs/chat/src/dialogue.rs +++ b/libs/chat/src/dialogue.rs @@ -95,33 +95,20 @@ impl Dialogue { pub fn post_find(&self, author: &str, timestamp: u64) -> Option { if let Some(author_id) = self.author_lookup.get(author) { - match self - .posts - .binary_search_by(|x| x.timestamp().cmp(×tamp)) - { - Ok(index) => { - // matched timestamp but maybe duplicates by different authors - for (id, post) in self.posts[..index].iter().rev().enumerate() { - if post.timestamp() != timestamp { - break; - } + let i = self.posts.partition_point(|p| p.timestamp() < timestamp); + let last = self.posts.len()-1; + for n in i..last { + if let Some(post) = self.posts.get(n) { + if post.timestamp() == timestamp { if post.author_id() == *author_id { - return Some(id); - } + return Some(n); + } + } else { + break; } - for (id, post) in self.posts[index..].iter().enumerate() { - if post.timestamp() != timestamp { - break; - } - if post.author_id() == *author_id { - return Some(id); - } - } - return None; } - Err(_) => None::, - }; - }; + } + } None }