Skip to content

Instantly share code, notes, and snippets.

@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>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <stdio.h>
class Base
{
public:
virtual void foo()
{
printf("Base::foo\n");
@chenshuo
chenshuo / iprange.cc
Created April 3, 2011 03:27
从一组 IP range --> value 的数据中查找单个 IP
#include <assert.h>
#include <stdint.h>
#include <algorithm>
#include <vector>
using namespace std;
struct IPrange
{
@chenshuo
chenshuo / ten_in_a_row.java
Created April 29, 2011 12:07
prob of 10 in a row, picking 60 out of 228.
public class Main {
public final static int kHoles = 228;
public final static int kBalls = 60;
public static void main(String[] args) {
Random r = new Random();
int[] hist = new int[kBalls + 1];
for (int round = 0; round < 1000*1000*10; ++round) {
boolean[] holes = new boolean[kHoles+1];
int balls = 0;
@chenshuo
chenshuo / nbody.c
Created August 20, 2011 09:17
Data Abstraction in C++
/*
* The Computer Language Benchmarks Game
* http://shootout.alioth.debian.org/
*
* contributed by Christoph Bauer
* modified by bearophile
*/
#include <math.h>
@chenshuo
chenshuo / join.cc
Created December 11, 2011 12:59
Join two sets
#include <algorithm>
#include <iterator>
#include <vector>
#include <boost/unordered_set.hpp>
#include <stdint.h>
#include <stdio.h>
#include <sys/time.h>
using namespace std;
@chenshuo
chenshuo / poj1035.cc
Created January 15, 2012 03:25
poj1035
#include <string>
#include <set>
#include <vector>
#include <iostream>
#include <assert.h>
#include <stdio.h>
using namespace std;
typedef vector<string> Dictionary;
@chenshuo
chenshuo / buysell.cc
Created February 4, 2012 06:36
buy sell
#include <assert.h>
#include <stdio.h>
#include <vector>
int buysell(const std::vector<int>& prices)
{
assert(!prices.empty());
size_t lowestBuy = 0;
size_t lowestBuySoFar = 0;
size_t highestSell = 0;
@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 / 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 == ' ';
}
};