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
mod display;
mod serialize;
use std::collections::BTreeMap;
#[repr(transparent)]
pub struct TreeBag<H: Ord, T>(pub(crate) BTreeMap<H, Vec<T>>);
impl<H: Ord, T> TreeBag<H, T> {
pub fn duplicates(&self) -> impl Iterator<Item = &Vec<T>> {
self.values().filter(|b| b.len() > 1)
}
}
impl<H: Ord, T> std::iter::FromIterator<(H, T)> for TreeBag<H, T> {
fn from_iter<I: IntoIterator<Item = (H, T)>>(iter: I) -> Self {
let mut map: BTreeMap<H, Vec<T>> = BTreeMap::new();
for (hash, item) in iter {
map.entry(hash).or_default().push(item);
}
Self(map)
}
}
impl<H: Ord, T> std::ops::Deref for TreeBag<H, T> {
type Target = BTreeMap<H, Vec<T>>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[derive(Debug)]
pub struct Machine;
#[derive(Debug)]
pub struct Fdupes;
pub struct Display<'a, H: Ord, T, U: marker::OutputFormat> {
_marker: std::marker::PhantomData<U>,
counter: &'a TreeBag<H, T>,
}
impl<H: Ord, T> TreeBag<H, T> {
pub fn display<D: marker::OutputFormat>(&self) -> Display<'_, H, T, D> {
Display {
counter: self,
_marker: std::marker::PhantomData,
}
}
}
pub mod marker {
pub trait OutputFormat {}
impl OutputFormat for super::Fdupes {}
impl OutputFormat for super::Machine {}
}