reformat code and write benchmark

This commit is contained in:
2026-05-12 16:57:05 +03:00
parent ee817a8e8c
commit 6d656c35e1
9 changed files with 367 additions and 62 deletions
+53 -45
View File
@@ -15,7 +15,10 @@ use iced::widget::space::horizontal;
use iced::widget::*;
use iced::{Border, Color, Length, Shadow, Task};
use rand::random_range;
use std::collections::HashMap;
use rayon::iter::IndexedParallelIterator;
use rayon::iter::ParallelIterator;
use rayon::prelude::IntoParallelRefIterator;
use std::collections::{HashMap, HashSet};
use std::fs;
use std::ops::Add;
use std::path::PathBuf;
@@ -71,11 +74,14 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
let mut words = vec![];
let dict = &self.state.lock().unwrap().dictionary;
for i in 0..self.include_map.len() {
if self.include_map[i] {
words.push(dict[i].clone());
}
}
words = self
.include_map
.par_iter()
.zip(0..self.include_map.len())
.filter(|(flag, _)| **flag)
.map(|(_, index)| dict[index].clone())
.collect();
return Some(Page::DictionaryQuiz(DictionaryQuizState::new(
words,
self.reverse,
@@ -136,15 +142,14 @@ impl DictionaryState {
let dict = &mut self.state.lock().unwrap().dictionary;
dict[i].key = v;
}
return self.launch_auto_save_offset(i)
return self.launch_auto_save_offset(i);
}
DictionaryMessage::SetValue(i, v) => {
{
let dict = &mut self.state.lock().unwrap().dictionary;
dict[i].value = v
}
return self.launch_auto_save_offset(i)
return self.launch_auto_save_offset(i);
}
DictionaryMessage::SetTags(i, v) => {
{
@@ -153,7 +158,7 @@ impl DictionaryState {
}
self.update_tags();
return self.launch_auto_save_offset(i)
return self.launch_auto_save_offset(i);
}
DictionaryMessage::WordAction(i) => {
@@ -166,9 +171,9 @@ impl DictionaryState {
}
DictionaryMessage::Include(i, b) => self.include_map[i] = b,
DictionaryMessage::IncludeTag(t, v) => {
let index : u32;
let index: u32;
{
let mut state = self.state.lock().unwrap();
let state = self.state.lock().unwrap();
index = state.word_groups[self.selected_group_index].id;
}
self.tag_map.insert(t, v);
@@ -184,9 +189,7 @@ impl DictionaryState {
self.search = s;
}
DictionaryMessage::SetTyping(b) => self.no_typing = b,
DictionaryMessage::SubmitWord(i) => {
self.save_word(i)
}
DictionaryMessage::SubmitWord(i) => self.save_word(i),
Back => {}
Test => {}
DictionaryMessage::CreateGroup => {
@@ -218,13 +221,12 @@ impl DictionaryState {
}
DictionaryMessage::SelectGroup(i) => {
self.selected_group_index = i;
let index : u32;
let index: u32;
{
let state = self.state.lock().unwrap();
index = state.word_groups[i].id;
}
self.update_words_include(index)
}
DeleteGroup => {
if self.selected_group_index == 0 {
@@ -242,10 +244,12 @@ impl DictionaryState {
}
ChangeDirection => {
self.reverse_list = !self.reverse_list;
},
}
DictionaryMessage::TrySave(word_index) => {
let now = Utc::now();
if !self.auto_save_queue.contains_key(&word_index) { return Task::none(); }
if !self.auto_save_queue.contains_key(&word_index) {
return Task::none();
}
if self.auto_save_queue[&word_index] <= now {
self.auto_save_queue.remove(&word_index);
self.save_word(word_index);
@@ -266,10 +270,13 @@ impl DictionaryState {
}
fn launch_auto_save_offset(&mut self, index: usize) -> Task<RootMessage> {
let save_time = Utc::now().add(TimeDelta::milliseconds(2800));
let save_time = Utc::now().add(TimeDelta::milliseconds(900));
self.auto_save_queue.insert(index, save_time);
let message = RootMessage::Dictionary(DictionaryMessage::TrySave(index));
Task::perform(async { tokio::time::sleep(Duration::from_secs(3)).await }, |_| message)
Task::perform(
async { tokio::time::sleep(Duration::from_secs(1)).await },
|_| message,
)
}
pub fn view(&self) -> iced::Element<'_, DictionaryMessage> {
@@ -429,36 +436,40 @@ impl DictionaryState {
}
fn update_tags(&mut self) {
let mut tags_list: Vec<String> = vec![];
let mut tags_list: HashSet<String> = HashSet::new();
let dict = &self.state.lock().unwrap().dictionary;
for element in dict {
tags_list.append(&mut split_with_coma(element.tags.clone()));
}
dict.iter().for_each(|element| {
split_with_coma(element.tags.as_str())
.iter()
.for_each(|tag| {
tags_list.insert(tag.clone());
});
});
let current_tags = self
.tag_map
.keys()
.map(|k| k.clone().to_string())
.collect::<Vec<String>>();
for current in current_tags {
if !tags_list.contains(&current) {
self.tag_map.remove(&current.clone());
}
}
for found_tag in &tags_list {
if !self.tag_map.contains_key(&found_tag.clone()) {
self.tag_map.insert(found_tag.clone(), false);
}
}
current_tags
.iter()
.filter(|tag| !tags_list.contains(*tag))
.for_each(|tag| {
self.tag_map.remove(tag);
});
tags_list.iter().for_each(|tag| {
self.tag_map.insert(tag.clone(), false);
});
}
fn update_words_include(&mut self, group_id: u32) {
let include_tags = self
.tag_map
.iter()
.filter(|i| *(*i).1)
.filter(|(_, value)| **value)
.map(|(t, _)| t.clone())
.collect::<Vec<String>>();
@@ -469,14 +480,11 @@ impl DictionaryState {
let dict = &self.state.lock().unwrap().dictionary;
for i in 0..self.include_map.len() {
let tags = split_with_coma(dict[i].tags.clone());
if tags.iter().all(|t| include_tags.contains(t)) && dict[i].group_id == group_id {
self.include_map[i] = true;
} else {
self.include_map[i] = false;
}
}
self.include_map = dict.par_iter()
.map(|word| (split_with_coma(word.tags.as_str()), word.group_id))
.map(|(tags, word_group_id)| {
tags.iter().all(|t| include_tags.contains(t)) && word_group_id == group_id
}).collect();
}
fn groups_panel(&self) -> iced::Element<'_, DictionaryMessage> {
@@ -519,7 +527,7 @@ impl DictionaryState {
}
}
pub fn split_with_coma(ts: String) -> Vec<String> {
pub fn split_with_coma(ts: &str) -> Vec<String> {
ts.split(',')
.map(|ts| ts.to_lowercase().trim().to_string())
.filter(|t| !t.is_empty())
+1 -1
View File
@@ -144,7 +144,7 @@ impl DictionaryQuizState {
self.score.total += 1;
}
if self.answer == self.correct
|| split_with_coma(self.correct.clone()).contains(&self.answer)
|| split_with_coma(self.correct.as_str()).contains(&self.answer)
{
if self.is_help == false {
self.score.correct += 1;
+3 -2
View File
@@ -349,11 +349,12 @@ impl CardSet {
if !word_ids.contains(&stat.word_id) {
delete_stat(&stat, &state_locked.connection);
current_set.remove(index);
}else{
index += 1;
}
index += 1;
}
let weights = current_set.iter().map(|s| (100.0 / s.calculated_score()).powf(2.0)).collect::<Vec<f32>>();
let weights = current_set.iter().map(|s| (100.0 / s.calculated_score()).powf(2.0) * 2.0).collect::<Vec<f32>>();
let indexes = WeightedIndex::new(weights).unwrap();
Self {
+2 -12
View File
@@ -63,7 +63,7 @@ pub enum RootMessage {
Keyboard(Event),
}
enum Page {
pub enum Page {
Selector(SelectorState),
Quiz(QuizState),
Writing(WritingState),
@@ -207,14 +207,4 @@ trait NavigatedPage<T> {
pub trait KeyPressedPage {
fn press(&mut self, message: &Event) -> Task<RootMessage>;
}
/*pub fn danger_button(x: &Theme, status: Status) -> Style{
Style {
background: Some(Color(x.palette().danger)),
text_color: x.palette().text,
border: Border::default().rounded(2),
shadow: Default::default(),
snap: false,
}
}*/
}
+1 -1
View File
@@ -195,7 +195,7 @@ impl RepetitionsState {
.style(move |_x: &Theme, _status| Style {
background: None,
text_color: if self.correct_filters[i.clone()] {
_x.palette().text
_x.palette().primary
} else {
_x.palette().warning
},
+2 -1
View File
@@ -41,7 +41,8 @@ impl WordState {
WordMessage::Save => {
let mut state = self.state.lock().unwrap();
state.dictionary[self.index] = self.word.clone();
update_word(&mut self.word, &state.connection)
update_word(&mut self.word, &state.connection);
return Task::done(RootMessage::Word(WordMessage::Back))
}
WordMessage::Delete => {
let mut state = self.state.lock().unwrap();