当前位置: 首页 > news >正文

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

一、源码

这段代码是用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 = -1type 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) = 0type 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 = -1type 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) = 0type 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的类型系统来保证数字操作的安全性。

http://www.lryc.cn/news/575436.html

相关文章:

  • 组织策略性陪伴顾问
  • Java后端中的并发控制:从锁机制到无锁编程的实现
  • 供应链管理:主要生产计划类型及其相关信息
  • Vue-14-前端框架Vue之应用基础嵌套路由和路由传参
  • 【fish-speech】新模型openaudio-s1-mini尝鲜
  • 【windows处理技巧】如何缩小PDF
  • R语言机器学习算法实战系列(二十六)基于tidymodels的XGBoost二分类器全流程实战
  • 【力扣 困难 C】32. 最长有效括号
  • 数据结构进阶 - 第三章 栈与队列
  • ubuntu 下cursor的安装
  • 深入了解 AWS EventBridge
  • 多相机人脸扫描设备如何助力高效打造数字教育孪生体?
  • Java设计模式->责任链模式的介绍
  • 书籍在行列都排好序的矩阵中找数(8)0626
  • 【音视频】Ubuntu下配置ffmpeg库
  • Maven Javadoc 插件使用详解
  • 【WebSocket】学习总结
  • Python 数据分析与可视化 Day 8 - Pandas 高级操作技巧
  • MFC制作动态波形图( ChartCtrl)
  • Python(一)实现一个爬取微信小程序数据的爬虫+工程化初步实践
  • 【FR801xH】Ubuntu24.04搭建富芮坤FR801xH系列开发环境教程
  • 美团京东Clean Architecture实战
  • 【算法深练】栈特性的解题密码:LIFO规则在题型中的灵活运用
  • 生僻字处理工具类
  • 价格敏感带争夺战!澳洲电商双雄增长密码,3大本土护城河尚存
  • C# 项目使用obfuscar混淆
  • 华曦达港股IPO递表,AI Home生态构建智能生活新蓝图
  • 2025 Java开发生态全景图:云原生、AI与性能优化的技术融合
  • 广州华锐互动:技术与创意双驱动的 VR 先锋​
  • 基于组件的软件开发(CBSD)与面向服务的架构(SOA)的对比分析