use prelude::*;
use params::IntoOwned;
pub trait GraphsSliceExt<T> {
fn is_sorted_by_prop<P, K>(&self, prop: P) -> bool
where
P: PropGet<K>,
for<'a> &'a T: IntoOwned<K>,
P::Output: Ord;
fn sort_by_prop<P, K>(&mut self, prop: P)
where
P: PropGet<K>,
for<'a> &'a T: IntoOwned<K>,
P::Output: Ord;
fn sort_unstable_by_prop<P, K>(&mut self, prop: P)
where
P: PropGet<K>,
for<'a> &'a T: IntoOwned<K>,
P::Output: Ord;
fn binary_search_by_prop<P, K>(&self, prop_value: &P::Output, prop: P) -> Result<usize, usize>
where
P: PropGet<K>,
for<'a> &'a T: IntoOwned<K>,
P::Output: Ord;
}
impl<T> GraphsSliceExt<T> for [T] {
fn is_sorted_by_prop<P, K>(&self, prop: P) -> bool
where
P: PropGet<K>,
for<'a> &'a T: IntoOwned<K>,
P::Output: Ord,
{
let mut iter = self.iter();
if let Some(item) = iter.next() {
let mut p = prop.get(item.into_owned());
for item in iter {
let q = prop.get(item.into_owned());
if p > q {
return false;
}
p = q;
}
}
true
}
#[inline]
fn sort_by_prop<P, K>(&mut self, prop: P)
where
P: PropGet<K>,
for<'a> &'a T: IntoOwned<K>,
P::Output: Ord,
{
self.sort_by_key(|item| prop.get(item.into_owned()))
}
#[inline]
fn sort_unstable_by_prop<P, K>(&mut self, prop: P)
where
P: PropGet<K>,
for<'a> &'a T: IntoOwned<K>,
P::Output: Ord,
{
self.sort_unstable_by_key(|item| prop.get(item.into_owned()))
}
#[inline]
fn binary_search_by_prop<P, K>(&self, prop_value: &P::Output, prop: P) -> Result<usize, usize>
where
P: PropGet<K>,
for<'a> &'a T: IntoOwned<K>,
P::Output: Ord,
{
self.binary_search_by_key(prop_value, |item| prop.get(item.into_owned()))
}
}
pub trait GraphsVecExt<T> {
fn sorted_by_prop<P, K>(self, prop: P) -> Self
where
P: PropGet<K>,
for<'a> &'a T: IntoOwned<K>,
P::Output: Ord;
fn sorted_unstable_by_prop<P, K>(self, prop: P) -> Self
where
P: PropGet<K>,
for<'a> &'a T: IntoOwned<K>,
P::Output: Ord;
}
impl<T> GraphsVecExt<T> for Vec<T> {
#[inline]
fn sorted_by_prop<P, K>(mut self, prop: P) -> Self
where
P: PropGet<K>,
for<'a> &'a T: IntoOwned<K>,
P::Output: Ord,
{
self.sort_by_prop(prop);
self
}
#[inline]
fn sorted_unstable_by_prop<P, K>(mut self, prop: P) -> Self
where
P: PropGet<K>,
for<'a> &'a T: IntoOwned<K>,
P::Output: Ord,
{
self.sort_unstable_by_prop(prop);
self
}
}