/* 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.
*/

#ifndef _POLYMAKE_FINITE_FIELDS_H
#define _POLYMAKE_FINITE_FIELDS_H "$Project: polymake $$Id: FiniteFields.h,v 1.6 2004/06/22 16:22:56 gawrilow Exp $"

#include <iostream>

namespace polymake { namespace topaz {
   
class GF2 {
   int value;
public:
   GF2(int v=0) : value(v&1) {}

   // identify non-trivial entries for sparse vectors/matrices
   operator void*() const { return value==0? 0 : const_cast<GF2*>(this); }
   bool operator!() const { return value==0? const_cast<GF2*>(this) : 0; }

   const GF2& operator+() const { return *this; }
   const GF2& operator-() const { return *this; }

   friend GF2 operator+ (const GF2 a, const GF2 b) { return GF2(a.value^b.value); }
   friend GF2 operator- (const GF2 a, const GF2 b) { return GF2(a.value^b.value); }
   friend GF2 operator* (const GF2 a, const GF2 b) { return GF2(a.value&b.value); }
   friend GF2 operator/ (const GF2 a, const GF2 b) { return GF2(a.value/b.value); }

   GF2& operator+= (const GF2 b) { value^=b.value; return *this; }
   GF2& operator-= (const GF2 b) { value^=b.value; return *this; }
   GF2& operator*= (const GF2 b) { value&=b.value; return *this; }
   GF2& operator/= (const GF2 b) { value/=b.value; return *this; }

   template <typename Traits> friend
   std::basic_istream<char, Traits>&
   operator>> (std::basic_istream<char, Traits>& is, GF2& a)
   {
      return is >> a.value;
   }

   template <typename Traits> friend
   std::basic_ostream<char, Traits>&
   operator<< (std::basic_ostream<char, Traits>& os, const GF2& a)
   {
      return os << a.value;
   }
};


} }

template <>
struct __type_traits<polymake::topaz::GF2> : __type_traits<int> { };

#endif  // _POLYMAKE_FINITE_FIELDS_H

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