/* Copyright (c) 1997-2006
   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_INTEGER_H
#define _POLYMAKE_INTEGER_H "$Project: polymake $$Id: Integer.h,v 1.35 2006/03/07 18:06:42 gawrilow Exp $"

#if defined(__GNUC__) && __GNUC__==3 && __GNUC_MINOR__<3 && !defined(__APPLE__) && !defined(__COMO__)
#pragma interface
#endif

#include_next <Integer.h>
#ifndef _POLYMAKE_OPERATIONS_BASIC_DEFS_H
#  include <operations_basic_defs.h>
#endif
#ifndef _POLYMAKE_COMPARATORS_BASIC_DEFS_H
#  include <comparators_basic_defs.h>
#endif
#ifndef _POLYMAKE_CONVERTERS_BASIC_DEFS_H
#  include <converters_basic_defs.h>
#endif
#ifndef _POLYMAKE_INLINES_INT_H
#  include <inlines_int.h>
#endif

namespace pm {

template <> inline
cmp_value sign(const Integer& a) { return sign(a.get_rep()->_mp_size); }

template <> inline
Integer& inv_sign(Integer& a) { return a.negate(); }

namespace operations {

template <>
struct cmp_numeric<Integer> : cmp_common<Integer> {
   using cmp_common<Integer>::operator();

   result_type operator() (const Integer& a, const Integer& b) const
   {
      return sign(a.compare(b));
   }
   result_type operator() (const Integer& a, long b) const
   {
      return sign(a.compare(b));
   }
   result_type operator() (long a, const Integer& b) const
   {
      return sign(-b.compare(a));
   }
   result_type operator() (const Integer& a, double b) const
   {
      return sign(a.compare(b));
   }
   result_type operator() (double a, const Integer& b) const
   {
      return sign(-b.compare(a));
   }
   inline result_type operator() (const Integer& a, const Rational& b) const;
   inline result_type operator() (const Rational& a, const Integer& b) const;

   template <typename Iterator2>
   result_type operator() (partial_left, const Integer& a, const Iterator2&) const
   {
      return cmp_value(sign(a));
   }
   template <typename Iterator1>
   result_type operator() (partial_right, const Iterator1&, const Integer& b) const
   {
      return cmp_value(-sign(b));
   }
};
} // end namespace operations

template <>
struct conv<Integer, std::string> {
   typedef Integer argument_type;
   typedef std::string result_type;
   result_type operator() (const Integer& a) const { return a.to_string(); }
};

} // end namespace pm

inline int log2_floor(const Integer& a) {
   int n=mpz_size(a.get_rep())-1;
   return n>=0 ? n*mp_bits_per_limb + log2_floor(mpz_getlimbn(a.get_rep(),n)) : 0;
}
inline int log2_ceil(const Integer& a) {
   int n=mpz_size(a.get_rep())-1;
   return n>=0 ? n*mp_bits_per_limb + log2_ceil(mpz_getlimbn(a.get_rep(),n)) : 0;
}

#endif // _POLYMAKE_INTEGER_H

// Local Variables:
// mode:C++
// End:
