Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Zang MingJie kghost

🎯
Focusing
View GitHub Profile
View fiber.cpp
#include <iterator>
#include <iostream>
#include <future>
#include <experimental/coroutine>
std::function<void()> resume;
template <typename _Ty, typename _Alloc = std::allocator<char>>
class future {
public:
View lock.scala
val s = Array("A", "B", "C")
val lock = new java.util.concurrent.locks.ReentrantLock()
val total = s.length
var token: Int = 0
val cvs = s.map(i => lock.newCondition())
val threads = for (t <- 0 until 3) yield {
val thread = new Thread {
override def run {
lock.lock()
View unfold.js
function unfold(p, f, g, seed, reduce, tailgen) {
function recursive(iter) {
if (p(iter)) return tailgen(iter);
return reduce(f(iter), recursive(g(iter)));
}
return recursive(seed);
}
function sum(a) {
View unfold.js
function unfold(p, f, g, seed, reduce, tailgen) {
function recursive(iter) {
if (p(iter)) return tailgen(iter);
return reduce(f(iter), recursive(g(iter)));
}
return recursive(seed);
}
@kghost
kghost / cpu-time-profile.cpp
Created Jan 8, 2016
Profile CPU time usage
View cpu-time-profile.cpp
#include <stdio.h>
#if defined(__i386__)
static __inline__ unsigned long long rdtsc(void)
{
unsigned long long int x;
__asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
return x;
}
@kghost
kghost / route.sh
Last active Oct 17, 2015
route.sh
View route.sh
#! /bin/sh
MAX=10000
case "$script_type" in
route-up)
op=add
;;
route-pre-down)
op=delete
View TileAndTrouble
package test
object TileAndTrouble extends App {
//val row = List(8, 8, 20, 17, 25, 25, 11)
//val col = List(15, 13, 10, 21, 20, 18, 17)
val row = List(12, 17, 43, 44, 34, 42, 43, 21, 36, 29, 30, 26)
val col = List(30, 35, 45, 43, 41, 28, 25, 29, 25, 38, 18, 20)
val size = row.length
def seperate_inner(n: Int, c: Int, max: Int): Seq[Map[Int, Int]] = {
@kghost
kghost / gist:7065653
Last active Dec 26, 2015
number of partitions of n into at most m parts
View gist:7065653
// use dp to speed up
def Count(n: Int, m: Int) = {
def Loop(n: Int, m: Int, max: Int): Int = {
if (m == 0) {
if (n == 0) 1 else 0
} else {
(0 to Math.min(max, n)).map({ x => Loop(n - x, m - 1, x) }).sum
}
}
View gist:7064870
def Conbination(n: Int, m: Int) {
def Loop(n: Int, m: Int, prefix: List[Int]) {
if (m == 0)
println(prefix)
else
for (x <- m to n)
Loop(x - 1, m - 1, x :: prefix)
}
Loop(n, m, Nil)
}
View gist:7064763
case class BinaryTree(left: BinaryTree, right: BinaryTree)
def isFull(tree: BinaryTree): Boolean = {
def FullWithHeight(tree: BinaryTree): Int = {
if (tree == null) 0 else {
val l = FullWithHeight(tree.left)
val r = FullWithHeight(tree.right)
if (l >= 0 && l == r) l+1 else -1
}
}