Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / Einstein.java
Created November 16, 2012 06:14
Einstein's Puzzle
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import JaCoP.constraints.Alldifferent;
import JaCoP.constraints.Distance;
import JaCoP.constraints.XeqC;
import JaCoP.constraints.XeqY;
import JaCoP.constraints.XplusCeqZ;
import JaCoP.core.Domain;
@chenshuo
chenshuo / dance.cc
Created November 17, 2012 17:18
Einstein's Puzzle using Dancing Links
#include <assert.h>
#include <memory.h>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
struct Node;
@chenshuo
chenshuo / word_freq.cc
Created January 12, 2013 19:03
sort words by frequencies.
#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>
typedef std::unordered_map<std::string, int> WordCount;
struct Greater
{
bool operator()(const std::pair<int, WordCount::iterator>& lhs,
@chenshuo
chenshuo / tree-bench.cc
Last active October 9, 2021 07:48
STL tree structure and benchmark.
#include <set>
#include <stdio.h>
#include <sys/time.h>
double now()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + tv.tv_usec / 1000000.0;
@chenshuo
chenshuo / data_race.cc
Last active December 12, 2015 00:59
Show why lock is needed for reading/writing one shared_ptr from two threads simultaneously.
#include <muduo/base/Mutex.h>
#include <muduo/base/Thread.h>
#include <boost/shared_ptr.hpp>
#include <stdio.h>
boost::shared_ptr<int> g;
muduo::MutexLock mutex;
void read_g()
{
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool covered(const vector<string>& filters, const string& line)
{
@chenshuo
chenshuo / echo-server.c
Created February 17, 2013 01:17
line echo server with libevent
#include <event2/listener.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <assert.h>
#include <signal.h>
#include <string.h>
@chenshuo
chenshuo / waiter.h
Last active April 23, 2024 10:02
A handful of implementations of Waiter class for discussion.
#include <boost/noncopyable.hpp>
#include <pthread.h>
#include <stdlib.h>
// a superfluous check for pedantic people
inline void CHECK_SUCCESS(int ret)
{
if (ret != 0)
{
abort();