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
| /// It represents index of parent if positive, otherwise size of tree. | |
| #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | |
| struct UnionFindIndex(isize); | |
| impl UnionFindIndex { | |
| fn new(index: usize) -> Self { | |
| debug_assert!(index < (isize::MAX as usize)); | |
| Self(index as isize) | |
| } |
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; | |
| fn sieve(max: usize) -> (Vec<usize>, HashMap<usize, usize>) { | |
| let mut found_primes = vec![]; | |
| let mut found_least_factor = HashMap::with_capacity(max + 1); | |
| for n in 2..=max { | |
| let factor = *found_least_factor.entry(n).or_insert_with(|| { | |
| found_primes.push(n); | |
| 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
| let alpha_num_pat = RegExp::pat() | |
| .with_alphabetic() | |
| .with_numeric(); | |
| let email_user_chars = RegExp::new().one_of(RegExp::pat().add( | |
| "!#$%&'*+/=?^_`{|}~-" | |
| ).with_alphabetic().with_numeric()); | |
| let email_num_chars = RegExp::new().or( | |
| RegExp::new().text("25").one_of(RegExp::pat().add('0'..='5')), | |
| RegExp::new().or( | |
| RegExp::new() |
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::num::NonZeroUsize; | |
| /// Calculates the lowest common ancestor on given graph. | |
| #[derive(Debug)] | |
| pub struct LowestCommonAncestor { | |
| parents_by_step: Vec<Vec<Option<NonZeroUsize>>>, | |
| distances: Vec<usize>, | |
| } | |
| impl LowestCommonAncestor { |
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
| /// Calculates the inverse of square root quickly. | |
| fn inv_sqrt(x: f64) -> f64 { | |
| // x must be positive. | |
| assert!(x.is_sign_positive()); | |
| // mu := 0.045 | |
| // log(x) ≒ x + mu | |
| // log(a) := log(1 / √x) = - 1/2 log(x) | |
| // 1/2^52 (a_man + 2^52 * a_exp) - 1023 + mu | |
| // = - 1/2 (1/2^52 (x_man + 2^52 * x_exp) - 1023 + mu) |
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::{env, fs, io::Write, path::PathBuf}; | |
| const ICON_MAGIC: &[u8] = b"icon"; | |
| fn next_partition(buf: &[u8]) -> Option<usize> { | |
| buf.windows(ICON_MAGIC.len()) | |
| .position(|win| win == ICON_MAGIC) | |
| } | |
| fn hoge() -> Result<(), Box<dyn std::error::Error + 'static>> { |
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::cmp::Ordering; | |
| pub fn binary_search(max: usize, mut f: impl FnMut(usize) -> Ordering) -> Result<usize, usize> { | |
| let mut left = 0; | |
| let mut right = max; | |
| while left < right { | |
| let mid = left + (right - left) / 2; | |
| match f(mid) { | |
| Ordering::Less => left = mid + 1, | |
| Ordering::Greater => right = mid - 1, |
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
| /// 必要な環境変数名を可変長引数に渡すと, 環境変数ごとに対応した値が入ったオブジェクトを返す. | |
| /// | |
| /// 対応した環境変数が定義されていない場合は `defaults` に設定した値へとフォールバックする. | |
| /// | |
| /// # 例外 | |
| /// | |
| /// 対応した環境変数が定義されておらず `defaults` にも存在しない場合は例外を送出する. | |
| /// | |
| /// # 使用例 | |
| /// |
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
| pub fn divisors(with: u64) -> Vec<u64> { | |
| assert_ne!(with, 0, "cannot find divisors with 0"); | |
| let at_most = ((with as f64).sqrt() * 2.0).floor() as usize; | |
| let mut divs = Vec::with_capacity(at_most); | |
| for i in (1..) | |
| .filter(|&i| with % i == 0) | |
| .take_while(|&i| i * i <= with) | |
| { | |
| divs.push(i); | |
| if i * i != with { |
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
| type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) ? true : false; | |
| type Curried<F> = F extends (...args: infer A) => infer R | |
| ? Equal<F, () => R> extends true | |
| ? () => R | |
| : (A extends [infer F, ...infer S] | |
| ? (arg: F) => Curried<(...rest: S) => R> | |
| : R | |
| ) | |
| : never; |