File size: 3,844 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use loda_rust_core::oeis::OeisIdHashSet;
use super::NameRow;
use crate::common::SimpleLog;
use std::io;
use std::io::BufRead;

pub struct ProcessNamesFile {
    count_bytes: usize,
    count_lines: usize,
    count_junk: usize,
    count_callback: usize,
    count_ignored_program_id: usize,
}

impl ProcessNamesFile {
    pub fn new() -> Self {
        Self {
            count_bytes: 0,
            count_lines: 0,
            count_junk: 0,
            count_callback: 0,
            count_ignored_program_id: 0,
        }        
    }

    #[allow(dead_code)]
    pub fn print_summary(&self, simple_log: SimpleLog) {
        simple_log.println(format!("number of rows in oeis 'names' file: {}", self.count_lines));
        simple_log.println(format!("count_callback: {}", self.count_callback));
        simple_log.println(format!("count_ignored_program_id: {}", self.count_ignored_program_id));
        simple_log.println(format!("count_junk: {}", self.count_junk));
    }

    /// Traverse all the rows of the OEIS `names` file.
    pub fn execute<F>(
        &mut self,
        reader: &mut dyn io::BufRead,
        oeis_ids_to_ignore: &OeisIdHashSet,
        mut callback: F
    )
        where F: FnMut(&NameRow, usize)
    {
        for line in reader.lines() {
            let line: String = line.unwrap();
            self.count_bytes += line.len();
            self.count_lines += 1;
            let row: NameRow = match NameRow::parse(&line) {
                Some(value) => value,
                None => {
                    self.count_junk += 1;
                    continue;
                }
            };
            if oeis_ids_to_ignore.contains(&row.oeis_id()) {
                self.count_ignored_program_id += 1;
                continue;
            }
            callback(&row, self.count_bytes);
            self.count_callback += 1;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashSet;
    use loda_rust_core::oeis::OeisId;

    const INPUT_MOCKDATA: &str = r#"
# OEIS Sequence Names (http://oeis.org/names.gz)
# Last Modified: Sol 15 00:02 IFC 1984
# Use of this content is governed by the
# OEIS End-User License: http://oeis.org/LICENSE
A000001 Number of groups of order n.
A000002 Kolakoski sequence: a(n) is length of n-th run; a(1) = 1; sequence consists just of 1's and 2's.
A000003 Number of classes of primitive positive definite binary quadratic forms of discriminant D = -4n; or equivalently the class number of the quadratic order of discriminant D = -4n.
A000004 The zero sequence.
A000005 d(n) (also called tau(n) or sigma_0(n)), the number of divisors of n.
A000006 Integer part of square root of n-th prime.
A000007 The characteristic function of {0}: a(n) = 0^n.
A000008 Number of ways of making change for n cents using coins of 1, 2, 5, 10 cents.
A000040 The prime numbers.
A000045 Fibonacci numbers: F(n) = F(n-1) + F(n-2) with F(0) = 0 and F(1) = 1.
"#;

    #[test]
    fn test_10000_execute() {
        // Arrange
        let mut input: &[u8] = INPUT_MOCKDATA.as_bytes();

        let mut callback_items = Vec::<String>::new();
        let callback = |row: &NameRow, _| {
            callback_items.push(format!("{}", row.oeis_id().raw()));
        };
        let mut oeis_ids_to_ignore = HashSet::<OeisId>::new();
        oeis_ids_to_ignore.insert(OeisId::from(5));
        oeis_ids_to_ignore.insert(OeisId::from(40));

        let mut processor = ProcessNamesFile::new();

        // Act
        processor.execute(
            &mut input,
            &oeis_ids_to_ignore,
            callback
        );

        // Assert
        let callback_dump = callback_items.join(",");
        assert_eq!(callback_dump, "1,2,3,4,6,7,8,45");
        assert_eq!(processor.count_ignored_program_id, 2);
        assert_eq!(processor.count_junk, 5);
    }
}