Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

setutil.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                                                                      |
00003 |                      __   __    ____ _____ ____                      |
00004 |                      \ \ / /_ _/ ___|_   _|___ \                     |
00005 |                       \ V / _` \___ \ | |   __) |                    |
00006 |                        | | (_| |___) || |  / __/                     |
00007 |                        |_|\__,_|____/ |_| |_____|                    |
00008 |                                                                      |
00009 |                               core system                            |
00010 |                                                        (C) SuSE GmbH |
00011 \----------------------------------------------------------------------/
00012 
00013   File:       setutil.h
00014 
00015   Author:     Michael Andres <ma@suse.de>
00016   Maintainer: Michael Andres <ma@suse.de>
00017 
00018   Purpose: Common set operations
00019 
00020 /-*/
00021 #ifndef setutil_h
00022 #define setutil_h
00023 
00024 #include <set>
00025 
00026 /******************************************************************
00027 **
00028 **
00029 **      FUNCTION NAME : includes
00030 **      FUNCTION TYPE : bool
00031 **
00032 **      Return true if lhs includes rhs
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 **      FUNCTION NAME : set_union
00056 **      FUNCTION TYPE : set<_T>
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 **      FUNCTION NAME : set_intersection
00070 **      FUNCTION TYPE : set<_T>
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 **      FUNCTION NAME : set_difference
00096 **      FUNCTION TYPE : set<_T>
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 **      FUNCTION NAME : set_symmetric_difference
00126 **      FUNCTION TYPE : set<_T>
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

Generated on Thu Feb 23 23:56:10 2006 for liby2util by doxygen 1.3.6