use num_traits::Float;
#[derive(Clone, Copy, Debug)]
pub struct OrderedFloat<T>(pub T);
impl<T: Float> PartialEq for OrderedFloat<T> {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl<T: Float> Eq for OrderedFloat<T> {}
impl<T: Float> PartialOrd for OrderedFloat<T> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl<T: Float> Ord for OrderedFloat<T> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0
.partial_cmp(&other.0)
.unwrap_or(std::cmp::Ordering::Equal)
}
}
pub struct SortedBuffer<T> {
data: Vec<T>,
}
impl<T: Ord> SortedBuffer<T> {
pub fn new() -> Self {
Self { data: Vec::new() }
}
pub fn with_capacity(capacity: usize) -> Self {
Self {
data: Vec::with_capacity(capacity),
}
}
pub fn clear(&mut self) {
self.data.clear();
}
pub fn len(&self) -> usize {
self.data.len()
}
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
pub fn reserve(&mut self, additional: usize) {
self.data.reserve(additional);
}
#[inline]
pub fn insert(&mut self, item: T, limit: usize) -> bool {
if self.data.len() < limit {
let pos = self.data.binary_search(&item).unwrap_or_else(|e| e);
self.data.insert(pos, item);
true
} else if let Some(last) = self.data.last() {
if &item < last {
let pos = self.data.binary_search(&item).unwrap_or_else(|e| e);
self.data.pop();
self.data.insert(pos, item);
true
} else {
false
}
} else {
false
}
}
#[inline]
pub fn top(&self) -> Option<&T> {
self.data.last()
}
pub fn data(&self) -> &[T] {
&self.data
}
#[inline]
pub fn sort_ascending(&mut self) {
}
pub fn size(&self) -> usize {
self.data.len()
}
}