Skip to content

Add insert_sorted_by{,_key} methods for map/set #402

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

4e554c4c
Copy link

Resolves #401.

///
/// This is equivalent to finding the position with
/// [`binary_search_by`][Self::binary_search_by], then calling
/// [`insert_before`][Self::insert_before] for a new key.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The insertion call is not just for new keys.

Suggested change
/// [`insert_before`][Self::insert_before] for a new key.
/// [`insert_before`][Self::insert_before] with the given key and value.

///
/// This is equivalent to finding the position with
/// [`binary_search_by_key`][Self::binary_search_by_key] with `cmp(key)`, then
/// calling [`insert_before`][Self::insert_before] for a new key.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// calling [`insert_before`][Self::insert_before] for a new key.
/// calling [`insert_before`][Self::insert_before] with the given key and value.

///
/// If the sorted item is found in the set, it returns the index of that
/// existing item and `false`. Otherwise, it inserts the new item and
/// returns its sorted index and `true`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This paragraph needs adjustment because existing items also move -- or maybe just leave it out like you did with the map method.

///
/// If the sorted item is found in the set, it returns the index of that
/// existing item and `false`. Otherwise, it inserts the new item and
/// returns its sorted index and `true`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, either update or remove this paragraph.

Comment on lines +1237 to +1238
let values: Vec<_> = map.iter().map(|(k,v)|(*k,*v)).collect();
map.sort_by_cached_key(|key, _| key.abs());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better for this to sort the original values as a more independent check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add insert_sorted_by and insert_sorted_by_key
2 participants