#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;
}
Friday, December 17, 2010
A C++ way of tokenizing string
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++.
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 : 22⁄7
function fraction(a, b) {
return "<span style=\"font-size:80%;letter-spacing:-1px;\"><sup>"+a+"</sup>⁄<sub>"+b+"</sub> </span>";
}
Thursday, July 15, 2010
C++ code printing itself
#include <iostream>
#include <string>
using namespace std;
int f;void s(char *m);void o(char *m){char *p=m,c=*p;while(*p>0&&(c=*p++))
if(!f||c!=(char)10)cout<<c;else cout<<(char)92<<(char)110;f=1;}
char m[310];int main(){s(m);o(m);cout<<(char)34;o(m);
cout<<(char)34<<(char)41<<(char)59<<(char)125<<endl;return 0;}
void s(char *m){strcpy(m,"#include <iostream>\n#include <string>\nusing namespace std;\nint f;void s(char *m);void o(char *m){char *p=m,c=*p;while(*p>0&&(c=*p++))\nif(!f||c!=(char)10)cout<<c;else cout<<(char)92<<(char)110;f=1;}\nchar m[310];int main(){s(m);o(m);cout<<(char)34;o(m);\ncout<<(char)34<<(char)41<<(char)59<<(char)125<<endl;return 0;}\nvoid s(char *m){strcpy(m,");}
Simple Metronom for Windows
Only for WIN32 console. not sure conio.h is deprecated
#include <windows.h>
#include <conio.h>
#include <stdio.h>
void main() {
while(!kbhit()) {
puts("\x07");
Sleep(750);
}
}
Finding Wi-Fi IP address of iphone
It will work for any unix-like system- might need to change "en0" to something else
#include <ifaddrs.h>
const char* http_ip_address(void)
{
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success != 0) return NULL;
// Loop through linked list of interfaces
temp_addr = interfaces;
while(temp_addr != NULL)
{
if(temp_addr->ifa_addr->sa_family == AF_INET)
{
// Check if interface is en0 which is the wifi connection on the iPhone
if(!strcmp(temp_addr->ifa_name,"en0"))
{
freeifaddrs(interfaces);
return inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr);
}
}
temp_addr = temp_addr->ifa_next;
}
// Free memory
freeifaddrs(interfaces);
return NULL;
}
Wednesday, July 14, 2010
Converting unix timestamp to "~ seconds ago", etc.
Written in PHP
function datestring($date) {
$diff=time()-$date;
if($diff<60) {
$date=$diff."seconds ago";
} else if($diff<3600) {
$date=round($diff/60)."minutes ago";
} else if($diff<86400) {
$date=round($diff/3600)."hours ago";
} else {
$date=round($diff/86400)."days ago";
}
return $date;
}
Fast Fourier Transform
// fft function- the length n has to be a power of 2
// v[] is composed of imaginary and real parts of the input:
// [ imag[0], real[0], imag[1], real[1], .... ]
// the function is in-place thus the result is in the original array.
void FFT(float v[], int n) {
int ip, k, length;
float theta, pi = 3.1415926535897932384f;
float wr, wi, ur, ui, tr, ti, tmp;
for (int i=0, j=0; i < n-1; i++,j+=k) {
if (i<j) {
tmp = v[i*2]; v[i*2] = v[j*2]; v[j*2] = tmp;
tmp = v[i*2+1]; v[i*2+1] = v[j*2+1]; v[j*2+1] = tmp;
}
for(k=n/2;k<:=j;k>>=1) j -= k;
}
for (int li=1; li<:n; li*=2 ){
length = 2*li;
theta = pi/li;
ur = 1.0f;
ui = 0.0f;
if ( li == 1 ) {
wr = -1.0f;
wi = 0.0f;
} else if ( li == 2 ) {
wr = 0.0f;
wi = 1.0f;
} else {
wr = cos(theta);
wi = sin(theta);
}
for (int j = 0; j <: li; j++ ) {
for (int i = j; i <: n; i += length ) {
ip=i+li;
tr=v[ip*2]*ur-v[ip*2+1]*ui; ti=v[ip*2]*ui+v[ip*2+1]*ur;
v[ip*2]=v[i*2]-tr; v[ip*2+1]=v[i*2+1]-ti;
v[i*2]+=tr; v[i*2+1]+=ti;
}
ur = ur*wr - ui*wi;
ui = ui*wr + ur*wi;
}
}
}
Intersection of two line segments
The JAVA code below finds intersection of two line segments (x00,y00)-(x01,y01) and (x10,y10)-(x11,y11)
It returns float array of the coordinates of the intersection, or null if there is no intersection,
It returns float array of the coordinates of the intersection, or null if there is no intersection,
public static float[] intersection(float x00, float y00, float x01, float y01,
float x10, float y10, float x11, float y11) {
float ABx=x01-x00, ABy=y01-y00;
float CDx=x11-x10, CDy=y11-y10;
float BCx=x10-x01, BCy=y10-y01;
float BDx=x11-x01, BDy=y11-y01;
float DAx=x00-x11, DAy=y00-y11;
float DBx=x01-x11, DBy=y01-y11;
// check if the line segments are not crossing
if((ABx*BCy-ABy*BCx)*(ABx*BDy-ABy*BDx)>=0) return null;
if((CDx*DAy-CDy*DAx)*(CDx*DBy-CDy*DBx)>=0) return null;
float d=(x00-x01)*(y10-y11)-(y00-y01)*(x10-x11);
if(d==0) return null;
float x=((x00*y01-y00*x01)*(x10-x11)-(x00-x01)*(x10*y11-y10*x11))/d;
float y=((x00*y01-y00*x01)*(y10-y11)-(y00-y01)*(x10*y11-y10*x11))/d;
float result[]={x,y};
return result;
}
Labels:
coordinate,
cross,
intersection,
line,
segments
Getting current timestamp in UNIX
static double now_ms(void) {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec*1000. + tv.tv_usec/1000.;
}
Labels:
getTickCount,
gettimeofday,
time,
timestamp,
UNIX
Syntax Highlighting Test
#include <stdio.h>
int main(int argc, char ** argv) {
printf("Hello World\n");
return 0;
}
<link href='http://www-personal.umich.edu/~jongwook/SyntaxHighlighter/Styles/SyntaxHighlighter.css' rel='stylesheet' type='text/css' />
<script src='http://www-personal.umich.edu/~jongwook/SyntaxHighlighter/Scripts/shCore.js' type='text/javascript'/>
<script src='http://www-personal.umich.edu/~jongwook/SyntaxHighlighter/Scripts/shBrushCpp.js' type='text/javascript'/>
<script src='http://www-personal.umich.edu/~jongwook/SyntaxHighlighter/Scripts/shBrushPython.js' type='text/javascript'/>
<script src='http://www-personal.umich.edu/~jongwook/SyntaxHighlighter/Scripts/shBrushPhp.js' type='text/javascript'/>
<script src='http://www-personal.umich.edu/~jongwook/SyntaxHighlighter/Scripts/shBrushJava.js' type='text/javascript'/>
<script src='http://www-personal.umich.edu/~jongwook/SyntaxHighlighter/Scripts/shBrushJScript.js' type='text/javascript'/>
<script src='http://www-personal.umich.edu/~jongwook/SyntaxHighlighter/Scripts/shBrushXml.js' type='text/javascript'/>
<script src='http://www-personal.umich.edu/~jongwook/SyntaxHighlighter/Scripts/shBrushCss.js' type='text/javascript'/>
<script type='text/javascript'>
dp.SyntaxHighlighter.BloggerMode();
dp.SyntaxHighlighter.HighlightAll();
</script>
Thursday, May 20, 2010
Subscribe to:
Posts (Atom)