Monday, September 5, 2011

Wednesday, January 19, 2011

getting current time as double

linux, mac :

#include <sys/time.h>

static double now_ms(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec*1000. + tv.tv_usec/1000.;
}


windows

static double now_ms(void)
{
static LARGE_INTEGER frequency;
static bool dummy = QueryPerformanceFrequency(&frequency);

LARGE_INTEGER count;
QueryPerformanceCounter(&count);
if(frequency.QuadPart == 0) return 0;

return (double)count.QuadPart/(double)frequency.QuadPart;
}

Friday, December 17, 2010

A C++ way of tokenizing string



#include <string>
#include <sstream>
#include <iostream>
#include <iterator>
#include <vector>

std::string str("quick brown fox jumps into something that I have forgot");
std::stringstream ss(str);
std::istream_iterator<std::string> it(ss), end;
std::vector<std::string> tokens(it,end);
for(std::vector<std::string>::iterator token=tokens.begin(); token!=tokens.end(); ++token) {
std::cout << *token << std::endl;
}

Tuesday, August 3, 2010

Getting Internal Storage Directory in Android NDK

The function name should be changed to your package name and activity class name.

Writtin in C++.


JNIEXPORT void JNICALL Java_com_jongwook_test_TestActivity_test(JNIEnv * env, jobject obj)
{
jclass cls = env->GetObjectClass(obj);
jmethodID getFilesDir = env->GetMethodID(cls, "getFilesDir", "()Ljava/io/File;");
jobject dirobj = env->CallObjectMethod(obj,getFilesDir);
jclass dir = env->GetObjectClass(dirobj);
jmethodID getStoragePath =
env->GetMethodID(dir, "getAbsolutePath", "()Ljava/lang/String;");
jstring path=(jstring)env->CallObjectMethod(dirobj, getStoragePath);
const char *pathstr=env->GetStringUTFChars(path, 0);
chdir(pathstr);
env->ReleaseStringUTFChars(path, pathstr);
}

Friday, July 16, 2010

Finding the greatest power of 2 less than given integer

assuming that n is a 32-bit integer

if((n&(n-1))!=0) {
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n=n+1;
}

Efficiently calculating integer powers of float


static float pow(float base, int exp) {
float result = 1;
if(exp<0) return 1.0f/pow(base,-exp);
while (exp!=0) {
if ((exp & 1)!=0) result *= base;
exp >>= 1;
base *= base;
}
return result;
}

Displaying fraction in HTML

Showing something like this : 227


function fraction(a, b) {
return "<span style=\"font-size:80%;letter-spacing:-1px;\"><sup>"+a+"</sup>⁄<sub>"+b+"</sub> </span>";
}