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 rand::random_range;
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;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use std::time::{Duration, Instant};
use DictionaryMessage::Back;
#[derive(Clone)]
@@ -76,7 +74,7 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
words = self
.include_map
.par_iter()
.iter()
.zip(0..self.include_map.len())
.filter(|(flag, _)| **flag)
.map(|(_, index)| dict[index].clone())
@@ -479,12 +477,16 @@ impl DictionaryState {
}
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(|(tags, word_group_id)| {
tags.iter().all(|t| include_tags.contains(t)) && word_group_id == group_id
}).collect();
println!("Time {}", time.elapsed().as_micros());
}
fn groups_panel(&self) -> iced::Element<'_, DictionaryMessage> {
+31 -25
View File
@@ -1,5 +1,6 @@
use std::cmp::min;
use crate::data_provider::card_stats::{add_stat, delete_stat, load_stats_of_set, update_stat_score};
use crate::data_provider::card_stats::{
add_stat, delete_stat, load_stats_of_set, update_stat_score,
};
use crate::repetitions::CardSetSettings;
use crate::AppState;
use chrono::{DateTime, Utc};
@@ -8,6 +9,7 @@ use rand::distr::Distribution;
use rand::rng;
use rand::rngs::ThreadRng;
use serde::{Deserialize, Serialize};
use std::cmp::min;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
@@ -235,7 +237,7 @@ pub struct WordData {
pub value: String,
pub tags: String,
pub additional: HashMap<String, String>,
pub group_id: u32
pub group_id: u32,
}
impl WordData {
@@ -246,7 +248,7 @@ impl WordData {
value: String::new(),
tags: String::new(),
additional: Default::default(),
group_id: 1
group_id: 1,
}
}
}
@@ -272,9 +274,7 @@ impl CardStatistics {
WordOpenMode::Easy => {
self.score = (self.calculated_score() + 5.0).round() as i32;
}
WordOpenMode::Ok => {
self.score = (self.calculated_score() + 2.0).round() as i32
}
WordOpenMode::Ok => self.score = (self.calculated_score() + 2.0).round() as i32,
WordOpenMode::Hard => {
self.score = (self.calculated_score() - 1.0).round() as i32;
}
@@ -289,7 +289,6 @@ impl CardStatistics {
self.score = MAX_SCORE
}
self.last_open = Utc::now();
}
pub fn calculated_score(&self) -> f32 {
@@ -316,7 +315,7 @@ pub struct CardSet {
current_word_index: Option<usize>,
generator: ThreadRng,
state: Arc<Mutex<AppState>>,
history: Vec<usize>
history: Vec<usize>,
}
impl CardSet {
@@ -328,21 +327,23 @@ impl CardSet {
let last_list = settings.get_word_list(&state_locked);
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>>();
for word in &last_list {
if !saved_ids.contains(&word.id) {
let mut new_statistic = CardStatistics {
last_list
.iter()
.filter(|word| !saved_ids.contains(&word.id))
.map(|word| {
CardStatistics {
id: 0,
word_id: word.id.clone(),
last_open: Utc::now(),
score: 1,
set_id: settings.id.clone(),
};
}
})
.for_each(|mut new_statistic| {
add_stat(&mut new_statistic, &state_locked.connection);
current_set.push(new_statistic);
}
}
});
let mut index = 0;
for stat in current_set.clone() {
@@ -354,7 +355,10 @@ impl CardSet {
}
}
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();
Self {
@@ -368,12 +372,11 @@ impl CardSet {
}
}
pub fn next(&mut self) -> (WordData, CardStatistics) {
let index = self.last_weights.sample(&mut self.generator);
if self.history.contains(&index) {
return self.next()
return self.next();
}
if self.history.len() == self.history_len() {
@@ -392,14 +395,17 @@ impl CardSet {
let word = &mut self.set[self.current_word_index.unwrap()];
word.update(status);
let new_weight = (100.0 / word.calculated_score()).powf(2.0);
self.last_weights.update_weights(&[(self.current_word_index.unwrap(), &new_weight)]).unwrap();
{
update_stat_score(word, &self.state.lock().unwrap().connection)
}
self.last_weights
.update_weights(&[(self.current_word_index.unwrap(), &new_weight)])
.unwrap();
{ update_stat_score(word, &self.state.lock().unwrap().connection) }
}
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 {