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

【typenum】 24 去除尾部零的特性(private.rs片段)

一、源码

这段代码实现了一个去除尾部零的特性(trait),专门用于处理倒序存储的无符号整数。

  1. 定义及别名
/// Gets rid of all zeros until it hits a one.
// ONLY IMPLEMENT FOR INVERTED NUMBERS!
pub trait TrimTrailingZeros {type Output;fn trim_trailing_zeros(self) -> Self::Output;
}
pub type TrimTrailingZerosOut<A> = <A as TrimTrailingZeros>::Output;
  1. 实现
impl TrimTrailingZeros for InvertedUTerm {type Output = InvertedUTerm;#[inline]fn trim_trailing_zeros(self) -> Self::Output {InvertedUTerm}
}impl<IU: InvertedUnsigned> TrimTrailingZeros for InvertedUInt<IU, B1> {type Output = Self;#[inline]fn trim_trailing_zeros(self) -> Self::Output {self}
}impl<IU: InvertedUnsigned> TrimTrailingZeros for InvertedUInt<IU, B0>
whereIU: TrimTrailingZeros,
{type Output = <IU as TrimTrailingZeros>::Output;#[inline]fn trim_trailing_zeros(self) -> Self::Output {self.msb.trim_trailing_zeros()}
}

二、核心概念

TrimTrailingZeros 特性用于移除二进制数末尾的零,直到遇到第一个1为止。

示例:

  • 10100(倒序存储)→ 去除尾部零 → 101

  • 100 → 1

  • 000 → 0(完全去除)

三、特性定义


pub trait TrimTrailingZeros {type Output;  // 关联类型:修剪后的输出类型fn trim_trailing_zeros(self) -> Self::Output;
}

四、三种实现情况

  1. 基础情况:空项(数字0)

impl TrimTrailingZeros for InvertedUTerm {type Output = InvertedUTerm;  // 0修剪后还是0fn trim_trailing_zeros(self) -> Self::Output {InvertedUTerm  // 直接返回空项}
}

处理数字0的情况,没有零需要去除。
2. 遇到1:停止修剪


impl<IU: InvertedUnsigned> TrimTrailingZeros for InvertedUInt<IU, B1> {type Output = Self;  // 输出类型不变fn trim_trailing_zeros(self) -> Self::Output {self  // 直接返回自身,停止修剪}
}

当遇到最低位是1时,说明已经到达有效数字的末尾,停止修剪。
3. 遇到0:继续递归修剪


impl<IU: InvertedUnsigned> TrimTrailingZeros for InvertedUInt<IU, B0>
whereIU: TrimTrailingZeros,  // 要求内部类型也能修剪
{type Output = <IU as TrimTrailingZeros>::Output;  // 使用内部的输出类型fn trim_trailing_zeros(self) -> Self::Output {self.msb.trim_trailing_zeros()  // 递归修剪更高位}
}

当最低位是0时,丢弃这个零,继续修剪更高位。

五、工作流程示例

假设有倒序数字 100(传统二进制 001 = 1):


// 类型:InvertedUInt<InvertedUInt<InvertedUInt<InvertedUTerm, B1>, B0>, B0>
// 修剪过程:
1. 最外层是B0 → 丢弃,递归修剪内部
2. 内部是B0 → 丢弃,递归修剪内部  
3. 内部是B1 → 停止修剪,返回自身
// 最终结果:InvertedUInt<InvertedUTerm, B1>(即数字1)

六、设计特点

  • 递归处理:通过类型递归逐步去除尾部零

  • 编译时操作:所有修剪在编译期完成

  • 类型级编程:使用关联类型保持类型安全

  • 条件实现:使用where子句约束类型能力

这种设计常用于需要精确控制数值表示的场景,如二进制协议处理或数值优化算法。

http://www.xdnf.cn/news/18507.html

相关文章:

  • MERGE 语句在 Delta Lake 中的原子更新原理
  • nodejs 集成mongodb实现增删改查
  • Kubernetes相关问题集(四)
  • 什么是正态分布
  • B.30.01.1-Java并发编程及电商场景应用
  • Socket 编程预备
  • 软件测试从入门到精通:通用知识点+APP专项实战
  • 使用Screenpipe+本地大模型实现私人助手Agent
  • 某电器5G智慧工厂网络建设全解析
  • Linux学习:信号的保存
  • TypeReference 泛型的使用场景及具体使用流程
  • GEO优化服务商:AI时代数字经济的新引擎——解码行业发展与技术创新实践
  • 【Spring Boot】集成Redis超详细指南 Redis在Spring Boot中的应用场景
  • kubernetes-dashboard使用http不登录
  • 【卷积神经网络详解与实例】1——计算机中的图像原理
  • 卓伊凡的开源战略与PHP-SG16加密技术深度解析-sg加密技术详解-卓伊凡
  • pixijs基础学习
  • pyecharts可视化图表-map:从入门到精通
  • 【手撕JAVA多线程:2.线程安全】 2.1.JVM层面的线程安全保证
  • C++算法·进制转换
  • DeepSeek V3.1深度解析:一个模型两种思维,迈向Agent时代的第一步!
  • 并查集详解
  • 基于Python的农作物病虫害防治网站 Python+Django+Vue.js
  • 说说你对Integer缓存的理解?
  • 文献阅读笔记【物理信息机器学习】:Physics-informed machine learning
  • 【秋招笔试】2025.08.23美团研发岗秋招笔试题
  • SpringBoot applicationContext.getBeansOfType获取某一接口所有实现类,应用于策略模式
  • 深入理解Java虚拟机:JVM高级特性与最佳实践(第3版)第五章整理
  • 墨刀原型设计工具操作使用指南及实践操作
  • 玩转Vue3高级特性:Teleport、Suspense与自定义渲染