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
| fn gcd(a: i32, b: i32) -> i32 { | |
| if b == 0 { | |
| return a; | |
| } | |
| gcd(b, a % b) | |
| } |
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 std::iter::{from_fn, Peekable}; | |
| fn take_while_eq<I>(it: &mut Peekable<I>, eq: I::Item) -> impl Iterator<Item = I::Item> + '_ | |
| where | |
| I: Iterator, | |
| I::Item: Eq, | |
| { | |
| from_fn(move || it.next_if_eq(&eq)) | |
| } |
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
| fn into_iter(list: Option<Box<ListNode>>) -> LLIntoIter { | |
| LLIntoIter(list) | |
| } | |
| struct LLIntoIter(Option<Box<ListNode>>); | |
| impl Iterator for LLIntoIter { | |
| type Item = Box<ListNode>; | |
| fn next(&mut self) -> Option<Box<ListNode>> { | |
| let mut node = self.0.take()?; |
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
| fn interleave<I>(its: Vec<I>) -> Interleave<I> { | |
| Interleave { its: its.into_iter().collect() } | |
| } | |
| struct Interleave<I> { | |
| its: std::collections::VecDeque<I>, | |
| } | |
| impl<I: Iterator> Iterator for Interleave<I> { | |
| type Item = I::Item; |
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 std::collections::HashMap; | |
| use std::hash; | |
| struct DisjointSet<T> { | |
| elements: HashMap<T, usize>, | |
| parents: HashMap<usize, usize>, | |
| } | |
| impl<T: Eq + hash::Hash> DisjointSet<T> { | |
| pub fn new() -> Self { |
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
| // https://stackoverflow.com/a/39647997/1163020 | |
| // probably unsuitable for production. | |
| use std::hash; | |
| #[derive(Debug, Copy, Clone, PartialOrd)] | |
| pub struct NormalF64(f64); | |
| impl NormalF64 { | |
| pub fn new(n: f64) -> Option<Self> { | |
| if n == 0.0 { return Some(Self(0.0)); } | |
| n.is_normal().then(|| Self(n)) |
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
| // Adapted from Python's | |
| struct Permutations<T> { | |
| pool: Vec<T>, | |
| indices: Vec<usize>, | |
| cycles: Vec<usize>, | |
| r: usize, | |
| once: bool, | |
| } | |
| impl<T: Copy> Iterator for Permutations<T> { |
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
| -- Tables which are logically "deleted" with a `deleted` column, | |
| -- but still respect foreign key constraints.. of a sort. | |
| CREATE SCHEMA test; | |
| CREATE TABLE test.foo_full ( | |
| id BIGSERIAL PRIMARY KEY, | |
| name TEXT NOT NULL, | |
| deleted BOOL DEFAULT false, | |
| UNIQUE (id, deleted), |
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
| struct SplitBy<'a, T> { | |
| slice: &'a [T], | |
| value: T, | |
| last_idx: usize, | |
| } | |
| impl<'a, T: PartialEq> Iterator for SplitBy<'a, T> { | |
| type Item = &'a [T]; | |
| fn next(&mut self) -> Option<Self::Item> { |
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
| const ZERO: u8 = '0' as u8; | |
| #[derive(Debug)] | |
| pub struct BigInt(Vec<u8>); | |
| impl BigInt { | |
| pub fn new(mut num: u64) -> Self { // not a good constructor, just for testing | |
| let mut digits = Vec::new(); | |
| while num != 0 { | |
| digits.push((num % 10) as u8); | |
| num /= 10; |
NewerOlder