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
#![cfg_attr(debug_assertions, allow(dead_code, unused_imports))]
#![deny(unused_must_use)]
#![deny(clippy::all)]
#![allow(clippy::needless_return)]
#![deny(clippy::pedantic)]
#![allow(clippy::missing_errors_doc)]
#![deny(missing_docs)]
use std::vec::Vec;
#[macro_use]
extern crate failure;
use indexmap::{IndexMap, IndexSet};
pub mod args;
use crate::args::OpName;
pub mod io;
use crate::io::lines_of;
pub(crate) type LineIterator<'a> = Box<dyn Iterator<Item = &'a [u8]> + 'a>;
type SliceSet<'data> = IndexSet<&'data [u8]>;
fn slice_set(operand: &[u8]) -> SliceSet {
let mut set = SliceSet::default();
for line in lines_of(operand) {
set.insert(line);
}
set
}
type UnionSet = IndexSet<Vec<u8>>;
type CountedSet = IndexMap<Vec<u8>, FoundIn>;
#[derive(PartialEq)]
enum FoundIn {
One,
Many,
}
pub fn do_calculation(
operation: OpName,
first_operand: &[u8],
rest: impl IntoIterator<Item = Result<Vec<u8>, failure::Error>>,
output: impl FnOnce(LineIterator) -> Result<(), failure::Error>,
) -> Result<(), failure::Error> {
let rest = rest.into_iter();
match operation {
OpName::Union => {
let mut set = UnionSet::default();
for line in lines_of(first_operand) {
set.insert(line.to_vec());
}
for operand in rest {
for line in lines_of(&operand?) {
set.insert(line.to_vec());
}
}
return output(Box::new(&mut set.iter().map(Vec::as_slice)));
}
OpName::Intersect | OpName::Diff => {
let mut set = slice_set(first_operand);
for operand in rest {
let operand = operand?;
let other = slice_set(&operand);
if operation == OpName::Intersect {
set.retain(|x| other.contains(x))
} else {
set.retain(|x| !other.contains(x))
}
}
return output(Box::new(set.iter().copied()));
}
OpName::Single | OpName::Multiple => {
let mut set = CountedSet::default();
for line in lines_of(first_operand) {
set.insert(line.to_vec(), FoundIn::One);
}
for operand in rest {
let operand = operand?;
let other = slice_set(&operand);
for line in other.iter() {
let found_in =
if set.contains_key(*line) { FoundIn::Many } else { FoundIn::One };
set.insert(line.to_vec(), found_in);
}
}
let wanted = if operation == OpName::Single { FoundIn::One } else { FoundIn::Many };
set.retain(|_k, v| *v == wanted);
return output(Box::new(set.keys().map(Vec::as_slice)));
}
};
}
#[allow(clippy::pedantic)]
#[cfg(test)]
mod test {
use super::*;
fn calc(operation: OpName, operands: &[&[u8]]) -> Vec<u8> {
fn add_eol(s: &[u8]) -> Vec<u8> {
let mut s = s.to_owned();
s.push(b'\n');
s
}
let mut answer = Vec::<u8>::new();
let mut operands = operands.iter().map(|s| Ok(s.to_vec()));
let first = operands.next().unwrap().unwrap();
do_calculation(operation, &first, operands, {
|iter| {
answer = iter.map(|s| add_eol(s)).flatten().collect();
Ok(())
}
})
.unwrap();
answer
}
use self::OpName::*;
#[test]
fn given_a_single_argument_all_ops_but_multiple_return_its_lines_in_order_without_dups() {
let arg: Vec<&[u8]> = vec![b"xxx\nabc\nxxx\nyyy\nxxx\nabc\n"];
let uniq = b"xxx\nabc\nyyy\n".to_vec();
let empty = b"".to_vec();
for op in &[Intersect, Union, Diff, Single, Multiple] {
let result = calc(*op, &arg);
let expected = if *op == Multiple { &empty } else { &uniq };
assert_eq!(result, *expected, "for {:?}", op);
}
}
#[test]
fn results_for_each_operation() {
let args: Vec<&[u8]> = vec![
b"xyz\nabc\nxy\nxz\nx\n",
b"xyz\nabc\nxy\nyz\ny\n",
b"xyz\nabc\nxz\nyz\nz\n",
];
assert_eq!(calc(Union, &args), b"xyz\nabc\nxy\nxz\nx\nyz\ny\nz\n", "for {:?}", Union);
assert_eq!(calc(Intersect, &args), b"xyz\nabc\n", "for {:?}", Intersect);
assert_eq!(calc(Diff, &args), b"x\n", "for {:?}", Diff);
assert_eq!(calc(Single, &args), b"x\ny\nz\n", "for {:?}", Single);
assert_eq!(calc(Multiple, &args), b"xyz\nabc\nxy\nxz\nyz\n", "for {:?}", Multiple);
}
}