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
use std::collections::HashMap;
use std::hash::{Hash, BuildHasher};
use crate::imp::structs::linked_m::LinkedMap;
use std::sync::Arc;

/// When items have identity objects, compare identity objects, otherwise compare items directly.
/// This can shortcut comparing and make it faster.
pub trait IdentityEqual{
    fn identity_eq(&self, other : &Self) -> bool;
}

impl<K,  V : IdentityEqual, S> IdentityEqual for HashMap<K,V,S>
    where K: Eq + Hash,
          S: BuildHasher{

    fn identity_eq(&self, other: &Self) -> bool {
        if self.len() != other.len() {
            return false;
        }

        self.iter().all(move |(key, value)| other.get(key).map_or(false, |v| value.identity_eq(v)))
    }
}

impl<T : IdentityEqual> IdentityEqual for Arc<T>{
    fn identity_eq(&self, other: &Self) -> bool {
        if Arc::ptr_eq(self, other){ true }
        else{ self.as_ref().identity_eq(other.as_ref())}
    }
}

impl IdentityEqual for bool{
    fn identity_eq(&self, other: &Self) -> bool {
        self == other
    }
}

impl IdentityEqual for i64{
    fn identity_eq(&self, other: &Self) -> bool {
        self == other
    }
}

impl IdentityEqual for f64{
    fn identity_eq(&self, other: &Self) -> bool {
        self == other
    }
}

impl IdentityEqual for String{
    fn identity_eq(&self, other: &Self) -> bool {
        self == other
    }
}

impl<T : IdentityEqual> IdentityEqual for LinkedMap<T>{
    fn identity_eq(&self, other: &Self) -> bool {
        if self.len() != other.len() {
            return false;
        }

        self.iter().all(move |(key, value)| other.get_item(*key).map_or(false, |v| value.identity_eq(v)))
    }
}