Skip to content

Commit

Permalink
feat: Use usize instead of u8 (#6)
Browse files Browse the repository at this point in the history
* feat: Use usize instead of u8

* fix: clippy warnings
  • Loading branch information
kdheepak authored Jul 29, 2024
1 parent 6db85e0 commit 356ff3e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl<'a> App<'a> {
ref_name_to_commit_index_map.insert(r.name(), i);
}
let (pos_x, _) = graph.commit_pos_map[&commit.commit_hash];
let graph_color = color_set.get(pos_x as usize).to_ratatui_color();
let graph_color = color_set.get(pos_x).to_ratatui_color();
CommitInfo::new(commit, image, refs, graph_color)
})
.collect();
Expand Down
12 changes: 6 additions & 6 deletions src/graph/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ impl ImageCacheDirKey {

#[derive(Debug, Serialize)]
pub struct ImageCacheFileKey {
pos_x: u8,
cell_count: u8,
edges: Vec<[u8; 3]>,
pos_x: usize,
cell_count: usize,
edges: Vec<[usize; 3]>,
}

impl ImageCacheFileKey {
pub fn new(pos_x: u8, cell_count: u8, edges: Vec<Edge>) -> Self {
pub fn new(pos_x: usize, cell_count: usize, edges: Vec<Edge>) -> Self {
Self {
pos_x,
cell_count,
edges: edges
.iter()
.map(|e| [e.edge_type as u8, e.pos_x, e.associated_line_pos_x])
.map(|e| [e.edge_type as u8 as usize, e.pos_x, e.associated_line_pos_x])
.collect(),
}
}
Expand All @@ -74,7 +74,7 @@ impl ImageCache {
let bytes = std::fs::read(cache_file_path).unwrap();
let image = GraphRowImage {
bytes,
cell_count: key.cell_count as usize,
cell_count: key.cell_count,
};
Some(image)
} else {
Expand Down
35 changes: 16 additions & 19 deletions src/graph/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ use std::collections::HashMap;

use crate::git::{Commit, CommitHash, Repository};

type CommitPosMap<'a> = HashMap<&'a CommitHash, (u8, usize)>;
type CommitPosMap<'a> = HashMap<&'a CommitHash, (usize, usize)>;

#[derive(Debug)]
pub struct Graph<'a> {
pub commits: Vec<&'a Commit>,
pub commit_pos_map: CommitPosMap<'a>,
pub edges: Vec<Vec<Edge>>,
pub max_pos_x: u8,
pub max_pos_x: usize,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Edge {
pub edge_type: EdgeType,
pub pos_x: u8,
pub associated_line_pos_x: u8,
pub pos_x: usize,
pub associated_line_pos_x: usize,
}

impl Edge {
fn new(edge_type: EdgeType, pos_x: u8, line_pos_x: u8) -> Self {
fn new(edge_type: EdgeType, pos_x: usize, line_pos_x: usize) -> Self {
Self {
edge_type,
pos_x,
Expand Down Expand Up @@ -104,34 +104,31 @@ fn filtered_children_hash<'a>(
.collect()
}

fn get_first_vacant_line(commit_line_state: &[Option<&CommitHash>]) -> u8 {
fn get_first_vacant_line(commit_line_state: &[Option<&CommitHash>]) -> usize {
commit_line_state
.iter()
.position(|c| c.is_none())
.unwrap_or(commit_line_state.len()) as u8
.unwrap_or(commit_line_state.len())
}

fn add_commit_line<'a>(
commit: &'a Commit,
commit_line_state: &mut Vec<Option<&'a CommitHash>>,
pos_x: u8,
pos_x: usize,
) {
if commit_line_state.len() <= pos_x as usize {
if commit_line_state.len() <= pos_x {
commit_line_state.push(Some(&commit.commit_hash));
} else {
commit_line_state[pos_x as usize] = Some(&commit.commit_hash);
commit_line_state[pos_x] = Some(&commit.commit_hash);
}
}

fn update_commit_line<'a>(
commit: &'a Commit,
commit_line_state: &mut [Option<&'a CommitHash>],
target_commit_hashes: &[&CommitHash],
) -> u8 {
if commit_line_state.is_empty() {
return 0;
}
let mut min_pos_x = commit_line_state.len().saturating_sub(1);
) -> usize {
let mut min_pos_x = commit_line_state.len();
for target_hash in target_commit_hashes {
for (pos_x, commit_hash) in commit_line_state.iter().enumerate() {
if let Some(hash) = commit_hash {
Expand All @@ -146,7 +143,7 @@ fn update_commit_line<'a>(
}
}
commit_line_state[min_pos_x] = Some(&commit.commit_hash);
min_pos_x as u8
min_pos_x
}

#[derive(Debug, Clone)]
Expand All @@ -158,8 +155,8 @@ struct WrappedEdge<'a> {
impl<'a> WrappedEdge<'a> {
fn new(
edge_type: EdgeType,
pos_x: u8,
line_pos_x: u8,
pos_x: usize,
line_pos_x: usize,
edge_parent_hash: &'a CommitHash,
) -> Self {
Self {
Expand All @@ -173,7 +170,7 @@ fn calc_edges(
commit_pos_map: &CommitPosMap,
commits: &[&Commit],
repository: &Repository,
) -> (Vec<Vec<Edge>>, u8) {
) -> (Vec<Vec<Edge>>, usize) {
let mut max_pos_x = 0;
let mut edges: Vec<Vec<WrappedEdge>> = vec![vec![]; commits.len()];

Expand Down
19 changes: 9 additions & 10 deletions src/graph/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub fn build_graph_image(graph: &Graph<'_>, options: GraphImageOptions) -> Graph

let drawing_pixels = DrawingPixels::new(&image_params);

let graph_row_sources: HashSet<(u8, &Vec<Edge>)> = graph
let graph_row_sources: HashSet<(usize, &Vec<Edge>)> = graph
.commits
.iter()
.map(|commit| {
Expand All @@ -101,7 +101,7 @@ pub fn build_graph_image(graph: &Graph<'_>, options: GraphImageOptions) -> Graph
})
.collect();

let cell_count: u8 = graph.max_pos_x + 1;
let cell_count = graph.max_pos_x + 1;

let images = graph_row_sources
.into_par_iter()
Expand Down Expand Up @@ -412,13 +412,12 @@ fn calc_corner_edge_drawing_pixels(
}

fn build_graph_row_image(
commit_pos_x: u8,
cell_count: u8,
commit_pos_x: usize,
cell_count: usize,
edges: &[Edge],
image_params: &ImageParams,
drawing_pixels: &DrawingPixels,
) -> GraphRowImage {
let cell_count = cell_count as usize;
let image_width = (image_params.width as usize * cell_count) as u32;
let image_height = image_params.height as u32;

Expand All @@ -437,12 +436,12 @@ fn build_graph_row_image(

fn draw_commit_circle(
img_buf: &mut image::ImageBuffer<image::Rgba<u8>, Vec<u8>>,
circle_pos_x: u8,
circle_pos_x: usize,
image_params: &ImageParams,
drawing_pixels: &DrawingPixels,
) {
let x_offset = (circle_pos_x as usize * image_params.width as usize) as i32;
let color = image_params.edge_color(circle_pos_x as usize);
let x_offset = (circle_pos_x * image_params.width as usize) as i32;
let color = image_params.edge_color(circle_pos_x);

for (x, y) in &drawing_pixels.circle {
let x = (*x + x_offset) as u32;
Expand Down Expand Up @@ -472,8 +471,8 @@ fn draw_edge(
EdgeType::LeftBottom => &drawing_pixels.left_bottom_edge,
};

let x_offset = (edge.pos_x as usize * image_params.width as usize) as i32;
let color = image_params.edge_color(edge.associated_line_pos_x as usize);
let x_offset = (edge.pos_x * image_params.width as usize) as i32;
let color = image_params.edge_color(edge.associated_line_pos_x);

for (x, y) in pixels {
let x = (*x + x_offset) as u32;
Expand Down

0 comments on commit 356ff3e

Please sign in to comment.