Skip to content

Instantly share code, notes, and snippets.

@kevinlynx
kevinlynx / netty-discardserver.scala
Last active December 25, 2015 10:29
scala netty Discard Server sample
package netty.sample
import io.netty.channel.ChannelHandlerContext
import io.netty.channel.ChannelInboundHandlerAdapter
import io.netty.buffer.ByteBuf
import io.netty.bootstrap.ServerBootstrap
import io.netty.channel.ChannelOption
import io.netty.channel.ChannelOption._
import io.netty.channel.ChannelFuture
import io.netty.channel.ChannelInitializer
package squtest
import org.squeryl.PrimitiveTypeMode._
import org.squeryl.Schema
import org.squeryl.annotations.Column
import org.squeryl.SessionFactory
import org.squeryl.Session
import org.squeryl.KeyedEntity
import org.squeryl.adapters.H2Adapter
import java.util.Date
@kevinlynx
kevinlynx / function-literal.scala
Last active December 24, 2015 21:39
function literal in scala
object Test2 {
//Also: def callFn(a: Int, f: (Int) => Int) = {
def callFn(a: Int, f: Int => Int) = {
a + f(a)
}
// 2 parameters
def callFnMult(a: Int, f: (Int, Int) => Int) = {
a + f(a, a)
@kevinlynx
kevinlynx / implicit.scala
Last active December 24, 2015 21:39
implicit parameter
// `s' and `b' are both implicit parameters
def implFn(i: Int)(implicit s: String, b: Int) = {
println(s)
i + b
}
def main(args: Array[String]) {
// otherS will match parameter `s' by type, **not by name**
implicit val otherS = "hello"
// error, two variables with the same type
@kevinlynx
kevinlynx / gen_runjar.bat
Last active December 23, 2015 19:39
create a shell script to run jar specified some jars as class path
echo off
REM
REM Usage: put jars into lib/ext directory, put config file in ext directory
REM gen_runjar.bat
REM Kevin Lynx 09/24/2013
REM
if "%1"=="" goto usage
if not "%1"=="clear" goto dist
del /Q ext\*.*
#include <assert.h>
char find(const char *str) {
int cnt = 1;
char c = *str++;
for ( ; *str; ++str) {
c = cnt == 0 ? *str : c;
cnt = cnt + (*str == c ? 1 : (cnt - 1 < 0 ? -cnt : -1));
}
return cnt > 0 ? c : 0;
#include <assert.h>
typedef unsigned __int64 uint64;
typedef unsigned char byte;
void setBit(byte *data, int pos) {
byte bits[] = {
0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01
};
/* note the position starts from 1, not 0 */
@kevinlynx
kevinlynx / start_match.cpp
Created August 12, 2013 13:24
match a string using `*' operator i.e: *abc, a*bc
// test.cpp : 定义控制台应用程序的入口点。
//
#include <stdio.h>
#include <vector>
#include <string>
#include <assert.h>
struct OpCode {
/* 0: indicate `*' operator, 1: string match */
@kevinlynx
kevinlynx / gist:3413212
Created August 21, 2012 07:47
chat server
class Entity {
public:
enum { CLIENT, SERVER };
Entity(const CGUID &id, int type, const std::string &brief);
// send text to this entity, this function will preprocess `text`
bool Send(const std::string &text);
private:
#include <stdio.h>
#include <string>
#include <map>
#include <vector>
#include <list>
void ds_str_i(void *p) {
char **raw = (char**)p;
char *s = *raw;
size_t len = *(size_t*)(s - sizeof(size_t) * 3);