malachite_nz/integer/conversion/natural_from_integer.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::integer::Integer;
10use crate::natural::Natural;
11use malachite_base::num::basic::traits::Zero;
12use malachite_base::num::conversion::traits::{ConvertibleFrom, SaturatingFrom};
13
14#[derive(Clone, Copy, Debug, Eq, PartialEq)]
15pub struct NaturalFromIntegerError;
16
17impl TryFrom<Integer> for Natural {
18 type Error = NaturalFromIntegerError;
19
20 /// Converts an [`Integer`] to a [`Natural`], taking the [`Natural`] by value. If the
21 /// [`Integer`] is negative, an error is returned.
22 ///
23 /// # Worst-case complexity
24 /// Constant time and additional memory.
25 ///
26 /// # Examples
27 /// ```
28 /// use malachite_base::num::arithmetic::traits::Pow;
29 /// use malachite_base::strings::ToDebugString;
30 /// use malachite_nz::integer::Integer;
31 /// use malachite_nz::natural::Natural;
32 ///
33 /// assert_eq!(
34 /// Natural::try_from(Integer::from(123)).to_debug_string(),
35 /// "Ok(123)"
36 /// );
37 /// assert_eq!(
38 /// Natural::try_from(Integer::from(-123)).to_debug_string(),
39 /// "Err(NaturalFromIntegerError)"
40 /// );
41 /// assert_eq!(
42 /// Natural::try_from(Integer::from(10u32).pow(12)).to_debug_string(),
43 /// "Ok(1000000000000)"
44 /// );
45 /// assert_eq!(
46 /// Natural::try_from(-Integer::from(10u32).pow(12)).to_debug_string(),
47 /// "Err(NaturalFromIntegerError)"
48 /// );
49 /// ```
50 fn try_from(value: Integer) -> Result<Self, Self::Error> {
51 match value {
52 Integer { sign: false, .. } => Err(NaturalFromIntegerError),
53 Integer { sign: true, abs } => Ok(abs),
54 }
55 }
56}
57
58impl<'a> TryFrom<&'a Integer> for Natural {
59 type Error = NaturalFromIntegerError;
60
61 /// Converts an [`Integer`] to a [`Natural`], taking the [`Natural`] by reference. If the
62 /// [`Integer`] is negative, an error is returned.
63 ///
64 /// # Worst-case complexity
65 /// $T(n) = O(n)$
66 ///
67 /// $M(n) = O(n)$
68 ///
69 /// where $T$ is time, $M$ is additional memory, and $n$ is `value.significant_bits()`.
70 ///
71 /// # Examples
72 /// ```
73 /// use malachite_base::num::arithmetic::traits::Pow;
74 /// use malachite_base::strings::ToDebugString;
75 /// use malachite_nz::integer::Integer;
76 /// use malachite_nz::natural::Natural;
77 ///
78 /// assert_eq!(
79 /// Natural::try_from(&Integer::from(123)).to_debug_string(),
80 /// "Ok(123)"
81 /// );
82 /// assert_eq!(
83 /// Natural::try_from(&Integer::from(-123)).to_debug_string(),
84 /// "Err(NaturalFromIntegerError)"
85 /// );
86 /// assert_eq!(
87 /// Natural::try_from(&Integer::from(10u32).pow(12)).to_debug_string(),
88 /// "Ok(1000000000000)"
89 /// );
90 /// assert_eq!(
91 /// Natural::try_from(&(-Integer::from(10u32).pow(12))).to_debug_string(),
92 /// "Err(NaturalFromIntegerError)"
93 /// );
94 /// ```
95 fn try_from(value: &'a Integer) -> Result<Self, Self::Error> {
96 match *value {
97 Integer { sign: false, .. } => Err(NaturalFromIntegerError),
98 Integer {
99 sign: true,
100 ref abs,
101 } => Ok(abs.clone()),
102 }
103 }
104}
105
106impl SaturatingFrom<Integer> for Natural {
107 /// Converts an [`Integer`] to a [`Natural`], taking the [`Natural`] by value. If the
108 /// [`Integer`] is negative, 0 is returned.
109 ///
110 /// # Worst-case complexity
111 /// Constant time and additional memory.
112 ///
113 /// # Examples
114 /// ```
115 /// use malachite_base::num::arithmetic::traits::Pow;
116 /// use malachite_base::num::conversion::traits::SaturatingFrom;
117 /// use malachite_nz::integer::Integer;
118 /// use malachite_nz::natural::Natural;
119 ///
120 /// assert_eq!(Natural::saturating_from(Integer::from(123)), 123);
121 /// assert_eq!(Natural::saturating_from(Integer::from(-123)), 0);
122 /// assert_eq!(
123 /// Natural::saturating_from(Integer::from(10u32).pow(12)),
124 /// 1000000000000u64
125 /// );
126 /// assert_eq!(Natural::saturating_from(-Integer::from(10u32).pow(12)), 0);
127 /// ```
128 fn saturating_from(value: Integer) -> Self {
129 match value {
130 Integer { sign: false, .. } => Self::ZERO,
131 Integer { sign: true, abs } => abs,
132 }
133 }
134}
135
136impl<'a> SaturatingFrom<&'a Integer> for Natural {
137 /// Converts an [`Integer`] to a [`Natural`], taking the [`Natural`] by reference. If the
138 /// [`Integer`] is negative, 0 is returned.
139 ///
140 /// # Worst-case complexity
141 /// Constant time and additional memory.
142 ///
143 /// # Examples
144 /// ```
145 /// use malachite_base::num::arithmetic::traits::Pow;
146 /// use malachite_base::num::conversion::traits::SaturatingFrom;
147 /// use malachite_nz::integer::Integer;
148 /// use malachite_nz::natural::Natural;
149 ///
150 /// assert_eq!(Natural::saturating_from(&Integer::from(123)), 123);
151 /// assert_eq!(Natural::saturating_from(&Integer::from(-123)), 0);
152 /// assert_eq!(
153 /// Natural::saturating_from(&Integer::from(10u32).pow(12)),
154 /// 1000000000000u64
155 /// );
156 /// assert_eq!(Natural::saturating_from(&-Integer::from(10u32).pow(12)), 0);
157 /// ```
158 fn saturating_from(value: &'a Integer) -> Self {
159 match *value {
160 Integer { sign: false, .. } => Self::ZERO,
161 Integer {
162 sign: true,
163 ref abs,
164 } => abs.clone(),
165 }
166 }
167}
168
169impl ConvertibleFrom<Integer> for Natural {
170 /// Determines whether an [`Integer`] can be converted to a [`Natural`] (when the [`Integer`] is
171 /// non-negative). Takes the [`Integer`] by value.
172 ///
173 /// # Worst-case complexity
174 /// Constant time and additional memory.
175 ///
176 /// # Examples
177 /// ```
178 /// use malachite_base::num::arithmetic::traits::Pow;
179 /// use malachite_base::num::conversion::traits::ConvertibleFrom;
180 /// use malachite_nz::integer::Integer;
181 /// use malachite_nz::natural::Natural;
182 ///
183 /// assert_eq!(Natural::convertible_from(Integer::from(123)), true);
184 /// assert_eq!(Natural::convertible_from(Integer::from(-123)), false);
185 /// assert_eq!(
186 /// Natural::convertible_from(Integer::from(10u32).pow(12)),
187 /// true
188 /// );
189 /// assert_eq!(
190 /// Natural::convertible_from(-Integer::from(10u32).pow(12)),
191 /// false
192 /// );
193 /// ```
194 #[inline]
195 fn convertible_from(value: Integer) -> bool {
196 value.sign
197 }
198}
199
200impl<'a> ConvertibleFrom<&'a Integer> for Natural {
201 /// Determines whether an [`Integer`] can be converted to a [`Natural`] (when the [`Integer`] is
202 /// non-negative). Takes the [`Integer`] by reference.
203 ///
204 /// # Worst-case complexity
205 /// Constant time and additional memory.
206 ///
207 /// # Examples
208 /// ```
209 /// use malachite_base::num::arithmetic::traits::Pow;
210 /// use malachite_base::num::conversion::traits::ConvertibleFrom;
211 /// use malachite_nz::integer::Integer;
212 /// use malachite_nz::natural::Natural;
213 ///
214 /// assert_eq!(Natural::convertible_from(&Integer::from(123)), true);
215 /// assert_eq!(Natural::convertible_from(&Integer::from(-123)), false);
216 /// assert_eq!(
217 /// Natural::convertible_from(&Integer::from(10u32).pow(12)),
218 /// true
219 /// );
220 /// assert_eq!(
221 /// Natural::convertible_from(&-Integer::from(10u32).pow(12)),
222 /// false
223 /// );
224 /// ```
225 #[inline]
226 fn convertible_from(value: &'a Integer) -> bool {
227 value.sign
228 }
229}