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

#include <Poly.h>
#include <read_int_args.h>
#include <read_str_args.h>
#include <Array.h>
#include <Set.h>
#include <FacetList.h>
#include <argv_options.h>

#include <ext/hash_map>
#include <sstream>

namespace polymake {
namespace topaz {

void h_induced_quotient(Poly& p_out, Poly& p_in, const char* apex_label,
			const std::list<std::string> L_in, Set<int> V_in,
			const bool no_labels, const std::string& comment)
{
   const Array< Set<int> > C = p_in.give("FACETS");
   Array<std::string> L = p_in.give("VERTEX_LABELS");
   const int n_vert = L.size();
      
   // creating vertex map
   std_ext::hash_map<std::string,int> map(L.size());
   int count = 0;
   for (Entire< Array<std::string> >::const_iterator l=entire(L);
	!l.at_end(); ++l, ++count)
      map[*l] = count;
   
   const int apex = n_vert;
   
   if (!no_labels) {
      L.resize(n_vert+1);
      
      if (strcmp(apex_label,"")) {  // apex label given
	 if ( map.find(apex_label) != map.end() )  // check if unique
	    throw std::runtime_error("h_induced_quotient: Input label not unique.");
	 
	 L[n_vert] = apex_label;
	 
      } else {
	 std::ostringstream label;
	 label << "apex";
	 std::string l=label.str(), ll=l;
	 
	 // test if ll is unique
	 int i=0;
	 while (map.find(ll) != map.end()) {
	    ++i;
	    label.str("");
	    label << l << "_" << i;
	    ll=label.str();
	 }
	 
	 L[n_vert] = ll;
      }
   }

   // checking input
   if (!L_in.empty())
      for (Entire< std::list<std::string> >::const_iterator l=entire(L_in);
	   !l.at_end(); ++l) {
	 if (map.find(*l) == map.end()) {
	    std::ostringstream e;
	    e << "h_induced_quotient: Input label " << *l << " is not a vertex.";
	    throw std::runtime_error(e.str());
	 }
	 
	 V_in += map[*l];
      }
   
   else
      if (V_in.front()<0 || V_in.back()>n_vert-1)
	 throw std::runtime_error("h_induced_quotient: Specified vertices are not contained in VERTICES.");

   // computing the subcomplex
   FacetList Quotient(C.begin(), C.end());
   for (Entire< Array< Set<int> > >::const_iterator it=entire(C); !it.at_end(); ++it) {
      Set<int> S=V_in*(*it);
      if (!S.empty()) {
	 S+=apex;
	 Quotient.insertMax(S);
      }
   }

   p_out.take("DESCRIPTION") << comment;
   p_out.take("FACETS") << Quotient;		  

   if (!no_labels)
      p_out.take("VERTEX_LABELS") << L;
}
   
} }

using namespace polymake;

/** @file h_induced_quotient
 *
 *  Let $C$ be the given simplicial complex and $A$ the subcomplex induced by
 *  the given vertices. Then this client produces a simplicial complex
 *  homotopy equivalent to $C$ mod $A$, by adding the cone over $A$ with
 *  apex $a$ to $C$. 
 *
 *  The apex $a$ may be specified (by its <i>label</i>) and 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  h_induced_quotient <out_file> <in_file> [ apex ] { -v <v_1> <v_2>...<v_i> ... | -l <l_1> <l_2> ... } [ -nol ]
 *
 *  @reading  FACETS, may read 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) || argc < 3 ) {
      cerr << "usage: " << argv[0] << " <out_file> <in_file> [ apex ] { -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 apex label
      const bool label_given = !options[no_labels] && strcmp(argv[3],"-v") && strcmp(argv[3],"-l");
      const char* apex_label = label_given ? argv[3] : "";
      
      // reading vertices
      Set<int> V_in;
      std::list<std::string> L_in;

      if ( !strcmp(argv[3],"-v") )
	 V_in = topaz::read_int_args_as_set(argc, argv, 4);
      
      if ( !strcmp(argv[4],"-v") )
	 V_in = topaz::read_int_args_as_set(argc, argv, 5);
      
      if ( !strcmp(argv[3],"-l") )
	 L_in = topaz::read_str_args(argc, argv, 4);

      if ( !strcmp(argv[4],"-l") )
	 L_in = topaz::read_str_args(argc, argv, 5);

      std::ostringstream comment;
      comment << "A homotopy equivalent complex to " << argv[2] << " modulo the subcomplexed induced by the vertices ";
      if (L_in.empty())  wrap(comment) << V_in;
      else               wrap(comment) << "labeled "  << L_in;
      comment << ".";
      
      topaz::h_induced_quotient(p_out, p_in, apex_label, L_in, V_in, 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:
