Skip to content

Instantly share code, notes, and snippets.

View zhangyuchi's full-sized avatar

Terrell Franzman zhangyuchi

View GitHub Profile
@zhangyuchi
zhangyuchi / insert_sort.cc
Last active August 29, 2015 14:26
insert sort
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> v{2, 4, 2, 0, 5, 10, 7, 3, 7, 1};
// insertion sort
for (auto i = v.begin(); i != v.end(); ++i) {
std::rotate(std::upper_bound(v.begin(), i, *i), i, i+1);
}
@zhangyuchi
zhangyuchi / getline.sh
Created November 23, 2015 07:05
取文件的第N行
#!/bin/bash
awk -v a=$1 'NR==a {print $1}' $2
@zhangyuchi
zhangyuchi / variadic_template.cc
Created January 25, 2016 10:13
c++11 variadic templates exsample
#include <iostream>
using namespace std;
void f()
{
cout<<"0 args"<<endl;
}
void f(int a)
{
cout<<"1 args"<<endl;
@zhangyuchi
zhangyuchi / int_seqs.cc
Created February 6, 2016 08:39
var template and tuple
#include <iostream>
#include <tuple>
#include <utility>
template<typename Func, typename Tup, std::size_t... index>
decltype(auto) invoke_helper(Func&& func, Tup&& tup, std::index_sequence<index...>)
{
return func(std::get<index>(std::forward<Tup>(tup))...);
}
@zhangyuchi
zhangyuchi / unblock_push.go
Created April 14, 2016 07:17
unblock wait
quitWriteSignal := make(bool chan)
select {
case quitWriteSignal <- true:
default:
}
@zhangyuchi
zhangyuchi / iprule.sh
Created April 14, 2016 07:26
ip rule command
#Route packets with source addresses from 192.203.80/24 according to routing table inr.ruhep:
ip ru add from 192.203.80.0/24 table inr.ruhep prio 220
#Translate packet source address 193.233.7.83 into 192.203.80.144 and route it according to table #1 (actually, it is inr.ruhep):
ip ru add from 193.233.7.83 nat 192.203.80.144 table 1 prio 320
#Delete the unused default rule:
ip ru del prio 32767
kuznet@amber:~ $ ip ru ls
@zhangyuchi
zhangyuchi / reg_reflect.go
Last active April 28, 2016 09:36
注册本地回调函数时如何应用reflect
func (server *Server) Register(rcvr interface{}) error {
s := new(service)
s.typ = reflect.TypeOf(rcvr)
s.rcvr = reflect.ValueOf(rcvr)
sname := reflect.Indirect(s.rcvr).Type().Name()
if sname == "" {
s := "rpc.Register: no service name for type " + s.typ.String()
log.Print(s)
return errors.New(s)
}
@zhangyuchi
zhangyuchi / 0_reuse_code.js
Created May 3, 2016 07:44
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@zhangyuchi
zhangyuchi / gorc.sh
Created May 3, 2016 10:49
gorc file
if [ ! -z "$ZSH_NAME" ]; then
setopt shwordsplit
fi
export GOROOT=/opt/go
export GOPATH=$HOME/gowork:$HOME/Projects/qchat/utility:$HOME/Projects/qchat:$HOME/Opensource/etcd:$HOME/Opensource/docker
PATH=$PATH:$GOROOT/bin
for GOP in ${GOPATH//:/ }; do
func getActiveUserInzone(appid uint16, zone uint16) ([]*session.UserSession, error) {
var resp []*session.UserSession
keyname := fmt.Sprintf(userStatSetKey, appid, zone)
userids, err := SessionPool.Call(getSessionAddr(keyname)).SMEMBERS(keyname)
if err != nil {
return resp, errors.New(keyname + ":" + err.Error())
}