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
#[derive(Debug, Clone)] | |
pub struct Crc32 { | |
table: [u32; 256], | |
} | |
impl Crc32 { | |
pub const SOURCE: u32 = 0xedb8_8320; | |
pub fn new() -> Self { | |
let mut table = [0u32; 256]; |
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
pub fn algorithm_x( | |
list: &mut DancingLinkedList, | |
stack: &mut Vec<usize>, | |
solutions: &mut Vec<Vec<usize>>, | |
) { | |
if list.is_empty() { | |
solutions.push(stack.clone()); | |
return; | |
} |
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
//! The example implementation of full-scratch LR(0) parser, referenced [Wikipedia](https://en.wikipedia.org/wiki/LR_parser). | |
//! | |
//! # Rules | |
//! | |
//! There are non-terminal terms: Start, Expr and Bool. And there are terminal | |
//! terms: 0, 1, +. * and $ (end of input). | |
//! | |
//! 1. Introduction: `Start :- Expr $` | |
//! 2. Expr from multiplication: `Expr :- Expr * Bool` | |
//! 3. Expr from addition: `Expr :- Expr + Bool` |
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
#[derive(Debug, Clone, PartialEq, Eq, Hash)] | |
struct TrieNode<const L: usize> { | |
children: [Option<Box<TrieNode<L>>>; L], | |
words: usize, | |
} | |
impl<const L: usize> TrieNode<L> { | |
fn new() -> Self { | |
TrieNode { | |
children: std::array::from_fn(|_| None), |
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
#[derive(Debug, Clone, PartialEq, Eq)] | |
pub struct Counter<T: Eq + std::hash::Hash> { | |
counts: std::collections::HashMap<T, usize>, | |
} | |
impl<T, Q> std::ops::Index<&Q> for Counter<T> | |
where | |
T: Eq + std::hash::Hash + std::borrow::Borrow<Q>, | |
Q: Eq + std::hash::Hash + ?Sized, | |
{ |
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
/// Finds `x` where `alpha` ** `x` == `beta`. | |
pub fn discrete_log<const MOD_1: u64, const MOD: u64>(alpha: ModInt<MOD>, beta: ModInt<MOD>) -> Option<u64> { | |
assert_eq!(MOD_1 + 1, MOD); | |
fn step<const MOD_1: u64, const MOD: u64>(alpha: ModInt<MOD>, beta: ModInt<MOD>, x: &mut ModInt<MOD>, a: &mut ModInt<MOD_1>, b: &mut ModInt<MOD_1>) { | |
match x.as_u64() % 3 { | |
0 => { | |
*x = *x * *x; | |
*a = *a * 2.into(); | |
*b = *b * 2.into(); | |
} |
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
/// Group must satisfy: | |
/// ```rs | |
/// fn test<G: Group>(m: G, n: G, l: G) { | |
/// assert_eq!(m.op(&G::id()), m); | |
/// assert_eq!(G::id().op(&m), m); | |
/// assert_eq!(m.op(&n.op(&l)), m.op(&n).op(&l)); | |
/// assert_eq!(m.op(&m.inv()), G::id()); | |
/// assert_eq!(m.inv().op(&m), G::id()); | |
/// } | |
/// ``` |
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
pub fn cum<T: Default + std::ops::AddAssign + Copy>( | |
nums: impl IntoIterator<Item = T>, | |
) -> impl Iterator<Item = T> { | |
nums.into_iter().scan(T::default(), |state, item| { | |
*state += item; | |
Some(*state) | |
}) | |
} | |
pub fn cum2<T: Default + std::ops::AddAssign + Copy, NNS, NS>(nums: NNS) -> Vec<Vec<T>> |
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
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | |
pub struct AltSum { | |
sum: i64, | |
len: usize, | |
} | |
impl AltSum { | |
pub fn empty() -> Self { | |
Self { sum: 0, len: 0 } | |
} |
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
export type Paradoxical<A, R> = (f: Paradoxical<A, R>) => (a: A) => R; | |
export const Y = <A, R>(f: (g: (a: A) => R) => (a: A) => R): ((a: A) => R) => | |
((x: Paradoxical<A, R>) => f((lazy) => x(x)(lazy)))( | |
(x: Paradoxical<A, R>) => f((lazy) => x(x)(lazy)), | |
); |
NewerOlder