470,815 Members | 1,175 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

q re abstracting set operations (I'm baffled)

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;
}

May 11 '06 #1
1 1232
tu*****@gmail.com wrote:
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<set_dds>.
#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> >;

May 11 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Scott Auge | last post: by
4 posts views Thread by Mark Thomas | last post: by
11 posts views Thread by Full Decent | last post: by
3 posts views Thread by Hallvard B Furuseth | last post: by
17 posts views Thread by sheldonlg | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.