////////////////////////////////////////////////////////////////////////////////
// 
// VertexFacetTable.hh 
//
//    produced: 27/02/98 jr
//
////////////////////////////////////////////////////////////////////////////////
#ifndef VERTEXFACETTABLE_HH
#define VERTEXFACETTABLE_HH

//#include <bool.h>

#include "SimplicialComplex.hh"

#include "CommandlineOptions.hh"

#ifdef TOPCOM_CONTAINERS
#include "PlainArray.hh"
namespace topcom {
  typedef PlainArray<FastSimplicialComplex>   vertex_facet_table_data;
};
#else
#include <vector>
namespace topcom {
  typedef std::vector<FastSimplicialComplex>  vertex_facet_table_data;
};
#endif

namespace topcom {

  class VertexFacetTable : public vertex_facet_table_data {

    // this class is a data structure mapping each element
    // to the set of simplices stored in a SimplicialComplex containing it;
    // it is mainly use for finding faces in the underlying SimplicialComplex;
    // as for SimplicialComplex, absolutely no guarantees are made
    // concerning whether or not these simplices
    // are facets or closed under taking faces:
  private:
    FastSimplicialComplex _union_complex;
  public:
    inline VertexFacetTable();
    inline VertexFacetTable(const parameter_type);
    inline VertexFacetTable(const VertexFacetTable&);
    inline VertexFacetTable(VertexFacetTable&&);
    inline ~VertexFacetTable();
    
    // additional constructors:
    VertexFacetTable(const parameter_type,
		     const FastSimplicialComplex&);

    // assignment:
    inline VertexFacetTable& operator=(const VertexFacetTable&);
    inline VertexFacetTable& operator=(VertexFacetTable&&);
    
    // additional accessors:
    inline const FastSimplicialComplex& facets(const parameter_type) const;
    
    // writable accessors:
    inline FastSimplicialComplex* facetsptr(const parameter_type);
    
    // basic operations:
    inline VertexFacetTable& operator+=(const Simplex&);
    inline VertexFacetTable& operator-=(const Simplex&);
    inline VertexFacetTable& operator+=(const FastSimplicialComplex&);
    inline VertexFacetTable& operator-=(const FastSimplicialComplex&);
    
    // additional functions:
    operator FastSimplicialComplex() const;
    FastSimplicialComplex star(const Simplex&) const;
    FastSimplicialComplex link(const Simplex&) const;
    void remove_star(const Simplex&, SimplicialComplex&) const;
    const bool contains(const Simplex&) const;
    const bool contains_face(const Simplex&, const parameter_type, const FastSimplicialComplex&) const;
    const bool contains_face(const Simplex&, const FastSimplicialComplex&) const;
    const bool contains_free_face(const Simplex&, const parameter_type, 
				  const FastSimplicialComplex&, Simplex&) const;
    inline const bool contains_free_face(const Simplex&, const FastSimplicialComplex&, Simplex&) const;
  };

  // constructors:
  inline VertexFacetTable::VertexFacetTable() :
    vertex_facet_table_data(),
    _union_complex() {}

  inline VertexFacetTable::VertexFacetTable(const parameter_type no) :
    vertex_facet_table_data(no),
    _union_complex() {}
  
  inline VertexFacetTable::VertexFacetTable(const VertexFacetTable& vft) :
    vertex_facet_table_data(vft),
    _union_complex(vft._union_complex) {}

  inline VertexFacetTable::VertexFacetTable(VertexFacetTable&& vft) :
    vertex_facet_table_data(std::move(static_cast<vertex_facet_table_data>(vft))),
    _union_complex(std::move(vft._union_complex)) {}

  // destructor:
  inline VertexFacetTable::~VertexFacetTable() {}

  // assignment:
  inline VertexFacetTable& VertexFacetTable::operator=(const VertexFacetTable& vft) {
    if (this == &vft) {
      return *this;
    }
    vertex_facet_table_data::operator=(vft);
    _union_complex = vft._union_complex;
    return *this;
  }
  
  inline VertexFacetTable& VertexFacetTable::operator=(VertexFacetTable&& vft) {
    if (this == &vft) {
      return *this;
    }
    vertex_facet_table_data::operator=(std::move(vft));
    _union_complex = std::move(vft._union_complex);
    return *this;
  }

  // additional accessors:
  inline const FastSimplicialComplex& VertexFacetTable::facets(const parameter_type n) const {
    return (*this)[n];
  }

  // writable accessors:
  inline FastSimplicialComplex* VertexFacetTable::facetsptr(const parameter_type n) {
    return &(*this)[n];
  }

  // basic operations:
  inline VertexFacetTable& VertexFacetTable::operator+=(const Simplex& simp) {

    // the data must have been resized appropriately:
    for (Simplex::const_iterator iter = simp.begin(); iter != simp.end(); ++iter) {
      (*this)[*iter] += simp;
    }
    _union_complex += simp;
    return *this;
  } 

  inline VertexFacetTable& VertexFacetTable::operator-=(const Simplex& simp) {
    for (Simplex::const_iterator iter = simp.begin(); iter != simp.end(); ++iter) {
      (*this)[*iter] -= simp;
    }
    _union_complex -= simp;
    return *this;
  }

  inline VertexFacetTable& VertexFacetTable::operator+=(const FastSimplicialComplex& sc) {
    for (SimplicialComplex::const_iterator iter = sc.begin();
	 iter != sc.end();
	 ++iter) {
      (*this) += *iter;
    }
    return *this;
  } 

  inline VertexFacetTable& VertexFacetTable::operator-=(const FastSimplicialComplex& sc) {
    for (SimplicialComplex::const_iterator iter = sc.begin();
	 iter != sc.end();
	 ++iter) {
      (*this) -= *iter;
    }
    return *this;
  }

  inline const bool VertexFacetTable::contains_face(const Simplex& simp, 
						    const FastSimplicialComplex& sub) const {
    const parameter_type card = simp.card();
    return contains_face(simp, card, sub);
  }

  inline const bool VertexFacetTable::contains_free_face(const Simplex& simp, 
							 const FastSimplicialComplex& sub,
							 Simplex& facet) const {
    const parameter_type card = simp.card();
    return contains_free_face(simp, card, sub, facet);
  }

}; // namespace topcom

#endif
