Skip to content

Instantly share code, notes, and snippets.

@AnthonyMikh
AnthonyMikh / Cargo.toml
Last active August 29, 2023 21:25
Как написать Fizzbuzz в .rodata
[package]
name = "fizzbuzz"
version = "0.1.0"
edition = "2021"
[features]
use_nightly = []
compare_with_previous_impl = []
enum Expr {
Lit(i32),
Neg(Box<Self>),
Add(Box<Self>, Box<Self>),
}
fn lit(x: i32) -> Expr {
Expr::Lit(x)
}
@AnthonyMikh
AnthonyMikh / typed.cpp
Created August 6, 2022 23:21
Typed versions of C memory managing utilities
#include <cstddef>
#include <cstring>
#include <cstdlib>
// #define TYPED_TIGHT_MALLOC
namespace typed {
template <typename T>
T* memset_n(T *dest, int ch, std::size_t count) {
@AnthonyMikh
AnthonyMikh / lib.rs
Last active May 16, 2022 22:00
Neumann and Moore neighborhood calculation for rectangular grid
#[derive(Clone, Copy)]
pub struct Bounds {
rows: usize,
cols: usize,
}
type Pos = (usize, usize);
impl Bounds {
pub fn around_neumann(
@AnthonyMikh
AnthonyMikh / lib.rs
Created April 9, 2022 00:42
Sorting by borrowed key (advanced variant requires GATs)
#![feature(generic_associated_types)]
pub struct Data {
pub name: String,
pub whatever: u32,
pub bytes: Vec<u8>,
}
trait BorrowedKeyFamily {
type Key<'a>: Ord + 'a;
@AnthonyMikh
AnthonyMikh / main.rs
Created January 13, 2022 22:11
Двоичное дерево со статически ограниченной максимальной глубиной вложенности
use std::cmp::Ordering;
use std::convert::Infallible as Never;
use std::fmt::{self, Debug};
struct Z;
struct S<T>(T);
#[derive(Clone)]
struct Node<T, Next> {
value: T,
@AnthonyMikh
AnthonyMikh / lib.rs
Last active November 16, 2021 20:54
Решение задачи №292 от UniLecs (Расшифровывать строку)
struct Lexer<'a> {
s: &'a str,
}
type Num = usize;
#[derive(Debug)]
enum Lexeme<'a> {
String(&'a str),
Num(Num),
@AnthonyMikh
AnthonyMikh / main.rs
Last active November 5, 2021 16:50
Pretty Debug implementation for heterogrenous list
struct HNil;
struct HCons<H, T> {
head: H,
tail: T,
}
#[cfg(feature = "")]
mod via_debug_vec {
use super::{HCons, HNil};
@AnthonyMikh
AnthonyMikh / example.rs
Last active October 29, 2021 23:09
Illustration of composable parametrized behavior in Rust
trait Provide<Item, Path> {
fn provide(&mut self) -> Item;
}
struct Itself;
struct Head<T>(T);
struct Tail<T>(T);
struct HNil;
struct HCons<H, T>(H, T);
@AnthonyMikh
AnthonyMikh / lib.rs
Created September 28, 2021 14:40
Набросок разделяемой между потоками хэшмапы, поддерживающей поиск по предвычисленному хэшу
use hashbrown::HashMap;
use std::{
borrow::Borrow,
hash::{BuildHasher, Hash, Hasher},
sync::{Mutex, MutexGuard, PoisonError},
};
pub struct LockedHashMap<K, V, S> {
inner: Mutex<HashMap<K, V, S>>,