Help with generics

T

Tom Smith

I'm trying to write a generic function that will take an stl map
and return a simplified model of it.

What I mean is, I've got a map of pointers to a complicated object
stored in a map and I'm interested in a particular property of the
objects which is the return value of some function of the object. I
want my function to take the map and return a map with the same
structure; but instead of being filled with pointers to the object,
it should be filled with the return values of the function. This
will (I promise) be an enormous optimisation...

For instance:


/***********************************************************************/

#include <map>
#include <functional>
// and any others I've forgotten

using namespace std;

class BigObject; // BigObject has a complicated function
// bool BigObject::property()
map<int, BigObject*, less<int>> my_complicated_map;

....

map<int, bool, less<int>> my_simplified_map =
map_model<int, bool, BigObject*, less<int>>
(my_complicated_map, mem_fun(&Big_Object::property));

// then do lots and lots of computing with the cached bool values

/***********************************************************************/


But I can't get this to work. I've tried two approaches, one slightly
more general than the other (the general one allows global functions as
the 2nd parameter, like my example, whereas the less general one only
allows member functions - but I can't really see why it should make a
difference).

Here's my (general) attempt:



/***********************************************************************/

#include <map>
#include <functional>
// and any others I've forgotten

using namespace std;

template <class K, class N, class O, class C>
inline map<K, N, C> map_model(map<K, O, C>& om, unary_function<O, N> f)
{
map<K, N, C> nm;
for (typename map<K, O, C>::const_iterator i = om.begin(); i != om.end(); i++)
{
nm[(*i).first] = f((*i).second);
}
return nm;
}

***********************************************************************/

Any help or ideas would be greatly appreciated.


Thanks,

Tom
 
B

Bob Hairgrove

I'm trying to write a generic function that will take an stl map
and return a simplified model of it.

What I mean is, I've got a map of pointers to a complicated object
stored in a map and I'm interested in a particular property of the
objects which is the return value of some function of the object. I
want my function to take the map and return a map with the same
structure; but instead of being filled with pointers to the object,
it should be filled with the return values of the function. This
will (I promise) be an enormous optimisation...

I wonder if it would be easier to cache the value of the computation
in the object itself? This assumes that you would be calculating it
once and reading it many times. Also, storing the calculation in a
separate object seems to be like an invitation for inconsistency.
For instance:


/***********************************************************************/

#include <map>
#include <functional>
// and any others I've forgotten

using namespace std;

class BigObject; // BigObject has a complicated function
// bool BigObject::property()
map<int, BigObject*, less<int>> my_complicated_map;

You need whitespace between the two ">>" tokens.
...

map<int, bool, less<int>> my_simplified_map =
map_model<int, bool, BigObject*, less<int>>

same as above, re: whitespace...
(my_complicated_map, mem_fun(&Big_Object::property));

// then do lots and lots of computing with the cached bool values

/***********************************************************************/


But I can't get this to work. I've tried two approaches, one slightly
more general than the other (the general one allows global functions as
the 2nd parameter, like my example, whereas the less general one only
allows member functions - but I can't really see why it should make a
difference).

When you say you "can't get this to work", what exactly do you mean?
Does it compile? Does it crash? Does it compute the wrong values?
Since you didn't provide enough code to compile and test it with, we
can only guess.
Here's my (general) attempt:



/***********************************************************************/

#include <map>
#include <functional>
// and any others I've forgotten

using namespace std;

template <class K, class N, class O, class C>
inline map<K, N, C> map_model(map<K, O, C>& om, unary_function<O, N> f)
{
map<K, N, C> nm;
for (typename map<K, O, C>::const_iterator i = om.begin(); i != om.end(); i++)
{
nm[(*i).first] = f((*i).second);
}
return nm;
}

***********************************************************************/

Any help or ideas would be greatly appreciated.


Thanks,

Tom

Please post a minimal, but compilable section of code which
illustrates the problem. We would need to know how you are calling the
function, too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top