Skip to content

Instantly share code, notes, and snippets.

@m1el
m1el / utf8.rs
Created December 18, 2023 20:15
fn utf8_len(start: u8) -> usize {
match start {
0b0000_0000..=0b0111_1111 => 1,
0b1100_0000..=0b1101_1111 => 2,
0b1110_0000..=0b1110_1111 => 3,
0b1111_0000..=0b1111_0111 => 4,
_ => 0,
}
}
enum Utf8Error {
// Implements the cool-lex algorithm to generate (n,k)-combinations
// @article{Ruskey:2009fk,
// Author = {Frank Ruskey and Aaron Williams},
// Doi = {10.1016/j.disc.2007.11.048},
// Journal = {Discrete Mathematics},
// Month = {September},
// Number = {17},
// Pages = {5305-5320},
// Title = {The coolest way to generate combinations},
// Url = {http://www.sciencedirect.com/science/article/pii/S0012365X07009570},
// https://godbolt.org/z/KoMfsEszq
// https://godbolt.org/z/18PKTE6Ej
// https://godbolt.org/z/vGs9Eeoo5
// Base on Hacker's Delight section 2.3 THE 16 BINARY LOGICAL OPERATIONS
#![feature(portable_simd)]
extern crate core;
#[repr(u8)]
#![feature(portable_simd)]
/// A multiplier constant which shifts each bit of a byte into the lowest
/// bit of the corresponding byte in u64. Constant calculation:
/// `(0..8).fold(0, |acc, x| acc | (1 << ((8 + 1) * x)))`
const PACK_BYTES: u64 = 0x8040201008040201;
/// Retain only the lowest bit in each byte of u64, making it a valid [bool; 8].
/// Constant calculation: `(0..8).fold(0, |acc, x| acc | (1 << (8 * x)))`
const BOOL_MASK: u64 = 0x0101010101010101;
FOCAL: AYRIE,LANTS,KEMBO,VIFDA,FOCAL
CIGAR: ANOLE,TRAYS,HAMBA,FOCAL,CIGAR
NAVAL: AYRIE,COLTS,POUND,REBUT,NAVAL
REBUT: ALOES,DRICE,KEMPY,VANTS,REBUT
DWARF: ANOLE,SATYR,CRUMP,HUMPH,DWARF
SISSY: REALO,DUCTS,KNISH,HUMPH,SISSY
HUMPH: ARISE,COULD,FUNGO,YMPES,HUMPH
BLUSH: ATONE,DRIPS,SUSHI,FOCAL,BLUSH
KARMA: ANOLE,TRAYS,HIJAB,AWAKE,KARMA
BENCH: ALOES,DIRGE,THEFT,AWAKE,BENCH
@m1el
m1el / interrupt.rs
Last active November 3, 2021 15:46
#![feature(naked_functions)]
#![no_std]
#![no_main]
use esp8266_hal::prelude::*;
use esp8266_hal::interrupt::{enable_interrupt, disable_interrupt, InterruptType};
use esp8266_hal::gpio::InterruptMode;
use esp8266_hal::target::{self, Peripherals};
use core::fmt::Write;
use core::sync::atomic::{AtomicUsize, Ordering};
#![feature(platform_intrinsics)]
#![feature(portable_simd)]
use core_simd::*;
extern "platform-intrinsic" {
fn simd_cast<T,U>(x: T) -> U;
}
fn cast_16_32<const LANES: usize>(x: Simd<u16, LANES>) -> Simd<u32, LANES>
where LaneCount<LANES>: SupportedLaneCount
@m1el
m1el / ord_reverse.py
Last active May 29, 2021 20:24
Python wrapper for reverse ordering of a value.
class Reverse(object):
'''
Wrapper for reverse ordering of a value.
Usage:
array = [1, 2, 3, 4]
array.sort(key=Reverse)
# [4, 3, 2, 1]
from collections import namedtuple
/// Permutes the `slice` into the next permutation, where the set of all
/// permutations is ordered lexicographically.
/// Returns `true` if there is a next permutation.
///
/// # Example
/// ```rust
/// let mut values: Vec<usize> = (0..3).collect();
/// while next_permutation(&mut values) {
/// println!("current permutation: {:?}", values);
/// }
@m1el
m1el / sdist.plt
Last active March 28, 2021 17:05
sdutils
set term svg
set output 'sdist_under.svg'
set xrange [-4:4]
set yrange [0:0.45]
set samples 1000
plot \
x < 1 ? exp(-x*x/2)/sqrt(6.283185307179586) : 1/0 with filledcurves x1 lc rgb "grey", \
exp(-x*x/2)/sqrt(6.283185307179586) lw 2 lc rgb "black"
set output 'sdist_inside.svg'