heh
Diffstat (limited to 'src/util.rs')
| -rw-r--r-- | src/util.rs | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/util.rs b/src/util.rs index 04e0415..bb4309f 100644 --- a/src/util.rs +++ b/src/util.rs @@ -223,6 +223,14 @@ impl Dir { x => unreachable!("{}", x as char), } } + pub fn add_1(self) -> Self { + use Dir::*; + unsafe { std::mem::transmute((self as u8 + 1) % 4) } + } + pub fn sub_1(self) -> Self { + use Dir::*; + unsafe { std::mem::transmute(((self as u8).wrapping_sub(1)) % 4) } + } pub fn turdl(self) -> u8 { match self { Self::N => b'U', @@ -1958,11 +1966,15 @@ pub fn countmap<T: Hash + Eq + Ord + Copy>( pub trait MapWith<T: Copy> { fn map_w<U>(self, f: impl FnMut(T) -> U) -> impl Iterator<Item = (T, U)>; + fn fmap_w<U>(self, f: impl FnMut(T) -> Option<U>) -> impl Iterator<Item = (T, U)>; } impl<T: Copy, I: Iterator<Item = T>> MapWith<T> for I { fn map_w<U>(self, mut f: impl FnMut(T) -> U) -> impl Iterator<Item = (T, U)> { self.map(move |x| (x, f(x))) } + fn fmap_w<U>(self, mut f: impl FnMut(T) -> Option<U>) -> impl Iterator<Item = (T, U)> { + self.flat_map(move |x| Some(x).zip(f(x))) + } } pub fn md5(x: &[u8]) -> [u8; 16] { @@ -2026,6 +2038,14 @@ impl<const N: usize, const M: usize> GridFind for [[u8; N]; M] { (i % N, i / N) } } +impl GridFind for &[&[u8]] { + fn find(self, c: u8) -> (usize, usize) { + self.iter() + .zip(0..) + .find_map(|(x, y)| x.iter().position(|&x| x == c).map(|x| (x, y))) + .unwrap() + } +} pub trait TwoWayMapCollect<K, V>: Iterator { fn collect_twm(self) -> HashMap<(K, K), V>; |