Skip to content
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

Add Vector::insert_ord_by method #161

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/vector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,43 @@ impl<A: Clone> Vector<A> {
}
}

/// Insert an element into a sorted vector using a comparator function.
///
/// Insert an element into a vector in sorted order using the given
/// comparator function, assuming the vector is already in sorted order.
///
/// Time: O(log n)
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate im;
/// # use im::vector::Vector;
/// use std::cmp::Ordering;
///
/// fn reverse_cmp(x: &u8, y: &u8) -> Ordering {
/// match x.cmp(&y) {
/// Ordering::Equal => Ordering::Equal,
/// Ordering::Greater => Ordering::Less,
/// Ordering::Less => Ordering::Greater,
/// }
/// }
///
/// let mut vec: Vector<u8> = vector![9, 8, 7, 3, 2, 1];
/// vec.insert_ord_by(5, reverse_cmp);
/// assert_eq!(vector![9, 8, 7, 5, 3, 2, 1], vec);
/// ```
pub fn insert_ord_by<F>(&mut self, item: A, mut f: F)
where
A: Ord,
F: FnMut(&A, &A) -> Ordering,
{
match self.binary_search_by(|scan_item| f(scan_item, &item)) {
Ok(index) => self.insert(index, item),
Err(index) => self.insert(index, item),
}
}

/// Sort a vector.
///
/// Time: O(n log n)
Expand Down