colselect.cc

00001 // --8<--8<--8<--8<--
00002 //
00003 // Copyright (C) 2006 Smithsonian Astrophysical Observatory
00004 //
00005 // This file is part of suplibxx
00006 //
00007 // suplibxx is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // suplibxx is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with this program; if not, write to the 
00019 //       Free Software Foundation, Inc. 
00020 //       51 Franklin Street, Fifth Floor
00021 //       Boston, MA  02110-1301, USA
00022 //
00023 // -->8-->8-->8-->8--
00024 
00025 #include "colselect.h"
00026 
00027 namespace suplib {
00052 void colselect( 
00053   const vector<string>& icolumns,
00054   const vector<string>& exact_add, 
00055   const vector<string>& regex_add,
00056   const vector<string>& exact_del,
00057   const vector<string>& regex_del,
00058   vector<string>& ocolumns
00059   ) throw ( Exception ) {
00060   
00061   if ( 0 == (exact_add.size( ) + regex_add.size( ) + 
00062              exact_del.size( ) + regex_del.size( )) ) {
00063     ocolumns = icolumns;
00064 
00065   } else {
00066     set<string> 
00067       add_set, exact_add_set, regex_add_set, 
00068       del_set, exact_del_set, regex_del_set, 
00069       column_set;
00070 
00071     exact_add_set.insert( exact_add.begin( ), exact_add.end( ) );
00072     regex_add_set.insert( regex_add.begin( ), regex_add.end( ) );
00073     exact_del_set.insert( exact_del.begin( ), exact_del.end( ) );
00074     regex_del_set.insert( regex_del.begin( ), regex_del.end( ) );
00075     column_set.insert(     icolumns.begin( ),  icolumns.end( ) );
00076 
00077     ocolumns.erase( ocolumns.begin( ), ocolumns.end( ) );
00078 
00079     // Add exact matches...
00080     set_intersection( column_set.begin( ), column_set.end( ),
00081                       exact_add_set.begin( ), exact_add_set.end( ),
00082                       inserter( add_set, add_set.begin( ) )
00083                       );
00084 
00085     // Add regex matches...
00086     for ( set<string>::iterator pat = regex_add_set.begin( );
00087           pat != regex_add_set.end( );
00088           pat++ )
00089       for ( 
00090         set<string>::iterator nam = column_set.begin( ); 
00091         nam != column_set.end( ); 
00092         nam++ 
00093         ) 
00094         if ( match( *nam, *pat ) )
00095           add_set.insert( *nam );
00096 
00097     // Remove exact matches...
00098     set_difference( column_set.begin( ), column_set.end( ),
00099                     exact_del_set.begin( ), exact_del_set.end( ),
00100                     inserter( del_set, del_set.begin( ) )
00101                     );
00102     
00103     // Remove regex matches...
00104     for ( set<string>::iterator pat = regex_del_set.begin( );
00105           pat != regex_del_set.end( );
00106           pat++ )
00107       for ( 
00108         set<string>::iterator nam = del_set.begin( ); 
00109         nam != del_set.end( ); 
00110         nam++ 
00111         ) 
00112         if ( match( *nam, *pat ) )
00113           del_set.erase( *nam );
00114 
00115     if ( exact_add.size( ) + regex_add.size( ) ) {
00116       set<string> ocolumn_set;
00117       set_intersection( del_set.begin( ), del_set.end( ),
00118                         add_set.begin( ), add_set.end( ),
00119                         inserter( ocolumn_set, ocolumn_set.begin( ) ) );
00120       
00121       for ( vector<string>::size_type idx = 0; idx < icolumns.size( ); idx++ ) {
00122         if ( ocolumn_set.end( ) != ocolumn_set.find( icolumns[idx] ) ) {
00123           ocolumns.push_back( icolumns[idx] );
00124           
00125         }
00126       }
00127     } else {
00128       for ( vector<string>::size_type idx = 0; idx != icolumns.size( ); idx++ ) {
00129         if ( del_set.end( ) != del_set.find( icolumns[idx] ) ) {
00130           ocolumns.push_back( icolumns[idx] );
00131 
00132         }
00133       }
00134     }
00135   }
00136 }
00137 
00138 } // namespace suplib

Generated on Thu Oct 2 20:49:06 2008 for suplibxx by  doxygen 1.5.6