suplibxx  1.3.9
colselect.cc
1 // --8<--8<--8<--8<--
2 //
3 // Copyright (C) 2006 Smithsonian Astrophysical Observatory
4 //
5 // This file is part of suplibxx
6 //
7 // suplibxx is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // suplibxx is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the
19 // Free Software Foundation, Inc.
20 // 51 Franklin Street, Fifth Floor
21 // Boston, MA 02110-1301, USA
22 //
23 // -->8-->8-->8-->8--
24 
25 #include "colselect.h"
26 
27 namespace suplib {
52 void colselect(
53  const vector<string>& icolumns,
54  const vector<string>& exact_add,
55  const vector<string>& regex_add,
56  const vector<string>& exact_del,
57  const vector<string>& regex_del,
58  vector<string>& ocolumns
59  ) throw ( Exception ) {
60 
61  if ( 0 == (exact_add.size( ) + regex_add.size( ) +
62  exact_del.size( ) + regex_del.size( )) ) {
63  ocolumns = icolumns;
64 
65  } else {
66  set<string>
67  add_set, exact_add_set, regex_add_set,
68  del_set, exact_del_set, regex_del_set,
69  column_set;
70 
71  exact_add_set.insert( exact_add.begin( ), exact_add.end( ) );
72  regex_add_set.insert( regex_add.begin( ), regex_add.end( ) );
73  exact_del_set.insert( exact_del.begin( ), exact_del.end( ) );
74  regex_del_set.insert( regex_del.begin( ), regex_del.end( ) );
75  column_set.insert( icolumns.begin( ), icolumns.end( ) );
76 
77  ocolumns.erase( ocolumns.begin( ), ocolumns.end( ) );
78 
79  // Add exact matches...
80  set_intersection( column_set.begin( ), column_set.end( ),
81  exact_add_set.begin( ), exact_add_set.end( ),
82  inserter( add_set, add_set.begin( ) )
83  );
84 
85  // Add regex matches...
86  for ( set<string>::iterator pat = regex_add_set.begin( );
87  pat != regex_add_set.end( );
88  pat++ )
89  for (
90  set<string>::iterator nam = column_set.begin( );
91  nam != column_set.end( );
92  nam++
93  )
94  if ( match( *nam, *pat ) )
95  add_set.insert( *nam );
96 
97  // Remove exact matches...
98  set_difference( column_set.begin( ), column_set.end( ),
99  exact_del_set.begin( ), exact_del_set.end( ),
100  inserter( del_set, del_set.begin( ) )
101  );
102 
103  // Remove regex matches...
104  for ( set<string>::iterator pat = regex_del_set.begin( );
105  pat != regex_del_set.end( );
106  pat++ )
107  for (
108  set<string>::iterator nam = del_set.begin( );
109  nam != del_set.end( );
110  nam++
111  )
112  if ( match( *nam, *pat ) )
113  del_set.erase( *nam );
114 
115  if ( exact_add.size( ) + regex_add.size( ) ) {
116  set<string> ocolumn_set;
117  set_intersection( del_set.begin( ), del_set.end( ),
118  add_set.begin( ), add_set.end( ),
119  inserter( ocolumn_set, ocolumn_set.begin( ) ) );
120 
121  for ( vector<string>::size_type idx = 0; idx < icolumns.size( ); idx++ ) {
122  if ( ocolumn_set.end( ) != ocolumn_set.find( icolumns[idx] ) ) {
123  ocolumns.push_back( icolumns[idx] );
124 
125  }
126  }
127  } else {
128  for ( vector<string>::size_type idx = 0; idx != icolumns.size( ); idx++ ) {
129  if ( del_set.end( ) != del_set.find( icolumns[idx] ) ) {
130  ocolumns.push_back( icolumns[idx] );
131 
132  }
133  }
134  }
135  }
136 }
137 
138 } // namespace suplib
bool match(const string &str, const string &pattern)
handles Perl regular expression matching.
Definition: match.cc:45
The suplib namespace encompasses all of the functions in the suplib++ library.
void colselect(const vector< string > &icolumns, const vector< string > &exact_add, const vector< string > &regex_add, const vector< string > &exact_del, const vector< string > &regex_del, vector< string > &ocolumns)
select columns based on exact/regex matching/exclusion.
Definition: colselect.cc:52