Skip to content

Instantly share code, notes, and snippets.

#include <algorithm>
#include <map>
#include <string>
#include <vector>
#include <ext/numeric>
#include <assert.h>
#include <stdint.h>
class UnsignedInt // copyable
{
@chenshuo
chenshuo / nqueens.cc
Created December 11, 2013 23:03
Multithreaded N-queens in C++11
#include <algorithm>
#include <atomic>
#include <thread>
#include <vector>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/time.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();
@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>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool covered(const vector<string>& filters, const string& line)
{
@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()
{
@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 / 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 / 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 / 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;