00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef setutil_h
00022 #define setutil_h
00023
00024 #include <set>
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 template<typename _T>
00035 bool includes( const std::set<_T> & lhs, const std::set<_T> & rhs )
00036 {
00037 typename std::set<_T>::const_iterator li = lhs.begin();
00038 typename std::set<_T>::const_iterator ri = rhs.begin();
00039 while ( li != lhs.end() && ri != rhs.end() ) {
00040 if ( *ri < *li ) {
00041 return false;
00042 } else if ( *li < *ri ){
00043 ++li;
00044 } else {
00045 ++li;
00046 ++ri;
00047 }
00048 }
00049 return( ri == rhs.end() );
00050 }
00051
00052
00053
00054
00055
00056
00057
00058 template<typename _T>
00059 std::set<_T> set_union( const std::set<_T> & lhs, const std::set<_T> & rhs )
00060 {
00061 std::set<_T> res( lhs );
00062 res.insert( rhs.begin(), rhs.end() );
00063 return res;
00064 }
00065
00066
00067
00068
00069
00070
00071
00072 template<typename _T>
00073 std::set<_T> set_intersection( const std::set<_T> & lhs, const std::set<_T> & rhs )
00074 {
00075 std::set<_T> res;
00076 typename std::set<_T>::const_iterator li = lhs.begin();
00077 typename std::set<_T>::const_iterator ri = rhs.begin();
00078 while ( li != lhs.end() && ri != rhs.end() ) {
00079 if ( *li < *ri ) {
00080 ++li;
00081 } else if ( *ri < *li ) {
00082 ++ri;
00083 } else {
00084 res.insert( res.end(), *li );
00085 ++li;
00086 ++ri;
00087 }
00088 }
00089 return res;
00090 }
00091
00092
00093
00094
00095
00096
00097
00098 template<typename _T>
00099 std::set<_T> set_difference( const std::set<_T> & lhs, const std::set<_T> & rhs )
00100 {
00101 std::set<_T> res;
00102 typename std::set<_T>::const_iterator li = lhs.begin();
00103 typename std::set<_T>::const_iterator ri = rhs.begin();
00104 while ( li != lhs.end() && ri != rhs.end() ) {
00105 if ( *li < *ri ) {
00106 res.insert( res.end(), *li );
00107 ++li;
00108 } else if ( *ri < *li ) {
00109 ++ri;
00110 } else {
00111 ++li;
00112 ++ri;
00113 }
00114 }
00115 while ( li != lhs.end() ) {
00116 res.insert( res.end(), *li );
00117 ++li;
00118 }
00119 return res;
00120 }
00121
00122
00123
00124
00125
00126
00127
00128 template<typename _T>
00129 std::set<_T> set_symmetric_difference( const std::set<_T> & lhs, const std::set<_T> & rhs )
00130 {
00131 std::set<_T> res;
00132 typename std::set<_T>::const_iterator li = lhs.begin();
00133 typename std::set<_T>::const_iterator ri = rhs.begin();
00134 while ( li != lhs.end() && ri != rhs.end() ) {
00135 if ( *li < *ri ) {
00136 res.insert( res.end(), *li );
00137 ++li;
00138 } else if ( *ri < *li ) {
00139 res.insert( res.end(), *ri );
00140 ++ri;
00141 } else {
00142 ++li;
00143 ++ri;
00144 }
00145 }
00146 while ( li != lhs.end() ) {
00147 res.insert( res.end(), *li );
00148 ++li;
00149 }
00150 while ( ri != rhs.end() ) {
00151 res.insert( res.end(), *ri );
00152 ++ri;
00153 }
00154 return res;
00155 }
00156
00157 #endif // setutil_h