Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Having multiple BTree indexers to a Vec now that BTrees don't support custom comparison functionality
I have a Vec<Person>
, V
. Person
s will be pushed to V
, though never popped from. However, a Person
's details may be modified.
V
can grow quite large, and I would like to keep indexers to Person
s based on their .FirstName
(String
) and .LastName
(String
). I will store these indexers for both fields in two BTrees.
I also want to iterate through V
in V
's order, and in that case I want to be able to see their FirstName
and LastName
immediately. So it is crucial that FirstName
and LastName
are stored in Person
s themselves.
Now, since it would be quite wasteful to have a String
copy for each FirstName
and LastName
, I want to store the index of a Person
in V
in these BTrees instead.
This would be doable if BTrees allowed custom comparator Fn
s. Eg. for FirstName
it would look like:
|left : &usize, right : &usize| {
V[*left].FirstName.cmp(&V[*right].FirstName)
}
But BTrees don't have this functionality.
In similar cases, a wrapper type has been suggested for a custom std::cmp::Ord
implementation. But in my case, this can't be solved with wrapper types since these comparisons also require an external argument: &V
.
I also can't simply make these BTree element values (index, &V)
, since V
would then be shared-referenced, meaning its mutability is locked.
What options remain?
2 posts - 2 participants
🏷️ rust_feed