Skip to content

Commit

Permalink
feat(397): Add min and max value helpers for builtin types (#398)
Browse files Browse the repository at this point in the history
Fixes #397.

Adding these helpers for computing multidimensional index bounds.
Any unbounded or infinite dimensions need to be made finite by computing
the min or max value for the type of said dimension.

For example
```
a < 5 and b < 6
```
needs to become
```
a >= MIN and a < 5 and b >= MIN and b < 5
```
  • Loading branch information
joshua-spacetime authored Oct 10, 2023
1 parent 1ce99bd commit c1fd28a
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions crates/sats/src/algebraic_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,46 @@ impl AlgebraicType {
pub fn from_value(value: &AlgebraicValue) -> Result<Self, ValueDeserializeError> {
Self::deserialize(ValueDeserializer::from_ref(value))
}

#[inline]
/// Given an AlgebraicType, returns the min value for that type.
pub fn min_value(&self) -> Option<AlgebraicValue> {
match *self {
Self::I8 => Some(i8::MIN.into()),
Self::U8 => Some(u8::MIN.into()),
Self::I16 => Some(i16::MIN.into()),
Self::U16 => Some(u16::MIN.into()),
Self::I32 => Some(i32::MIN.into()),
Self::U32 => Some(u32::MIN.into()),
Self::I64 => Some(i64::MIN.into()),
Self::U64 => Some(u64::MIN.into()),
Self::I128 => Some(i128::MIN.into()),
Self::U128 => Some(u128::MIN.into()),
Self::F32 => Some(f32::MIN.into()),
Self::F64 => Some(f64::MIN.into()),
_ => None,
}
}

#[inline]
/// Given an AlgebraicType, returns the max value for that type.
pub fn max_value(&self) -> Option<AlgebraicValue> {
match *self {
Self::I8 => Some(i8::MAX.into()),
Self::U8 => Some(u8::MAX.into()),
Self::I16 => Some(i16::MAX.into()),
Self::U16 => Some(u16::MAX.into()),
Self::I32 => Some(i32::MAX.into()),
Self::U32 => Some(u32::MAX.into()),
Self::I64 => Some(i64::MAX.into()),
Self::U64 => Some(u64::MAX.into()),
Self::I128 => Some(i128::MAX.into()),
Self::U128 => Some(u128::MAX.into()),
Self::F32 => Some(f32::MAX.into()),
Self::F64 => Some(f64::MAX.into()),
_ => None,
}
}
}

#[cfg(test)]
Expand Down

0 comments on commit c1fd28a

Please sign in to comment.