File size: 2,739 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
use std::collections::HashSet;
use loda_rust_core::util::{BigIntVec, BigIntVecToString};

/// The `mine-event` dir can quickly get filled up with candidate programs
/// that output the same terms. I have experienced that 80k files was
/// created in a few seconds with nearly identical programs.
///
/// This is a mechanism to prevent the `mine-event` dir from getting filled up.
/// The way it works. If there already is a program with the same terms in the `mine-event` dir,
/// then prevent the new program from being written to the dir.
///
/// On load, the `mine-event` dir is scanned for what programs are there.
pub struct PreventFlooding {
    hashset: HashSet<String>,
}

pub enum PreventFloodingError {
    AlreadyRegistered
}

impl PreventFlooding {
    pub fn new() -> Self {
        Self {
            hashset: HashSet::<String>::new(),
        }
    }

    #[allow(dead_code)]
    pub fn contains(&self, bigintvec: &BigIntVec) -> bool {
        let s: String = bigintvec.to_compact_comma_string();
        if self.hashset.contains(&s) {
            return true;
        }
        false
    }

    pub fn try_register(&mut self, bigintvec: &BigIntVec) -> Result<(), PreventFloodingError> {
        let s: String = bigintvec.to_compact_comma_string();
        if self.hashset.contains(&s) {
            // The `mine-event` dir already contains a program with these terms.
            return Err(PreventFloodingError::AlreadyRegistered);
        }
        self.hashset.insert(s);

        // Successfully registered the new program.
        Ok(())
    }

    pub fn len(&self) -> usize {
        self.hashset.len()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use loda_rust_core::util::BigIntVecFromI64;
    
    #[test]
    fn test_10000_try_register() {
        let mut pf = PreventFlooding::new();
        let bigintvec = BigIntVec::from_i64array(&[1, 2, 3, 4, 5]);
        assert_eq!(pf.try_register(&bigintvec).is_ok(), true);
        assert_eq!(pf.try_register(&bigintvec).is_ok(), false);
    }

    #[test]
    fn test_10001_contains() {
        let mut pf = PreventFlooding::new();
        assert_eq!(pf.contains(&BigIntVec::from_i64array(&[1, 1, 1, 1, 1])), false);
        assert_eq!(pf.contains(&BigIntVec::from_i64array(&[1, 2, 3, 4, 5])), false);
        assert_eq!(pf.try_register(&BigIntVec::from_i64array(&[1, 2, 3, 4, 5])).is_ok(), true);
        assert_eq!(pf.try_register(&BigIntVec::from_i64array(&[1, 1, 1, 1, 1])).is_ok(), true);
        assert_eq!(pf.contains(&BigIntVec::from_i64array(&[1, 1, 1, 1, 1])), true);
        assert_eq!(pf.contains(&BigIntVec::from_i64array(&[1, 2, 3, 4, 5])), true);
        assert_eq!(pf.contains(&BigIntVec::from_i64array(&[1984, 1984, 1984])), false);
    }
}