-
Notifications
You must be signed in to change notification settings - Fork 152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A design for single-pass borrowed insert_if_not_there #93
Comments
In parity with the set.get(key).unwrap_or_else(|| {
let (i, _) = set.insert_full(key.to_owned());
set.get_index(i).unwrap()
}) The |
This does need to do a double lookup, and is what I've got written right now. (As |
Right, so it seems like you really want a borrowing version of I believe |
Something you may not have realized -- your You can functionally think of struct IndexMap<K, V> {
map: HashMap<K, usize>,
vec: Vec<(K, V)>,
} ... but optimized to not duplicate key storage. |
See rust-lang/rust#60894 -- I think |
I have an
IndexSet<String>
that I want to be able to insert or get index from&str
without allocating needlessly if the value is already in the set.This is actually possible with one small library addition:
entry_index
. It would return an entry for the value at the index up to and including.len()
, thus allowing insertion of the value there. It wouldn't even need to be unsafe: having two keys that compare equal in the map is only very bad for domain correctness, but not for memory correctness if I understand correctly.It could then be used for inserting if not present with something like the following:
The text was updated successfully, but these errors were encountered: