#ifndef SIMPLEXD2INDEXED_H
#define SIMPLEXD2INDEXED_H

#include <typedefs.h>
#include <sequencesequal.h>


/*!
\brief Indexed triangle.

The indexes refer to points.
 These may or may not be ordered depending on the
 application.
*/
template< typename INDX=uint >
class simplexD2indexed
{
public:

  /** Indexes to points or vertexes. */
  INDX pi[3];

  /** Construct a null triangle. */
  simplexD2indexed()
    { pi[0] = pi[1] = pi[2] = 0; }
  /** Construct a triangle. */
  simplexD2indexed(INDX const a, INDX const b, INDX const c)
    { pi[0]=a; pi[1]=b; pi[2]=c; }

  /** Are two triangles the same? */
  boolc operator == (simplexD2indexed<INDX> const & t) const;

  /** Set all fields to 0. */
  void setnull()
    { pi[0] = pi[1] = pi[2] = 0; }
  /** Is this a triangle? Are all the fields set to zero? */
  boolc isnull() const;

  /** Global << calls this. */
  ostream & print(ostream & os) const
    { return os << pi[0] << " " << pi[1] << " " << pi[2]; }

  /** Serialize this object. */
  operator stringc () const;
};

template< typename INDX >
ostream & operator << ( ostream & os, simplexD2indexed<INDX> const & s)
  { return s.print(os); }

//---------------------------------------------------------
// Implementation

template< typename INDX >
simplexD2indexed<INDX>::operator stringc () const
{
  stringstream ss;
  ss << pi[0] << " " << pi[1] << " " << pi[2];
  return ss.str();
}

template< typename INDX >
boolc simplexD2indexed<INDX>::operator == 
(
  simplexD2indexed<INDX> const & t
) const
{
//  for (uint k=0; k<3; ++k)
//    if (((pi[k]==t.pi[0])||(pi[k]==t.pi[1])||(pi[k]==t.pi[2]))==false)
//      return false;

  return sequencesequal3(pi,t.pi);
}

template< typename INDX >
boolc simplexD2indexed<INDX>::isnull() const
{
//  I have commented this out but I have no idea why I wrote this code.
//  It is here in case something breaks as continuous integration(the death star)
//  is not operational.
/*
  if (pi[0]==0)
    return true;

  if (pi[1]==0)
    return true;

  if (pi[2]==0)
    return true;

  return false;
*/

  if (pi[0]!=0)
    return false;

  if (pi[1]!=0)
    return false;

  if (pi[2]!=0)
    return false;

  return true;
}



#endif



