Last active
July 28, 2022 07:56
-
-
Save MikuroXina/c611fbef52dcc4ea9db672e3c1d1ed1e to your computer and use it in GitHub Desktop.
Tropical semi-ring definition in Rust.
This file contains hidden or 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
| use num::{One, Zero}; | |
| use std::ops; | |
| #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | |
| pub enum Tropical { | |
| Value(u64), | |
| Infty, | |
| } | |
| impl Zero for Tropical { | |
| fn zero() -> Self { | |
| Self::Infty | |
| } | |
| fn is_zero(&self) -> bool { | |
| matches!(self, Tropical::Infty) | |
| } | |
| } | |
| impl One for Tropical { | |
| fn one() -> Self { | |
| Self::Value(0) | |
| } | |
| } | |
| impl ops::Add for Tropical { | |
| type Output = Self; | |
| fn add(self, rhs: Self) -> Self::Output { | |
| std::cmp::min(self, rhs) | |
| } | |
| } | |
| impl ops::AddAssign for Tropical { | |
| fn add_assign(&mut self, rhs: Self) { | |
| *self = *self + rhs; | |
| } | |
| } | |
| impl ops::Mul for Tropical { | |
| type Output = Self; | |
| fn mul(self, rhs: Self) -> Self::Output { | |
| match (self, rhs) { | |
| (Tropical::Value(l), Tropical::Value(r)) => Tropical::Value(l + r), | |
| _ => Tropical::Infty, | |
| } | |
| } | |
| } | |
| impl ops::MulAssign for Tropical { | |
| fn mul_assign(&mut self, rhs: Self) { | |
| *self = *self * rhs; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment