malachite_nz/integer/
mod.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::natural::Natural;
10use alloc::string::String;
11use malachite_base::named::Named;
12use malachite_base::num::basic::traits::{NegativeOne, One, Two, Zero};
13
14/// An integer.
15///
16/// Any `Integer` whose absolute value is small enough to fit into a [`Limb`](crate#limbs) is
17/// represented inline. Only integers outside this range incur the costs of heap-allocation.
18#[derive(Clone, Hash, Eq, PartialEq)]
19#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
20#[cfg_attr(
21    feature = "serde",
22    serde(try_from = "SerdeInteger", into = "SerdeInteger")
23)]
24pub struct Integer {
25    // whether the `Integer` is non-negative
26    pub(crate) sign: bool,
27    pub(crate) abs: Natural,
28}
29
30#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
31#[cfg_attr(feature = "serde", serde(transparent))]
32pub(crate) struct SerdeInteger(String);
33
34impl Integer {
35    // Returns true iff `self` is valid.
36    //
37    // To be valid, its absolute value must be valid, and if the absolute value is zero, the sign
38    // must be `true`. All `Integer`s must be valid.
39    #[cfg(feature = "test_build")]
40    pub fn is_valid(&self) -> bool {
41        self.abs.is_valid() && (self.sign || self.abs != 0)
42    }
43}
44
45macro_rules! integer_zero {
46    () => {
47        Integer {
48            sign: true,
49            abs: Natural::ZERO,
50        }
51    };
52}
53
54macro_rules! integer_one {
55    () => {
56        Integer {
57            sign: true,
58            abs: Natural::ONE,
59        }
60    };
61}
62
63macro_rules! integer_two {
64    () => {
65        Integer {
66            sign: true,
67            abs: Natural::TWO,
68        }
69    };
70}
71
72macro_rules! integer_negative_one {
73    () => {
74        Integer {
75            sign: false,
76            abs: Natural::ONE,
77        }
78    };
79}
80
81/// The constant 0.
82impl Zero for Integer {
83    const ZERO: Self = integer_zero!();
84}
85
86/// The constant 1.
87impl One for Integer {
88    const ONE: Self = integer_one!();
89}
90
91/// The constant 2.
92impl Two for Integer {
93    const TWO: Self = integer_two!();
94}
95
96/// The constant -1.
97impl NegativeOne for Integer {
98    const NEGATIVE_ONE: Self = integer_negative_one!();
99}
100
101impl Default for Integer {
102    /// The default value of an [`Integer`], 0.
103    fn default() -> Self {
104        Self::ZERO
105    }
106}
107
108// Implements `Named` for `Integer`.
109impl_named!(Integer);
110
111/// Traits for arithmetic.
112pub mod arithmetic;
113/// Traits for comparing [`Integer`]s for equality or order.
114pub mod comparison;
115/// Traits for converting to and from [`Integer`]s, converting to and from strings, and extracting
116/// digits.
117pub mod conversion;
118/// Iterators that generate [`Integer`]s without repetition.
119pub mod exhaustive;
120/// Traits for logic and bit manipulation.
121pub mod logic;
122#[cfg(feature = "random")]
123/// Iterators that generate [`Integer`]s randomly.
124pub mod random;