malachite_nz/natural/logic/
or.rs

1// Copyright © 2025 Mikhail Hogrefe
2//
3// Uses code adopted from the GNU MP Library.
4//
5//      Copyright © 1991-2018 Free Software Foundation, Inc.
6//
7// This file is part of Malachite.
8//
9// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
10// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
11// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
12
13use crate::natural::InnerNatural::{Large, Small};
14use crate::natural::Natural;
15use crate::platform::Limb;
16use alloc::vec::Vec;
17use core::mem::swap;
18use core::ops::{BitOr, BitOrAssign};
19
20// Interpreting a slice of `Limb`s as the limbs (in ascending order) of a `Natural`, returns the
21// limbs of the bitwise or of the `Natural` and a `Limb`. `xs` cannot be empty.
22//
23// # Worst-case complexity
24// $T(n) = O(n)$
25//
26// $M(n) = O(n)$
27//
28// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
29//
30// # Panics
31// Panics if `xs` is empty.
32pub fn limbs_or_limb(xs: &[Limb], y: Limb) -> Vec<Limb> {
33    let mut result = xs.to_vec();
34    limbs_or_limb_in_place(&mut result, y);
35    result
36}
37
38// Interpreting a slice of `Limb`s as the limbs (in ascending order) of a `Natural`, writes the
39// limbs of the bitwise or of the `Natural` and a `Limb` to an output slice. The output slice must
40// be at least as long as the input slice. `xs` cannot be empty.
41//
42// # Worst-case complexity
43// $T(n) = O(n)$
44//
45// $M(n) = O(1)$
46//
47// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
48//
49// # Panics
50// Panics if `out` is shorter than `xs` or if `xs` is empty.
51pub_test! {limbs_or_limb_to_out(out: &mut [Limb], xs: &[Limb], y: Limb) {
52    out[..xs.len()].copy_from_slice(xs);
53    limbs_or_limb_in_place(out, y);
54}}
55
56// Interpreting a slice of `Limb`s as the limbs (in ascending order) of a `Natural`, writes the
57// limbs of the bitwise or of the `Natural` and a `Limb` to the input slice. `xs` cannot be empty.
58//
59// # Worst-case complexity
60// Constant time and additional memory.
61//
62// # Panics
63// Panics if `xs` is empty.
64pub_test! {limbs_or_limb_in_place(xs: &mut [Limb], y: Limb) {
65    xs[0] |= y;
66}}
67
68// Interpreting two equal-length slices of `Limb`s as the limbs (in ascending order) of two
69// `Natural`s, returns a `Vec` of the limbs of the bitwise or of the `Natural`s. The length of the
70// result is the length of one of the input slices.
71//
72// # Worst-case complexity
73// $T(n) = O(n)$
74//
75// $M(n) = O(n)$
76//
77// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
78//
79// # Panics
80// Panics if `xs` and `ys` have different lengths.
81//
82// This is equivalent to `mpn_ior_n` from `gmp-impl.h`, GMP 6.2.1, where `rp` is returned.
83pub_test! {limbs_or_same_length(xs: &[Limb], ys: &[Limb]) -> Vec<Limb> {
84    assert_eq!(xs.len(), ys.len());
85    xs.iter().zip(ys.iter()).map(|(x, y)| x | y).collect()
86}}
87
88// Interpreting two slices of `Limb`s as the limbs (in ascending order) of two `Natural`s, returns a
89// `Vec` of the limbs of the bitwise or of the `Natural`s. The length of the result is the length of
90// the longer input slice.
91//
92// # Worst-case complexity
93// $T(n) = O(n)$
94//
95// $M(n) = O(n)$
96//
97// where $T$ is time, $M$ is additional memory, and $n$ is `max(xs.len(), ys.len())`.
98//
99// This is equivalent to `mpz_ior` from `mpz/ior.c`, GMP 6.2.1, where `res` is returned and both
100// inputs are non-negative.
101pub_test! {limbs_or(xs: &[Limb], ys: &[Limb]) -> Vec<Limb> {
102    let xs_len = xs.len();
103    let ys_len = ys.len();
104    let mut result;
105    if xs_len >= ys_len {
106        result = limbs_or_same_length(&xs[..ys_len], ys);
107        result.extend_from_slice(&xs[ys_len..]);
108    } else {
109        result = limbs_or_same_length(xs, &ys[..xs_len]);
110        result.extend_from_slice(&ys[xs_len..]);
111    }
112    result
113}}
114
115// Interpreting two equal-length slices of `Limb`s as the limbs (in ascending order) of two
116// `Natural`s, writes the limbs of the bitwise or of the `Natural`s to an output slice. The output
117// must be at least as long as one of the input slices.
118//
119// # Worst-case complexity
120// $T(n) = O(n)$
121//
122// $M(n) = O(1)$
123//
124// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
125//
126// # Panics
127// Panics if `xs` and `ys` have different lengths or if `out` is too short.
128//
129// This is equivalent to `mpn_ior_n` from `gmp-impl.h`, GMP 6.2.1.
130pub_test! {limbs_or_same_length_to_out(out: &mut [Limb], xs: &[Limb], ys: &[Limb]) {
131    let len = xs.len();
132    assert_eq!(len, ys.len());
133    assert!(out.len() >= len);
134    for (out_x, (x, y)) in out.iter_mut().zip(xs.iter().zip(ys.iter())) {
135        *out_x = x | y;
136    }
137}}
138
139// Interpreting two slices of `Limb`s as the limbs (in ascending order) of two `Natural`s, writes
140// the limbs of the bitwise or of the `Natural`s to an output slice. The output must be at least as
141// long as the longer input slice.
142//
143// # Worst-case complexity
144// $T(n) = O(n)$
145//
146// $M(n) = O(1)$
147//
148// where $T$ is time, $M$ is additional memory, and $n$ is `max(xs.len(), ys.len())`.
149//
150// # Panics
151// Panics if `out` is too short.
152//
153// This is equivalent to `mpz_ior` from `mpz/ior.c`, GMP 6.2.1, where both inputs are non-negative.
154pub_test! {limbs_or_to_out(out: &mut [Limb], xs: &[Limb], ys: &[Limb]) {
155    let xs_len = xs.len();
156    let ys_len = ys.len();
157    if xs_len >= ys_len {
158        assert!(out.len() >= xs_len);
159        limbs_or_same_length_to_out(out, &xs[..ys_len], ys);
160        out[ys_len..xs_len].copy_from_slice(&xs[ys_len..]);
161    } else {
162        assert!(out.len() >= ys_len);
163        limbs_or_same_length_to_out(out, xs, &ys[..xs_len]);
164        out[xs_len..ys_len].copy_from_slice(&ys[xs_len..]);
165    }
166}}
167
168// Interpreting two equal-length slices of `Limb`s as the limbs (in ascending order) of two
169// `Natural`s, writes the limbs of the bitwise or of the `Natural`s to the first (left) slice.
170//
171// # Worst-case complexity
172// $T(n) = O(n)$
173//
174// $M(n) = O(1)$
175//
176// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
177//
178// # Panics
179// Panics if `xs` and `ys` have different lengths.
180//
181// This is equivalent to `mpn_ior_n` from `gmp-impl.h`, GMP 6.2.1, where `rp == up`.
182pub_test! {limbs_or_same_length_in_place_left(xs: &mut [Limb], ys: &[Limb]) {
183    assert_eq!(xs.len(), ys.len());
184    for (x, &y) in xs.iter_mut().zip(ys.iter()) {
185        *x |= y;
186    }
187}}
188
189// Interpreting a `Vec` of `Limb`s and a slice of `Limb`s as the limbs (in ascending order) of two
190// `Natural`s, writes the limbs of the bitwise or of the `Natural`s to the `Vec`. If `ys` is longer
191// than `xs`, `xs` will be extended.
192//
193// # Worst-case complexity
194// $T(n) = O(n)$
195//
196// $M(n) = O(n)$
197//
198// where $T$ is time, $M$ is additional memory, and $n$ is `ys.len()`.
199//
200// This is equivalent to `mpz_ior` from `mpz/ior.c`, GMP 6.2.1, where `res == op1` and both inputs
201// are non-negative.
202pub_test! {limbs_or_in_place_left(xs: &mut Vec<Limb>, ys: &[Limb]) {
203    let xs_len = xs.len();
204    let ys_len = ys.len();
205    if xs_len >= ys_len {
206        limbs_or_same_length_in_place_left(&mut xs[..ys_len], ys);
207    } else {
208        limbs_or_same_length_in_place_left(xs, &ys[..xs_len]);
209        xs.extend_from_slice(&ys[xs_len..]);
210    }
211}}
212
213// Interpreting two `Vec`s of `Limb`s as the limbs (in ascending order) of two `Natural`s, writes
214// the limbs of the bitwise or of the `Natural`s to the longer slice (or the first one, if they are
215// equally long). Returns a `bool` which is `false` when the output is to the first slice and `true`
216// when it's to the second slice.
217//
218// # Worst-case complexity
219// $T(n) = O(n)$
220//
221// $M(n) = O(1)$
222//
223// where $T$ is time, $M$ is additional memory, and $n$ is `min(xs.len(), ys.len())`.
224//
225// This is equivalent to `mpz_ior` from `mpz/ior.c`, GMP 6.2.1, where both inputs are non-negative
226// and the result is written to the longer input slice.
227pub_test! {limbs_or_in_place_either(xs: &mut [Limb], ys: &mut [Limb]) -> bool {
228    let xs_len = xs.len();
229    let ys_len = ys.len();
230    let right = xs_len < ys_len;
231    if right {
232        limbs_or_same_length_in_place_left(&mut ys[..xs_len], xs);
233    } else {
234        limbs_or_same_length_in_place_left(&mut xs[..ys_len], ys);
235    }
236    right
237}}
238
239impl Natural {
240    #[inline]
241    fn or_limb(mut self, other: Limb) -> Natural {
242        self.or_assign_limb(other);
243        self
244    }
245
246    fn or_limb_ref(&self, other: Limb) -> Natural {
247        Natural(match self {
248            Natural(Small(small)) => Small(small | other),
249            Natural(Large(limbs)) => Large(limbs_or_limb(limbs, other)),
250        })
251    }
252
253    fn or_assign_limb(&mut self, other: Limb) {
254        match self {
255            Natural(Small(small)) => *small |= other,
256            Natural(Large(limbs)) => limbs_or_limb_in_place(limbs, other),
257        }
258    }
259}
260
261impl BitOr<Natural> for Natural {
262    type Output = Natural;
263
264    /// Takes the bitwise or of two [`Natural`]s, taking both by value.
265    ///
266    /// $$
267    /// f(x, y) = x \vee y.
268    /// $$
269    ///
270    /// # Worst-case complexity
271    /// $T(n) = O(n)$
272    ///
273    /// $M(n) = O(1)$
274    ///
275    /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.significant_bits(),
276    /// other.significant_bits())`.
277    ///
278    /// # Examples
279    /// ```
280    /// use malachite_base::num::arithmetic::traits::Pow;
281    /// use malachite_base::num::basic::traits::One;
282    /// use malachite_nz::natural::Natural;
283    ///
284    /// assert_eq!(Natural::from(123u32) | Natural::from(456u32), 507);
285    /// assert_eq!(
286    ///     Natural::from(10u32).pow(12) | (Natural::from(10u32).pow(12) - Natural::ONE),
287    ///     1000000004095u64
288    /// );
289    /// ```
290    #[inline]
291    fn bitor(mut self, other: Natural) -> Natural {
292        self |= other;
293        self
294    }
295}
296
297impl<'a> BitOr<&'a Natural> for Natural {
298    type Output = Natural;
299
300    /// Takes the bitwise or of two [`Natural`]s, taking the first by value and the second by
301    /// reference.
302    ///
303    /// $$
304    /// f(x, y) = x \vee y.
305    /// $$
306    ///
307    /// # Worst-case complexity
308    /// $T(n) = O(n)$
309    ///
310    /// $M(n) = O(n)$
311    ///
312    /// where $T$ is time, $M$ is additional memory, and $n$ is `other.significant_bits()`.
313    ///
314    /// # Examples
315    /// ```
316    /// use malachite_base::num::arithmetic::traits::Pow;
317    /// use malachite_base::num::basic::traits::One;
318    /// use malachite_nz::natural::Natural;
319    ///
320    /// assert_eq!(Natural::from(123u32) | &Natural::from(456u32), 507);
321    /// assert_eq!(
322    ///     Natural::from(10u32).pow(12) | &(Natural::from(10u32).pow(12) - Natural::ONE),
323    ///     1000000004095u64
324    /// );
325    /// ```
326    #[inline]
327    fn bitor(mut self, other: &'a Natural) -> Natural {
328        self |= other;
329        self
330    }
331}
332
333impl BitOr<Natural> for &Natural {
334    type Output = Natural;
335
336    /// Takes the bitwise or of two [`Natural`]s, taking the first by reference and the second by
337    /// value.
338    ///
339    /// $$
340    /// f(x, y) = x \vee y.
341    /// $$
342    ///
343    /// # Worst-case complexity
344    /// $T(n) = O(n)$
345    ///
346    /// $M(n) = O(n)$
347    ///
348    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
349    ///
350    /// # Examples
351    /// ```
352    /// use malachite_base::num::arithmetic::traits::Pow;
353    /// use malachite_base::num::basic::traits::One;
354    /// use malachite_nz::natural::Natural;
355    ///
356    /// assert_eq!(&Natural::from(123u32) | Natural::from(456u32), 507);
357    /// assert_eq!(
358    ///     &Natural::from(10u32).pow(12) | (Natural::from(10u32).pow(12) - Natural::ONE),
359    ///     1000000004095u64
360    /// );
361    /// ```
362    #[inline]
363    fn bitor(self, mut other: Natural) -> Natural {
364        other |= self;
365        other
366    }
367}
368
369impl BitOr<&Natural> for &Natural {
370    type Output = Natural;
371
372    /// Takes the bitwise or of two [`Natural`]s, taking both by reference.
373    ///
374    /// $$
375    /// f(x, y) = x \vee y.
376    /// $$
377    ///
378    /// # Worst-case complexity
379    /// $T(n) = O(n)$
380    ///
381    /// $M(n) = O(n)$
382    ///
383    /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
384    /// other.significant_bits())`.
385    ///
386    /// # Examples
387    /// ```
388    /// use malachite_base::num::arithmetic::traits::Pow;
389    /// use malachite_base::num::basic::traits::One;
390    /// use malachite_nz::natural::Natural;
391    ///
392    /// assert_eq!(&Natural::from(123u32) | &Natural::from(456u32), 507);
393    /// assert_eq!(
394    ///     &Natural::from(10u32).pow(12) | &(Natural::from(10u32).pow(12) - Natural::ONE),
395    ///     1000000004095u64
396    /// );
397    /// ```
398    fn bitor(self, other: &Natural) -> Natural {
399        match (self, other) {
400            (x, &Natural(Small(y))) => x.or_limb_ref(y),
401            (&Natural(Small(x)), y) => y.or_limb_ref(x),
402            (Natural(Large(xs)), Natural(Large(ys))) => Natural(Large(limbs_or(xs, ys))),
403        }
404    }
405}
406
407impl BitOrAssign<Natural> for Natural {
408    /// Bitwise-ors a [`Natural`] with another [`Natural`] in place, taking the [`Natural`] on the
409    /// right-hand side by value.
410    ///
411    /// # Worst-case complexity
412    /// $T(n) = O(n)$
413    ///
414    /// $M(n) = O(1)$
415    ///
416    /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.significant_bits(),
417    /// other.significant_bits())`.
418    ///
419    /// # Examples
420    /// ```
421    /// use malachite_base::num::basic::traits::Zero;
422    /// use malachite_nz::natural::Natural;
423    ///
424    /// let mut x = Natural::ZERO;
425    /// x |= Natural::from(0x0000000fu32);
426    /// x |= Natural::from(0x00000f00u32);
427    /// x |= Natural::from(0x000f_0000u32);
428    /// x |= Natural::from(0x0f000000u32);
429    /// assert_eq!(x, 0x0f0f_0f0f);
430    /// ```
431    fn bitor_assign(&mut self, mut other: Natural) {
432        match (&mut *self, &mut other) {
433            (_, Natural(Small(y))) => self.or_assign_limb(*y),
434            (Natural(Small(x)), _) => *self = other.or_limb(*x),
435            (Natural(Large(xs)), Natural(Large(ys))) => {
436                if limbs_or_in_place_either(xs, ys) {
437                    swap(xs, ys);
438                }
439            }
440        }
441    }
442}
443
444impl<'a> BitOrAssign<&'a Natural> for Natural {
445    /// Bitwise-ors a [`Natural`] with another [`Natural`] in place, taking the [`Natural`] on the
446    /// right-hand side by reference.
447    ///
448    /// # Worst-case complexity
449    /// $T(n) = O(n)$
450    ///
451    /// $M(n) = O(n)$
452    ///
453    /// where $T$ is time, $M$ is additional memory, and $n$ is `other.significant_bits()`.
454    ///
455    /// # Examples
456    /// ```
457    /// use malachite_base::num::basic::traits::Zero;
458    /// use malachite_nz::natural::Natural;
459    ///
460    /// let mut x = Natural::ZERO;
461    /// x |= &Natural::from(0x0000000fu32);
462    /// x |= &Natural::from(0x00000f00u32);
463    /// x |= &Natural::from(0x000f_0000u32);
464    /// x |= &Natural::from(0x0f000000u32);
465    /// assert_eq!(x, 0x0f0f_0f0f);
466    /// ```
467    fn bitor_assign(&mut self, other: &'a Natural) {
468        match (&mut *self, other) {
469            (_, Natural(Small(y))) => self.or_assign_limb(*y),
470            (Natural(Small(x)), _) => *self = other.or_limb_ref(*x),
471            (Natural(Large(xs)), Natural(Large(ys))) => {
472                limbs_or_in_place_left(xs, ys);
473            }
474        }
475    }
476}