pub trait Signed: IntBase + Neg<Output = Self> {
type Unsigned: Unsigned<Signed = Self>;
// Required methods
fn unsigned(self) -> Self::Unsigned;
fn abs(self) -> Self;
fn abs_diff(self, other: Self) -> Self::Unsigned;
fn is_negative(self) -> bool;
fn is_positive(self) -> bool;
fn signum(self) -> Self;
// Provided method
fn extgcd(self, other: Self) -> ExtendedGcd<Self> { ... }
}
Expand description
Trait for signed integer operations.
Required Associated Types§
Required Methods§
fn unsigned(self) -> Self::Unsigned
fn abs(self) -> Self
fn abs_diff(self, other: Self) -> Self::Unsigned
fn is_negative(self) -> bool
fn is_positive(self) -> bool
fn signum(self) -> Self
Provided Methods§
Sourcefn extgcd(self, other: Self) -> ExtendedGcd<Self>
fn extgcd(self, other: Self) -> ExtendedGcd<Self>
Examples found in repository?
crates/competitive/src/num/integer.rs (line 96)
90 fn mod_inv(self, modulo: Self) -> Self {
91 assert!(
92 !self.is_zero(),
93 "attempt to inverse zero with modulo {}",
94 modulo
95 );
96 let extgcd = self.signed().extgcd(modulo.signed());
97 assert!(
98 extgcd.g.is_one(),
99 "there is no inverse {} modulo {}",
100 self,
101 modulo
102 );
103 extgcd.x.rem_euclid(modulo.signed()).unsigned()
104 }
More examples
crates/competitive/src/math/linear_diophantine.rs (line 139)
133pub fn solve_linear_diophantine<T>(a: T, b: T, c: T) -> Option<LinearDiophantineSolution<T>>
134where
135 T: Signed,
136{
137 assert!(!a.is_zero(), "a must be non-zero");
138 assert!(!b.is_zero(), "b must be non-zero");
139 let ExtendedGcd { g, x: x0, y: y0 } = a.extgcd(b);
140 let g = g.signed();
141 let a = a / g;
142 let b = b / g;
143 if c.is_zero() {
144 return Some(LinearDiophantineSolution {
145 x: Linear::new(b, T::zero()),
146 y: Linear::new(-a, T::zero()),
147 k_range: (T::minimum(), T::maximum()),
148 });
149 }
150 if !(c % g).is_zero() {
151 return None;
152 }
153 let c = c / g;
154 let x = Linear::new(b, x0 * c);
155 let y = Linear::new(-a, y0 * c);
156 Some(LinearDiophantineSolution {
157 x,
158 y,
159 k_range: (T::minimum(), T::maximum()),
160 })
161}
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.