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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use std::cmp::Ordering;
use std::fmt;
use std::ops::{Add, Div, Mul, Sub};

use ffi::*;
use libc::c_int;

#[derive(Copy, Clone)]
pub struct Rational(pub i32, pub i32);

impl Rational {
    #[inline]
    pub fn new(numerator: i32, denominator: i32) -> Self {
        Rational(numerator, denominator)
    }

    #[inline]
    pub fn numerator(&self) -> i32 {
        self.0
    }

    #[inline]
    pub fn denominator(&self) -> i32 {
        self.1
    }

    #[inline]
    pub fn reduce(&self) -> Rational {
        match self.reduce_with_limit(i32::max_value()) {
            Ok(r) => r,
            Err(r) => r,
        }
    }

    #[inline]
    pub fn reduce_with_limit(&self, max: i32) -> Result<Rational, Rational> {
        unsafe {
            let mut dst_num: c_int = 0;
            let mut dst_den: c_int = 0;

            let exact = av_reduce(
                &mut dst_num,
                &mut dst_den,
                i64::from(self.numerator()),
                i64::from(self.denominator()),
                i64::from(max),
            );

            if exact == 1 {
                Ok(Rational(dst_num, dst_den))
            } else {
                Err(Rational(dst_num, dst_den))
            }
        }
    }

    #[inline]
    pub fn invert(&self) -> Rational {
        unsafe { Rational::from(av_inv_q((*self).into())) }
    }
}

impl From<AVRational> for Rational {
    #[inline]
    fn from(value: AVRational) -> Rational {
        Rational(value.num, value.den)
    }
}

impl From<Rational> for AVRational {
    #[inline]
    fn from(value: Rational) -> AVRational {
        AVRational {
            num: value.0,
            den: value.1,
        }
    }
}

impl From<f64> for Rational {
    #[inline]
    fn from(value: f64) -> Rational {
        unsafe { Rational::from(av_d2q(value, c_int::max_value())) }
    }
}

impl From<Rational> for f64 {
    #[inline]
    fn from(value: Rational) -> f64 {
        unsafe { av_q2d(value.into()) }
    }
}

impl From<Rational> for u32 {
    #[inline]
    fn from(value: Rational) -> u32 {
        unsafe { av_q2intfloat(value.into()) }
    }
}

impl From<(i32, i32)> for Rational {
    fn from((num, den): (i32, i32)) -> Rational {
        Rational::new(num, den)
    }
}

impl PartialEq for Rational {
    fn eq(&self, other: &Rational) -> bool {
        if self.0 == other.0 && self.1 == other.1 {
            return true;
        }

        let a = self.reduce();
        let b = other.reduce();

        if a.0 == b.0 && a.1 == b.1 {
            return true;
        }

        false
    }
}

impl Eq for Rational {}

impl PartialOrd for Rational {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        unsafe {
            match av_cmp_q((*self).into(), (*other).into()) {
                0 => Some(Ordering::Equal),
                1 => Some(Ordering::Greater),
                -1 => Some(Ordering::Less),

                _ => None,
            }
        }
    }
}

impl Add for Rational {
    type Output = Rational;

    #[inline]
    fn add(self, other: Rational) -> Rational {
        unsafe { Rational::from(av_add_q(self.into(), other.into())) }
    }
}

impl Sub for Rational {
    type Output = Rational;

    #[inline]
    fn sub(self, other: Rational) -> Rational {
        unsafe { Rational::from(av_sub_q(self.into(), other.into())) }
    }
}

impl Mul for Rational {
    type Output = Rational;

    #[inline]
    fn mul(self, other: Rational) -> Rational {
        unsafe { Rational::from(av_mul_q(self.into(), other.into())) }
    }
}

impl Div for Rational {
    type Output = Rational;

    #[inline]
    fn div(self, other: Rational) -> Rational {
        unsafe { Rational::from(av_div_q(self.into(), other.into())) }
    }
}

impl fmt::Display for Rational {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        f.write_str(&format!("{}/{}", self.numerator(), self.denominator()))
    }
}

impl fmt::Debug for Rational {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        f.write_str(&format!(
            "Rational({}/{})",
            self.numerator(),
            self.denominator()
        ))
    }
}

#[inline]
pub fn nearer(q: Rational, q1: Rational, q2: Rational) -> Ordering {
    unsafe {
        match av_nearer_q(q.into(), q1.into(), q2.into()) {
            1 => Ordering::Greater,
            -1 => Ordering::Less,
            _ => Ordering::Equal,
        }
    }
}