Re: Is there any library supporting set operation.
kuangye wrote:
Hi, all.
Is there any library supporting set operation such as union,
intersection, difference on sets of integer numbers.
Using operator abuse and std::set:
/*
A, B : sets
a, b : elements
union: A + B A + b A += B A += b
intersection: A * B A * b A *= B A *= b
difference: A - B A - b A -= B A -= b
symmetric difference: A ^ B A ^ b A ^= B A ^= b
*/
#include <set>
#include <algorithm>
#include <iterator>
// union:
// ======
template < typename T, typename Alloc, typename Compare >
std::set<T,Alloc,Compare> operator+ ( std::set<T,Alloc,Compare> const & a,
std::set<T,Alloc,Compare> const & b ) {
std::set<T,Alloc,Compare> result;
std::set_union( a.begin(), a.end(),
b.begin(), b.end(),
std::inserter( result, result.begin() ) );
return( result );
}
template < typename T, typename Alloc, typename Compare >
std::set<T,Alloc,Compare> operator+ ( std::set<T,Alloc,Compare> const & a,
T const & t ) {
std::set<T,Alloc,Compare> result ( a );
result.insert( t );
return( result );
}
template < typename T, typename Alloc, typename Compare >
std::set<T,Alloc,Compare> & operator+= ( std::set<T,Alloc,Compare> & me, T
const & other ) {
me.insert( other );
return ( me );
}
template < typename T, typename Alloc, typename Compare >
std::set<T,Alloc,Compare> & operator+= ( std::set<T,Alloc,Compare> & me,
std::set<T,Alloc,Compare> const & other ) {
std::copy( other.begin(), other.end(),
std::inserter( me, me.begin() ) );
return( me );
}
// intersection:
// =============
template < typename T, typename Alloc, typename Compare >
std::set<T,Alloc,Compare> operator* ( std::set<T,Alloc,Compare> const & a,
std::set<T,Alloc,Compare> const & b ) {
std::set<T,Alloc,Compare> result;
std::set_intersection( a.begin(), a.end(),
b.begin(), b.end(),
std::inserter( result, result.begin() ) );
return( result );
}
template < typename T, typename Alloc, typename Compare >
std::set<T,Alloc,Compare> operator* ( std::set<T,Alloc,Compare> const & a,
T const & t ) {
std::set<T,Alloc,Compare> result;
result.insert( t );
result = result * a;
return( result );
}
template < typename T, typename Alloc, typename Compare >
std::set<T,Alloc,Compare> & operator*= ( std::set<T,Alloc,Compare> & me, T
const & other ) {
me = me * other;
return ( me );
}
template < typename T, typename Alloc, typename Compare >
std::set<T,Alloc,Compare> & operator*= ( std::set<T,Alloc,Compare> & me,
std::set<T,Alloc,Compare> const & other ) {
me = me * other;
return( me );
}
// difference:
// ===========
template < typename T, typename Alloc, typename Compare >
std::set<T,Alloc,Compare> operator- ( std::set<T,Alloc,Compare> const & a,
std::set<T,Alloc,Compare> const & b ) {
std::set<T,Alloc,Compare> result;;
std::set_difference( a.begin(), a.end(),
b.begin(), b.end(),
std::inserter( result, result.begin() ) );
return( result );
}
template < typename T, typename Alloc, typename Compare >
std::set<T,Alloc,Compare> operator- ( std::set<T,Alloc,Compare> const & a,
T const & t ) {
std::set<T,Alloc,Compare> result ( a );
result.erase( t );
return( result );
}
template < typename T, typename Alloc, typename Compare >
std::set<T,Alloc,Compare> & operator-= ( std::set<T,Alloc,Compare> & me,
std::set<T,Alloc,Compare> const & other ) {
for ( typename std::set<T,Alloc,Compare>::const_iterator iter =
other.begin();
iter != other.end(); ++ iter ) {
me.erase( *iter );
}
return( me );
}
template < typename T, typename Alloc, typename Compare >
std::set<T,Alloc,Compare> & operator-= ( std::set<T,Alloc,Compare> & me,
T const & t ) {
me.erase( t );
return ( me );
}
// symmetric difference:
// =====================
template < typename T, typename Alloc, typename Compare >
std::set<T,Alloc,Compare> operator^ ( std::set<T,Alloc,Compare> const & a,
std::set<T,Alloc,Compare> const & b ) {
std::set<T,Alloc,Compare> result;
std::set_symmetric_difference( a.begin(), a.end(),
b.begin(), b.end(),
std::inserter( result, result.begin() ) );
return( result );
}
template < typename T, typename Alloc, typename Compare >
std::set<T,Alloc,Compare> operator^ ( std::set<T,Alloc,Compare> const & a,
T const & t ) {
return( a ^ ( std::set<T,Alloc,Compare>() + t ) );
}
template < typename T, typename Alloc, typename Compare >
std::set<T,Alloc,Compare> & operator^= ( std::set<T,Alloc,Compare> & me,
std::set<T,Alloc,Compare> const & other ) {
me = me ^ other;
return( me );
}
template < typename T, typename Alloc, typename Compare >
std::set<T,Alloc,Compare> & operator^= ( std::set<T,Alloc,Compare> & me,
T const & t ) {
me = me ^ t;
return( me );
}
Best
Kai-Uwe Bux