File size: 3,748 Bytes
d5bfab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use crate::common::SimpleLog;
use crate::config::Config;
use std::error::Error;
use std::path::PathBuf;
use std::collections::HashSet;
use super::{AnalyticsDirectory, load_program_ids_from_deny_file};
use crate::common::{load_program_ids_csv_file, save_program_ids_csv_file};

/// Generate the `dontmine.csv` file.
/// 
/// These are the program ids that should NOT be mined.
pub struct DontMine {
    analytics_directory: AnalyticsDirectory,
    simple_log: SimpleLog,
    config: Config,
    program_ids: HashSet<u32>
}

impl DontMine {
    pub fn run(analytics_directory: AnalyticsDirectory, simple_log: SimpleLog) -> Result<(), Box<dyn Error>> {
        simple_log.println("\nDontMine");
        let mut instance = Self {
            analytics_directory,
            simple_log,
            config: Config::load(),
            program_ids: HashSet::new()
        };
        instance.extend_program_ids_with_loda_programs_deny_txt()?;
        instance.extend_program_ids_with_dont_optimize_csv()?;
        instance.remove_invalid_programs()?;
        instance.save()?;
        Ok(())
    }

    /// Ignore simple programs, that doesn't have complex loops nor dependencies to other sequences.
    /// It doesn't make sense trying to optimize a program that is already well-optimized.
    fn extend_program_ids_with_dont_optimize_csv(&mut self) -> Result<(), Box<dyn Error>> {
        let path: PathBuf = self.analytics_directory.complexity_dont_optimize_file();
        let program_ids: Vec<u32> = load_program_ids_csv_file(&path)?;
        let content = format!("number of program ids in the 'dont_optimize.csv' file: {:?}", program_ids.len());
        self.simple_log.println(content);
        self.program_ids.extend(program_ids);
        Ok(())
    }

    /// If a program is on the loda-programs `deny.txt` list, then ignore it.
    /// This is for duplicates in oeis and things to be ignored.
    fn extend_program_ids_with_loda_programs_deny_txt(&mut self) -> Result<(), Box<dyn Error>> {
        let path = self.config.loda_programs_oeis_deny_file();
        let program_ids: Vec<u32> = load_program_ids_from_deny_file(&path)?;
        let content = format!("number of program ids in the 'deny.txt' file: {:?}", program_ids.len());
        self.simple_log.println(content);
        self.program_ids.extend(program_ids);
        Ok(())
    }

    /// If the `loda-programs` repo contains a program that is invalid,
    /// then we want to mine for the program anyways.
    fn remove_invalid_programs(&mut self) -> Result<(), Box<dyn Error>> {
        let path: PathBuf = self.analytics_directory.programs_invalid_file();
        let program_ids: Vec<u32> = load_program_ids_csv_file(&path)?;
        let mut remove_count = 0;
        for program_id in program_ids {
            if self.program_ids.remove(&program_id) {
                remove_count += 1;
            }
        }
        let content = format!("number of program ids removed because they are in the 'programs_invalid.csv' file: {}", remove_count);
        self.simple_log.println(content);
        Ok(())
    }

    fn sorted_vec(program_ids: &HashSet<u32>) -> Vec<u32> {
        let mut program_ids_sorted: Vec<u32> = program_ids.clone().into_iter().collect();
        program_ids_sorted.sort();
        program_ids_sorted
    }

    fn save(&self) -> Result<(), Box<dyn Error>> {
        let program_ids_sorted: Vec<u32> = Self::sorted_vec(&self.program_ids);
        let content = format!("number of program ids in the 'dontmine.csv' file: {:?}", program_ids_sorted.len());
        self.simple_log.println(content);
        let output_path: PathBuf = self.analytics_directory.dont_mine_file();
        save_program_ids_csv_file(&program_ids_sorted, &output_path)
    }
}