Skip to content

Instantly share code, notes, and snippets.

@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>
#include <algorithm>
#include <map>
#include <string>
#include <vector>
#include <ext/numeric>
#include <assert.h>
#include <stdint.h>
class UnsignedInt // copyable
{
@chenshuo
chenshuo / intrusive1.cc
Created March 11, 2015 05:33
intrusive ptr
#include <string>
#include <unordered_set>
#include <boost/intrusive_ptr.hpp>
#include <boost/noncopyable.hpp>
#include <gperftools/malloc_extension.h>
#include <assert.h>
#include <string.h>
@chenshuo
chenshuo / badthreadpool.cc
Last active May 4, 2016 06:38
Bad thread pool example from C++ Concurrency in Action, it's a busy-loop.
#include <atomic>
#include <mutex>
#include <condition_variable>
#include <memory>
#include <queue>
#include <thread>
#include <unistd.h>
// Listing 4.5
template<typename T>
@chenshuo
chenshuo / 24.cc
Created November 6, 2015 18:41
Calculate 24 points.
#include <boost/rational.hpp>
#include <iostream>
#include <vector>
#include <stdio.h>
using boost::rational;
using std::cout;
using std::vector;
typedef vector<rational<int>> Numbers;
@chenshuo
chenshuo / khash.hpp
Last active July 13, 2020 06:28
khash in C++
#pragma once
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <type_traits>
typedef uint32_t khint_t;
typedef khint_t khiter_t;
@chenshuo
chenshuo / gunzip.cc
Created July 12, 2016 18:19
inplace gunzip
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <zlib.h>
#include <boost/noncopyable.hpp>
#include <deque>
// FIXME: re-implement this with disk buffer.
class Buffer : boost::noncopyable
{
@chenshuo
chenshuo / ThreadTest.java
Last active November 20, 2016 02:52
Two threads
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class ThreadTest {
enum Work {
RUN, STOP
}
static class Worker implements Runnable {
@chenshuo
chenshuo / nontype.cc
Last active August 28, 2017 20:44
Array as non-type template argument in C++17
#include "nontype.h"
const char tag[] = "abc";
int main()
{
}
@chenshuo
chenshuo / tree.cc
Last active October 29, 2020 15:14
expr tree
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <deque>
struct Node
{
Node(char v) : val(v) {}
bool isOperator() const