This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Win32DLL.h" | |
#include <iostream> | |
#ifdef UNICODE | |
#define tcout std::wcout | |
#else | |
#define tcout std::cout | |
#endif | |
EXTERN_C IMAGE_DOS_HEADER __ImageBase; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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++; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool IsStackGrowDownwards() | |
{ | |
int mark1 = 0; | |
int mark2 = 0; | |
return &mark2 < &mark1; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
template <class T> | |
class Queue | |
{ | |
public: | |
static int Size; | |
}; | |
#ifdef TEMPLATE_EXPORTS |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function f() { | |
sleep "$1" | |
echo "$1" | |
} | |
while [ -n "$1" ] | |
do | |
f "$1" & | |
shift | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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);} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--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 |