mirror of
https://github.com/emilk/egui.git
synced 2026-08-30 13:20:05 -04:00
OrderedFloat refactor (#918)
* Move egui/util/float_ord.rs -> epaint/util/ordered_float.rs * Implement Hash on OrderedFloat * Generic OrderedFloat<T>; impl Hash; documentation
This commit is contained in:
@@ -1,64 +0,0 @@
|
||||
//! Total order on floating point types, assuming absence of NaN.
|
||||
//! Can be used for sorting, min/max computation, and other collection algorithms.
|
||||
|
||||
use std::cmp::Ordering;
|
||||
|
||||
/// Totally orderable floating-point value
|
||||
/// For not `f32` is supported; could be made generic if necessary.
|
||||
pub(crate) struct OrderedFloat(f32);
|
||||
|
||||
impl Eq for OrderedFloat {}
|
||||
|
||||
impl PartialEq<Self> for OrderedFloat {
|
||||
#[inline]
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
// NaNs are considered equal (equivalent when it comes to ordering
|
||||
if self.0.is_nan() {
|
||||
other.0.is_nan()
|
||||
} else {
|
||||
self.0 == other.0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd<Self> for OrderedFloat {
|
||||
#[inline]
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
match self.0.partial_cmp(&other.0) {
|
||||
Some(ord) => Some(ord),
|
||||
None => Some(self.0.is_nan().cmp(&other.0.is_nan())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for OrderedFloat {
|
||||
#[inline]
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
match self.partial_cmp(other) {
|
||||
Some(ord) => ord,
|
||||
None => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Extension trait to provide `ord` method
|
||||
pub(crate) trait FloatOrd {
|
||||
/// Type to provide total order, useful as key in sorted contexts.
|
||||
fn ord(self) -> OrderedFloat;
|
||||
}
|
||||
|
||||
impl FloatOrd for f32 {
|
||||
#[inline]
|
||||
fn ord(self) -> OrderedFloat {
|
||||
OrderedFloat(self)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO ordering may break down at least significant digits due to f64 -> f32 conversion
|
||||
// Possible solutions: generic OrderedFloat<T>, always OrderedFloat(f64)
|
||||
impl FloatOrd for f64 {
|
||||
#[inline]
|
||||
fn ord(self) -> OrderedFloat {
|
||||
OrderedFloat(self as f32)
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
pub mod cache;
|
||||
pub(crate) mod fixed_cache;
|
||||
pub(crate) mod float_ord;
|
||||
mod history;
|
||||
pub mod id_type_map;
|
||||
pub mod undoer;
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
use std::ops::RangeInclusive;
|
||||
|
||||
use epaint::util::FloatOrd;
|
||||
use epaint::Mesh;
|
||||
|
||||
use crate::util::float_ord::FloatOrd;
|
||||
use crate::*;
|
||||
|
||||
use super::{PlotBounds, ScreenTransform};
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
//! Simple plotting library.
|
||||
|
||||
use crate::util::float_ord::FloatOrd;
|
||||
use crate::*;
|
||||
use color::Hsva;
|
||||
use epaint::ahash::AHashSet;
|
||||
use epaint::color::Hsva;
|
||||
use epaint::util::FloatOrd;
|
||||
use items::PlotItem;
|
||||
use legend::LegendWidget;
|
||||
use transform::{PlotBounds, ScreenTransform};
|
||||
|
||||
Reference in New Issue
Block a user