Skip to content

Instantly share code, notes, and snippets.

@MikuroXina
MikuroXina / tropical.rs
Last active July 28, 2022 07:56
Tropical semi-ring definition in Rust.
use num::{One, Zero};
use std::ops;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Tropical {
Value(u64),
Infty,
}
impl Zero for Tropical {
@MikuroXina
MikuroXina / gen.ts
Last active September 4, 2022 02:45
Utility generator collection.
function* empty<T>(): Generator<T, void> {}
function* once<T>(elem: T): Generator<T, void> {
yield elem;
}
function* onceWith<T>(fn: () => T): Generator<T, void> {
yield fn();
}
@MikuroXina
MikuroXina / reducer.ts
Created August 15, 2022 08:55
The template for creating a custom reducer.
export interface State {
/* Type your state definition. */
}
export const initialState = (): State => ({
/* Type your initial state. */
});
const reducers = {
/* Type your reducer by key of the action name. */
@MikuroXina
MikuroXina / pairs.rs
Created September 9, 2022 12:48
Generate permutations on macro arguments in Rust.
macro_rules! pairs {
($macro:ident; $($items:literal),*) => {
pairs!(@ $macro; $($items),*; $($items),*);
};
(@ $macro:ident; $x:literal; $($ys:literal),*) => {
$(
$macro!($x, $ys);
)*
};
(@ $macro:ident; $x:literal, $($xs:literal),*; $($ys:literal),*) => {
value=(quad quit queue quote)
second_value=(queue queen)
# 先頭から最短マッチした部分を削除
echo ${value#q*e}
# quad quit ue
# 先頭から最長マッチした部分を削除
echo ${value##q*e}
# quad quit
@MikuroXina
MikuroXina / mod_int.rs
Last active October 30, 2022 06:11
An integer modulo 998244353 with Montgomery Multiplication and Numeric Theory Transformation.
use num::{traits::Pow, One, Zero};
use serde::{Deserialize, Serialize};
const R: u64 = 1 << 32;
/// Find `modulo_inv` which satisifes `modulo * modulo_inv ≡ -1 (mod R)`.
const fn find_neg_inv(modulo: u32) -> u32 {
let mut inv_mod = 0u32;
let mut t = 0;
let mut i = 1u32;
@MikuroXina
MikuroXina / crt.rs
Last active November 8, 2022 13:15
Rust functions of Chinese Remainder Theorem.
/// Find values where satisify `a * p + b * q = gcd`.
pub fn ext_gcd(a: u64, b: u64) -> ExtGcd {
let mut s = (0i64, 1i64);
let mut t = (1i64, 0i64);
let mut r = (as_i64(b), as_i64(a));
while r.0 != 0 {
let q = r.1 / r.0;
let f = |mut r: (i64, i64)| {
std::mem::swap(&mut r.0, &mut r.1);
@MikuroXina
MikuroXina / reg_exp.rs
Last active November 30, 2025 07:48
The Brzozowski derivative of Regular Expression with Rust.
/// The regular expression tree.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum RegExp {
/// Declares a variable of the match result.
Var(String, Box<RegExp>),
/// Matching pattern of a chatacter.
Let(char),
/// Or pattern of patterns.
Or(Box<RegExp>, Box<RegExp>),
/// Concatenated pattern of patterns.
@MikuroXina
MikuroXina / endless-error-loop.py
Created December 26, 2022 15:50
Lyrics of "Endless Error Loop" by Neko Hacker feat. ななひら.
print('''<header>
ここをもうちょっとシンプルに変えてっと
え、なにこのエラー・・・
</header>''')
try:
uglyCode()
except:
print('よくあるError')
try:
@MikuroXina
MikuroXina / non_nan_f64.rs
Last active May 11, 2024 05:46
NonNanF64, the floating-point number which will not be NaN in Rust.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct NonNanF64(f64);
impl NonNanF64 {
/// # Safety
///
/// The value must not be `NaN`.
pub const unsafe fn new_unchecked(f: f64) -> Self {
// SAFETY: this is guaranteed to be safe by the caller.
Self(f)