liao_generator/
constants.rs

1use rand::{thread_rng, Rng};
2
3use crate::generator::generate;
4
5#[derive(Debug, Clone, Copy, PartialEq)]
6pub enum Operation {
7    Increment,
8    Decrement,
9}
10
11impl Operation {
12    pub fn opposite(&self) -> Operation {
13        match self {
14            Operation::Increment => Operation::Decrement,
15            Operation::Decrement => Operation::Increment,
16        }
17    }
18
19    pub fn get_random() -> Operation {
20        let mut rng = thread_rng();
21        if rng.gen_bool(0.5) {
22            Operation::Increment
23        } else {
24            Operation::Decrement
25        }
26    }
27}
28
29#[derive(Debug, Clone, Copy)]
30pub enum GenerateFormula {
31    NF,
32    LF,
33    BF,
34    FF,
35}
36
37#[derive(Debug)]
38pub struct GenerateOptions {
39    pub formula: GenerateFormula,
40    pub min: i64,
41    pub max: i64,
42    pub len: usize,
43}
44
45impl GenerateOptions {
46    // For tests
47    #[allow(dead_code)]
48    pub fn with_formula(&mut self, formula: GenerateFormula) {
49        self.formula = formula;
50    }
51
52    pub fn generate(&self) -> Vec<i64> {
53        generate(&self)
54    }
55}
56
57pub type Rule = Vec<u8>;
58
59#[derive(Debug, Clone)]
60pub struct GenerateRule {
61    pub zero: Rule,
62    pub one: Rule,
63    pub two: Rule,
64    pub three: Rule,
65    pub four: Rule,
66    pub five: Rule,
67    pub six: Rule,
68    pub seven: Rule,
69    pub eight: Rule,
70    pub nine: Rule,
71}
72
73#[derive(Debug)]
74pub struct GenerateRules {
75    pub increment: GenerateRule,
76    pub decrement: GenerateRule,
77}
78
79// impl GenerateRules {
80//     pub fn get_random_rule(&self) -> GenerateRule {
81//         if thread_rng().gen_bool(0.5) {
82//             self.increment.clone()
83//         } else {
84//             self.decrement.clone()
85//         }
86//     }
87// }
88
89pub struct TermOptions {
90    pub cur_num: Vec<i64>,
91    pub formula: GenerateFormula,
92    pub terms_len: i64,
93    pub schema: TermSchema,
94    pub max: i64,
95    pub min: i64,
96    pub previos_term: Option<i64>,
97}
98
99#[derive(Debug, Clone)]
100pub struct FactorOptions {
101    pub number: u8,
102    pub formula: GenerateFormula,
103    pub schema: TermSchema,
104    pub forbidden_number: Option<u8>,
105}
106
107pub struct SchemaOptions {
108    pub terms_len: usize,
109}
110
111#[derive(Debug, Clone, Copy)]
112pub struct TermSchema {
113    /**
114     * If true, the factor can be zero.
115     */
116    pub can_generate_zero: bool,
117    /**
118     * If true, the factor will be generated with
119     * first rule array items (before "0" item).
120     */
121    pub force_formula: bool,
122    /**
123     * Operation for the factor.
124     */
125    pub operation: Option<Operation>,
126}