Skip to content

Instantly share code, notes, and snippets.

View samuelyao314's full-sized avatar

samuelyao314 samuelyao314

View GitHub Profile
#!/bin/bash
###
### my-script — does one thing well
###
### Usage:
### my-script <input> <output>
###
### Options:
### <input> Input file to read.
### <output> Output file to write. Use '-' for stdout.
@samuelyao314
samuelyao314 / snowflake.go
Created July 7, 2020 11:29 — forked from asyncins/snowflake.go
[snowflake-go]
package main
import (
"errors"
"fmt"
"sync"
"time"
)
const (
@samuelyao314
samuelyao314 / next_pow.c
Created November 1, 2018 07:13
[ 一个 32bit 整数,取不小于它的 2 的整数次幂]
static inline bool
is_pow_of_2(uint32_t x) {
return !(x & (x-1));
}
static inline uint32_t
next_pow_of_2(uint32_t x) {
if ( is_pow_of_2(x) )
return x;
x |= x>>1;
@samuelyao314
samuelyao314 / lua_hash.c
Created November 1, 2018 07:12
不错的字符串 hash 函数
// 一个方便的 hash 函数应该散列的比较开,计算速度跟字符串长度关系不大,又不能只计算字符串的开头或末尾。这里的算法是从 Lua 中看来的。
unsigned long hash(const char *name,size_t len)
{
unsigned long h=(unsigned long)len;
size_t step = (len>>5)+1;
for (size_t i=len; i>=step; i-=step)
h = h ^ ((h<<5)+(h>>2)+(unsigned long)name[i-1]);
return h;
}
@samuelyao314
samuelyao314 / double2int.c
Created November 1, 2018 07:09
快速 double 转整型
union luai_Cast { double l_d; long l_l; };
#define lua_number2int(i,d) \
{ volatile union luai_Cast u; u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; }