implement compare_vector util
[Mitarbeiter/Tim-Zeitz/stud-rust-base.git] / src / bin / compare_vector.rs
1 extern crate stud_rust_base;
2 use stud_rust_base::io::*;
3 use std::env;
4
5 fn main() {
6     let mut args = env::args();
7     args.next();
8
9     match &args.collect::<Vec<String>>()[..] {
10         [data_type, input1, input2] => {
11             match data_type.as_ref() {
12                 "i8" => compare_values(&Vec::<i8>::load_from(input1).expect("Failed to read from input1"), &Vec::<i8>::load_from(input2).expect("Failed to read from input2")),
13                 "u8" => compare_values(&Vec::<u8>::load_from(input1).expect("Failed to read from input1"), &Vec::<u8>::load_from(input2).expect("Failed to read from input2")),
14                 "i16" => compare_values(&Vec::<i16>::load_from(input1).expect("Failed to read from input1"), &Vec::<i16>::load_from(input2).expect("Failed to read from input2")),
15                 "u16" => compare_values(&Vec::<u16>::load_from(input1).expect("Failed to read from input1"), &Vec::<u16>::load_from(input2).expect("Failed to read from input2")),
16                 "i32" => compare_values(&Vec::<i32>::load_from(input1).expect("Failed to read from input1"), &Vec::<i32>::load_from(input2).expect("Failed to read from input2")),
17                 "u32" => compare_values(&Vec::<u32>::load_from(input1).expect("Failed to read from input1"), &Vec::<u32>::load_from(input2).expect("Failed to read from input2")),
18                 "i64" => compare_values(&Vec::<i64>::load_from(input1).expect("Failed to read from input1"), &Vec::<i64>::load_from(input2).expect("Failed to read from input2")),
19                 "u64" => compare_values(&Vec::<u64>::load_from(input1).expect("Failed to read from input1"), &Vec::<u64>::load_from(input2).expect("Failed to read from input2")),
20                 "f32" => compare_values(&Vec::<f32>::load_from(input1).expect("Failed to read from input1"), &Vec::<f32>::load_from(input2).expect("Failed to read from input2")),
21                 "f64" => compare_values(&Vec::<f64>::load_from(input1).expect("Failed to read from input1"), &Vec::<f64>::load_from(input2).expect("Failed to read from input2")),
22                 "int8" => compare_values(&Vec::<i8>::load_from(input1).expect("Failed to read from input1"), &Vec::<i8>::load_from(input2).expect("Failed to read from input2")),
23                 "uint8" => compare_values(&Vec::<u8>::load_from(input1).expect("Failed to read from input1"), &Vec::<u8>::load_from(input2).expect("Failed to read from input2")),
24                 "int16" => compare_values(&Vec::<i16>::load_from(input1).expect("Failed to read from input1"), &Vec::<i16>::load_from(input2).expect("Failed to read from input2")),
25                 "uint16" => compare_values(&Vec::<u16>::load_from(input1).expect("Failed to read from input1"), &Vec::<u16>::load_from(input2).expect("Failed to read from input2")),
26                 "int32" => compare_values(&Vec::<i32>::load_from(input1).expect("Failed to read from input1"), &Vec::<i32>::load_from(input2).expect("Failed to read from input2")),
27                 "uint32" => compare_values(&Vec::<u32>::load_from(input1).expect("Failed to read from input1"), &Vec::<u32>::load_from(input2).expect("Failed to read from input2")),
28                 "int64" => compare_values(&Vec::<i64>::load_from(input1).expect("Failed to read from input1"), &Vec::<i64>::load_from(input2).expect("Failed to read from input2")),
29                 "uint64" => compare_values(&Vec::<u64>::load_from(input1).expect("Failed to read from input1"), &Vec::<u64>::load_from(input2).expect("Failed to read from input2")),
30                 "float32" => compare_values(&Vec::<f32>::load_from(input1).expect("Failed to read from input1"), &Vec::<f32>::load_from(input2).expect("Failed to read from input2")),
31                 "float64" => compare_values(&Vec::<f64>::load_from(input1).expect("Failed to read from input1"), &Vec::<f64>::load_from(input2).expect("Failed to read from input2")),
32                 _ => {
33                     print_usage();
34                     panic!("Unknown data_type {}", data_type);
35                 }
36             };
37         },
38         _ => {
39             print_usage();
40             panic!("Invalid input")
41         },
42     }
43 }
44
45 fn print_usage() {
46     eprintln!("Usage: decode_vector data_type vector1_file vector2_file
47
48 Compares two vectors of elements in binary format. data_type can be one of
49 * i8
50 * u8
51 * i16
52 * u16
53 * i32
54 * u32
55 * i64
56 * u64
57 * f32
58 * f64
59
60 ");
61 }
62
63 use std::fmt::Display;
64
65 fn compare_values<T>(values1: &[T], values2: &[T]) where
66     T: Display,
67     T: PartialOrd
68 {
69     if values1.len() != values2.len() {
70         println!("Can only compare vectors of equal size. The first vector has {} elements. The second vector has {} elements.", values1.len(), values2.len());
71         return
72     }
73
74     let mut v1_smaller_count = 0;
75     let mut v2_smaller_count = 0;
76     let mut first_diff = None;
77
78     for (i, (v1, v2)) in values1.iter().zip(values2.iter()).enumerate() {
79         if v1 < v2 { v1_smaller_count += 1; }
80         if v2 < v1 { v2_smaller_count += 1; }
81
82         if first_diff.is_none() && v1 != v2 {
83             first_diff = Some(i)
84         }
85     }
86
87     match first_diff {
88         Some(index) => {
89             println!("The vectors differ.");
90             println!("{} elements are smaller in the first vector.", v1_smaller_count);
91             println!("{} elements are smaller in the second vector.", v2_smaller_count);
92             println!("{} elements are the same.", values1.len() - v1_smaller_count - v2_smaller_count);
93             println!("{} elements are different.", v1_smaller_count + v2_smaller_count);
94             println!("The vectors have  {} elements.", values1.len());
95             println!("The first element that differs is at index {}.", index);
96         },
97         None => {
98             println!("The vectors are the same and have  {} elements.", values1.len());
99         },
100     }
101 }