Created
November 8, 2015 03:01
-
-
Save anonymous/e823e0e84891738bd911 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn old() -> Vec<u8> { | |
let mut hash = vec![0u8; 8]; // vec of u8 representing the bits and bytes of the hash computed for the image. | |
let raw: Vec<_> = (0..8).flat_map(|_| (0 .. 9).rev()).collect(); // vec of u8 representing gray pixels of an 9x8 image. | |
for x in 0..8 { | |
for y in 0..8 { | |
if raw[x*9+y] > raw[x*9+y+1] { | |
hash[x] |= 0x1 << y | |
} | |
} | |
} | |
hash | |
} | |
fn new() -> Vec<u8> { | |
let mut hash = vec![0u8; 8]; // vec of u8 representing the bits and bytes of the hash computed for the image. | |
let raw: Vec<_> = (0..8).flat_map(|_| (0 .. 9).rev()).collect(); // vec of u8 representing gray pixels of an 9x8 image. | |
for (row, hash_byte) in raw.chunks(9).zip(&mut hash) { | |
*hash_byte = row.windows(2).enumerate().filter_map(|(i, window)| { | |
if window[0] > window[1] { Some(1 << i) } else { None } | |
}).fold(0, |accum, curr| accum | curr); | |
} | |
hash | |
} | |
fn main(){ | |
println!("old: {:?}", old()); | |
println!("new: {:?}", new()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment