suplibxx  1.3.13
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 <algorithm>
26 #include <set>
27 #include <pcre.h>
28 #include <Exception/Exception.h>
29 
30 #include "colselect.h"
31 
32 namespace suplib {
57 void colselect(
58  const std::vector<std::string>& icolumns,
59  const std::vector<std::string>& exact_add,
60  const std::vector<std::string>& regex_add,
61  const std::vector<std::string>& exact_del,
62  const std::vector<std::string>& regex_del,
63  std::vector<std::string>& ocolumns
64  ) {
65 
66  if ( 0 == (exact_add.size( ) + regex_add.size( ) +
67  exact_del.size( ) + regex_del.size( )) ) {
68  ocolumns = icolumns;
69 
70  } else {
71  std::set<std::string>
72  add_set, exact_add_set, regex_add_set,
73  del_set, exact_del_set, regex_del_set,
74  column_set;
75 
76  exact_add_set.insert( exact_add.begin( ), exact_add.end( ) );
77  regex_add_set.insert( regex_add.begin( ), regex_add.end( ) );
78  exact_del_set.insert( exact_del.begin( ), exact_del.end( ) );
79  regex_del_set.insert( regex_del.begin( ), regex_del.end( ) );
80  column_set.insert( icolumns.begin( ), icolumns.end( ) );
81 
82  ocolumns.clear();
83 
84  // Add exact matches...
85  set_intersection( column_set.begin( ), column_set.end( ),
86  exact_add_set.begin( ), exact_add_set.end( ),
87  inserter( add_set, add_set.begin( ) )
88  );
89 
90  // Add regex matches...
91  for ( const auto& pat : regex_add_set )
92  for ( auto& nam : column_set )
93  if ( match( nam, pat ) )
94  add_set.insert( nam );
95 
96  // Remove exact matches...
97  set_difference( column_set.begin( ), column_set.end( ),
98  exact_del_set.begin( ), exact_del_set.end( ),
99  inserter( del_set, del_set.begin( ) )
100  );
101 
102  // Remove regex matches...
103  for ( auto& pat : regex_del_set )
104 
105  for ( auto nam = del_set.begin( ); nam != del_set.end( ) ; ) {
106  if ( match( *nam, pat ) )
107  nam = del_set.erase( nam );
108  else
109  ++nam;
110  }
111 
112  if ( exact_add.size( ) + regex_add.size( ) ) {
113  std::set<std::string> ocolumn_set;
114  set_intersection( del_set.begin( ), del_set.end( ),
115  add_set.begin( ), add_set.end( ),
116  inserter( ocolumn_set, ocolumn_set.begin( ) ) );
117 
118  for ( std::vector<std::string>::size_type idx = 0; idx < icolumns.size( ); idx++ ) {
119  if ( ocolumn_set.end( ) != ocolumn_set.find( icolumns[idx] ) ) {
120  ocolumns.push_back( icolumns[idx] );
121 
122  }
123  }
124  } else {
125  for ( std::vector<std::string>::size_type idx = 0; idx != icolumns.size( ); idx++ ) {
126  if ( del_set.end( ) != del_set.find( icolumns[idx] ) ) {
127  ocolumns.push_back( icolumns[idx] );
128 
129  }
130  }
131  }
132  }
133 }
134 
135 } // namespace suplib
The suplib namespace encompasses all of the functions in the suplib++ library.
bool match(const std::string &str, const std::string &pattern)
handles Perl regular expression matching.
Definition: match.cc:50
void colselect(const std::vector< std::string > &icolumns, const std::vector< std::string > &exact_add, const std::vector< std::string > &regex_add, const std::vector< std::string > &exact_del, const std::vector< std::string > &regex_del, std::vector< std::string > &ocolumns)
select columns based on exact/regex matching/exclusion.
Definition: colselect.cc:57