/* Copyright (c) 1997-2005
   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: morse_matching_n_critical_faces.cc,v 1.4 2006/01/20 14:26:06 gawrilow Exp $"


#include <Poly.h>
#include <HasseDiagram.h>

#include <morse_matching_tools.h>


using namespace polymake;




/** @file morse_matching_n_critical_faces
 *
 *  Input: Morse matching
 *
 *  Output: number of critical cells in Morse matching
 *
 *  @client   morse_matching_n_critical_faces
 *  @synopsis morse_matching_n_critical_faces <file>
 *
 *  @reading  MORSE_MATCHING
 *  @writing  MORSE_MATCHING_N_CRITICAL_FACES
 *
 *  @author   Marc Pfetsch
 *  @date     Fall 2005
 */

int main(int argc, const char* argv[])
{
   typedef Entire<HasseDiagram<Set<int>,bool>::out_edge_list>::const_iterator HasseDiagramOutConstIterator;

   if (argc != 2)
   {
      cerr << "usage: " << argv[0] << " <file> " << endl;
      return 1;
   }

   // read Hasse diagram
   try
   {
      Poly p(argv[1], ios::in | ios::out);
      HasseDiagram<Set<int>,bool> M;

      // read Morse
      if ( p.exists("MORSE_MATCHING") )
      {
	 p.give("MORSE_MATCHING") >> M;

	 // get critical faces
	 Bitset critical = collectCriticalFaces(M);

	 p.take("MORSE_MATCHING_N_CRITICAL_FACES") << critical.size();
      }
   }
   catch (const std::exception& e)
   {
      std::cerr << e.what() << endl;
      abort();
   }

   return 0;
}
