perf: replace returned vec with iterator

This commit is contained in:
Zixuan Chen 2022-11-16 17:38:25 +08:00
parent 402b174842
commit 521615b1a0

View file

@ -318,24 +318,23 @@ impl<
} }
#[inline] #[inline]
pub fn get_range(&self, start: Index, end: Index) -> Vec<&Value> { pub fn get_range(&self, start: Index, end: Index) -> impl Iterator<Item = &Value> {
let mut ans = Vec::new(); self.tree
for value in self.tree.iter_range(start, Some(end)) { .iter_range(start, Some(end))
ans.push(&value.as_tree_ref().value) .map(|x| &x.as_tree_ref().value)
}
ans
} }
/// TODO: need double check this method /// TODO: need double check this method
#[inline] #[inline]
pub fn get_range_with_index(&self, start: Index, end: Index) -> Vec<(Index, &Value)> { pub fn get_range_with_index(
let mut ans = Vec::new(); &self,
for value in self.tree.iter_range(start, Some(end)) { start: Index,
let value = value.as_tree_ref(); end: Index,
ans.push((value.index, &value.value)); ) -> impl Iterator<Item = (Index, &Value)> {
} self.tree.iter_range(start, Some(end)).map(|x| {
let x = x.as_tree_ref();
ans (x.index, &x.value)
})
} }
#[inline] #[inline]