Skip to content

Commit

Permalink
Add IterBytes impls for float/f32/f64. This allows creating
Browse files Browse the repository at this point in the history
HashMaps with floats as keys.
  • Loading branch information
gareth authored and gareth committed Jun 15, 2013
1 parent eac0200 commit d22f417
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/libstd/hash.rs
Expand Up @@ -558,4 +558,15 @@ mod tests {
val & !(0xff << (byte * 8))
}
}

#[test]
fn test_float_hashes_differ() {
assert!(0.0.hash() != 1.0.hash());
assert!(1.0.hash() != (-1.0).hash());
}

#[test]
fn test_float_hashes_of_zero() {
assert_eq!(0.0.hash(), (-0.0).hash());
}
}
30 changes: 30 additions & 0 deletions src/libstd/to_bytes.rs
Expand Up @@ -14,6 +14,7 @@ The `ToBytes` and `IterBytes` traits
*/

use cast;
use io;
use io::Writer;
use option::{None, Option, Some};
Expand Down Expand Up @@ -190,6 +191,35 @@ impl IterBytes for int {
}
}

impl IterBytes for float {
#[inline(always)]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
(*self as f64).iter_bytes(lsb0, f)
}
}

impl IterBytes for f32 {
#[inline(always)]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
let i: u32 = unsafe {
// 0.0 == -0.0 so they should also have the same hashcode
cast::transmute(if *self == -0.0 { 0.0 } else { *self })
};
i.iter_bytes(lsb0, f)
}
}

impl IterBytes for f64 {
#[inline(always)]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
let i: u64 = unsafe {
// 0.0 == -0.0 so they should also have the same hashcode
cast::transmute(if *self == -0.0 { 0.0 } else { *self })
};
i.iter_bytes(lsb0, f)
}
}

impl<'self,A:IterBytes> IterBytes for &'self [A] {
#[inline(always)]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
Expand Down

5 comments on commit d22f417

@bors
Copy link
Contributor

@bors bors commented on d22f417 Jun 16, 2013

Choose a reason for hiding this comment

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

saw approval from graydon
at Dretch@d22f417

@bors
Copy link
Contributor

@bors bors commented on d22f417 Jun 16, 2013

Choose a reason for hiding this comment

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

merging Dretch/rust/float-hash = d22f417 into auto

@bors
Copy link
Contributor

@bors bors commented on d22f417 Jun 16, 2013

Choose a reason for hiding this comment

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

Dretch/rust/float-hash = d22f417 merged ok, testing candidate = d1a2360

@bors
Copy link
Contributor

@bors bors commented on d22f417 Jun 16, 2013

Choose a reason for hiding this comment

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

@bors
Copy link
Contributor

@bors bors commented on d22f417 Jun 16, 2013

Choose a reason for hiding this comment

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

fast-forwarding master to auto = d1a2360

Please sign in to comment.