Skip to content

Instantly share code, notes, and snippets.

@phg1024
phg1024 / LRU.hpp
Created June 30, 2014 02:59
A simple LRU cache.
#include <vector>
#include <unordered_map>
#include <iostream>
using namespace std;
template <typename K, typename V>
struct LRUNode {
K key;
V value;
@phg1024
phg1024 / mulitprocessing.py
Created July 10, 2014 21:39
Simple multiprocessing example
from multiprocessing import Process, Queue
import random
import time
def f(i, q):
t = random.random() * 2
time.sleep(t)
print 'hello world', i
q.put((i, 'hello world from %d' % i))
@phg1024
phg1024 / test_unique_ptr.cpp
Created July 15, 2014 19:54
demo use of unique pointer
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
struct dummy_t {
dummy_t(){}
dummy_t(int v):val(v){
cout << "dummy " << val << " constructred." << endl;
}
vertices :: Integer -> Integer -> Integer -> [(Integer, Integer)]
vertices 1 w h = [(quot (w+1) 2, h)]
vertices n w h = foldl (\acc v -> acc ++ [((fst v) - dx, snd v), (fst v, (snd v) - dy), ((fst v) + dx, snd v)]) [] p
where p = vertices (n-1) w h
dx = quot (w+1) (2^n)
dy = quot h (2^(n-1))
inrange :: Integer -> Integer -> Integer -> Bool
inrange 0 x y = abs (x - 32) < y
inrange n x y = inrange (n-1) x y && (foldl (\acc v -> acc && (not (abs (x-(fst v)) <= (snd v) - y && (snd v) - y < dy))) True verts)
#include <iostream>
#include <vector>
#include <stack>
#include <stdlib.h>
#include <algorithm>
#include <cfloat>
using namespace std;
struct Point {
Point():x(0), y(0){}
for /l %%x in (11, 1, 27) do (
echo 0%%x
:: do something here
)
@phg1024
phg1024 / cachetest.cpp
Last active August 29, 2015 14:15
effect of cache demonstration
#include <iostream>
#include <time.h>
#include <vector>
using namespace std;
int main(int argc, char **argv) {
const int MAX = 10000;
vector<vector<double>> A(MAX, vector<double>(MAX));
vector<double> x(MAX), y(MAX);
for(int i=0;i<MAX;++i)
@phg1024
phg1024 / nextperm.cpp
Created June 1, 2015 00:46
Sample implementation of next permutation
#include <iostream>
#include <algorithm>
using namespace std;
template <typename Container>
bool nextperm(Container &s) {
using T = Container;
typename T::iterator first = s.begin();
typename T::iterator last = s.end() - 1;
typename T::iterator i = last;
@phg1024
phg1024 / simple_vector.cpp
Created October 17, 2015 07:05
Simple vector
#include <iostream>
#include <vector>
using namespace std;
const int trunk_size = 8 * 1024 * 1024;
class A {
public:
static int counter;
A() { id = counter++; cout << "alloc " << id << " @ " << this << endl; trunk.resize(trunk_size); }
@phg1024
phg1024 / bfs_recursive.py
Created October 20, 2015 15:46
Recursive BFS
class Node(object):
def __init__(self, val, leftchild=None, rightchild=None):
self.val = val
self.left = leftchild
self.right = rightchild
def bfs_iterative(tree, visitor):
q = []
q.append((tree, 0))