Skip to content

Instantly share code, notes, and snippets.

View kghost's full-sized avatar
🎯
Focusing

Zang MingJie kghost

🎯
Focusing
View GitHub Profile
@kghost
kghost / fiber.cpp
Created August 2, 2019 03:28
coroutine
#include <iterator>
#include <iostream>
#include <future>
#include <experimental/coroutine>
std::function<void()> resume;
template <typename _Ty, typename _Alloc = std::allocator<char>>
class future {
public:
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()
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) {
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 January 8, 2016 08:41
Profile CPU time usage
#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 October 17, 2015 20:14
route.sh
#! /bin/sh
MAX=10000
case "$script_type" in
route-up)
op=add
;;
route-pre-down)
op=delete
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 December 26, 2015 00:39
number of partitions of n into at most m parts
// 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
}
}
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)
}
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
}
}