From 523f938253c2558d11f9bf4c4409b37ceb4552ad Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Fri, 8 Dec 2023 21:02:42 +0200 Subject: [PATCH 1/2] refactor(parser): avoid unnecessary cloning of the filename --- parser/src/include_logic.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/parser/src/include_logic.rs b/parser/src/include_logic.rs index 88d95a034..0fd306e84 100644 --- a/parser/src/include_logic.rs +++ b/parser/src/include_logic.rs @@ -42,7 +42,7 @@ impl FileStack { } } } - Result::Err( produce_report_with_message(ReportCode::IncludeNotFound, name)) + Result::Err(produce_report_with_message(ReportCode::IncludeNotFound, name)) } pub fn take_next(f_stack: &mut FileStack) -> Option { @@ -91,7 +91,7 @@ impl IncludesGraph { let mut crr = PathBuf::new(); crr.push(old_path.clone()); let path = std::fs::canonicalize(crr) - .map_err(|_e| produce_report_with_message(ReportCode::FileOs,old_path))?; + .map_err(|_e| produce_report_with_message(ReportCode::FileOs, old_path))?; let edges = self.adjacency.entry(path).or_insert(vec![]); edges.push(self.nodes.len() - 1); Ok(()) @@ -150,7 +150,7 @@ impl IncludesGraph { .map(|file| -> String { let file = format!("{}", file.display()); let (_, file) = file.rsplit_once("/").unwrap(); - file.clone().to_string() + file.to_string() }) .collect::>(); let mut path_covered = format!("{}", path[0]); From e648d7994d0879bb1274306ac433d90b95ea3f62 Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Fri, 8 Dec 2023 21:03:52 +0200 Subject: [PATCH 2/2] chore: format memory_slice.rs --- program_structure/src/utils/memory_slice.rs | 69 ++++++++++++--------- 1 file changed, 41 insertions(+), 28 deletions(-) diff --git a/program_structure/src/utils/memory_slice.rs b/program_structure/src/utils/memory_slice.rs index 8a8d4f10f..dd3861fd6 100644 --- a/program_structure/src/utils/memory_slice.rs +++ b/program_structure/src/utils/memory_slice.rs @@ -1,16 +1,16 @@ use num_bigint_dig::BigInt; use std::fmt::{Display, Formatter}; -pub enum TypeInvalidAccess{ +pub enum TypeInvalidAccess { MissingInputs(String), MissingInputTags(String), NoInitializedComponent, - NoInitializedSignal + NoInitializedSignal, } -pub enum TypeAssignmentError{ +pub enum TypeAssignmentError { MultipleAssignments, - AssignmentOutput + AssignmentOutput, } pub enum MemoryError { @@ -26,7 +26,7 @@ pub enum MemoryError { AssignmentTagInputTwice(String, String), AssignmentTagInput, TagValueNotInitializedAccess, - MissingInputs(String) + MissingInputs(String), } pub type SliceCapacity = usize; pub type SimpleSlice = MemorySlice; @@ -44,7 +44,11 @@ pub struct MemorySlice { impl Clone for MemorySlice { fn clone(&self) -> Self { - MemorySlice { route: self.route.clone(), values: self.values.clone(), number_inserts: self.number_inserts} + MemorySlice { + route: self.route.clone(), + values: self.values.clone(), + number_inserts: self.number_inserts, + } } } @@ -71,7 +75,6 @@ impl MemorySlice { memory_slice: &MemorySlice, access: &[SliceCapacity], ) -> Result { - if access.len() > memory_slice.route.len() { return Result::Err(MemoryError::OutOfBoundsError); } @@ -95,13 +98,12 @@ impl MemorySlice { new_values: &MemorySlice, is_strict: bool, ) -> Result<(), MemoryError> { - if access.len() + new_values.route.len() > memory_slice.route.len() { return Result::Err(MemoryError::OutOfBoundsError); } let mut i: SliceCapacity = 0; - + while i < access.len() { if access[i] >= memory_slice.route[i] { return Result::Err(MemoryError::OutOfBoundsError); @@ -114,14 +116,26 @@ impl MemorySlice { while i < new_values.route.len() { if new_values.route[i] < memory_slice.route[initial_index_new + i] { - if is_strict{ // case signals: we do not allow - return Result::Err(MemoryError::MismatchedDimensions(new_values.route[i], memory_slice.route[initial_index_new + i])); - } else{ // case variables: we allow the assignment of smaller arrays - return Result::Err(MemoryError::MismatchedDimensionsWeak(new_values.route[i], memory_slice.route[initial_index_new + i])); + // case signals: we do not allow + if is_strict { + return Result::Err(MemoryError::MismatchedDimensions( + new_values.route[i], + memory_slice.route[initial_index_new + i], + )); } + + // case variables: we allow the assignment of smaller arrays + return Result::Err(MemoryError::MismatchedDimensionsWeak( + new_values.route[i], + memory_slice.route[initial_index_new + i], + )); } + if new_values.route[i] > memory_slice.route[initial_index_new + i] { - return Result::Err(MemoryError::MismatchedDimensions(new_values.route[i], memory_slice.route[initial_index_new + i])); + return Result::Err(MemoryError::MismatchedDimensions( + new_values.route[i], + memory_slice.route[initial_index_new + i], + )); } i += 1; } @@ -193,9 +207,9 @@ impl MemorySlice { memory_slice: &mut MemorySlice, access: &[SliceCapacity], new_values: &MemorySlice, - is_strict:bool, + is_strict: bool, ) -> Result<(), MemoryError> { - match MemorySlice::check_correct_dims(memory_slice, access, new_values, is_strict){ + match MemorySlice::check_correct_dims(memory_slice, access, new_values, is_strict) { Result::Ok(_) => { let mut cell = MemorySlice::get_initial_cell(memory_slice, access)?; @@ -203,14 +217,14 @@ impl MemorySlice { // > (MemorySlice::get_number_of_cells(memory_slice) - cell) // { // return Result::Err(MemoryError::OutOfBoundsError); - - memory_slice.number_inserts += MemorySlice::get_number_of_cells(new_values); + + memory_slice.number_inserts += MemorySlice::get_number_of_cells(new_values); for value in new_values.values.iter() { memory_slice.values[cell] = value.clone(); cell += 1; } Result::Ok(()) - }, + } Result::Err(MemoryError::MismatchedDimensionsWeak(dim_1, dim_2)) => { let mut cell = MemorySlice::get_initial_cell(memory_slice, access)?; // if MemorySlice::get_number_of_cells(new_values) @@ -223,7 +237,7 @@ impl MemorySlice { cell += 1; } Result::Err(MemoryError::MismatchedDimensionsWeak(dim_1, dim_2)) - }, + } Result::Err(error) => return Err(error), } } @@ -232,7 +246,7 @@ impl MemorySlice { memory_slice: &mut MemorySlice, index: usize, new_value: C, - )-> Result<(), MemoryError> { + ) -> Result<(), MemoryError> { if index > MemorySlice::get_number_of_cells(memory_slice) { return Result::Err(MemoryError::OutOfBoundsError); } @@ -244,16 +258,15 @@ impl MemorySlice { pub fn get_access_index( memory_slice: &MemorySlice, index: usize, - ) -> Result, MemoryError>{ + ) -> Result, MemoryError> { let mut number_cells = MemorySlice::get_number_of_cells(memory_slice); if index > number_cells { return Result::Err(MemoryError::OutOfBoundsError); - } - else{ + } else { let mut access = vec![]; let mut index_aux = index; - for pos in &memory_slice.route{ - number_cells = number_cells/pos; + for pos in &memory_slice.route { + number_cells = number_cells / pos; access.push(index_aux / number_cells); index_aux = index_aux % number_cells; } @@ -270,7 +283,7 @@ impl MemorySlice { pub fn access_value_by_index( memory_slice: &MemorySlice, index: usize, - )-> Result { + ) -> Result { if index > MemorySlice::get_number_of_cells(memory_slice) { return Result::Err(MemoryError::OutOfBoundsError); } @@ -381,7 +394,7 @@ mod tests { let mut slice = U32Slice::new_with_route(&route, &0); let new_row = U32Slice::new_with_route(&[4], &4); - let res = U32Slice::insert_values(&mut slice, &[2], &new_row); + let res = U32Slice::insert_values(&mut slice, &[2], &new_row, false); if let Result::Ok(_) = res { for c in 0..4 { let memory_result = U32Slice::get_reference_to_single_value(&slice, &[2, c]);