【unitrix】 4.7 库数字取反(not.rs)

发布于:2025-06-27 ⋅ 阅读:(17) ⋅ 点赞:(0)

一、源码

这段代码是用Rust语言实现的一个库,主要功能是对数字进行位取反操作(按位NOT运算)。

/*库数字取反
 * 编制人: $ource
 * 修改版次:0版完成版
 * 本版次创建时间: 2025年6月25日
 * 最后修改时间: 无
 * 待完善问题:无
 */
use core::ops::Not;
use crate::number::{Z0, P1, N1, B0, B1, NonNegOne, NonZero, Var, PrimitiveInt};

// ==================== 位取反运算实现 ====================

// 基础类型实现
impl Not for Z0 {  // !0 = -1
    type Output = N1;
    fn not(self) -> Self::Output { N1 }
}

impl Not for P1 {  // !1 = -2 (二进制表示为 B0<N1>)
    type Output = B0<N1>;
    fn not(self) -> Self::Output { B0::new() }
}

impl Not for N1 {  // !(-1) = 0
    type Output = Z0;
    fn not(self) -> Self::Output { Z0 }
}

// 递归类型实现
impl<Other: NonZero + NonNegOne + Not> Not for B0<Other> {  // !B0<T> = B1<!T>
    type Output = B1<Other::Output>;
    fn not(self) -> Self::Output { B1::new() }
}

impl<Other: NonZero + Not> Not for B1<Other> {  // !B1<T> = B0<!T>
    type Output = B0<Other::Output>;
    fn not(self) -> Self::Output { B0::new() }
}

// 特殊处理
impl Not for B0<N1> {  // !(-2) = 1 特例
    type Output = P1;
    fn not(self) -> Self::Output { P1 }
}

/* 注意:
1. 小数类型未实现取反,因为小数部分取反会产生无限尾部1
2. 浮点类型不支持位取反操作(无实际意义)
*/

// 变量类型取反
impl<T: PrimitiveInt + Not> Not for Var<T> {  // !Var<T> = Var<!T>
    type Output = Var<<T as Not>::Output>;
    #[inline(always)]
    fn not(self) -> Self::Output { Var(!self.0) }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_basic_not() {
        assert_eq!(!Z0, N1);
        assert_eq!(!P1, B0::<N1>::new());
        assert_eq!(!N1, Z0);
    }
    
    #[test]
    fn test_recursive_not() {
        let b0n1 = B0::<N1>::new();
        assert_eq!(!b0n1, P1); // 特殊处理
        
        let b1z0 = B1::<P1>::new();
        assert_eq!(!b1z0, B0::<B0<N1>>::new());
    }
    
    #[test]
    fn test_var_not() {
        let var = Var(42i32);
        let res = !var;
        assert_eq!(res.0, !42i32);
    }
}

二、代码分析

  1. 导入依赖

use core::ops::Not;
use crate::number::{Z0, P1, N1, B0, B1, NonNegOne, NonZero, Var, PrimitiveInt};

导入了Rust核心库中的Not trait(用于实现取反操作)和自定义的数字类型。
2. 基础类型实现


impl Not for Z0 {  // !0 = -1
    type Output = N1;
    fn not(self) -> Self::Output { N1 }
}

impl Not for P1 {  // !1 = -2 (二进制表示为 B0<N1>)
    type Output = B0<N1>;
    fn not(self) -> Self::Output { B0::new() }
}

impl Not for N1 {  // !(-1) = 0
    type Output = Z0;
    fn not(self) -> Self::Output { Z0 }
}

这部分为基本数字类型实现了Not trait:

  • Z0(零):取反结果为N1(负一)

  • P1(正一):取反结果为B0(负二)

  • N1(负一):取反结果为Z0(零)

  1. 递归类型实现

impl<Other: NonZero + NonNegOne + Not> Not for B0<Other> {  // !B0<T> = B1<!T>
    type Output = B1<Other::Output>;
    fn not(self) -> Self::Output { B1::new() }
}

impl<Other: NonZero + Not> Not for B1<Other> {  // !B1<T> = B0<!T>
    type Output = B0<Other::Output>;
    fn not(self) -> Self::Output { B0::new() }
}

这部分为递归数字类型实现了Not trait:

  • B0(二进制0前缀的数):取反结果为B1<!T>

  • B1(二进制1前缀的数):取反结果为B0<!T>

  1. 特殊处理

impl Not for B0<N1> {  // !(-2) = 1 特例
    type Output = P1;
    fn not(self) -> Self::Output { P1 }
}

对B0(即-2)做了特殊处理,直接返回P1(1),因为这是递归实现的边界情况。
5. 变量类型实现


impl<T: PrimitiveInt + Not> Not for Var<T> {  // !Var<T> = Var<!T>
    type Output = Var<<T as Not>::Output>;
    #[inline(always)]
    fn not(self) -> Self::Output { Var(!self.0) }
}

为Var类型实现了Not trait,它包装了一个原始整数类型,直接调用该类型的取反操作。
6. 测试模块


#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_basic_not() {
        assert_eq!(!Z0, N1);
        assert_eq!(!P1, B0::<N1>::new());
        assert_eq!(!N1, Z0);
    }
    
    #[test]
    fn test_recursive_not() {
        let b0n1 = B0::<N1>::new();
        assert_eq!(!b0n1, P1); // 特殊处理
        
        let b1z0 = B1::<P1>::new();
        assert_eq!(!b1z0, B0::<B0<N1>>::new());
    }
    
    #[test]
    fn test_var_not() {
        let var = Var(42i32);
        let res = !var;
        assert_eq!(res.0, !42i32);
    }
}

测试模块包含三个测试:

  • 测试基本类型的取反操作

  • 测试递归类型的取反操作

  • 测试变量类型的取反操作

三、总结

这段代码实现了一个类型安全的数字取反操作库,特点包括:

  • 支持基本数字类型(零、正一、负一)的取反

  • 支持递归定义的二进制数的取反

  • 对特定边界情况做了特殊处理

  • 提供了变量类型的取反支持

  • 通过泛型和trait实现了类型安全的操作

这个库是类型级数学计算的一部分,使用了Rust的类型系统来保证数字操作的安全性。


网站公告

今日签到

点亮在社区的每一天
去签到