changed inputs for second section and implemented Clone and Copy traits for Alignment data structure

This commit is contained in:
2024-04-14 20:13:15 +04:00
parent 5687d6773a
commit 48057646a8
11 changed files with 399 additions and 433 deletions

View File

@@ -1,30 +1,29 @@
use crate::{
model::model::*,
gtk::{
*,
prelude::*
},
};
use crate::{gtk::prelude::*, model::model::*};
impl<F, C> EventHandler<F, C>
where F: Fn(&C) + FnOnce(&C) + FnMut(&C){
pub fn new(component: C, callback: F) -> EventHandler<F, C>{
Self{
where
F: Fn(&C) + FnOnce(&C) + FnMut(&C),
{
pub fn new(component: C, callback: F) -> EventHandler<F, C> {
Self {
component,
callback,
}
}
}
pub trait BtnEventHandler{
pub trait BtnEventHandler {
fn on_click(self) -> ();
}
impl<F, C> BtnEventHandler for EventHandler<F, C>
where F: Fn(&C) + FnOnce(&C) + FnMut(&C) + 'static, C: ButtonExt + WidgetExt{
where
F: Fn(&C) + FnOnce(&C) + FnMut(&C) + 'static,
C: ButtonExt + WidgetExt,
{
fn on_click(self) -> () {
self.component.connect_clicked(move |button| {
(self.callback)(button)
});
self.component
.connect_clicked(move |button| (self.callback)(button));
}
}
}

View File

@@ -1,9 +1,6 @@
use std::{borrow::Borrow, collections::HashMap};
use std::collections::HashMap;
use crate::{
model::model::*,
controller::view_utils::input_utils::*,
};
use crate::{controller::view_utils::input_utils::*, model::model::*};
/// **Синдромы**
///
@@ -21,21 +18,17 @@ use crate::{
///
/// ошибочная позиция 7 false false false.
pub fn hamming(raw_input: String, mode: HammingMode) -> Result<String, String>{
pub fn hamming(raw_input: String, mode: HammingMode) -> Result<String, String> {
let length_of_code: usize = mode.clone() as usize;
let length_of_code : usize = mode.clone() as usize;
let prepared_input : String = processing_input(&raw_input);
let prepared_input: String = processing_input(&raw_input);
let (fc, sc) = check_correct_input(&raw_input, &prepared_input, length_of_code);
if !fc || !sc {
Err("Ошибка. Проверьте корректность ввода.".to_string())
} else {
let mut data : String = String::new();
let mut data: String = String::new();
let prepared_data: Vec<u8> = from_string_to_vec_bits(prepared_input);
@@ -45,103 +38,90 @@ pub fn hamming(raw_input: String, mode: HammingMode) -> Result<String, String>{
}
return Ok(data);
}
}
pub fn hamming_encrypt_data(
data: &Vec<u8>,
result_string: &mut String,
length_of_code: usize
) {
let mut i : usize = length_of_code;
while i <= data.len(){
pub fn hamming_encrypt_data(data: &Vec<u8>, result_string: &mut String, length_of_code: usize) {
let mut i: usize = length_of_code;
while i <= data.len() {
let data_bits = &data[i - length_of_code..i];
let (check_bit_1, check_bit_2, check_bit_3) = (
data_bits[0] ^ data_bits[1] ^ data_bits[3],
data_bits[0] ^ data_bits[2] ^ data_bits[3],
data_bits[1] ^ data_bits[2] ^ data_bits[3]
data_bits[1] ^ data_bits[2] ^ data_bits[3],
);
result_string.push_str(&*format!("{check_bit_1}{}{check_bit_2}{}{check_bit_3}{}{} ",
data_bits[0],
data_bits[1],
data_bits[2],
data_bits[3]));
result_string.push_str(&*format!(
"{check_bit_1}{}{check_bit_2}{}{check_bit_3}{}{} ",
data_bits[0], data_bits[1], data_bits[2], data_bits[3]
));
i += length_of_code;
}
}
pub fn hamming_decrypt_data(
data: &Vec<u8>,
result_string: &mut String,
length_of_code: usize
) {
pub fn hamming_decrypt_data(data: &Vec<u8>, result_string: &mut String, length_of_code: usize) {
let mut general_length: usize = length_of_code;
let mut i : usize = length_of_code;
let syndromes: HashMap<usize, (bool, bool, bool)> = HashMap::from([
(1, (false, true, true)),
(2, (false, false, true)),
(3, (true, false, true)),
(4, (false, true, false)),
(5, (true, true, false)),
(6, (true, false, false)),
(7, (false, false, false)),
]);
let syndromes : HashMap<usize, (bool, bool, bool)> = HashMap::from(
[
(1, (false, true, true)),
(2, (false, false, true)),
(3, (true, false, true)),
(4, (false, true, false)),
(5, (true, true, false)),
(6, (true, false, false)),
(7, (false, false, false)),
]
);
let mut errors: String = String::new();
let mut errors : String = String::new();
while general_length <= data.len() {
let data_bits = &data[general_length - length_of_code..general_length];
while i <= data.len(){
let mut data_bits = &data[i - length_of_code..i];
let checked_bits : (bool, bool, bool) =
(
(data_bits[1] ^ data_bits[3] ^ data_bits[6]) == data_bits[0],
(data_bits[1] ^ data_bits[5] ^ data_bits[6]) == data_bits[2],
(data_bits[3] ^ data_bits[5] ^ data_bits[6]) == data_bits[4]
);
let checked_bits: (bool, bool, bool) = (
(data_bits[1] ^ data_bits[3] ^ data_bits[6]) == data_bits[0],
(data_bits[1] ^ data_bits[5] ^ data_bits[6]) == data_bits[2],
(data_bits[3] ^ data_bits[5] ^ data_bits[6]) == data_bits[4],
);
match checked_bits {
(true, true, true) => {
i += length_of_code;
general_length += length_of_code;
continue;
},
}
_ => {
let error_position = syndromes
.iter()
.find(move |&(&error_position, &error)| error == checked_bits).
unwrap().0;
.find(move |&(&_error_position, &error)| error == checked_bits)
.unwrap()
.0;
let correctly_code : Vec<u8> = data_bits
let correctly_code: Vec<u8> = data_bits
.iter()
.enumerate()
.map(|(index, bit)| {
if index == error_position - 1 {
if *bit == 1u8 { 0u8 } else { 1u8 }
} else {
*bit
}
}).collect();
if index == error_position - 1 {
if *bit == 1u8 {
0u8
} else {
1u8
}
} else {
*bit
}
})
.collect();
let error = format!("Ошибка в коде {} {:?}, позиция ошибки {}, корректный код: {:?}; \n",
i / 7,
&data_bits,
error_position,
correctly_code
let error = format!(
"Ошибка в коде {} {:?}, позиция ошибки {}, корректный код: {:?}; \n",
general_length / 7,
&data_bits,
error_position,
correctly_code
);
errors.push_str(error.as_str());
i += length_of_code;
general_length += length_of_code;
}
}
}
@@ -151,5 +131,4 @@ pub fn hamming_decrypt_data(
} else {
result_string.push_str(errors.as_str())
}
}
}