【unitrix】 4.4 類型級整數比較系統(cmp.rs)

一、源碼

這段代碼實現了一個類型級別的整數比較系統,允許在編譯時進行整數比較操作。它定義了一套類型來表示比較結果,并為不同類型的整數實現了比較邏輯。

use core::cmp::Ordering;
use core::default::Default;
use crate::sealed::Sealed;
use crate::number::{Z0, P1, N1, B0, B1, NonZero};
///類型級比較,僅考慮類型級整數比較//-------------基本定義-------------------------------------------------
// 比較結果類型別名
pub type Equal = Z0;   // 相等
pub type Less = N1;    // 小于
pub type Greater = P1; // 大于/// 標記特征(trait),用于比較結果類型:`Greater`、`Equal` 和 `Less`
pub trait Ord: Sealed {/// 將類型轉換為運行時的 `Ordering` 值fn to_ordering() -> Ordering;
}impl Ord for Greater {#[inline]fn to_ordering() -> Ordering {Ordering::Greater  // 返回"大于"的比較結果}
}impl Ord for Less {#[inline]fn to_ordering() -> Ordering {Ordering::Less  // 返回"小于"的比較結果}
}impl Ord for Equal {#[inline]fn to_ordering() -> Ordering {Ordering::Equal  // 返回"等于"的比較結果}
}/// 類型比較運算符,用于比較 `Self` 和 `Rhs` 類型。
/// 類似于 [`core::cmp::Ord::cmp`] 但用于類型比較。
///
/// # 示例
/// ```rust
/// use unitrix::number::{Cmp, Ord, N1, Z0, P1};
/// use std::cmp::Ordering;
///
/// assert_eq!(<Z0 as Cmp<N1>>::Output::to_ordering(), Ordering::Greater);
/// assert_eq!(<Z0 as Cmp<Z0>>::Output::to_ordering(), Ordering::Equal);
/// assert_eq!(<Z0 as Cmp<P1>>::Output::to_ordering(), Ordering::Less);
/// ```
pub trait Cmp<Rhs = Self> {/// 比較結果的類型(只能是 `Greater`、`Less` 或 `Equal` 之一)type Output;#[doc(hidden)]  // 隱藏內部實現fn compare(self, rhs: Rhs) -> Self::Output;
}//---------------------bit 相對獨立,集中保存bit.rs文件---------------------------------//---------------------Special 未來改為錯誤處理,不實現比較---------------------------------//---------------------Z0比較All---------------------------------
/// 0 == 0
impl Cmp<Z0> for Z0 {type Output = Equal;#[inline]fn compare(self, _: Z0) -> Self::Output {Equal::new()}
}/// 0 < 1
impl Cmp<P1> for Z0 {type Output = Less;#[inline]fn compare(self, _: P1) -> Self::Output {Less::new()}
}/// 0 > -1
impl Cmp<N1> for Z0 {type Output = Greater;#[inline]fn compare(self, _: N1) -> Self::Output {Greater::new()}
}/// 0 <==> B0<H>
impl<H: NonZero + Default> Cmp<B0<H>> for Z0
whereZ0: Cmp<H>,
{type Output = <Z0 as Cmp<H>>::Output;#[inline]fn compare(self, _: B0<H>) -> Self::Output {let _h = H::default();<Z0 as Cmp<H>>::compare(self, _h)}
}/// 0 <==> B1<H>
impl<H: NonZero + Default> Cmp<B1<H>> for Z0
whereZ0: Cmp<H>,
{type Output = <Z0 as Cmp<H>>::Output;#[inline]fn compare(self, _: B1<H>) -> Self::Output {let _h = H::default();<Z0 as Cmp<H>>::compare(self, _h)}
}//---------------------P1比較All---------------------------------
//---------------------P1比較All---------------------------------
/// 1 > 0
impl Cmp<Z0> for P1 {type Output = Greater;#[inline]fn compare(self, _: Z0) -> Self::Output {Greater::new()}
}/// 1 == 1
impl Cmp<P1> for P1 {type Output = Equal;#[inline]fn compare(self, _: P1) -> Self::Output {Equal::new()}
}/// 1 > -1
impl Cmp<N1> for P1 {type Output = Greater;#[inline]fn compare(self, _: N1) -> Self::Output {Greater::new()}
}/// P1 <==> B0<H>
impl<H: NonZero + Default> Cmp<B0<H>> for P1
whereZ0: Cmp<H>,
{type Output = <Z0 as Cmp<H>>::Output;#[inline]fn compare(self, _: B0<H>) -> Self::Output {let _h = H::default();<Z0 as Cmp<H>>::compare(Z0::new(), _h)}
}/// P1 <==> B1<H>
impl<H: NonZero + Default> Cmp<B1<H>> for P1
whereZ0: Cmp<H>,
{type Output = <Z0 as Cmp<H>>::Output;#[inline]fn compare(self, _: B1<H>) -> Self::Output {let _h = H::default();<Z0 as Cmp<H>>::compare(Z0::new(), _h)}
}//---------------------N1比較All---------------------------------
/// -1 < 0
impl Cmp<Z0> for N1 {type Output = Less;#[inline]fn compare(self, _: Z0) -> Self::Output {Less::new()}
}/// -1 < 1
impl Cmp<P1> for N1 {type Output = Less;#[inline]fn compare(self, _: P1) -> Self::Output {Less::new()}
}/// -1 == -1
impl Cmp<N1> for N1 {type Output = Equal;#[inline]fn compare(self, _: N1) -> Self::Output {Equal::new()}
}/// N1 <==> B0<H>
impl<H: NonZero + Default> Cmp<B0<H>> for N1
whereZ0: Cmp<H>,
{type Output = <Z0 as Cmp<H>>::Output;#[inline]fn compare(self, _: B0<H>) -> Self::Output {let _h = H::default();<Z0 as Cmp<H>>::compare(Z0::new(), _h)}
}/// N1 <==> B1<H>
impl<H: NonZero + Default> Cmp<B1<H>> for N1
whereZ0: Cmp<H>,
{type Output = <Z0 as Cmp<H>>::Output;#[inline]fn compare(self, _: B1<H>) -> Self::Output {let _h = H::default();<Z0 as Cmp<H>>::compare(Z0::new(), _h)}
}//---------------------B0<H>比較All---------------------------------/// B0<H> vs Z0: 比較H與Z0
impl<H: NonZero + Default> Cmp<Z0> for B0<H>
whereH: Cmp<Z0>,
{type Output = <H as Cmp<Z0>>::Output;#[inline]fn compare(self, _: Z0) -> Self::Output {let _h = H::default();<H as Cmp<Z0>>::compare(_h, Z0::new())}
}/// B0<H> vs P1: 比較H與Z0
impl<H: NonZero + Default> Cmp<P1> for B0<H>
whereH: Cmp<Z0>,
{type Output = <H as Cmp<Z0>>::Output;#[inline]fn compare(self, _: P1) -> Self::Output {let _h = H::default();<H as Cmp<Z0>>::compare(_h, Z0)}
}/// B0<H> vs N1: 比較H與Z0 
impl<H: NonZero + Default> Cmp<N1> for B0<H>
whereH: Cmp<Z0>,
{type Output = <H as Cmp<Z0>>::Output;#[inline]fn compare(self, _: N1) -> Self::Output {let _h = H::default();<H as Cmp<Z0>>::compare(_h, Z0)}
}/// B0<H1> vs B0<H2>: 比較H1與H2
impl<H1: NonZero + Default, H2: NonZero + Default> Cmp<B0<H2>> for B0<H1>
whereH1: Cmp<H2>,
{type Output = <H1 as Cmp<H2>>::Output;#[inline]fn compare(self, _: B0<H2>) -> Self::Output {let _h1 = H1::default();let _h2 = H2::default();<H1 as Cmp<H2>>::compare(_h1, _h2)}
}/// B0<H1> vs B1<H2>: 先比較H1與H2,如果相等則B1>B0
impl<H1: NonZero + Default, H2: NonZero + Default> Cmp<B1<H2>> for B0<H1>
whereH1: Cmp<H2>,<H1 as Cmp<H2>>::Output: IfLess,<<H1 as Cmp<H2>>::Output as IfLess>::Output:Default,
{type Output = <<H1 as Cmp<H2>>::Output as IfLess>::Output;#[inline]fn compare(self, _: B1<H2>) -> Self::Output {Self::Output::default()}
}// 輔助類型用于處理比較結果
pub trait IfLess {type Output;fn less() -> Self::Output;
}impl IfLess for Equal {type Output = Less;fn less() -> Self::Output{Less::new()}
}impl IfLess for Less {type Output = Less;fn less() -> Self::Output{Less::new()}
}impl IfLess for Greater {type Output = Greater;fn less() -> Self::Output{Greater::new()}
}//---------------------B1<H>比較All---------------------------------/// B1<H> vs Z0: 比較H與Z0
impl<H: NonZero + Default> Cmp<Z0> for B1<H>
whereH: Cmp<Z0>,
{type Output = <H as Cmp<Z0>>::Output;#[inline]fn compare(self, _: Z0) -> Self::Output {let _h = H::default();<H as Cmp<Z0>>::compare(_h, Z0)}
}/// B1<H> vs P1: 比較H與Z0
impl<H: NonZero + Default> Cmp<P1> for B1<H>
whereH: Cmp<Z0>,
{type Output = <H as Cmp<Z0>>::Output;#[inline]fn compare(self, _: P1) -> Self::Output {let _h = H::default();<H as Cmp<Z0>>::compare(_h, Z0)}
}/// B1<H> vs N1: 比較H與Z0
impl<H: NonZero + Default> Cmp<N1> for B1<H>
whereH: Cmp<Z0>,
{type Output = <H as Cmp<Z0>>::Output;#[inline]fn compare(self, _: N1) -> Self::Output {let _h = H::default();<H as Cmp<Z0>>::compare(_h, Z0)}
}/// B1<H1> vs B0<H2>: 先比較H1與H2,如果相等則B1>B0
impl<H1: NonZero + Default, H2: NonZero + Default> Cmp<B0<H2>> for B1<H1>
whereH1: Cmp<H2>,<H1 as Cmp<H2>>::Output: IfGreater,<<H1 as Cmp<H2>>::Output as IfGreater>::Output: Default,
{type Output = <<H1 as Cmp<H2>>::Output as IfGreater>::Output;#[inline]fn compare(self, _: B0<H2>) -> Self::Output {Self::Output::default()}
}/// B1<H1> vs B1<H2>: 比較H1與H2
impl<H1: NonZero + Default, H2: NonZero + Default> Cmp<B1<H2>> for B1<H1>
whereH1: Cmp<H2>,
{type Output = <H1 as Cmp<H2>>::Output;#[inline]fn compare(self, _: B1<H2>) -> Self::Output {let _h1 = H1::default();let _h2 = H2::default();<H1 as Cmp<H2>>::compare(_h1, _h2)}
}// 新增輔助trait用于處理B1>B0的情況
pub trait IfGreater {type Output;fn greater() -> Self::Output;
}impl IfGreater for Equal {type Output = Greater;fn greater() -> Self::Output {Greater::new()}
}impl IfGreater for Less {type Output = Less;fn greater() -> Self::Output {Less::new()}
}impl IfGreater for Greater {type Output = Greater;fn greater() -> Self::Output {Greater::new()}
}

二、核心組件

  1. 比較結果類型

    • Equal (Z0): 表示相等

    • Less (N1): 表示小于

    • Greater (P1): 表示大于

  2. 主要特征(trait)

    • Ord: 標記特征,用于比較結果類型,可以將類型轉換為運行時的 Ordering 值

    • Cmp: 類型比較運算符,類似于 core::cmp::Ord::cmp 但用于類型比較

三、實現細節

基本類型比較

代碼為基本類型實現了相互比較:

  • Z0 (零)

  • P1 (正一)

  • N1 (負一)

例如:


// 0 == 0
impl Cmp<Z0> for Z0 {type Output = Equal;// ...
}// 0 < 1
impl Cmp<P1> for Z0 {type Output = Less;// ...
}
二進制數比較

代碼還處理了二進制數類型 B0 和 B1 的比較,其中 H 是高位部分:

  • B0 表示二進制數以0結尾的數

  • B1 表示二進制數以1結尾的數

比較規則:

  1. 當比較 B0 和 B1 時,先比較高位數 H,如果相等則 B1 大于 B0

  2. 其他情況直接比較高位數

輔助特征
  • IfLess: 處理 B0

    與 B1

    比較時的邏輯

  • IfGreater: 處理 B1

    與 B0

    比較時的邏輯

四、使用示例

···rust

use unitrix::number::{Cmp, Ord, N1, Z0, P1};
use std::cmp::Ordering;

assert_eq!(<Z0 as Cmp>::Output::to_ordering(), Ordering::Greater);
assert_eq!(<Z0 as Cmp>::Output::to_ordering(), Ordering::Equal);
assert_eq!(<Z0 as Cmp>::Output::to_ordering(), Ordering::Less);


### 五、總結這段代碼實現了一個編譯時的類型級整數比較系統,通過Rust的類型系統在編譯期完成整數比較操作,避免了運行時開銷。它支持基本整數和二進制表示的數字之間的比較,并提供了將比較結果轉換為運行時 Ordering 值的能力。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/911914.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/911914.shtml
英文地址,請注明出處:http://en.pswp.cn/news/911914.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

2025年滲透測試面試題總結-2025年HW(護網面試) 14(題目+回答)

安全領域各種資源&#xff0c;學習文檔&#xff0c;以及工具分享、前沿信息分享、POC、EXP分享。不定期分享各種好玩的項目及好用的工具&#xff0c;歡迎關注。 目錄 1. SQL注入原理 &#x1f4a5; 2. XXE攻擊&#xff08;XML外部實體注入&#xff09; &#x1f9e9; 3. SQ…

Android開發根據滑動距離標題欄進行漸變

Android開發根據滑動距離標題欄進行漸變 假設滑動控件是NestedScrollView。 先監聽NestedScrollView的滑動距離&#xff1a; nslv_preview_me.setOnScrollChangeListener(object :NestedScrollView.OnScrollChangeListener{override fun onScrollChange(v: NestedScrollView…

高中成績可視化平臺開發筆記

高中成績可視化平臺&#xff08;1&#xff09; 一、項目概述 本系統是一個基于 PyQt5 和 Matplotlib 的高中成績數據可視化分析平臺&#xff0c;旨在幫助教師快速了解學生成績分布、班級對比、學科表現等關鍵指標。平臺支持文科與理科的數據切換&#xff0c;并提供多個維度的圖…

自動化按需導入組件庫的工具rust版本完成開源了

背景 當我為每個Vue項目使用ui組件庫的時候&#xff0c;都會使用按需導入的方式來使用ui組件庫。但是每次按需導入&#xff0c;不可避免的就需要做以下三步。我們以element plus ui組件庫為例。 1. 安裝依賴 第一步&#xff0c;當然是需要安裝依賴。命令如下: pnpm add unp…

Linux內核中TCP分段的核心機制:tcp_fragment函數解析

引言:TCP分段的必要性 在TCP/IP協議棧中,MSS(最大分段大小) 限制了單次傳輸的數據量。當應用層發送的數據超過當前路徑的MSS時,內核必須執行分段操作。tcp_fragment函數正是Linux內核中處理TCP分段的核心機制,它巧妙地在協議合規性、內存安全和性能效率之間取得平衡。 一…

【趙渝強老師】OceanBase OBServer節點的SQL層

OceanBase OBServer節點的SQL層將用戶的SQL請求轉化成對一個或多個Tablet的數據訪問。SQL層會按照以下順序經過一系列組件來處理一個SQL&#xff1a; Parser -->Resolver-->Transformer-->Optimizer-->CodeGenerator-->Executor。視頻講解如下 【趙渝強老師】O…

從“高配”到“普惠”,黑芝麻智能攜手Nullmax打造輔助駕駛主流量產方案

近日&#xff0c;黑芝麻智能攜手Nullmax打造的輔助駕駛主流量產方案正式發布。該方案面向8-15萬元級別主流車型&#xff0c;基于單顆黑芝麻智能武當C1236跨域計算芯片&#xff0c;集成Nullmax全棧自研的軟件技術架構&#xff0c;結合領先的視覺感知算法&#xff0c;打造高性能輔…

信息安全認證體系全解析:從資質證明到行業準入的實踐指南

Hello&#xff01;大家好&#xff0c;小編是一名專注IT領域的資深探索家&#xff0c;大家發現了嗎&#xff1f;現在刷招聘軟件&#xff0c;國企安全崗必標 "CISP 優先"&#xff0c;外企招聘悄悄寫著 "CISSP 加分"—— 這些帶字母的證書到底是啥&#xff1f…

優雅地創建實體類——Builder 鏈式調用

我們來看以下的代碼。改造前構造實體類用重載構造器或用 setter 對變量進行賦值&#xff0c;一旦變量變多則需要對每個變量進行 set 賦值&#xff0c;并且有可能會賦值錯對象。 private static void test() {//1.構造器賦值Task task1 new Task("2023000000009439"…

如何輕松將照片從 iPhone 傳輸到 Android?

從 iPhone 換到 Android 手機后&#xff0c;你肯定不想丟掉珍貴的照片回憶吧&#xff1f;好在&#xff0c;本文分享了 6 種有效的解決方案&#xff0c;教你如何輕松地將照片從 iPhone 傳輸到 Android。 第 1 部分&#xff1a;如何通過 iReaShare Phone Transfer 將照片從 iPhon…

AI編程:[體驗]存量微服務架構下植入WebSocket的“踩坑”與“填坑”

一、核心需求 功能需求&#xff1a;用戶可以通過語音與AI對話&#xff0c;并實現類似ChatGPT的實時交互&#xff08;流式響應&#xff0c;打字機效果&#xff09;技術需求&#xff1a;在現有微服務架構中進行擴展&#xff08;SpringCloud&#xff09; 二、技術盲點 陌生領域 …

uniapp事件onLoad區分大小寫

區分大小寫。不然會不起作用。onLoad方法中的功能均不會被執行。 除了功能邏輯要檢查外。大小寫是要認真檢查的一部分

《打破微前端困局:樣式沖突與資源隔離破局指南》

微前端架構憑借其獨特優勢&#xff0c;正逐漸成為眾多大型項目的首選架構模式。它將一個龐大的前端應用拆解為多個相對獨立的子應用&#xff0c;每個子應用可以獨立開發、部署和維護&#xff0c;極大地提升了開發效率與團隊協作的靈活性。然而&#xff0c;隨著微前端架構的廣泛…

OpenCV——邊緣檢測

邊緣檢測 一、邊緣檢測二、邊緣檢測算子2.1、Sobel算子2.2、Scharr算子2.3、Laplacian算子 三、Canny邊緣檢測3.1、Canny邊緣檢測的步驟3.2、Canny算法的實現 一、邊緣檢測 邊緣是指圖像中像素的灰度值發生劇烈變化的區域&#xff1a; 圖像中的邊緣主要有以下幾種成因&#x…

2506認證資訊|工信部出手整治多品牌充電寶,WMC上海稍遜往年,RED修訂Common Charger,WiFi7 FCC測試

01 — 中國 工信部擬制定移動電源強制性國家標準 該標準將從以下方面全面提升移動電源安全性&#xff1a; 1. 擬在GB 31241、GB 4943.1基礎上&#xff0c;新增或加嚴過充電、針刺等試驗要求。 2. 擬提出影響電池安全的正負極材料、隔膜等關鍵材料要求。 3. 擬規范鋰離子電池…

Linux Regulator 子系統核心邏輯與關鍵問題全解析

Linux Regulator 子系統核心邏輯與關鍵問題全解析 一、什么是 regulator 子系統&#xff1f;核心作用&#xff1f; regulator 子系統是 Linux 內核為板級/SoC 多路可控電源設計的統一電源管理框架。它的主要作用是&#xff1a; 為每一路可控電源&#xff08;Buck、LDO、DCDC …

制造業官網3D應用,讓產品會“說話”

在當今數字化時代&#xff0c;裝備制造業正經歷著前所未有的變革。隨著消費升級和國內經濟的蓬勃發展&#xff0c;中國社會的經濟格局從傳統的“工業經濟”向多元化的“服務經濟”轉型。裝備制造業作為制造業與服務業融合的核心領域&#xff0c;積極探索全新的“服務化”發展模…

SCAU15--氣球狂歡節

15 氣球狂歡節 Time Limit:1000MS Memory Limit:65535K 題型: 編程題 語言: G;GCC 描述&#xff1a; 一個充滿魔法的國度中&#xff0c;存在一場年度的節日&#xff0c;名為“氣球狂歡節”。在這個節日中&#xff0c;有一個傳統的比賽&#xff0c;那就是“氣球挑戰賽”…

python打卡day56@浙大疏錦行

知識點回顧&#xff1a; 假設檢驗基礎知識 原假設與備擇假設P值、統計量、顯著水平、置信區間 白噪聲 白噪聲的定義自相關性檢驗&#xff1a;ACF檢驗和Ljung-Box 檢驗偏自相關性檢驗&#xff1a;PACF檢驗 平穩性 平穩性的定義單位根ADF檢驗: 越小越平穩 季節性檢驗 ACF檢驗序列…

采集文章+原創AI處理+發布網站詳細教程

簡數采集器是新一代的網站文章采集和發布平臺&#xff0c;完全在線配置和使用云采集&#xff0c;功能強大&#xff0c;操作簡單&#xff0c;配置快捷高效。 簡數不僅提供網頁文章采集、數據批量處理、定時采集、定時定量自動發布等基本功能&#xff0c;還集成強大的SEO工具與接…