-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
173 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,35 @@ | ||
// #[cfg(feature = "num-bigint")] | ||
// use num_bigint::{BigInt, BigUint, ToBigInt}; | ||
|
||
use crate::constant::Integer; | ||
|
||
pub trait ZigZag { | ||
type Zag; | ||
|
||
fn zigzag(self) -> Self::Zag; | ||
fn unzigzag(self) -> Self::Zag; | ||
} | ||
|
||
// #[cfg(feature = "num-bigint")] | ||
// impl ZigZag for BigInt { | ||
// type Zag = BigUint; | ||
|
||
// fn zigzag(self) -> Self::Zag where { | ||
// if self >= 0.into() { | ||
// self << 1 | ||
// } else { | ||
// let double: BigInt = self << 1; | ||
// -double - <u8 as Into<BigInt>>::into(1) | ||
// } | ||
// .to_biguint() | ||
// .expect("number is positive") | ||
// } | ||
// } | ||
|
||
impl ZigZag for i128 { | ||
type Zag = usize; | ||
|
||
fn zigzag(self) -> Self::Zag where { | ||
let bits = i128::BITS as i128; | ||
let i = self; | ||
((i << 1) ^ (i >> (bits - 1))) as usize | ||
impl ZigZag for &Integer { | ||
type Zag = Integer; | ||
|
||
fn zigzag(self) -> Self::Zag { | ||
if *self >= 0 { | ||
// For non-negative numbers, just multiply by 2 (left shift by 1) | ||
self.clone() << 1 | ||
} else { | ||
// For negative numbers: -(2 * n) - 1 | ||
// First multiply by 2 | ||
let double: Integer = self.clone() << 1; | ||
|
||
// Then negate and subtract 1 | ||
-double - 1 | ||
} | ||
} | ||
} | ||
|
||
// #[cfg(feature = "num-bigint")] | ||
// impl ZigZag for BigUint { | ||
// type Zag = BigInt; | ||
|
||
// fn zigzag(self) -> Self::Zag where { | ||
// let i = self.to_bigint().expect("always possible"); | ||
// (i.clone() >> 1) ^ -(i & <u8 as Into<BigInt>>::into(1)) | ||
// } | ||
// } | ||
|
||
impl ZigZag for usize { | ||
type Zag = i128; | ||
fn unzigzag(self) -> Self::Zag { | ||
let temp: Integer = self.clone() & 1; | ||
|
||
fn zigzag(self) -> Self::Zag where { | ||
((self >> 1) as i128) ^ -((self & 1) as i128) | ||
(self.clone() >> 1) ^ -(temp) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters