/* Copyright (c) 1997-2004
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
   http://www.math.tu-berlin.de/polymake,  mailto:polymake@math.tu-berlin.de

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
*/

#ident "$Project: polymake $$Id: does_contain.cc,v 1.2 2004/06/22 16:23:09 gawrilow Exp $"

#include <Rational.h>
#include <Poly.h>
#include <Set.h>
#include <IncidenceMatrix.h>
#include <argv_options.h>

namespace {
  enum { complement };
}

namespace polymake { namespace polytope {

void does_contain(Poly& p,
		  const char* does_contain_section,
		  const char* complement_section,
		  const char* incidence_section, const char* set_section)
{
   const IncidenceMatrix<> IM=p.give(incidence_section);
   const Set<int> set_to_meet=p.give(set_section);

   Set<int> does_contain_set;
   accumulate_in(entire(select(cols(IM),set_to_meet)),operations::add(),does_contain_set);

   if (does_contain_section) {
      p.take(does_contain_section) << does_contain_set;
   }
   if (complement_section) {
      Set<int> complement_set = sequence(0,IM.rows()) - does_contain_set;
      p.take(complement_section) << complement_set;
   }
}

} } 

using namespace polymake;

/** @file does_contain
 *
 *  Finds all rows in an incidence matrix which do contain elements from a given set.
 *
 *  @synopsis does_contain <file> <does_contain_section> [ <complement_section> | -complement ] <incidence_section> <set_section>
 *
 *  @reading <incidence_section> <set_section>
 *  @writing <does_contain_section>
 */

int main(int argc, const char *argv[])
{
   argv_option options[]={ { "-complement" } };
   if (!extract_options(argc,argv,1,options) || argc < 5 || argc > 6-(options[complement] != 0)) {
      cerr << "usage: " << argv[0] << " <file> <does_contain_section> [ <complement_section> | -complement ] <incidence_section> <set_section>" << endl;
      return 1;
   }
   try {
      Poly p(argv[1], ios::in | ios::out);
      const bool require_both=argc==6;
      polytope::does_contain(p, require_both || !options[complement] ? argv[2] : 0,
			        require_both || options[complement] ? argv[3] : 0,
			     argv[argc-2], argv[argc-1]);
   } catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}
