diff --git a/klout/Cargo.toml b/klout/Cargo.toml index ce200d5..9e6b42f 100644 --- a/klout/Cargo.toml +++ b/klout/Cargo.toml @@ -3,6 +3,10 @@ name = "klout" version = "0.0.0" edition = "2024" +[[bin]] +name = "klout" +path = "src/klout.rs" + [dependencies] eyre = "0.6.12" rand = "0.8.5" \ No newline at end of file diff --git a/klout/data/initial_layout.txt b/klout/data/initial_layout.txt new file mode 100644 index 0000000..fefffdb --- /dev/null +++ b/klout/data/initial_layout.txt @@ -0,0 +1,3 @@ +q w f p b j l u y ' +a r s t g m n e i o +x c d v z k h , . / diff --git a/klout/data/matrices.txt b/klout/data/matrices.txt new file mode 100644 index 0000000..e55c1c0 --- /dev/null +++ b/klout/data/matrices.txt @@ -0,0 +1,7 @@ +LPINKY LRING LMIDDLE LINDEX LINDEX RINDEX RINDEX RMIDDLE RRING RPINKY +LPINKY LRING LMIDDLE LINDEX LINDEX RINDEX RINDEX RMIDDLE RRING RPINKY +LRING LMIDDLE LINDEX LINDEX LINDEX RINDEX RINDEX RMIDDLE RRING RPINKY + +100 4 2 4 30 30 4 2 4 100 +50 1 0.1 0.1 5 5 0.1 0.1 1 50 +2 3 2 10 30 10 2 3 2 100 diff --git a/klout/src/klout.rs b/klout/src/klout.rs new file mode 100644 index 0000000..8cf0fb5 --- /dev/null +++ b/klout/src/klout.rs @@ -0,0 +1,106 @@ +use std::{collections::HashMap, hash::Hash}; + +use eyre::{eyre, Result}; + +#[derive(Debug)] +struct Matrix { + width: usize, + height: usize, + data: Vec, + to_coord: HashMap, +} + +#[derive(Copy, Clone, Eq, PartialEq, Debug)] +struct MatrixCoord { + x: usize, + y: usize, +} +impl MatrixCoord { + fn new(x: usize, y: usize) -> Self { + Self { x, y } + } +} + +impl Matrix { + fn new(width: usize, height: usize) -> Self { + let data = vec![T::default(); width * height]; + + Self { + width, + height, + data, + to_coord: Default::default(), + } + } + + fn val_to_coord(&self, v: &T) -> MatrixCoord { + *self.to_coord.get(v).unwrap() + } + + fn val_to_index(&self, v: &T) -> usize { + self.coord_to_index(self.val_to_coord(v)) + } + + fn val_at_coord(&self, c: MatrixCoord) -> &T { + self.val_at_index(self.coord_to_index(c)) + } + + fn val_at_index(&self, i: usize) -> &T { + &self.data[i] + } + + fn index_to_coord(&self, i: usize) -> MatrixCoord { + let x = i % self.width; + let y = i / self.width; + + MatrixCoord { x, y } + } + + fn coord_to_index(&self, c: MatrixCoord) -> usize { + c.y * self.width + c.x + } + + fn set(&mut self, v: T, c: MatrixCoord) { + let i = self.coord_to_index(c); + self.data[i] = v; + self.to_coord.insert(v, c); + } +} + +type Layout = Matrix; + +fn load_initial_layout() -> Result { + let data = std::fs::read_to_string("data/initial_layout.txt")?; + let data: Vec> = data + .lines() + .map(|l| { + l.split_whitespace() + .flat_map(|s| s.chars().next()) + .collect() + }) + .collect(); + + let width = data + .iter() + .map(|l| l.len()) + .max() + .ok_or(eyre!("Initial layout was empty"))?; + + let height = data.len(); + + let mut layout = Layout::new(width, height); + for (y, row) in data.iter().enumerate() { + for (x, ch) in row.iter().enumerate() { + layout.set(*ch, MatrixCoord::new(x, y)); + } + } + + Ok(layout) +} + +fn main() -> Result<()> { + let layout = load_initial_layout()?; + // TODO: let (m_finger, m_effort) = load_matrices()?; + + Ok(()) +}