Skip to content

Commit

Permalink
feat: Use usize instead of u8
Browse files Browse the repository at this point in the history
  • Loading branch information
kdheepak committed Jul 29, 2024
1 parent 779bd6e commit 8edeb6e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
16 changes: 11 additions & 5 deletions src/graph/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,25 @@ 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 as usize,
e.associated_line_pos_x as usize,
]
})
.collect(),
}
}
Expand Down
31 changes: 17 additions & 14 deletions src/graph/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@ use crate::{
graph::queue::PriorityQueue,
};

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 @@ -187,17 +187,17 @@ 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()) as usize
}

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 {
commit_line_state.push(Some(&commit.commit_hash));
Expand All @@ -210,8 +210,11 @@ fn update_commit_line<'a>(
commit: &'a Commit,
commit_line_state: &mut [Option<&'a CommitHash>],
target_commit_hashes: &[&CommitHash],
) -> u8 {
let mut min_pos_x = commit_line_state.len();
) -> usize {
if commit_line_state.is_empty() {
return 0;
}
let mut min_pos_x = commit_line_state.len().saturating_sub(1);
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 @@ -226,7 +229,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 @@ -238,8 +241,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 @@ -253,7 +256,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
15 changes: 7 additions & 8 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

0 comments on commit 8edeb6e

Please sign in to comment.