Ebeworld’s Weblog

Trying to create

Header files

1. It is best to include a.h file in a.cpp at the top to insure order of header files are irrelevant, which is very good design. There could be forward declarations.

A header file should be included only when a forward declaration would not do the job.
a.The size of the class’ objects is not needed.
b.The names or types of any of the class’ members are not needed.
c.The prototypes to or names of any of the class’ member functions are not needed
The header file should be so designed that the order of header file inclusion is not important.
The header file inclusion mechanism should be tolerant to duplicate header file inclusions.

March 29, 2011 Posted by | Algorithms, CPP, Uncategorized | Leave a Comment

An easy problem

My little brother just asked me about finding number of ways to divide numbers from 1 to N into two sets where the sums are equal. N<37. It feels like simple DP problem.

March 29, 2011 Posted by | Algorithms | Leave a Comment

Combinable containers

Preventing from excessive use of locks, uses combinable containers, such as following

        vector<int> values(10);
	int i = 0;
	generate(values.begin(), values.end(), [&]{return ++i;} );

	// 1. Calculate the sum of the elements of the vector in parallel.
	combinable<int> sums;
	parallel_for_each(values.begin(),values.end(), [&](int n)
	{
		sums.local() += n;}
	);

	// 2. Combine/Reduce into final result
	int result = sums.combine([](int left, int right) { return left + right; });

March 23, 2011 Posted by | Algorithms, CPP, Uncategorized | 1 Comment

The reason behind nullptr

Due to template type prediction, either of the following declaration would result to deduct int version of the template and eventually cause compilation to fail.

#define NULL 0
//or sometimes
#define NULL (void *)0

This is explained at http://channel9.msdn.com/shows/Going+Deep/Stephan-T-Lavavej-Everything-you-ever-wanted-to-know-about-nullptr/

March 23, 2011 Posted by | Algorithms, CPP, Uncategorized | Leave a Comment

parallel_for_each Example

#include<iostream>
#include <conio.h>
#include <ppl.h>
#include <windows.h>
#include <vector>
#include <algorithm>
using namespace std;
using namespace Concurrency;

__int64 GetTime(){
	return GetTickCount();
}

void makeDouble(vector<int> vec){
	for_each(vec.begin(),vec.end(),[&](int i){
		cout<<2*i<<" ";
	});
	cout<<endl<<endl<<endl<<endl;
}

void parallel_makeDouble(vector<int> vec){
	parallel_for_each(vec.begin(),vec.end(),[=](int i){
		cout<<2*i<<" ";
	});
	cout<<endl<<endl<<endl<<endl;
}

int main(int argc, char** argv){
	vector<int> v(200);
	int n=0;
	generate(v.begin(),v.end(),[&](){ return n++;});

	__int64 begin=GetTime();
	makeDouble(v);
	__int64 end=GetTime();
	cout<<"Seq:"<<end-begin<<endl;

	begin=GetTime();
	parallel_makeDouble(v);
	end=GetTime();
	cout<<"Parallel:"<<end-begin<<endl;
	getch();
	return 0;
}

March 23, 2011 Posted by | Algorithms, CPP, Uncategorized | Leave a Comment

parallel_for example

#include<iostream>
#include <conio.h>
#include <ppl.h>
using namespace std;
using namespace Concurrency;

void makeDouble(int n){
	for(int i=0;i<n;i++){
		cout<<2*i<<" ";
	}
	cout<<endl<<endl<<endl<<endl;
}

void parallel_makeDouble(int n){
	parallel_for(0,n,[](int i){
		cout<<2*i<<" ";
	});

}

int main(int argc, char** argv){
	makeDouble(100);
	parallel_makeDouble(100);
	getch();
	return 0;
}

Output:

0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 5
6 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106
108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146
148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186
188 190 192 194 196 198

50 0 150 100 52 54 56 58 60 176 178 180 182 184 186 188 190 192 194 196 198 62 1
02 104 152 154 156 164 166 168 1702 64 66 68 70 72 172 174 158 106 108 110 112
114 116 118 120 162 4 122 124 126 128 130 132 134 136 138 140 28 74 76 78 80 82
84 86 88 90 92 94 96 98 30 32 34 36 38 40 42 44 46 48 142 6 144 160 146 8 10 12
18 20 148 22 14 24 16 26

March 23, 2011 Posted by | Algorithms, CPP, Uncategorized | Leave a Comment

Secret History of SV

On google tech talk i saw this talk and it is quite entertaining.

http://www.youtube.com/user/GoogleTechTalks#p/u/6/hFSPHfZQpIQ

March 23, 2011 Posted by | Uncategorized | Leave a Comment

Boost signals

Much more like event handler in C# and still working on its difference with delegate, which is combinable.

#include <boost/signal.hpp> 
#include <iostream> 

void func() 
{ 
	std::cout << "Hello, world!" << std::endl; 
}

void func2(){
	std::cout<<"Me too!"<<std::endl;
}

int main() 
{ 
	boost::signal<void ()> s; 
	s.connect(func);
	s.connect(func2);
	s.connect(func);
	s.disconnect(func);
	s(); 
} 

March 21, 2011 Posted by | Algorithms, CPP, Uncategorized | Leave a Comment

Getting Number of CPUs and Thread IDs

cout<<boost::this_thread::get_id()<<endl;
cout<<boost::thread::hardware_concurrency()<<endl;

March 15, 2011 Posted by | Algorithms, CPP, Uncategorized | Leave a Comment

Boost thread

#include<boost/thread.hpp>
#include<iostream>

using namespace std;

void wait(int seconds){
	boost::this_thread::sleep(boost::posix_time::seconds(seconds));
}

void thread(){
	for(int i=0;i<10;++i){
		wait(1);
		cout<<i<<endl;
	}
}

int main(){
	boost::thread t(thread);
	t.join();
}

And here is one with lock…

#include<boost/thread.hpp>
#include<iostream>
#include "boost/thread.hpp"

using namespace std;

void wait(int seconds){
	boost::this_thread::sleep(boost::posix_time::seconds(seconds));
}

boost::mutex mutex;

void thread(){
	for(int i=0;i<10;++i){
		wait(1);
		mutex.lock();
		cout<<boost::this_thread::get_id()<<"      "<<i<<endl;
		mutex.unlock();
	}
}

int main(){
	boost::thread t1(thread);
	boost::thread t2(thread);
	t1.join();
	t2.join();
}

March 15, 2011 Posted by | Algorithms, CPP, Uncategorized | Leave a Comment

Follow

Get every new post delivered to your Inbox.