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
mod display;
mod serialize;

use std::collections::BTreeMap;

/// Counter structure.
///
/// # Example :
///
/// ```
/// use yadf::TreeBag;
///
/// let bag: TreeBag<i32, &str> = vec![
///     (3, "hello world"),
///     (3, "foobar"),
///     (7, "fizz"),
///     (7, "buzz"),
///     (6, "rust"),
/// ].into_iter().collect();
/// assert_eq!(bag[&3].len(), 2);
/// assert_eq!(bag[&6].len(), 1);
/// assert_eq!(bag[&3][0], "hello world");
/// ```
#[repr(transparent)]
pub struct TreeBag<H: Ord, T>(pub(crate) BTreeMap<H, Vec<T>>);

/// Display marker.
#[derive(Debug)]
pub struct Machine;
#[derive(Debug)]
/// Display marker.
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> {
    /// Provides a view on all the buckets containing more than one element.
    pub fn duplicates(&self) -> impl Iterator<Item = &[T]> {
        self.0.values().filter(|b| b.len() > 1).map(AsRef::as_ref)
    }

    /// Returns an object that implements [`Display`](https://doc.rust-lang.org/stable/std/fmt/trait.Display.html).
    ///
    /// Depending on the contents of the [`TreeBag`](struct.TreeBag.html), the display object
    /// can be parameterized to get a different `Display` implemenation.
    pub fn display<D: marker::OutputFormat>(&self) -> Display<'_, H, T, D> {
        Display {
            counter: self,
            _marker: std::marker::PhantomData,
        }
    }
}

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
    }
}

pub mod marker {
    pub trait OutputFormat {}
    impl OutputFormat for super::Fdupes {}
    impl OutputFormat for super::Machine {}
}