Skip to content

Instantly share code, notes, and snippets.

@Reboare
Created July 16, 2014 18:37
Show Gist options
  • Save Reboare/cfab4f937100fb5bcd8c to your computer and use it in GitHub Desktop.
Save Reboare/cfab4f937100fb5bcd8c to your computer and use it in GitHub Desktop.
extern crate collections;
use std::num::from_str_radix;
use std::fmt::radix;
use std::iter::{range_step};
struct HexMap {
data: Vec<Vec<u8>>
}
impl HexMap {
fn show(&self) {
for line in self.data.iter() {
println!("{0}", String::from_utf8(line.clone()).unwrap())
}
}
fn from_hex(hex: &str) -> HexMap {
let mut tempstorage = Vec::new();
for word in hex.words() {
let radix_conv = radix(from_str_radix::<uint>(word, 16).unwrap(), 2);
let replaced = format!("{0}", radix_conv).replace("1", "x").replace("0", " ");
let padded = String::from_str(" ").repeat(8-replaced.len()) + replaced;
tempstorage.push(padded.into_bytes());
}
HexMap {data: tempstorage}
}
fn rot_anti(&self) -> HexMap {
//equivalent to a rotate 90 degrees clockwise
//create a new vector to store the tranposed
let mut nvec: Vec<Vec<u8>> = range(0, self.data.len()).map(|_| Vec::new()).collect();
for vec in self.data.iter() {
let mut temp_vec = vec.clone();
temp_vec.reverse();
for (each, &val) in nvec.mut_iter().zip(temp_vec.iter()) {
each.push(val);
}
}
HexMap {
data: nvec
}
}
fn rot(&self) -> HexMap {
//clockwise rotation
//yeah this is inefficient but I'm lazy
self.rot_anti().rot_anti().rot_anti()
}
fn invert(&self) -> HexMap {
//not sure if there's a replace for
//vectors. Couldn't find it but this works
let data =
self.data.iter()
.map(|vec|
vec.iter().map(|&val| match val {
120 => 32,
32 => 120,
_ => fail!("")
}).collect()).collect();
HexMap {
data: data
}
}
fn zoom(&self, rate: uint) -> HexMap {
if rate > 4u {fail!("")}
//makes me wish we had matrix support
let mut nvec: Vec<Vec<u8>> = Vec::new();
for each in self.data.iter() {
//we'll move everything in here
let mut temp = Vec::new();
let _ : Vec<()> = each.iter().map(|i| temp.grow(rate, i)).collect();
nvec.grow(rate, &temp);
}
HexMap {
data: nvec
}
}
fn zoom_in(&self, rate: uint) -> HexMap{
if rate > 4u {fail!("")}
let mut nvec: Vec<Vec<u8>> = Vec::new();
for i_vec in range_step(0, self.data.len(), rate) {
//we'll move everything in here
let mut temp = Vec::new();
let each = self.data.get(i_vec);
for i_data in range_step(0, self.data.len(), rate){
temp.push(*each.get(i_data))
}
nvec.push(temp);
}
HexMap {
data: nvec
}
}
}
fn main() {
/*
let arg = args();
let hexarg = arg.get(1).as_slice();*/
let hx = "18 3C 7E 7E 18 18 18 18";
let map = HexMap::from_hex(hx);
map.zoom(4).zoom_in(2).show();
map.rot().show();
map.invert().show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment