refine Chat::Dialogue.post_find()

This commit is contained in:
nworbnhoj 2023-08-11 06:40:37 +10:00
parent dc8102c627
commit 0ef1d79725

View file

@ -95,33 +95,20 @@ impl Dialogue {
pub fn post_find(&self, author: &str, timestamp: u64) -> Option<usize> { pub fn post_find(&self, author: &str, timestamp: u64) -> Option<usize> {
if let Some(author_id) = self.author_lookup.get(author) { if let Some(author_id) = self.author_lookup.get(author) {
match self let i = self.posts.partition_point(|p| p.timestamp() < timestamp);
.posts let last = self.posts.len()-1;
.binary_search_by(|x| x.timestamp().cmp(&timestamp)) for n in i..last {
{ if let Some(post) = self.posts.get(n) {
Ok(index) => { if post.timestamp() == timestamp {
// matched timestamp but maybe duplicates by different authors
for (id, post) in self.posts[..index].iter().rev().enumerate() {
if post.timestamp() != timestamp {
break;
}
if post.author_id() == *author_id { 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::<usize>, }
}; }
};
None None
} }