Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. use std::collections::BTreeMap;
  2. use std::cmp::Ordering;
  3. use std::ops::Bound::{Included, Excluded};
  4.  
  5. #[derive( Debug)]
  6. struct FloatWrapper(f32);
  7.  
  8. impl Eq for FloatWrapper { }
  9.  
  10. impl PartialEq for FloatWrapper {
  11. fn eq(&self, other: &Self) -> bool {
  12. (self.0 - other.0).abs() < 1.17549435e-36f32
  13. }
  14. }
  15.  
  16. impl Ord for FloatWrapper {
  17. fn cmp(&self, other: &Self) -> Ordering {
  18. if (self.0 - other.0).abs() < 1.17549435e-36f32 {
  19. Ordering::Equal
  20. } else if self.0 - other.0 > 0.0 {
  21. Ordering::Greater
  22. } else if self.0 - other.0 < 0.0 {
  23. Ordering::Less
  24. } else {
  25. Ordering::Equal
  26. }
  27.  
  28. }
  29. }
  30.  
  31. impl PartialOrd for FloatWrapper {
  32. fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
  33. Some(self.cmp(other))
  34. }
  35. }
  36.  
  37. fn main() {
  38.  
  39. let mut ww = BTreeMap::new();
  40. ww.insert(FloatWrapper(1.0), "one");
  41. ww.insert(FloatWrapper(2.0), "two");
  42. ww.insert(FloatWrapper(3.0), "three");
  43. ww.insert(FloatWrapper(4.0), "three");
  44. let rez : Vec<&str> = ww.range((Included(&FloatWrapper(1.5)),(Excluded(&FloatWrapper(5.0)) )))
  45. .take(1)
  46. .map(|(_,&v)|v)
  47. .collect();
  48.  
  49.  
  50. println!("{:?}",rez)
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement