rdbstats  2.0.9
SelectedCols.cc
1 // File: SelectedCols.cc
2 
3 // --8<--8<--8<--8<--
4 //
5 // Copyright (C) 2006 Smithsonian Astrophysical Observatory
6 //
7 // This file is part of rdbstats
8 //
9 // rdbstats is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License
11 // as published by the Free Software Foundation; either version 2
12 // of the License, or (at your option) any later version.
13 //
14 // rdbstats is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the
21 // Free Software Foundation, Inc.
22 // 51 Franklin Street, Fifth Floor
23 // Boston, MA 02110-1301, USA
24 //
25 // -->8-->8-->8-->8--
26 
27 #include <iterator>
28 #include <suplibxx/colselect.h>
29 
30 #include "SelectedCols.h"
31 
32 static void
33 update_column_definitions( RDB& rdbtable, const DefOverrideList& override_defn ) {
34 
35  for ( const auto& odef : override_defn ) {
36 
37  auto column = rdbtable.getColumn( odef.colname );
38 
39  std::string type;
40  switch ( odef.type ) {
41 
42  case OverrideType::Toggle :
43  type = RDBColumn::NUMERIC == column->getType( ) ? "S" : "N";
44  break;
45 
46  case OverrideType::Number :
47  type = "N";
48  break;
49 
50  case OverrideType::String :
51  type = "S";
52  break;
53  }
54 
55  column->setDef( type );
56  }
57 
58 }
59 
60 SelectedCols::SelectedCols( RDB& rdbtable,
61  const std::vector<std::string>& include_exact,
62  const std::vector<std::string>& include_re,
63  const std::vector<std::string>& exclude_exact,
64  const std::vector<std::string>& exclude_re,
65  const std::vector<std::string>& groups,
66  const DefOverrideList& override_defn
67  ) {
68 
69  update_column_definitions( rdbtable, override_defn );
70 
71  // this is the input set of columns for the exclude/include logic
72  std::vector<std::string> icolumns;
73 
74  // select all numeric, non-group columns
75  {
76  size_t num = rdbtable.nColumns( );
77  for ( size_t ii = 0; ii < num; ii++ ) {
78  std::string name = rdbtable.getColumn( ii )->getName( );
79  if ( is_column_numeric( name, rdbtable ) && ! is_groupie( name, groups ) )
80  icolumns.push_back( name );
81  }
82  }
83 
84  // if nothing is specified for include_exact, include_re, or exclude_re
85  // use all of the columns
86  if (
87  include_exact.empty()
88  && include_re.empty()
89  && exclude_exact.empty()
90  && exclude_re.empty()
91  ) {
92  selected_cols = icolumns;
93  return;
94  }
95 
96  // if exact columns were requested, make sure they are existent
97  // numeric columns
98  if ( ! include_exact.empty() ) {
99  for ( const auto& name : include_exact )
100  if ( ! is_column_numeric( name, rdbtable ) )
101  throw Exception( name + "is not numeric" );
102  }
103 
104  suplib::colselect( icolumns, include_exact, include_re, exclude_exact, exclude_re, selected_cols );
105 
106 }
107 
108 bool SelectedCols::is_column_numeric( const std::string& colname,
109  RDB& rdbtable ) {
110 
111  return RDBColumn::NUMERIC == rdbtable.getColumn( colname )->getType( );
112 }
113 
114 
115 bool SelectedCols::is_groupie( const std::string& name,
116  const std::vector<std::string>& groupies ) {
117 
118  std::vector< std::string >::const_iterator current_group( groupies.begin( ) ),
119  end_group( groupies.end( ) );
120  for ( ; current_group != end_group; ++current_group )
121  if ( 0 == name.compare( *current_group ) )
122  return true;
123 
124  return false;
125 
126 }
127 
128 void SelectedCols::print( std::ostream& os ) const {
129 
130  copy( selected_cols.begin( ), selected_cols.end( ),
131  std::ostream_iterator< std::string >( os, " " ) );
132 
133 }