/* 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: remove_vertex_star.cc,v 1.18 2004/10/26 18:57:00 gawrilow Exp $"

#include <Poly.h>
#include <complex_tools.h>
#include <argv_options.h>

namespace {
   enum { no_labels, specified_vertex, specified_label };
}

namespace polymake { namespace topaz {
  
void remove_vertex_star(Poly& p_out, Poly& p_in,
			const argv_option *options, std::ostringstream& comment)
{
   HasseDiagram<> HD = p_in.give("HASSE_DIAGRAM");
   const Array<std::string> L = p_in.give("VERTEX_LABELS");
   const int n = L.size();

   int v;
   if (options[specified_vertex]) {  // vertex specified as index   
      v=atoi(options[specified_vertex]);
      if (v<0 || v>=n)
	 throw std::runtime_error("remove_vertex_star: Vertex index out of range");
      comment << v << '.';

   } else { // vertex specified as label  
      v = std::find(L.begin(), L.end(), (const char*)options[specified_label]) - L.begin();
      if (L.size() == v)
	 throw std::runtime_error("remove_vertex_star: Unknown vertex label");
      comment << (const char*)options[specified_label] << '.';
   }
     
   remove_vertex_star(HD,v);
   
   std::list< Set<int> > C_out;
   for (Entire<HasseDiagram<>::in_edge_list>::const_iterator e=entire(HD.in_edges(HD.top_node()));
	!e.at_end(); ++e) 
      C_out.push_back(HD.node(e.from_node()));

   // adj numbering
   const Set<int> V = accumulate(C_out, operations::add());
   adj_numbering(C_out, V);
   
   p_out.take("DESCRIPTION") << comment.str();
   p_out.take("FACETS") << as_array(C_out);
   
   if (!no_labels) {
      const Array<std::string> new_L(V.size(), select(L,V).begin());
      p_out.take("VERTEX_LABELS") << new_L;
   }
}

} }

using namespace polymake;

/** @file remove_vertex_star
 *
 *  Removes the vertex star of a given vertex, specified by
 *  it's index or label.
 *
 *  The @c -nol flag tells the client not to write any labels.
 *
 *  @synopsis remove_vertex_star <out_file> <in_file> { -v <vertex> | -l <vertex> } [ -nol ]
 *
 *  @reading  HASSE_DIAGRAM, VERTEX_LABELS
 *  @writing  DESCRIPTION, FACETS, may write VERTEX_LABELS
 *
 *  @index clients Producing a new simplicial complex from others
 */

int main(int argc, const char* argv[])
{
   argv_option options[]={ { "-nol" }, { "-v", true }, { "-l", true } };
   if (!extract_options(argc,argv,2,options) ||
       bool(options[specified_vertex]) == bool(options[specified_label])) {
      cerr << "usage: " << argv[0] << " <out_file> <in_file> { -v <vertex> | -l <vertex_label> } [ -nol ]" << endl;
      return 1;
   }
   try {
      Poly p_in(argv[2], ios::in);
      Poly p_out(argv[1], ios::out | ios::trunc, p_in.type());

      std::ostringstream comment;
      comment << argv[2] << " after removing the star of ";
      topaz::remove_vertex_star(p_out, p_in, options, comment);
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}


// Local Variables:
// mode:C++
// c-basic-offset:3
// End:
