2

Suppose i want something simple like the following:

I have an core-algorithm, which randomly selects one of the specialized algorithms (specialized at compile-time) and process this algorithm. These specialized algorithms are implemented trough functors.

The question is now: how to implement a container, which is build at compile-time, where the core-algorithm can first check the size of this container ("i got 4 algorithms -> need to randomly select algorithm 0-3") and can then execute the functor in this container ("randomly chosen 2 -> process the third functor in container").

How would one implement it as simple as possible? I suppose it is possible.

Is there any connection to the curiously recurring template idiom? (wiki link)
Is there a simple way with the use of Boost::Fusion? (official doc)

Edit: All the algorithms will be used in the core-algorithm. The use pattern (random-numbers) is a runtime-decision (so i don't need compile-time-rands). The algorithm just has to know the container of functors and the size of this container for safe access.

sascha
  • 32,238
  • 6
  • 68
  • 110
  • Why does it have to be built at compile time? – Björn Pollex Nov 09 '10 at 10:22
  • I have to admit: i'm not a good/experienced programmer and therefore i dont have much experience with Inheritance and more (runtime-binding). So i could build my algorithm during runtime (inheritance) or compile-time (templates and metaprogramming). The decision for compile-time is only because of personal interests because i got heavily interested in template libraries and metaprogramming :-). I could say, that i don't want any binding-overhead (which may be a factor in those big-libraries) but i suppose it isn't a big factor in my program. – sascha Nov 09 '10 at 10:37
  • Look at implementation of std::tuple. With slight modification it can do the job for you. – erjot Nov 09 '10 at 12:15

3 Answers3

3

If you want your core-algorithm to execute a specialized algorithm, there should be some kind of contract between the core-algorithm and the specialized algorithm.

If you define this contract as an interface, your container is simply a container containing pointers to these interfaces, e.g.:

class IAlgorithm
   {
   public:
      virtual double operator()(double d) = 0;
   };

typedef std::vector<IAlgorithm *> Algorithms;

Calling a random algorithm is then simply taking the size of the vector, taking a random value between zero and the size of the list (0..size-1), taking the entry at that position and calling the interface.

Alternatively, you can also use the new C++0x std::function construction, like this:

#include <functional>
typedef std::function<double(double)> Algorithm;
typedef std::vector<Algorithm> Algorithms;

Taking an algorithm is similar, you should be able to call an algorithm like this:

Algorithms myAlgorithms;
...
double myresult = myAlgorithms[2](mydouble);

This approach has the advantage that you can also use lambda's.

EDIT: This is an example that uses lambda's. It compiles and works as expected with Visual Studio 2010 (just tested this myself):

#include <iostream>
#include <vector>
#include <functional> 
typedef std::function<double(double)> Algorithm; 
typedef std::vector<Algorithm> Algorithms; 

int main()
{
Algorithms algorithms;
algorithms.push_back([](double d)->double{return d+d;});
algorithms.push_back([](double d)->double{return d*d;});

std::cout << algorithms[0](5) << std::endl;
std::cout << algorithms[1](5) << std::endl;
}
Patrick
  • 23,217
  • 12
  • 67
  • 130
  • Sort of related, but I have been using fastdelegates ( http://www.codeproject.com/KB/cpp/FastDelegate.aspx ) in my projects due to their performance. Do you know about the performance of std::function ? – Moo-Juice Nov 09 '10 at 10:38
  • I used an std::function in a templated class and I noticed no different in performance. – Patrick Nov 09 '10 at 12:49
  • Vaguely reminiscent of the strategy pattern. – Alexandre C. Nov 09 '10 at 13:02
  • Hmm. How would i define some "Algorithm" in the second version, the one using C++0x so that i can push_back this one to the container. Maybe it's more of a "how to use " question... – sascha Nov 09 '10 at 13:36
  • Small Update: I got already a compilation error after the line Algorithms myAlgorithms; in GCC 4.4.4. – sascha Nov 09 '10 at 13:50
  • I edited my post to include a working example. It compiles and works as expected with Visual Studio 2010. Maybe GCC 4.4.4 does not support C++0x? – Patrick Nov 09 '10 at 14:08
  • Thanks for your work, but still no luck. I'm thinking gcc should be able to support much of C++0x. There's a list [here](http://gcc.gnu.org/gcc-4.4/cxx0x_status.html) regarding my compiler version 4.4. It crashes when "accessing" algorithms. – sascha Nov 09 '10 at 14:25
  • Update: Compiles fine with GCC 4.5. Of course with param "-std=c++0x". – sascha Nov 09 '10 at 20:56
0

I'm not a specialist but I think that indeed boost::fusion and/or boost::mpl are the tools you're looking for.

Your class would take an mpl container as parameter, being the list of algorithms functor types, and then would work with it at compile time.

Klaim
  • 67,274
  • 36
  • 133
  • 188
0

I think an interesting subproblem is how to generate random numbers at compile-time.

Perhaps something like this :)

//compiletime_rand.h

#ifndef COMPILETIME_RAND_GENERATOR_H
#define COMPILETIME_RAND_GENERATOR_H

template <unsigned N, unsigned Seed, unsigned Modulo>
struct rand_c_impl
{
    static const unsigned value_impl = (1664525 * rand_c_impl<N - 1, Seed, Modulo>::value + 1013904223) % (1ull << 32);
    static const unsigned value = value_impl % Modulo;
};

template <unsigned Seed, unsigned Modulo>
struct rand_c_impl<0, Seed, Modulo>
{
    static const unsigned value_impl = Seed;
    static const unsigned value = value_impl;
};

#endif

//next_c_rand.h

#include BOOST_PP_UPDATE_COUNTER()

rand_c_impl<BOOST_PP_COUNTER, 0, MAX_C_RAND>::value

//main.cpp

#include <boost/preprocessor/slot/counter.hpp>
#include "compiletime_rand.h"

#include <iostream>

#define MAX_C_RAND 16

template <unsigned N>
void output_compiletime_value()
{
    std::cout << N << '\n';
}

int main()
{
    output_compiletime_value< 
#include "next_c_rand.h"
    >();
    output_compiletime_value< 
#include "next_c_rand.h"
    >();  
}

Output: 15 2

visitor
  • 8,564
  • 2
  • 26
  • 15