q re abstracting set operations (I'm baffled)

T

tutufan

Hi,

I've been trying to create a set of abstract set operations, so that I
could do something like this

set1 = union(set2, set3);

or even

set1 = set2 | set3;

in a generic way (write once, instantiate for different set types), and
in a way that abstracts out the housekeeping that has to be done to use
the set algorithms in <algorithm>.

So far I'm failing. I tried passing set_union and friends as a
template parameter, but I couldn't figure out a form that would work.
So far I'm failing to even create a pointer to set_union. Any
suggestions? Is this a solved problem?


typedef set<ddvector::size_type> set_dds;

typedef set_dds::iterator setop(set_dds::iterator, set_dds::iterator,
set_dds::iterator, set_dds::iterator,
set_dds::iterator);

#if 0
// this draws an error--no idea why
setop *f = &set_intersection<set_dds, set_dds, set_dds>;
#endif

#if 0
set_dds
safe_set_op(setop *op, set_dds set1, set_dds set2) {
set_dds tmp;
// compiler doesn't seem to like inserter use here--no idea why
op(set1.begin(), set1.end(), set2.begin(), set2.end(),
inserter(tmp, tmp.begin()));
return tmp;
}
#endif

#if 0
// wanted to do something like this
set_dds
operator&(set_dds set1, set_dds set2) {
return safe_set_op(set_intersection<set_dds, set_dds, set_dds>, set1,
set2);
}
#endif

// This code cries out for abstraction, but I don't see how to do it...
set_dds
operator&(set_dds &set1, set_dds &set2) {
set_dds tmp;
set_intersection(set1.begin(), set1.end(), set2.begin(), set2.end(),
inserter(tmp, tmp.begin()));
return tmp;
}

set_dds &
operator&=(set_dds &set1, set_dds &set2) {
return set1 = set1 & set2;
}

set_dds
operator|(set_dds set1, set_dds set2) {
set_dds tmp;
set_union(set1.begin(), set1.end(), set2.begin(), set2.end(),
inserter(tmp, tmp.begin()));
return tmp;
}

set_dds &
operator|=(set_dds &set1, set_dds &set2) {
return set1 = set1 | set2;
}
 
M

Markus Schoder

Hi,

I've been trying to create a set of abstract set operations, so that I
could do something like this

set1 = union(set2, set3);

or even

set1 = set2 | set3;

in a generic way (write once, instantiate for different set types), and
in a way that abstracts out the housekeeping that has to be done to use
the set algorithms in <algorithm>.

So far I'm failing. I tried passing set_union and friends as a
template parameter, but I couldn't figure out a form that would work.
So far I'm failing to even create a pointer to set_union. Any
suggestions? Is this a solved problem?


typedef set<ddvector::size_type> set_dds;

typedef set_dds::iterator setop(set_dds::iterator, set_dds::iterator,
set_dds::iterator, set_dds::iterator,
set_dds::iterator);

Last parameter of setop needs to be an output iterator e.g.
insert_iterator said:
#if 0
// this draws an error--no idea why
setop *f = &set_intersection<set_dds, set_dds, set_dds>;
#endif

set_intersection requires the types of iterators as template parameters
and the third one an output iterator in particular. So this should be
rather

setop *f = &set_intersection<set_dds::iterator, set_dds::iterator,
insert_iterator<set_dds> >;
 

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

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top