Permalink
Browse files

fnv1a in rust

  • Loading branch information...
stevej committed Dec 12, 2012
1 parent c5286f2 commit 4cd8f210790079d2257ec5463e3096fd69313109
Showing with 65 additions and 0 deletions.
  1. +2 −0 README.md
  2. +1 −0 crate.rc
  3. +62 −0 fnv1a.rs
View
@@ -9,3 +9,5 @@
`red_black_tree.rs` - A purely functional red-black tree.
`ctpop.rs` - A Rust implementation of the population count (native CTPOP instruction)
+
+`fnv1a.rs` - A Rust implementation of the FNV 1a hashing algorithm.
View
@@ -22,5 +22,6 @@ pub mod lazy;
pub mod pairing_heap;
pub mod red_black_tree;
pub mod ctpop;
+pub mod fnv1a;
mod test_tree;
View
@@ -0,0 +1,62 @@
+/**
+ * Pure rust implementations of 32-bit and 64-bit versions
+ * of the FNV 1a hashing algorithms.
+ */
+const FNV_prime_64 : u64 = 1099511628211u64;
+const FNV_offset_64 : u64 = 14695981039346656037u64;
+
+pure fn fnv1a_64(data: ~[u8]) -> u64 {
+ let mut hash = FNV_offset_64;
+
+ for vec::each(data) |datum| {
+ hash ^= (*datum as u64);
+ hash *= FNV_prime_64;
+ }
+
+ return hash;
+}
+
+const FNV_prime_32 : u32 = 16777619u32;
+const FNV_offset_32 : u32 = 2166136261u32;
+
+pure fn fnv1a_32(data: ~[u8]) -> u32 {
+ let mut hash = FNV_offset_32;
+
+ for vec::each(data) |datum| {
+ hash ^= (*datum as u32);
+ hash *= FNV_prime_32;
+ }
+
+ return hash;
+}
+
+#[cfg(test)]
+mod tests {
+ fn f64(bytes: ~str) -> u64 {
+ fnv1a_64(str::to_bytes(bytes))
+ }
+
+ #[test]
+ fn test_fnv1a64() {
+ assert(f64(~"foobar") == 0x85944171f73967e8);
+ assert(f64(~"") == 0xcbf29ce484222325);
+ assert(f64(~"f") == 0xaf63db4c8601ead9);
+ assert(f64(~"chongo was here!\n") == 0x46810940eff5f915);
+ assert(f64(~"\x54\x4e\x51\x40") == 0x4b7b10fa9fe83936);
+ assert(f64(~"64.81.78.84") == 0xe73042c5d2ae266d);
+ }
+
+ fn f32(bytes: ~str) -> u32 {
+ fnv1a_32(str::to_bytes(bytes))
+ }
+
+ #[test]
+ fn test_fnv1a32() {
+ assert(f32(~"foobar") == 0xbf9cf968);
+ assert(f32(~"") == 0x811c9dc5);
+ assert(f32(~"f") == 0xe30c2799);
+ assert(f32(~"chongo was here!\n") == 0xd49930d5);
+ assert(f32(~"\x54\x4e\x51\x40") == 0x772633d6);
+ assert(f32(~"64.81.78.84") == 0xa55b89ed);
+ }
+}

0 comments on commit 4cd8f21

Please sign in to comment.