/* Copyright (c) 1997-2002
   Technische Universitaet Berlin, Germany
   Department of Mathematics,
   Research Group Algorithmic and Discrete Mathematics
   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.

   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.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, 59 Temple Place - Suite 330 Boston, MA 02111-1307, USA.
*/

#ident "$Project: polymake $$Id: 2-face-sizes.cc,v 1.3 2006/02/23 11:39:29 thilosch Exp $"

#include <ext/hash_map>

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

using namespace polymake;

namespace{ 

   enum { primal, dual };

}

namespace polytope {

void two_face_sizes(Poly& p, const char* output_section, const argv_option options[]) {
      HasseDiagram<> G = p.give("HASSE_DIAGRAM");

      std_ext::hash_map<int,int> two_face_map, codim3_face_map;
      
      if(options[primal]) {
	 for (Entire<sequence>::iterator f=entire(G.nodes_of_dim(2)); !f.at_end(); ++f) {
	    const int this_size(G.in_degree(*f));
	    if (two_face_map.find(this_size)==two_face_map.end())
	       two_face_map[this_size]=1;
	    else
	       ++two_face_map[this_size];
	 }
	 p.take(output_section) << two_face_map;
      } else if(options[dual]){
	 for (Entire<sequence>::iterator f=entire(G.nodes_of_dim(-3)); !f.at_end(); ++f) {
	    const int this_size(G.out_degree(*f));
	    if (codim3_face_map.find(this_size)==codim3_face_map.end()) 
	       codim3_face_map[this_size]=1; 
	    else 
	       ++codim3_face_map[this_size]; 
	 }
	 p.take(output_section) <<codim3_face_map;
      }
} }

using namespace polymake;

/** @file 2-face-sizes
 *
 *  Compute the sizes of all 2-faces and codim-3-faces.
 *
 *  @synopsis 2-face-sizes <file> <output_section> { -primal | -dual }
 *
 *  @reading  HASSE_DIAGRAM
 *  @writing  <output_section>
 */
int main(int argc, const char *argv[]) {
   argv_option options[]={ { "-primal" }, { "-dual" } };
   if (argc != 4 ||
       !extract_options(argc,argv,1,options) ||
       argc != 3) {
      cerr << "usage: " << argv[0] << " <file> <output_section> { -primal | -dual }" << endl;
      return 1;
   }
   try {
      Poly p(argv[1], ios::in | ios::out);
      polytope::two_face_sizes(p,argv[2],options);
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}
