-
template method
Hello,
I have two functions that operate in much the same way on two different kinds of vectors.
if we allow
Code:
typedef pair<num_t, num_t> point;
then
one operates on Code:
vector< pair< point , vector<point> > >
and the other operates on where
Code:
struct span
{
num_t x;
num_t dy;
}
//It's essentially a pair, but I didn't want the 'first' and 'second' syntax.
So the first function does something involving vec[i].dy, and the other does the same thing using vec[i].second.size(), both for each element of vec.
How can I write these two as one general function, without the use of helper classes? The functions are small enough that writing it twice is not so horrible, but if there is a straightforward template method, that'd be better.
Thanks.
-
Why not show what you have written for each of the types involved?
-
For brevity. Here you go:
Code:
//. . . somewhere in the class grid:
struct span;
typedef std::vector<span> region;
typedef std::pair< point2d, std::vector<point2d> > normal_pair;
typedef std::vector<normal_pair> boundary;
then here are a couple of examples
Code:
grid::size_type num_points(const grid::region & r, unsigned int until)
{
unsigned long int points = 0;
for(unsigned int i = 0; i < r.size() && i < until; ++i)
points += r[i].dy;
return points;
}
grid::size_type num_points(const grid::boundary & b, unsigned int until)
{
unsigned long int points = 0;
for(unsigned int i = 0; i < b.size() && i < until; ++i)
points += b[i].second.size();
return points;
}
grid::size_type first_more_than(const grid::region & reg, unsigned long int quota, grid::size_type begin_index)
{
unsigned long int count = 0;
for(unsigned int i = begin_index; i < reg.size(); ++i)
{
count += reg[i].dy;
if(count > quota)
return i;
}
return reg.size();
}
grid::size_type first_more_than(const grid::boundary & bound, unsigned long int quota, grid::size_type begin_index)
{
unsigned long int count = 0;
for(unsigned int i = begin_index; i < bound.size(); ++i)
{
count += bound[i].second.size();
if(count > quota)
return i;
}
return bound.size();
}
Using custom functors in stl algorithms would do the trick, but is there a way to specify a member?
-
I guess I could just overload a function thing_to_count(vector), and then have the other functions use that one.
Code:
inline unsigned long thing_to_count(boundary::const_reference ref) { return b.second.size(); }
inline unsigned long thing_to_count(region::const_reference ref) { return r.dy; }
template<class vec_T>
grid::size_type num_points(const vec_T & v, unsigned int until)
{
unsigned long int points = 0;
for(unsigned int i = 0; i < b.size() && i < until; ++i)
points += thing_to_count(v[i]);
return points;
}
template<class vec_T>
grid::size_type first_more_than(const vec_T & v, unsigned long int quota, grid::size_type begin_index)
{
unsigned long int count = 0;
for(unsigned int i = begin_index; i < reg.size(); ++i)
{
count += thing_to_count(v[i]);
if(count > quota)
return i;
}
return reg.size();
}
It actually looks good to me.
-
That looks like a sensibl solution to me.