I have a Ref<'a, BTreeSet<T>>
and I would like to get the references of its content as Vec<Ref<'a, T>>
.
One method to do this is:
fn get_refs<'a, T: Ord>(btree: Ref<'a, BTreeSet<T>>) -> Vec<Ref<'a, T>> {
let mut result = Vec::new();
for e in btree.iter() {
result.push(Ref::map(Ref::clone(&btree), |t| t.get(&e).unwrap()))
}
result
}
Now let n
be the size of btree
.
Because getting a value from a binary tree takes O(log(n))
and because iterating through a binary tree takes O(n)
this method has time complexity O(n log(n))
.
Even though doing this in with &'a BTreeSet<T>
and &'a T
instead of Ref<'a, BTreeSet<T>>
and Ref<'a, T>
takes O(n)
(Because we only need to collect the references iterated in an array). An example of the method using plain references is the following.
fn get_refs<'a, T: Ord>(btree: &'a BTreeSet<T>) -> Vec<&'a T> {
btree.iter().collect()
}
My question is:
Given Ref<'a, BTreeSet<T>>
is there a way to get Vec<Ref<'a, T>>
in O(n)
time complexity?