Skip to content

Instantly share code, notes, and snippets.

@mo-xiaoming
mo-xiaoming / iterator_4_user_defined_type.rs
Created October 31, 2022 14:56
Rust iterator for user defined type
#![allow(dead_code, unused)]
#[derive(Debug, Default, Clone, std::hash::Hash, PartialEq, Eq, PartialOrd, Ord)]
struct Pair {
first: i32,
second: i32,
}
impl Pair {
fn new(first: i32, second: i32) -> Self {
@mo-xiaoming
mo-xiaoming / adjacent_difference.rs
Created October 31, 2022 14:54
c++ std::adjacent_difference in rust
// https://godbolt.org/z/93EE958vG
struct AdjacentDifference<I, F, R>
where
I: Iterator,
F: Fn(I::Item, I::Item) -> R,
R: From<I::Item>
{
prev: Option<I::Item>,
it: I,
@mo-xiaoming
mo-xiaoming / wildcard_match.rs
Created October 29, 2022 02:33
wildcards matching
//! https://www.drdobbs.com/architecture-and-design/matching-wildcards-an-algorithm/210200888
#![allow(dead_code)]
fn wildcard_match(pattern_text: &str, plain_text: &str) -> bool {
struct AfterWildcard {
plain_idx: usize,
pattern_idx: usize,
}
// it is None if there are no prev wildcard

microsoft/WSL#4698 (comment)

A simple fix for me was to set MTU to 1350 (same as VPN interface): sudo ifconfig eth0 mtu 1350

Even SSH connections are more stable now.

This solved my issues as well. I am using checkpoint VPN.

to get the MTU value of your VPN run below command for checkpoint, for example :

@mo-xiaoming
mo-xiaoming / show-color.sh
Created July 19, 2021 03:54
show all color on terminal
echo -e "\033[0mNC (No color)"
echo -e "\033[1;37mWHITE\t\033[0;30mBLACK"
echo -e "\033[0;34mBLUE\t\033[1;34mLIGHT_BLUE"
echo -e "\033[0;32mGREEN\t\033[1;32mLIGHT_GREEN"
echo -e "\033[0;36mCYAN\t\033[1;36mLIGHT_CYAN"
echo -e "\033[0;31mRED\t\033[1;31mLIGHT_RED"
echo -e "\033[0;35mPURPLE\t\033[1;35mLIGHT_PURPLE"
echo -e "\033[0;33mYELLOW\t\033[1;33mLIGHT_YELLOW"
echo -e "\033[1;30mGRAY\t\033[0;37mLIGHT_GRAY"
@mo-xiaoming
mo-xiaoming / pass_param_costs.cpp
Created February 9, 2020 13:20
move costs and perfect forwarding is the most effecient way to pass paremters
/*
`c` for copy/create, `m` for move
BENCHMARK CpuTime
-------------------------------------
// no alloc
bench_s_ref_short 2.53 ns 1c + 1c *
bench_s_copy_short 2.55 ns 1c + 1c
bench_s_copy_move_short 7.04 ns 1c + 1m . move costs more than mere copy if no alloc
@mo-xiaoming
mo-xiaoming / benchmark_pass_by_ref.cpp
Created February 9, 2020 12:01
Passing arguments by reference is waaaay better than fancy 'new' stuff
/*
auto const f = [inner](ParamType v) -> std::vector<int> {
for (int i = 0; i < inner; ++i)
v.push_back(i);
return ReturnType;
};
for (auto _ : state) {
for (auto n = 0; n < outer; ++n) {
v.clear();
v = f(v);
@mo-xiaoming
mo-xiaoming / cmp_gbm.py
Created February 6, 2020 14:25
Grouping google benchmark result
#!/usr/bin/env python3
"""
Usage: ./a.out --benchmark_format=json | ./cmp_gbm.py
from
bench_fastmod/16/32 176 ns 171 ns 3972393
bench_fastmod/16/128 192 ns 190 ns 3620738
bench_fastmod/16/224 195 ns 192 ns 3614277