-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
a97ae56 Add DoublePowwUsize (Christian Lewe) 85a8104 Add NonZeroPow2Usize (Christian Lewe) 2d122bc Use NonZeroUsize (Christian Lewe) adc9408 Add UnsignedDecimal (Christian Lewe) a519bd0 Add false | true shorthands (Christian Lewe) b49cc42 Named: Add OIH shorthands (Christian Lewe) 51e3f76 Switch Vec<A> to Arc<[A]> (Christian Lewe) f5640d2 Compile inner single expression (Christian Lewe) b39ad29 Grammar: Make rules atomic (Christian Lewe) 1a6a9e2 Test: Print compressed program (Christian Lewe) d50ea74 Fix boolean match statement (Christian Lewe) 4f5983b Fix inferred bound of list literals (Christian Lewe) Pull request description: Fixes and quality-of-live improvements that came up during the other PRs. I created a separate PR so we can merge these fixes faster. ACKs for top commit: apoelstra: ACK a97ae56 Tree-SHA512: c2c78af5467c2d54e71fa15238b21c5919074643775ea0354c5cf203f67fc87765dbe3bf4d20426c47efe23715f1b372928242584011b8910f0f202dab1eb4e4
- Loading branch information
Showing
6 changed files
with
291 additions
and
85 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/// Implementation for newtypes that wrap a number `u8`, `u16`, ... | ||
/// such that the number has some property. | ||
/// The newtype needs to have a constructor `Self::new(inner) -> Option<Self>`. | ||
macro_rules! checked_num { | ||
( | ||
$wrapper: ident, | ||
$inner: ty, | ||
$description: expr | ||
) => { | ||
impl $wrapper { | ||
/// Access the value as a primitive type. | ||
pub const fn get(&self) -> usize { | ||
self.0 | ||
} | ||
} | ||
|
||
impl std::fmt::Display for $wrapper { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}", self.0) | ||
} | ||
} | ||
|
||
impl std::fmt::Debug for $wrapper { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
std::fmt::Display::fmt(self, f) | ||
} | ||
} | ||
|
||
impl std::str::FromStr for $wrapper { | ||
type Err = String; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
let n = s.parse::<$inner>().map_err(|e| e.to_string())?; | ||
Self::new(n).ok_or(format!("{s} is not {}", $description)) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
/// An integer that is known to be a power of two with nonzero exponent. | ||
/// | ||
/// The integer is equal to 2^n for some n > 0. | ||
/// | ||
/// The integer is strictly greater than 1. | ||
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)] | ||
pub struct NonZeroPow2Usize(usize); | ||
|
||
impl NonZeroPow2Usize { | ||
/// Smallest power of two with nonzero exponent. | ||
// FIXME `std::option::Option::<T>::unwrap` is not yet stable as a const fn | ||
// pub const TWO: Self = Self::new(2).unwrap(); | ||
pub const TWO: Self = Self(2); | ||
|
||
/// Create a power of two with nonzero exponent. | ||
pub const fn new(n: usize) -> Option<Self> { | ||
if n.is_power_of_two() && 1 < n { | ||
Some(Self(n)) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
/// Create the smallest power of two with nonzero exponent greater equal `n`. | ||
pub const fn next(n: usize) -> Self { | ||
if n < 2 { | ||
Self::TWO | ||
} else { | ||
// FIXME `std::option::Option::<T>::unwrap` is not yet stable as a const fn | ||
// Self::new(n.next_power_of_two()).unwrap() | ||
Self(n.next_power_of_two()) | ||
} | ||
} | ||
|
||
/// Return the binary logarithm of the value. | ||
/// | ||
/// The integer is equal to 2^n. Return n. | ||
pub const fn log2(self) -> u32 { | ||
self.0.trailing_zeros() | ||
} | ||
} | ||
|
||
checked_num!(NonZeroPow2Usize, usize, "a power of two greater than 1"); | ||
|
||
/// An integer that is known to be a power _of a power_ of two. | ||
/// | ||
/// The integer is equal to 2^(2^n) for some n ≥ 0. | ||
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)] | ||
pub struct DoublePow2Usize(usize); | ||
|
||
checked_num!(DoublePow2Usize, usize, "a double power of two"); | ||
|
||
impl DoublePow2Usize { | ||
/// Create a double power of two. | ||
pub const fn new(n: usize) -> Option<Self> { | ||
if n.is_power_of_two() && n.trailing_zeros().is_power_of_two() { | ||
Some(Self(n)) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
/// Return the binary logarithm _of the binary logarithm_ of the value. | ||
/// | ||
/// The integer is equal to 2^(2^n). Return n. | ||
pub const fn log2_log2(self) -> u32 { | ||
self.0.trailing_zeros().trailing_zeros() | ||
} | ||
} |
Oops, something went wrong.