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

Bar charts + box plots continued #863

Merged
merged 9 commits into from
Nov 29, 2021
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w
## Unreleased

### Added ⭐
* Add bar charts and box plots ([#863](https://github.com/emilk/egui/pull/863)).
* Add context menus: See `Ui::menu_button` and `Response::context_menu` ([#543](https://github.com/emilk/egui/pull/543)).
* You can now read and write the cursor of a `TextEdit` ([#848](https://github.com/emilk/egui/pull/848)).
* Most widgets containing text (`Label`, `Button` etc) now supports rich text ([#855](https://github.com/emilk/egui/pull/855)).
Expand Down
40 changes: 40 additions & 0 deletions egui/src/util/float_ord.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//! Total order on floating point types, assuming absence of NaN.
//! Can be used for sorting, min/max computation, and other collection algorithms.
Copy link
Owner

Choose a reason for hiding this comment

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

I like this idea of having our own OrderedFloat without having to pull in the much bigger ordered-float crate. There already is an epaint::f32_hash function in epaint/src/lib.rs. Perhaps we should implement Hash on OrderedFloat and move it to epaint/src/util/ordered_float.rs.

But let's make it support NaN instead of panicing on them!

Copy link
Owner

Choose a reason for hiding this comment

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

I can also help with this refactor after merge

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I'll gladly open a new PR after this one, but probably makes sense to keep it out of this (already big) changeset.


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 {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
Bromeon marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl PartialOrd<Self> for OrderedFloat {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.0.partial_cmp(&other.0)
Bromeon marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl Ord for OrderedFloat {
fn cmp(&self, other: &Self) -> Ordering {
self.partial_cmp(other).expect("NaN or Inf not permitted")
Bromeon marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// 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 {
fn ord(self) -> OrderedFloat {
Bromeon marked this conversation as resolved.
Show resolved Hide resolved
OrderedFloat(self)
}
}
1 change: 1 addition & 0 deletions egui/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

pub mod cache;
pub(crate) mod fixed_cache;
pub(crate) mod float_ord;
mod history;
pub mod id_type_map;
pub mod undoer;
Expand Down
Loading