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
use crate::Bit;
use crate::Int;
use std::cmp::Ordering;
impl Ord for Int {
    fn cmp(&self, other: &Self) -> Ordering {
        
        if !self.sign && other.sign {
            Ordering::Greater
        }
        
        else if self.sign && !other.sign {
            Ordering::Less
        }
        
        else if !self.sign && !other.sign {
            comparator(&self.magnitude, &other.magnitude)
        }
        
        else {
    
            match comparator(&self.magnitude, &other.magnitude) {
                Ordering::Greater => Ordering::Less,
                Ordering::Less => Ordering::Greater,
                Ordering::Equal => Ordering::Equal
            }
    
        }
    }
}
impl PartialOrd for Int {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}
pub fn comparator(a: &Vec<Bit>, b: &Vec<Bit>) -> Ordering {
    let mut a_bits = a.clone();
    
    let mut b_bits = b.clone();
    while a_bits.len() > 1 && a_bits[0] == Bit::Zero {
        a_bits.remove(0);
    };
    while b_bits.len() > 1 && b_bits[0] == Bit::Zero {
        b_bits.remove(0);
    };
    let a_len = a_bits.len();
    
    let b_len = b_bits.len();
    if a_len > b_len {
        Ordering::Greater
    } else if a_len < b_len {
        Ordering::Less
    } else {
        if a_bits == b_bits {
            Ordering::Equal
        } else {
            while a_bits[0] == b_bits[0] {
                a_bits.remove(0);
                b_bits.remove(0);
            }
            if a_bits[0] == Bit::One {
                Ordering::Greater
            } else {
                Ordering::Less
            }
        }
    }
}