Some random module reformat

This commit is contained in:
2026-05-12 17:12:03 +03:00
parent 6d656c35e1
commit 3c27238b72
2 changed files with 41 additions and 33 deletions
+7 -5
View File
@@ -16,14 +16,12 @@ use iced::widget::*;
use iced::{Border, Color, Length, Shadow, Task}; use iced::{Border, Color, Length, Shadow, Task};
use rand::random_range; use rand::random_range;
use rayon::iter::IndexedParallelIterator; use rayon::iter::IndexedParallelIterator;
use rayon::iter::ParallelIterator;
use rayon::prelude::IntoParallelRefIterator;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::fs; use std::fs;
use std::ops::Add; use std::ops::Add;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::time::Duration; use std::time::{Duration, Instant};
use DictionaryMessage::Back; use DictionaryMessage::Back;
#[derive(Clone)] #[derive(Clone)]
@@ -76,7 +74,7 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
words = self words = self
.include_map .include_map
.par_iter() .iter()
.zip(0..self.include_map.len()) .zip(0..self.include_map.len())
.filter(|(flag, _)| **flag) .filter(|(flag, _)| **flag)
.map(|(_, index)| dict[index].clone()) .map(|(_, index)| dict[index].clone())
@@ -479,12 +477,16 @@ impl DictionaryState {
} }
let dict = &self.state.lock().unwrap().dictionary; let dict = &self.state.lock().unwrap().dictionary;
let time = Instant::now();
self.include_map = dict.par_iter() self.include_map = dict.iter()
.map(|word| (split_with_coma(word.tags.as_str()), word.group_id)) .map(|word| (split_with_coma(word.tags.as_str()), word.group_id))
.map(|(tags, word_group_id)| { .map(|(tags, word_group_id)| {
tags.iter().all(|t| include_tags.contains(t)) && word_group_id == group_id tags.iter().all(|t| include_tags.contains(t)) && word_group_id == group_id
}).collect(); }).collect();
println!("Time {}", time.elapsed().as_micros());
} }
fn groups_panel(&self) -> iced::Element<'_, DictionaryMessage> { fn groups_panel(&self) -> iced::Element<'_, DictionaryMessage> {
+33 -27
View File
@@ -1,5 +1,6 @@
use std::cmp::min; use crate::data_provider::card_stats::{
use crate::data_provider::card_stats::{add_stat, delete_stat, load_stats_of_set, update_stat_score}; add_stat, delete_stat, load_stats_of_set, update_stat_score,
};
use crate::repetitions::CardSetSettings; use crate::repetitions::CardSetSettings;
use crate::AppState; use crate::AppState;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
@@ -8,6 +9,7 @@ use rand::distr::Distribution;
use rand::rng; use rand::rng;
use rand::rngs::ThreadRng; use rand::rngs::ThreadRng;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::cmp::min;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
@@ -235,7 +237,7 @@ pub struct WordData {
pub value: String, pub value: String,
pub tags: String, pub tags: String,
pub additional: HashMap<String, String>, pub additional: HashMap<String, String>,
pub group_id: u32 pub group_id: u32,
} }
impl WordData { impl WordData {
@@ -246,13 +248,13 @@ impl WordData {
value: String::new(), value: String::new(),
tags: String::new(), tags: String::new(),
additional: Default::default(), additional: Default::default(),
group_id: 1 group_id: 1,
} }
} }
} }
#[derive(Clone)] #[derive(Clone)]
pub struct WordGroup{ pub struct WordGroup {
pub id: u32, pub id: u32,
pub name: String, pub name: String,
} }
@@ -272,9 +274,7 @@ impl CardStatistics {
WordOpenMode::Easy => { WordOpenMode::Easy => {
self.score = (self.calculated_score() + 5.0).round() as i32; self.score = (self.calculated_score() + 5.0).round() as i32;
} }
WordOpenMode::Ok => { WordOpenMode::Ok => self.score = (self.calculated_score() + 2.0).round() as i32,
self.score = (self.calculated_score() + 2.0).round() as i32
}
WordOpenMode::Hard => { WordOpenMode::Hard => {
self.score = (self.calculated_score() - 1.0).round() as i32; self.score = (self.calculated_score() - 1.0).round() as i32;
} }
@@ -289,7 +289,6 @@ impl CardStatistics {
self.score = MAX_SCORE self.score = MAX_SCORE
} }
self.last_open = Utc::now(); self.last_open = Utc::now();
} }
pub fn calculated_score(&self) -> f32 { pub fn calculated_score(&self) -> f32 {
@@ -316,7 +315,7 @@ pub struct CardSet {
current_word_index: Option<usize>, current_word_index: Option<usize>,
generator: ThreadRng, generator: ThreadRng,
state: Arc<Mutex<AppState>>, state: Arc<Mutex<AppState>>,
history: Vec<usize> history: Vec<usize>,
} }
impl CardSet { impl CardSet {
@@ -328,33 +327,38 @@ impl CardSet {
let last_list = settings.get_word_list(&state_locked); let last_list = settings.get_word_list(&state_locked);
let saved_ids = current_set.iter().map(|l| l.word_id).collect::<Vec<u32>>(); let saved_ids = current_set.iter().map(|l| l.word_id).collect::<Vec<u32>>();
let word_ids = last_list.iter().map(|l| l.id).collect::<Vec<u32>>(); let word_ids = last_list.iter().map(|l| l.id).collect::<Vec<u32>>();
for word in &last_list {
if !saved_ids.contains(&word.id) { last_list
let mut new_statistic = CardStatistics { .iter()
.filter(|word| !saved_ids.contains(&word.id))
.map(|word| {
CardStatistics {
id: 0, id: 0,
word_id: word.id.clone(), word_id: word.id.clone(),
last_open: Utc::now(), last_open: Utc::now(),
score: 1, score: 1,
set_id: settings.id.clone(), set_id: settings.id.clone(),
}; }
})
.for_each(|mut new_statistic| {
add_stat(&mut new_statistic, &state_locked.connection); add_stat(&mut new_statistic, &state_locked.connection);
current_set.push(new_statistic); current_set.push(new_statistic);
} });
}
let mut index = 0; let mut index = 0;
for stat in current_set.clone() { for stat in current_set.clone() {
if !word_ids.contains(&stat.word_id) { if !word_ids.contains(&stat.word_id) {
delete_stat(&stat, &state_locked.connection); delete_stat(&stat, &state_locked.connection);
current_set.remove(index); current_set.remove(index);
}else{ } else {
index += 1; index += 1;
} }
} }
let weights = current_set.iter().map(|s| (100.0 / s.calculated_score()).powf(2.0) * 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(); let indexes = WeightedIndex::new(weights).unwrap();
Self { Self {
@@ -368,12 +372,11 @@ impl CardSet {
} }
} }
pub fn next(&mut self) -> (WordData, CardStatistics) { pub fn next(&mut self) -> (WordData, CardStatistics) {
let index = self.last_weights.sample(&mut self.generator); let index = self.last_weights.sample(&mut self.generator);
if self.history.contains(&index) { if self.history.contains(&index) {
return self.next() return self.next();
} }
if self.history.len() == self.history_len() { if self.history.len() == self.history_len() {
@@ -392,14 +395,17 @@ impl CardSet {
let word = &mut self.set[self.current_word_index.unwrap()]; let word = &mut self.set[self.current_word_index.unwrap()];
word.update(status); word.update(status);
let new_weight = (100.0 / word.calculated_score()).powf(2.0); let new_weight = (100.0 / word.calculated_score()).powf(2.0);
self.last_weights.update_weights(&[(self.current_word_index.unwrap(), &new_weight)]).unwrap(); self.last_weights
{ .update_weights(&[(self.current_word_index.unwrap(), &new_weight)])
update_stat_score(word, &self.state.lock().unwrap().connection) .unwrap();
} { update_stat_score(word, &self.state.lock().unwrap().connection) }
} }
fn history_len(&self) -> usize { fn history_len(&self) -> usize {
min(MAX_HISTORY_LEN, (self.set.len() as f32 * MAX_HISTORY_LEN_PART) as usize) min(
MAX_HISTORY_LEN,
(self.set.len() as f32 * MAX_HISTORY_LEN_PART) as usize,
)
} }
pub fn len(&self) -> usize { pub fn len(&self) -> usize {