Added documentation.

This commit is contained in:
Rhinemann 2023-06-27 23:45:55 +03:00
parent 7ab69ecbe8
commit ca91c2ea56
1 changed files with 21 additions and 5 deletions

View File

@ -18,6 +18,7 @@ pub struct BasicRegister {
}
impl BasicRegister {
/// Adjusts a register to a given size in bits.
pub fn adjusted_by_size(&mut self, resulting_size: usize) -> Self {
let current_memory_size: usize = self.memory.len();
let difference: i32 = current_memory_size as i32 - resulting_size as i32;
@ -36,14 +37,12 @@ impl BasicRegister {
}
}
pub fn negate(&mut self) {
self.memory = self.memory.iter().map(|val| !val).collect();
}
/// Returns a register which was logically negated.
pub fn negated(&self) -> Self {
BasicRegister::new(self.memory.iter().map(|val| !val).collect())
}
/// Shifts the register to the left by a specified number of steps, shifting in the provided values.
pub fn left_shift(
&mut self,
shift_in_value_default: Option<bool>,
@ -61,6 +60,7 @@ impl BasicRegister {
shifted_bits
}
/// Shifts the register to the left by a specified number of steps, shifting in the provided values.
pub fn right_shift(
&mut self,
shift_in_value_default: Option<bool>,
@ -78,10 +78,12 @@ impl BasicRegister {
shifted_bits
}
/// Constructs a new BasicRegister from a given VecDeque<bool> as memory.
pub fn new(memory: VecDeque<bool>) -> Self {
Self { memory }
}
/// Returns the number of bits stored in a register.
pub fn len(&self) -> usize {
self.memory.len()
}
@ -122,10 +124,16 @@ impl fmt::Binary for BasicRegister {
}
}
/// Converts a u8 number to a boolean.
///
/// # Returns
/// - false - if the number is zero or below.
/// - true - if the number is above zero.
fn u8_to_bool(number: u8) -> bool {
number > 0
}
/// Reads a Vec<char> containing only 1-s and 0-s from user.
fn read_vec(variable_name: &str) -> Vec<char> {
loop {
print!("Enter {variable_name}: ");
@ -140,7 +148,8 @@ fn read_vec(variable_name: &str) -> Vec<char> {
}
}
pub fn char_to_bool_vector(char_vector: Vec<char>) -> VecDeque<bool> {
/// Converts a Vec<char> containing only 1-s and 0-s to VecDeque<bool>.
pub fn char_to_bool_vecdeque(char_vector: Vec<char>) -> VecDeque<bool> {
let mut bool_vector: VecDeque<bool> = VecDeque::new();
for value in char_vector.iter() {
@ -154,10 +163,12 @@ pub fn char_to_bool_vector(char_vector: Vec<char>) -> VecDeque<bool> {
bool_vector
}
/// Handles getting the memory for the register from the user.
pub fn get_memory(variable_name: &str) -> VecDeque<bool> {
char_to_bool_vector(read_vec(variable_name))
}
/// Aligns two registers by the length of the bigger one.
pub fn align_registers(
mut first_register: BasicRegister,
mut second_register: BasicRegister,
@ -172,6 +183,8 @@ pub fn align_registers(
)
}
/// Sums two terms containing binary numbers and keeps the carry-out.
/// Returns a tuple containing the register with result and a carry-out.
fn binary_sum_with_carry(
first_term: &BasicRegister,
second_term: &BasicRegister,
@ -189,10 +202,13 @@ fn binary_sum_with_carry(
(sum, carry)
}
/// Sums two terms containing binary numbers.
/// Returns the BasicRegister with the result.
pub fn binary_sum(first_term: &BasicRegister, second_term: &BasicRegister) -> BasicRegister {
binary_sum_with_carry(first_term, second_term).0
}
/// Subtracts the value of the subtrahend from the minuend in binary using ones' complement.
pub fn binary_subtraction(minuend: &BasicRegister, subtrahend: &BasicRegister) -> BasicRegister {
let (difference, final_carry) = binary_sum_with_carry(minuend, &subtrahend.negated());