Skip to content

Instantly share code, notes, and snippets.

Created September 30, 2015 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/0d21819b4345e2bc5aef to your computer and use it in GitHub Desktop.
Save anonymous/0d21819b4345e2bc5aef to your computer and use it in GitHub Desktop.
Shared via Rust Playground
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 .. 72).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*8+y] > raw[x*8+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 .. 72).rev().collect(); // vec of u8 representing gray pixels of an 9x8 image.
for (x, chunk) in raw.chunks(9).enumerate() {
chunk[1..].iter().enumerate().fold(chunk[0], |before, (y, &current)| -> u8 {
if before > current {
hash[x] |= 0x1 << y;
}
current
});
}
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