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

【unitrix】 3.7 类型级加一计算(Add1.rs)

一、源码

这段代码实现了一个类型级别的加法操作(Add1 trait),允许在编译时对数字类型进行加一操作。它使用了类型系统来表示数字(如 Z0、P1、N1 等),并定义了它们的加一行为。

//! 加一操作特质实现 / Increment operation trait implementation
//!
//! 说明:
//!     1. Z0、P1,、N1 + 1,常规计算
//!     2. B0<H> + 1,该位B1,无进位,原高位是N1时要规范格式,即H=N1时要特化,此时源码为B0<N1>
//!     3. B1<H> + 1,该位B0,有进位,当H+1 = Z0时要规范格式,即H=N1时要特化,此时源码为B1<N1>,不是简化格式use crate::number::{NonNegOne, NonZero, Primitive, Var, B0, B1, N1, P1, Z0};
/// 加一特质 / Increment trait
/// 
/// 为类型系统提供加一操作的计算能力
/// Provides increment operation capability for type system
pub trait Add1 {/// 加一后的输出类型 / Output type after incrementtype Output;fn add1(self) -> Self::Output;
}// ========== 基础类型实现 / Basic Type Implementations ==========/// Z0 (0) 加一实现 / Increment for Z0 (0)
/// 
/// 0 + 1 = 1 (B1<Z0>)
impl Add1 for Z0 {type Output = P1;  //P1替换B1<Z0>#[inline(always)]fn add1(self) -> Self::Output{P1::new()}
}/// P1 (1) 加一实现 / Increment for P1 (+1)
/// 
/// 1 + 1 = 2 (B0<P1>)
impl Add1 for P1 {type Output = B0<P1>;#[inline(always)]fn add1(self) -> Self::Output{B0::new()}
}/// N1 (-1) 加一实现 / Increment for N1 (-1)
/// 
/// -1 + 1 = 0 (Z0)
impl Add1 for N1 {type Output = Z0;#[inline(always)]fn add1(self) -> Self::Output{Z0::new()}
}// ========== 递归类型实现 / Recursive Type Implementations ==========/// B0<H> 加一实现 / Increment for B0<H>
/// 
/// 直接加一无需进位 / Direct increment without carry
/// ...0 + 1 = ...1 / ...0 + 1 = ...1
impl<H:NonZero + NonNegOne> Add1 for B0<H>{type Output = B1<H>;#[inline(always)]fn add1(self) -> Self::Output{B1::new()}
}/// B1<H> 加一实现 / Increment for B1<H>
/// 
/// 处理进位情况 / Handles carry case
/// 0...1 + 1 = 0...(高位进位) / ...1 + 1 = ...0(with carry)
impl<H:NonZero + NonNegOne + Add1<Output: NonZero>> Add1 for B1<H>{//P1替代B1<Z0>后,H不可能为Z0type Output = B0<H::Output>;#[inline(always)]fn add1(self) -> Self::Output{B0::new()}
}// 待Float加法完善后考虑其加一实现
/* impl<Mantissa, Exponent> Add1 for Float<Mantissa, Exponent> {type Output = <Float<Mantissa, Exponent> as Add<P1>>::out;#[inline(always)]fn add1(self) -> Self::Output{Float::new()}
} */
// ========== 特化实现 ==========
/// B0<N1> (-2) 加一特化实现 / Specialized increment for B0<N1> (-2)
impl Add1 for B0<N1> {type Output = N1;#[inline(always)]fn add1(self) -> Self::Output{N1::new()}
}// B1<N1> (-1) 加一特化实现,本身不允许B1<N1>出现,其结果也是不规范的格式,目前取消
/* impl Add1 for B1<N1> {type Output = Z0;
} *//// Val<T> 加一实现 / Increment for Val<T>
/// Val<T>
impl<T:Primitive + From<P1>> Add1 for Var<T> {type Output = Self;#[inline(always)]fn add1(self) -> Self::Output{Self(self.0 + T::from(P1))}
}

二、源码分析

  1. Add1 Trait 定义
pub trait Add1 {type Output;fn add1(self) -> Self::Output;
}
  • 作用:定义了一个类型级别的加一操作。

  • Output:表示加一后的结果类型。

  • add1(self):执行加一操作的方法。

  1. 基本数字类型的加一实现
    (1) Z0(零)加一
impl Add1 for Z0 {type Output = P1;  // 0 + 1 = 1fn add1(self) -> P1 { P1::new() }
}
  • Z0 表示 0,加一后变为 P1(正一)。

(2) P1(正一)加一

impl Add1 for P1 {type Output = B0<P1>;  // 1 + 1 = 2 (二进制 10)fn add1(self) -> B0<P1> { B0::new() }
}
  • P1 表示 1,加一后变为 B0(二进制 10,即十进制 2)。

(3) N1(负一)加一

impl Add1 for N1 {type Output = Z0;  // -1 + 1 = 0fn add1(self) -> Z0 { Z0::new() }
}
  • N1 表示 -1,加一后变为 Z0(0)。
  1. 二进制数字类型的加一实现
    (1) B0(最低位 0)加一
impl<H: NonZero + NonNegOne> Add1 for B0<H> {type Output = B1<H>;  // ...0 + 1 = ...1fn add1(self) -> B1<H> { B1::new() }
}
  • B0 表示一个二进制数,最低位是 0,高位是 H(非零)。

  • 加一后最低位变为 1,即 B1。

(2) B1(最低位 1)加一

impl<H: NonZero + NonNegOne + Add1<Output: NonZero>> Add1 for B1<H> {type Output = B0<H::Output>;  // ...1 + 1 = ...0 (进位)fn add1(self) -> B0<H::Output> { B0::new() }
}
  • B1 表示一个二进制数,最低位是 1,高位是 H(非零)。

  • 加一后最低位变为 0,并进位(H + 1),即 B0<H::Output>。

  1. 特化实现
    (1) B0(-2)加一
impl Add1 for B0<N1> {type Output = N1;  // -2 + 1 = -1fn add1(self) -> N1 { N1::new() }
}
  • B0 表示二进制 …0(高位是 N1,即 -1),实际表示 -2。

  • 加一后变为 N1(-1)。

(2) B1(-1)加一(已注释)

/* impl Add1 for B1<N1> {type Output = Z0;  // -1 + 1 = 0
} */
  • B1 表示二进制 …1(高位是 N1,即 -1),实际表示 -1。

  • 加一后应为 Z0(0),但代码中注释掉,为了避免使用非标准格式。

  1. 运行时数值类型 Var 的加一
impl<T: Primitive + From<P1>> Add1 for Var<T> {type Output = Self;fn add1(self) -> Self { Self(self.0 + T::from(P1)) }
}
  • Var 是一个运行时数值包装类型(如 Var)。

  • 加一操作直接调用 + 1(P1 转换为 T 类型后相加)。

  1. 关键点
  • 类型级计算:所有操作在编译期完成,无运行时开销。

  • 二进制表示:

    • B0 表示 …0(H * 2)。

    • B1 表示 …1(H * 2 + 1)。

  • 进位处理:

    • B1 + 1 会进位(H + 1)。
  • 特化情况:

    • B0 和 B1 用于处理负数。

三、总结

这段代码实现了一个类型安全的加法系统,支持:

  • 基本数字(Z0、P1、N1)。

  • 二进制数字(B0、B1)。

  • 运行时数值(Var)。

适用于需要编译期计算的场景,如静态数组大小计算、类型安全的单位系统等。

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

相关文章:

  • AI大模型学习之基础数学:微积分-AI大模型的数学引擎
  • Spring Boot的自动装配和自动配置
  • MySQL 数据库操作完整指南
  • ubuntu24.4 + ros2 jazzy 安装gazebo
  • vue3+arcgisAPI4案例:智慧林业资源监测分析平台(附源码下载)
  • C++ 的设计模式
  • 跟着AI学习C# Day29
  • 网站并发访问量达到1万以上需要注意哪些事项
  • 单点登录(SSO)系统
  • 海伯森3D闪测传感器,工业检测领域的高精度利器
  • JavaEE:使用JMeter进行接口并发测试
  • 跨平台轻量级RTSP服务:重构内网超低延迟直播体验
  • 区块链是什么
  • AI与SEO关键词协同进化
  • 【StarRocks系列】查询语句执行全流程
  • 1. 常见K线组合
  • 2025年渗透测试面试题总结-2025年HW(护网面试) 03(题目+回答)
  • 智慧医院核心引擎:IBMS 系统守护医疗环境高效与安全​
  • 内容搜索软件AnyTXT.Searcher忘记文件名也能搜,全文检索 1 秒定位文件
  • Python中字符串常用的操作方法
  • mysql导入大sql(比如10GB的sql文件)
  • 开源AI智能名片链动2+1模式S2B2C商城小程序:破解微商代理模式困局的数字化创新路径
  • MySQL存储引擎与架构
  • 在AI普及的大环境下神经网络在新能源汽车热管理系统中的应用简介
  • CLion开发Qt桌面程序_git的简单使用_小团体
  • opencv try-catch
  • day38-Django(4)
  • AI大模型学习之基础数学:高斯分布-AI大模型概率统计的基石
  • 自定义Django rest_framework中response的示例
  • 一个小BUG引发的对Mybatis-Plus的模糊查询的思考