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
use std::collections::BTreeMap;

use crate::{GrowableProducer, Iterable, IterableMap};

impl<K, V> Iterable for BTreeMap<K, V> {
    type C = Self;
    type CC<U> = Vec<U>;
    // remove below after `associated_type_defaults` stabilized
    type F = Self;
    type CF<U> = Vec<U>;

    fn add_one(mut self, a: Self::Item) -> Self::C
    where
        Self::C: GrowableProducer<Self::Item>,
    {
        self.grow_one(a);
        self
    }
}

impl<'a, K: 'a, V: 'a> Iterable for &'a BTreeMap<K, V> {
    type C = BTreeMap<&'a K, &'a V>;
    type CC<U> = Vec<U>;
    // remove below after `associated_type_defaults` stabilized
    type F = BTreeMap<&'a K, &'a V>;
    type CF<U> = Vec<U>;
}

impl<K, V> IterableMap<K, V> for BTreeMap<K, V> {
    type CCMap<X, Y> = BTreeMap<X, Y>;
}

impl<'a, K: 'a, V: 'a> IterableMap<&'a K, &'a V> for &'a BTreeMap<K, V> {
    type CCMap<X, Y> = BTreeMap<X, Y>;
}

delegate_into_iterator!(BTreeMap<K, V>, impl <K, V>);
delegate_into_iterator!(&'a BTreeMap<K, V>, impl <'a, K: 'a, V: 'a>);

delegate_from_iterator!(BTreeMap<K, V>, (K, V), impl <K: Ord, V>);
delegate_extend!(BTreeMap<K, V>, (K, V), impl <K: Ord, V>);

#[cfg(test)]
mod tests {
    use maplit::*;

    use super::*;

    #[test]
    fn test_c() {
        let v = btreemap![1 => "a",2 => "b",3 => "c"];
        let res = v.filter(|(i, _)| i > &1);
        assert_eq!(res, btreemap![2=>"b", 3 => "c"]);
    }

    #[test]
    fn test_cc() {
        let v = btreemap![1 => "a",2 => "b",3 => "c"];
        let mut res = v.map(|(i, _)| i.to_string());
        res.sort();
        assert_eq!(res, vec!["1".to_string(), "2".to_string(), "3".to_string()]);
    }

    #[test]
    fn test_c_r() {
        let v = btreemap![1 => "a",2 => "b",3 => "c"];
        let res = (&v).filter(|(i, _)| i > &&1);
        assert_eq!(res, btreemap![&2 => &"b", &3 => &"c"]);
    }

    #[test]
    fn test_cc_r() {
        let v = btreemap![1 => "a",2 => "b",3 => "c"];
        let mut res = (&v).map(|(i, _)| i.to_string());
        res.sort();
        assert_eq!(res, vec!["1".to_string(), "2".to_string(), "3".to_string()]);
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////
    // tests for IterableMap
    #[test]
    fn test_map_value() {
        let v = btreemap![1 => "a",2 => "b",3 => "c"];
        let res = v.map_value(|v| v.to_string());
        assert_eq!(
            res,
            btreemap![1 => "a".to_string(), 2 => "b".to_string(), 3 => "c".to_string()]
        );
    }

    #[test]
    fn test_map_kv() {
        let v = btreemap![1 => "a",2 => "b",3 => "c"];
        let res = v.map_kv(|(k, v)| (k + 1, v.to_string()));
        assert_eq!(
            res,
            btreemap![2 => "a".to_string(), 3 => "b".to_string(), 4 => "c".to_string()]
        );
    }

    #[test]
    fn test_map_value_r() {
        let v = btreemap![1 => "a",2 => "b",3 => "c"];
        let res = (&v).map_value(|v| v.to_string());
        assert_eq!(
            res,
            btreemap![&1 => "a".to_string(), &2 => "b".to_string(), &3 => "c".to_string()]
        );
    }

    #[test]
    fn test_map_kv_r() {
        let v = btreemap![1 => "a",2 => "b",3 => "c"];
        let res = (&v).map_kv(|(k, v)| (k.to_string(), v.to_string()));
        assert_eq!(
            res,
            btreemap!["1".to_string() => "a".to_string(), "2".to_string() => "b".to_string(), "3".to_string() => "c".to_string()]
        );
    }
}