malachite_base/num/arithmetic/abs_diff.rs
1// Copyright © 2025 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::num::arithmetic::traits::{AbsDiff, AbsDiffAssign};
10
11macro_rules! impl_abs_diff_unsigned {
12 ($t:ident) => {
13 impl AbsDiff for $t {
14 type Output = $t;
15
16 /// This is a wrapper over the `abs_diff` functions in the standard library, for example
17 /// [this one](u32::abs_diff).
18 #[inline]
19 fn abs_diff(self, other: $t) -> $t {
20 self.abs_diff(other)
21 }
22 }
23
24 impl AbsDiffAssign for $t {
25 /// Subtracts a number by another and takes the absolute value, in place. The output
26 /// type is the unsigned type with the same width.
27 ///
28 /// # Worst-case complexity
29 /// Constant time and additional memory.
30 ///
31 /// # Examples
32 /// See [here](super::abs_diff#abs_diff_assign).
33 #[inline]
34 fn abs_diff_assign(&mut self, other: $t) {
35 *self = self.abs_diff(other);
36 }
37 }
38 };
39}
40apply_to_unsigneds!(impl_abs_diff_unsigned);
41
42macro_rules! impl_abs_diff_signed {
43 ($u:ident, $s:ident) => {
44 impl AbsDiff for $s {
45 type Output = $u;
46
47 /// This is a wrapper over the `abs_diff` functions in the standard library, for example
48 /// [this one](i32::abs_diff).
49 #[inline]
50 fn abs_diff(self, other: $s) -> $u {
51 self.abs_diff(other)
52 }
53 }
54 };
55}
56apply_to_unsigned_signed_pairs!(impl_abs_diff_signed);