[klout] initial runtime data version

This commit is contained in:
soup 2024-12-22 01:05:25 -05:00
parent 8d838e3751
commit ce38e751ac
Signed by: soup
SSH key fingerprint: SHA256:GYxje8eQkJ6HZKzVWDdyOUF1TyDiprruGhE0Ym8qYDY
4 changed files with 120 additions and 0 deletions

View file

@ -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"

View file

@ -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 , . /

7
klout/data/matrices.txt Normal file
View file

@ -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

106
klout/src/klout.rs Normal file
View file

@ -0,0 +1,106 @@
use std::{collections::HashMap, hash::Hash};
use eyre::{eyre, Result};
#[derive(Debug)]
struct Matrix<T> {
width: usize,
height: usize,
data: Vec<T>,
to_coord: HashMap<T, MatrixCoord>,
}
#[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<T: Copy + Clone + Default + Eq + Hash> Matrix<T> {
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<char>;
fn load_initial_layout() -> Result<Layout> {
let data = std::fs::read_to_string("data/initial_layout.txt")?;
let data: Vec<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(())
}