Skip to content

Instantly share code, notes, and snippets.

@chenshuo
chenshuo / typoglycemia.cc
Created November 5, 2012 18:42
Typoglycemia
// answer to http://blog.zhaojie.me/2012/11/how-to-generate-typoglycemia-text.html
#include <algorithm>
#include <iostream>
#include <string>
#include <assert.h>
#include <ctype.h>
using std::string;
void randomizeWord(char* str, int len)
@chenshuo
chenshuo / query_freq.cc
Created November 3, 2012 22:59
Sort queries by their frequencies.
// answer to http://weibo.com/1915548291/z2UtyzuvQ
// see also http://www.cnblogs.com/baiyanhuang/archive/2012/11/11/2764914.html
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/noncopyable.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
#include <fstream>
#include <iostream>
@chenshuo
chenshuo / gist:3731612
Created September 16, 2012 08:39
char and wchar_t in template
#include <iostream>
template<typename CharT>
const CharT* ToString(const char* str, const wchar_t* wstr);
template<>
const char* ToString<char>(const char* str, const wchar_t* wstr)
{
return str;
}
@chenshuo
chenshuo / lower_bound.cc
Created July 6, 2012 09:02
find expired timer entries with lower_bound()
#include <map>
#include <set>
#include <stdio.h>
typedef std::pair<int64_t, uint32_t> Entry;
typedef std::set<Entry> TimerList;
TimerList timers;
void test(int64_t now)
@chenshuo
chenshuo / download.py
Created June 4, 2012 15:55
trivial file download
#!/usr/bin/python
import socket, os, hashlib, time
md5 = hashlib.md5()
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('server_address', 2021))
start = time.time()
length = 0
out = open('/tmp/download', 'wb')
@chenshuo
chenshuo / sort.cc
Created May 13, 2012 02:45
Sort 1GB integers
#include <algorithm>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
double getTime()
{
struct timeval tv;
gettimeofday(&tv, NULL);
@chenshuo
chenshuo / grep.rb
Created March 29, 2012 06:28
Grep in files
DEBUG = false
if ARGV.size < 2
puts "Usage: #$0 pattern file_pattern"
puts "Example: #$0 '\\d+' '*.rb'"
exit
end
dirname = File.dirname(ARGV[1])
file_pattern = File.basename(ARGV[1])
@chenshuo
chenshuo / unique.cc
Created March 29, 2012 03:25
Remove continuous spaces, answers gist.github.com/2227226
#include <algorithm>
#include <string.h>
struct AreBothSpaces
{
bool operator()(char x, char y) const
{
return x == ' ' && y == ' ';
}
};
@chenshuo
chenshuo / hashed_list.h
Created March 28, 2012 03:32
hashed list with O(1) push_back()/push_front()/remove() operations. answers gist.github.com/2216546
#include <assert.h>
#include <list>
#include <boost/unordered_map.hpp>
#include <boost/noncopyable.hpp>
template<typename T>
class HashedList : boost::noncopyable
{
typedef typename std::list<T>::iterator ListIterator;
typedef typename std::list<T>::reverse_iterator ReverseListIterator;
@chenshuo
chenshuo / nginx.conf
Created March 3, 2012 17:01
nginx conf using http echo module for '/hello'
#user nobody;
worker_processes 4;
events {
worker_connections 10240;
}
http {
include mime.types;
default_type application/octet-stream;