April 30, 2013

Siglot: a concise C++ STL implementation of Signals and Slots

I just finished a small project on Github, it's called Siglot. I started this little project to prove that Signals and Slots could be implemented easily and efficiently in C++ without calling heavy libraries like Qt, Boost or complicated headers like FastDelegates. I think I reached a satisfactory result with a clean 300-lines header (with comments), which includes only one STL header. I hope you'll find it useful!

If you try any benchmarking, I would be really interested to see the results! I'm also not sure if this is thread-safe and would be extremely grateful for any insight in this regard. In any case, send me a short message if you liked it ;)

April 15, 2013

Listing all Permutations: The Recursive Way

Here is a general algorithm for listing all permutations of an array recursively. The code is given in C++, but can easily be ported to any language:

#ifndef __PERMUTE__
#define __PERMUTE__

template <class T>
void swap( T *a, T *b )
{
    T c = *a;
    *a  = *b;
    *b  = c;
}

template <class T>
void permute( T *array, const unsigned& size, unsigned current )
{
    if ( current == size-1 )
    {
        // Do what you want with the current permutation
    }
    else
    {
        for ( unsigned i = current; i < size; ++i )
        {
            swap( array+current, array+i );
            permute( array, size, current+1 );
            swap( array+current, array+i );
        }
    }
}

#endif

April 14, 2013

Pseudo-Random Numbers Generation with C++11

A short post on how to generate random variables with the new header "random" in the STL. I provide a header file that is useful to me whenever I use this new feature; maybe you'll find it useful too. Here is a usage example (assuming you put the header in the same directory):


#include "random_engine.h"
#include <iostream>

using namespace std;

int main()
{
      std::uniform_int_distribution<unsigned> U(1, 15);
      auto generator = RandomEngine::bind_to(U);

      cout << generator() << endl;
      cout << generator() << endl;
      cout << generator() << endl;
      cout << generator() << endl; 

      cout << "------------" << endl; 

      cout << U( *RandomEngine::get() ) << endl;
      cout << U( *RandomEngine::get() ) << endl;
      cout << U( *RandomEngine::get() ) << endl;
      cout << U( *RandomEngine::get() ) << endl;
}

April 13, 2013

Rank Values in C++: Enhancing std::sort

I'm providing a header file that defines two functions for ranking values stored either in an iterable container or simply in a plain array. If you want to rank N values, make sure your "rank" array is of size N too! The trick is merely to exploit the third input of std::sort :)

Tokenize strings in C++

You might have wondered at least once how to simply tokenize a string in C++. Here is a nice and easy way to do it using string streams, inserting tokens into a vector:


#include <string>
#include <vector>
#include <sstream>
#include <iterator>
#include <algorithm>

using namespace std;

void tokenize_string( string words, vector<string>& tokens )
{
   tokens.clear();
   istringstream iss(words);
   copy( 
      istream_iterator<string>(iss), 
      istream_iterator<string>(), 
      back_inserter<vector<string> >(tokens) 
   );
}

Now, say you want to put the tokens into a set instead.. how would you do it? Here it is with a few changes:


#include <set>
#include <string>
#include <vector>
#include <sstream>
#include <iterator>
#include <algorithm>

using namespace std;

struct set_inserter : public iterator< input_iterator_tag, string >
{
   string word;
   set<string>* container;

   set_inserter( set<string>& c ): container(&c) {}
   inline reference operator* () { return word; }
   set_inserter& operator++ () { container->insert(word); return *this; }
};



void tokenize_string( string words, set<string>& tokens )
{
   tokens.clear();
   istringstream iss(words);
   copy( 
      istream_iterator<string>(iss), 
      istream_iterator<string>(), 
      set_inserter(tokens) 
   );
}

What did I do? I replaced the back inserter with my own structure, which behaves virtually as an interator, even though it's not. You can tweak this structure as you want to perform the treatment that you want, provided that you overload the indirection and the left-increment operators as I did :) That's all!