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