/* 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: deletion.cc,v 1.1 2004/12/14 10:15:39 witte Exp $"

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

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

namespace polymake { namespace topaz {
  
void deletion(Poly& p_out, Poly& p_in, Set<int> F, std::list<std::string> labeled_F,
	      const bool no_labels, const std::string& comment)
{
   const Array< Set<int> > C = p_in.give("FACETS");
   const Array<std::string> L = p_in.give("VERTEX_LABELS");
   const int n_vert = L.size();
   
   if (F.empty())  // face spefified by labels
      F = transform_label_args(L, labeled_F);
   
   if (F.front()<0 || F.back()>n_vert-1)
      throw std::runtime_error("deletion: Specified vertex indices out of range");
   
   std::list< Set<int> > Deletion;
   std::copy(entire(deletion(C,F)), back_inserter(Deletion));
   
   if ( int(Deletion.size())==int(C.size()) ) {
      std::ostringstream e;
      wrap(e) << "deletion: " << F << " does not specify a face.";
      throw std::runtime_error(e.str());
   }
   
   const Set<int> V = accumulate(Deletion, operations::add());
   adj_numbering(Deletion,V);
   
   p_out.take("DESCRIPTION") << comment;
   if (Deletion.empty())  p_out.take("FACETS") << Set<int>();
   else                   p_out.take("FACETS") << as_array(Deletion);
   
   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 deletion
 *
 *  Removes the vertex star of a given face, specified by
 *  it's vertices.
 *
 *  Vertices can be specified by their indices (using the @c -v option)
 *  or by their labels (using the @c -l option).
 *  Indices may be specified individually or as a sequence.
 *  e.g. {1 7...9 20} = {1 7 8 9 20}. (perl lovers might use just two points
 *  to indicate a sequence.)
 *
 *  The @c -nol flag tells the client not to write any labels.
 *
 *  @synopsis deletion <out_file> <in_file> { -v <v_1> <v_2>...<v_i> ... | -l <l_1> <l_2> ... } [ -nol ]
 *
 *  @reading  FACETS, 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" } };
   enum { no_labels };
   if ( !extract_options(argc,argv,4,options) ||
	(strcmp(argv[3], "-v") && strcmp(argv[3], "-l"))) {
      cerr << "usage: " << argv[0] << " <out_file> <in_file> { -v <v_1> <v_2>...<v_i> ... | -l <l_1> <l_2> ... } [ -nol ]" << endl;
      return 1;
   }
   try {
      Poly p_in(argv[2], ios::in);
      Poly p_out(argv[1], ios::out | ios::trunc, p_in.type());
      
      // reading vertices
      Set<int> F;
      std::list<std::string> labeled_F;
      if (argv[3][1]=='v')
	 F = topaz::read_int_args_as_set(argc, argv, 4);
      else
	 labeled_F = topaz::read_str_args(argc, argv, 4);
      
      std::ostringstream comment;
      comment << "Deletion of ";
      if (F.empty())  wrap(comment) << labeled_F;
      else            wrap(comment) << F;
      comment << " in " << argv[2] << ".";

      topaz::deletion(p_out, p_in, F, labeled_F, options[no_labels], comment.str());
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}


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