Installing gcc4.8 for C++11 support on ubuntu

sudo add-apt-repository ppa:ubuntu-toolchain-r/test sudo apt-get update sudo apt-cache search "g\+\+" sudo apt-get install gcc-4.8 g++-4.8 New problem : now system have 2 gcc compiler version 4.6 and 4.8. use following direction. sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 20 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 20 sudo update-alternatives --config gcc sudo update-alternatives --config g++

Polynomial Evaluation Algorithm in C++


// P(x) = (Cn * x^n )+ (Cn-1 * x^(n-1)) + .... (C0 * x ^ 0) template<typename T> T PolyEval(T coeff[], int n, const T& x){ T y=1, value = coeff[0]; for(int i=0;i<n; ++i){ y*=x; value +=y*coeff[i]; } return value }



//Horner rule for poly eval //P(x)= (.. (Cn * x + Cn-1) *x + Cn-2) * x + ...) * x + C0 template<typename T> T HornerPolyEval(T coeff[], int n, const T& x){ T value=coeff[n]; for(int i=1; i<=n; ++i){ value= value*x + coeff[n-i]; } return value }

Algo1 : Greatest Common Devisior


EuclidGCD(m,n){
    while(n mod m){
        r=n mod m;
        n=m;
        r=n;
    }
    return m;
}

upper bound iteration = log3/2(m+n)

Singleton Design Pattern

Singleton Design Pattern

from sourcemaking.com
  • Our group had a bad habit of using global data, so I did a study group on Singleton. The next thing I know Singletons appeared everywhere and none of the problems related to global data went away. The answer to the global data question is not, "Make it a Singleton." The answer is, "Why in the hell are you using global data?" Changing the name doesn't change the problem. In fact, it may make it worse because it gives you the opportunity to say, "Well I'm not doing that, I'm doing this" – even though this and that are the same thing.

  • How many think about why singleton class required?? but still we are following again and again this as design pattern.
  • Simplest code which i like to use as singleton class. No need to think about much about your class, just make sure wherever you need to use call this Instance() function, rest make a simple class as constructor, destructor, copy, assignment, so when you need to use as simple class object create it, when you need single instance or global object[sorry name change] use this Instance() function.
Singleton& Singleton::Instance() { 
    static _instance;
    return _instance; 
}


  • Double lock idiom singleton

Singleton *Singleton::Instance() {
// perform the Double-Check pattern... 
if (_instance == 0) { 
Guard monitor(_lock); 
if (_instance == 0) 
_instance = new TYPE; 

return _instance; 

}


Template class in template form
template<typename t=""> class C_VSingleton{ private: T element; C_VSingleton(){ static int counter=0; counter++; assert(counter>1); } C_VSingleton(C_VSingleton&){} C_VSingleton& operator=(C_VSingleton&){} public: T& new_instance(){ static T instance; return instance; } };
www.desikudi.in

Adapter Design Pattern

Adapter Design Pattern

you have old application which have access of some old component like this.

Now you introduce new component and this new component have new interface. client used to with old interface, so problem started, how to make sure client should not change his call.

Here Adapter pattern come in picture and help you, to make unrelated classes work together.


I have good example for this, when i was doing some application porting from windows to linux.
Window MFC have CFile class, but linux doesn't have this kind of class.
We had introduce a new class CFile and wrapper around C++ fstream object and provided same interface which is there for CFile.

Bingo problem resolved, not required much modification in client code, just added new class header.


www.desikudi.in

Proxy Design Pattern

Proxy Design Pattern


A simplest design pattern
just do hiding real object and introduce a placeholder.
use case :
when you want lazy initialization of real subject.
when do you want some other operation before making call on real object.
1. filtration of operation
2. logging of input.
3. after calling real object need to do logging of operation success or failure.

www.desikudi.in

Bridge Design Pattern

 Bridge Design Pattern 
Bridge Design Pattern


Simple one line design pattern : when do you need to do combinatorial hierarchy of classes.
handle/body idioms.

Example :
data structure implemented in two way of data.
1. Array form
2. List form

Now you need to create a Stack data strucutre hierarchy for
1. Stack basic interface.
2. StackHanoi only store push new element greater then top.
3. StackFIFO pop stack bottom element.

Now we will look diagram form of this hierarchy before using bridge pattern.
Before Bridge Design Pattern Class Hierarchy.

Now We will look when bridge design pattern used.
After Bridge Design Pattern Class Hierarchy


for code you can look sourcemaking.com . I just shared my understanding. If i got time and found some another example, I may share.

www.desikudi.in