utils: add misc and implement CustomBounded
This commit is contained in:
parent
de7ee8f0f6
commit
b7a1f05b1e
25
utils/misc/Cargo.lock
generated
Normal file
25
utils/misc/Cargo.lock
generated
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "autocfg"
|
||||||
|
version = "1.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "misc"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"num-traits",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num-traits"
|
||||||
|
version = "0.2.19"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
||||||
|
dependencies = [
|
||||||
|
"autocfg",
|
||||||
|
]
|
7
utils/misc/Cargo.toml
Normal file
7
utils/misc/Cargo.toml
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
[package]
|
||||||
|
name = "misc"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
num-traits = "0.2.19"
|
95
utils/misc/src/lib.rs
Normal file
95
utils/misc/src/lib.rs
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
use num_traits::Signed;
|
||||||
|
use std::ops::{Add, AddAssign};
|
||||||
|
use std::fmt::{Debug, Display};
|
||||||
|
|
||||||
|
/// Wrapped signed integer with custom upper bound with wrapping of 0s to the upper bound
|
||||||
|
#[derive(Eq, Clone, Copy)]
|
||||||
|
pub struct CustomWrapped<T: Signed + Copy> {
|
||||||
|
pub val: T,
|
||||||
|
pub bound: T,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Signed + Copy> Add<T> for CustomWrapped<T> {
|
||||||
|
type Output = CustomWrapped<T>;
|
||||||
|
fn add(self, rhs: T) -> Self::Output {
|
||||||
|
Self {
|
||||||
|
val: ((self.val + rhs % self.bound) + self.bound) % self.bound,
|
||||||
|
bound: self.bound,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Signed + Copy> Add<T> for &CustomWrapped<T> {
|
||||||
|
type Output = CustomWrapped<T>;
|
||||||
|
fn add(self, rhs: T) -> Self::Output {
|
||||||
|
CustomWrapped {
|
||||||
|
val: ((self.val + rhs % self.bound) + self.bound) % self.bound,
|
||||||
|
bound: self.bound,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Signed + Copy> AddAssign<T> for CustomWrapped<T> {
|
||||||
|
fn add_assign(&mut self, rhs: T) {
|
||||||
|
self.val = ((self.val + rhs % self.bound) + self.bound) % self.bound
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Signed + Copy> CustomWrapped<T> {
|
||||||
|
pub fn new(val: T, bound: T) -> Self {
|
||||||
|
Self { val, bound }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Signed + Copy + PartialEq> PartialEq for CustomWrapped<T> {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
self.val.eq(&other.val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Signed + PartialOrd + Copy> PartialOrd for CustomWrapped<T> {
|
||||||
|
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||||
|
self.val.partial_cmp(&other.val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Signed + Ord + Copy> Ord for CustomWrapped<T> {
|
||||||
|
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||||||
|
self.val.cmp(&other.val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Signed + PartialEq + Copy> PartialEq<T> for CustomWrapped<T> {
|
||||||
|
fn eq(&self, other: &T) -> bool {
|
||||||
|
self.val == *other
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Signed + PartialOrd + Copy> PartialOrd<T> for CustomWrapped<T> {
|
||||||
|
fn partial_cmp(&self, other: &T) -> Option<std::cmp::Ordering> {
|
||||||
|
self.val.partial_cmp(other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Display + Signed + Copy> Display for CustomWrapped<T> where T: Display {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
self.val.fmt(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// impl<T> Into<T> for CustomWrapped<T> {
|
||||||
|
// fn into(self) -> T {
|
||||||
|
// self.val
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn it_works() {
|
||||||
|
let result = add(2, 2);
|
||||||
|
assert_eq!(result, 4);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user