Skip to content

Instantly share code, notes, and snippets.

View baiyanhuang's full-sized avatar

Baiyan Huang baiyanhuang

  • The Trade Desk
  • Shanghai
View GitHub Profile
--sleep sort
function sleepsort(arr)
local res, thread = {}, {}
local nthreads = #arr
for i = 1, #arr do
thread[i] = coroutine.create(function()
for n=arr[i], 0, -1 do coroutine.yield() end
nthreads = nthreads - 1
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h> // for Sleep
#include <omp.h>
int main(int argc, const char* argv[])
{
omp_set_num_threads(argc);
#pragma omp parallel for
#include <string.h>
#define RUNTIME_TYPE_ID_ROOT(typestr) \
static const char* Type() {return typestr;} \
virtual bool IsKindOf(const char* typeName) {return 0 == strcmp(typeName, Type());}
// how about multiple inheritance
#define RUNTIME_TYPE_ID(typestr, baseclass) \
static const char* Type() {return typestr;} \
virtual bool IsKindOf(const char* typeName) {return (0 == strcmp(typeName, Type())) || baseclass::IsKindOf(typeName);}
#!/bin/bash
function f() {
sleep "$1"
echo "$1"
}
while [ -n "$1" ]
do
f "$1" &
shift
done
#pragma once
template <class T>
class Queue
{
public:
static int Size;
};
#ifdef TEMPLATE_EXPORTS
bool IsLittleEndian()
{
int code = 0x12345678;
char* pc = (char*)&code;
char a = (*pc);
char b = *(pc+1);
char c = *(pc+2);
char d = *(pc+3);
// 0x78 is the least significant byte,
@baiyanhuang
baiyanhuang / IsStackGrowDownwards.h
Created July 23, 2011 06:05
Check if the stack grow downwards - it is determined by cpu's push/pop instructions, but OS could redefine the behavior
bool IsStackGrowDownwards()
{
int mark1 = 0;
int mark2 = 0;
return &mark2 < &mark1;
}
// Following class doesn't have to be templated, but we use template here to:
// 1. Make sure function's declaration and definition are in same place, so it is convenient when you update the function signature.
// Although inline could also achieve this, but you can't prevent others from moving implementation into a cpp, while template provide stronger protect.
// 2. Code will only be instantiated if used for template functions, thus reduce the binary size
// Although modern compiler and linker could remove non-used functions, that requires additional compiler or linker time to make it happen.
// But it also have some drawbacks
// 1. Compile error message is hard to read for templates
// 2. As all code are in header file, it is more vulnerable to #defines and typedefs when included
// UTF-8 is a variable-width encoding, with each character represented by one to four bytes, and for each UTF character, all // its non-first byte are start with 10xxxxxx, so the number of UTF characters could be counted by count the bytes that not
// start with 10xxxxxx, and here comes the algorithrm
// http://en.wikipedia.org/wiki/UTF-8#Design
int strlen_utf8_c(char *s) {
int i = 0, j = 0;
while (s[i]) {
if ((s[i] & 0xc0) != 0x80) j++;
i++;
}
#include "Win32DLL.h"
#include <iostream>
#ifdef UNICODE
#define tcout std::wcout
#else
#define tcout std::cout
#endif
EXTERN_C IMAGE_DOS_HEADER __ImageBase;