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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208

extern crate regex;
use std::collections::HashMap;
use std::io::BufRead;
use std::io::Lines;

pub fn version() -> String{
    return "0.4.0".to_string();
}

// OK 0.4
pub fn get_reserved_matchers() -> HashMap<String,regex::Regex>
{
    let mut retvals:HashMap<String,regex::Regex> = HashMap::new();
    retvals.insert(
        "Date".to_string(),
        regex::Regex::new(r"\d{4}-\d{2}-\d{2}").unwrap());
    retvals
}

//TODO 0.4: [x] return vec of tuples
pub fn read_kvc_line_default( input_line: &String ) -> 
(
    Vec<(String,f32)>,
    Vec<(String,String)>
)
{
    read_kvc_line( input_line, &get_reserved_matchers())
}

//TODO 0.4: [x] return vec of tuples
pub fn read_kvc_line( input_line: &String, keywords: &HashMap<String,regex::Regex> ) -> 
(
    Vec<(String,f32)>,
    Vec<(String,String)>
)
{
    let mut line_strings: HashMap<String,String> = HashMap::new();
    let mut line_counter: HashMap<String,f32> = HashMap::new();
    if input_line.len()==0 {
        return (
            line_counter.into_iter().map(|(key,val)| (key,val)).collect(),
            line_strings.into_iter().map(|(key,val)| (key,val)).collect(),
        );
    }
    let mut tok_iter = input_line.split_whitespace();
    'nexttok: while let Some(kvpair) = tok_iter.next(){

        //sure hope I understand what that split_whitespace() was up to.
        assert!(kvpair.len() > 0);
        if kvpair.chars().next().unwrap()=='#'{
            break;
        }
        let mut kvitr = kvpair.split(":");
        if let Some(key)=kvitr.next(){
            //got a key, that's good.
            //if it's a date-matching key, we can specially process that one
            for (name,matcher) in keywords{
                if matcher.is_match(key)
                {
                    line_strings.insert(name.clone(),key.to_string().clone());
                    continue 'nexttok;
                }
            }

            //It's not one of the speically formatted keys, so let's just parse as accumulator keys
            //These are of the form K K K K K , which should compress to K:5
            //or K:4 K, which should compress also to K:5
            //e.g., of the form K:I, and if no :I, then let's assume :1.
            //get val -- thestuff after ':'
            let val=match kvitr.next(){
                None=>1.0,
                Some(s)=>{
                    if let Ok(f_val) = s.parse::<f32>(){
                        f_val
                    } else {
                        eprintln!("Got a non-accumulator (int/float) here: {}:{}",key,s);
                        continue 'nexttok;
                    }
                },
            };
            let countref = line_counter.entry(key.to_string()).or_insert(0.0);
            *countref =  *countref + val;
        } else {
            panic!("Bug! Cannot process: '{}' from '{}'",kvpair,input_line);
        }
    }
    return (
        line_counter.into_iter().map(|(key,val)| (key,val)).collect(),
        line_strings.into_iter().map(|(key,val)| (key,val)).collect(),
    );
}

pub fn load_table_from_kvc_stream<B:BufRead> (
    lines_input:Lines<B>, 
    keywords :&HashMap<String,regex::Regex> )->
(
    (usize,usize),  //size
    Vec<((usize,usize),f32)> , // data_entries
    Vec<String>  // col_names
)
{
    let mut rows = 0;
    let mut col_to_name: HashMap<usize,String> = HashMap::new();
    let mut name_to_col: HashMap<String,usize> = HashMap::new();
    let mut data_entries: HashMap<(usize,usize),f32> = HashMap::new();

    for line_res in lines_input{
        let line = match line_res{
            Ok(l)=>l,
            Err(_)=>"".to_string(),
        };
        let (key_counts,_)=read_kvc_line(&line,&keywords);
        if key_counts.len()> 0
        {
            rows+=1;
            for (key,count) in key_counts{
                let colsize = name_to_col.len();
                let colidx = name_to_col.entry(key.to_string()).or_insert(colsize);
                col_to_name.insert(*colidx,key.to_string());
                let cur_count_ref = data_entries.entry( (rows,*colidx)).or_insert(0.0);
                *cur_count_ref = *cur_count_ref + count;
            }
        }
    }

    //trial by fire: Assume the hash map is correctly set up 0..col_to_name.len() 
    let cols = col_to_name.len();
    let mut col_names:Vec<String> = vec!["".to_string(); cols];
    for (idx,name) in col_to_name{
        assert!(col_names[idx].len()==0,"Found non-zero column name! Error in read_kvc_line?");
        col_names[idx]+=&name.to_string();
    }
    for idx in 0..cols{
        assert!(col_names[idx].len()!=0,"Found zero-length column name! Error in read_kvc_line?")
    }

    return ( 
        (rows,cols),
        data_entries.into_iter().map(|x| x).collect(),
        col_names 
    )
}

pub fn load_table_from_kvc_stream_default<B:BufRead> (lines_input:Lines<B>)->
(
    (usize,usize),
    Vec<((usize,usize),f32)> , // data_entries
    Vec<String> // col_names
)
{
    return load_table_from_kvc_stream(lines_input, &get_reserved_matchers());
}

#[cfg(test)]
mod tests{
use super::*;
use std::io::Cursor;
    #[test]
    fn test_keywords_are_returned(){
        assert_eq!(get_reserved_matchers().len(),1);
    }

    #[test]
    fn test_line_gets_date(){
        let (counts,strs) =read_kvc_line_default(&"    2021-01-01 ".to_string());
        assert_eq!(strs.len(),1);
        assert_eq!(counts.len(),0);
    }

    #[test]
    fn test_line_counts_tokens(){
        let (counts,strs) =read_kvc_line_default(&" A A A B  B C Z:4 Y:2 Y:3 ".to_string());
        assert_eq!(strs.len(),0);
        assert_eq!(counts.len(),5);
        for (key,val) in counts{
            match &key[..]{
                "A"=>assert_eq!(val,3.0),
                "B"=>assert_eq!(val,2.0),
                "C"=>assert_eq!(val,1.0),
                "Y"=>assert_eq!(val,5.0),
                "Z"=>assert_eq!(val,4.0),
                _=>panic!("Found unexpected token:{}",key)
            }
        }
    }
    #[test]
    fn test_line_ignores_comments(){
        let (counts,strs) =read_kvc_line_default(&" A # A A B  B C Z:4 Y:2 Y:3 ".to_string());
        assert_eq!(strs.len(),0);
        assert_eq!(counts.len(),1);
        for (key,val) in counts{
            match &key[..]{
                "A"=>assert_eq!(val,1.0),
                _=>panic!("Found unexpected token:{}",key)
            }
        }
    }
    #[test]
    fn test_table_size(){
        let data =Cursor::new( "A # NO\n A A # \n A A A\n\n" );
        let ( (r,c) ,_entries,names)=load_table_from_kvc_stream_default(data.lines());
        assert_eq!(r,3);
        assert_eq!(c,1);
        assert_eq!(names[0],"A");
        assert_eq!(names.len(),c);
    }
}