Showing posts with label Nontype Template. Show all posts
Showing posts with label Nontype Template. Show all posts

Saturday, May 19, 2012

Random vector

Last post it was about lambdas but, indeed, I just focused with an utility; random vectors creation. Now, after still being researching I found more intuitive and elegant forms.

Let's see this amazing things:



#include <algorithm>
#include <iostream>
#include <vector>
#include <time.h>

//Start reading inside main!!!!
//first template is the rank of numbers we want
//second, where it starts
template<int N, int Start = 0>
class MyRand{
public:
     int operator()(void){
         return std::rand()%N + Start;
     }
};

int fRand(){
     return std::rand()%10 + 30;
}

int main()
{
      // Create a vector object that contains 10 elements.
      std::vector<int> a(10);

     // randomly initialize using lambda expressions
     srand((unsigned)time(0)); 

     //generating the vector by means of standard random function
     //This is nice but you don't have much control of which rank of numbers
     //will be returned. Indeed, you know, with the macro RAND_MAX
     //and this is a huge number.

     //And that perhaps isn't what we expected. So the solucion is to use
     //a custom function:
     std::generate(a.begin(), a.end(), std::rand );

     //Now, just changing fRand we can get our wished numbers, but
     //even that is not the best way because you can't use any function
     //that takes some parameter and then you'd need as many function
     //as you need; and that can be tedious. Instead of this, we can use templates
     //which we won't get that problem.
     std::generate(a.begin(), a.end(), fRand );

     //So, here we go:
     //With two templated parameters we can get any random rank of numberes we want.
     //for example, if we want numbers between 10 and 20 that's the example:
     std::generate(a.begin(), a.end(), MyRand<10,10>() );

     //O maybe we want numbers between 14 and 71
     //how many numbers there are between them? 71 - 14 = 57 and we start at 14
     //So:
     std::generate(a.begin(), a.end(), MyRand<57,14>() );


     //like last time, how to write this vector with just code a line
     std::for_each(a.begin(), a.end(), [](int a){ std::cout << a << std::endl; } );
     return 0;
}


Tuesday, February 28, 2012

Nontype Template Parameters


Konnichiwa!!

Lately I have been very excited programming and I really have to do an effort to continue chapter after chapter in order not to mess up the blog. However, if you are interested to get further feel free to check out the examples page.

So then, let's work!

First off, what's the meaning of nontype template parameters? Indeed, the idea is so simple, is just to use types like int inside templates. Till now, all template examples we have seen has been with the reserve word typename or class but the powerful of templates don't finish here.

For example, a Matrix:
template<int W, int H>
struct Matrix{
    double m[W][H];
};

Thus, now, we can simply constructor of a Matrix like this:

Matrix<4,4> A={0,0,0,0,
               0,1,0,0,
               0,0,1,0,
               0,0,0,1};
Matrix<4,1> B={1,2,3,4};

In the case using a class in lieu of a struct, we could not do it in this way unless we started using C++11.
Another example usefull with matrices could be the below:

template<int sizeAX,int sizeAY, int sizeBY>
void mult(const Matrix<sizeAX,sizeAY>& A,const Matrix<sizeAY,sizeBY>& B, 
Matrix<sizeAX,sizeBY>& C);
Note: I'm using pass by reference, check out FAQ if dont get it at all why I used &'s or const.

This function multipy does A · B = C
But not always matrices can be multiplied. With the definition we did, we can do A · B but not B · A.
In this case we'll have a compile error:

(I change the variable names to make it more understandable)

void mult(const Matrix<4,1>& A,const Matrix<4,4>& B,
void mult(const Matrix<sizeAX,sizeAY>& A,const Matrix<sizeAY,sizeBY>& B,

                     sizeAX = 4;         sizeAY = 4;
                     sizeAY = 1;         sizeBY = 4;
SizeAY don't match so here is where the error occurs. But, in fact, is better to have this error in compile time than runtime.

Struct Matrix here!!

A similar example, here:

template<typename T, int ROWS, int COLS>
class Matrix;
template<int InCOLS>
Matrix<T, ROWS, InCOLS> operator*(const Matrix<T, COLS, InCOLS>& in) const;
If template COLS of our matrix match with the rows of the supose matrix to be multiply then we are able to do it. And the result of this multiply will be ROWS and InCOLS.

Slowly:

A(3,2)*B(2*10) = C(3,10)

If colsA(2) is equal to rowsB(2).

sizeC = (rowsA, colsB)