BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/leda_integer.h
Go to the documentation of this file.
00001 // Copyright (c) 1999,2007  Utrecht University (The Netherlands),
00002 // ETH Zurich (Switzerland), Freie Universitaet Berlin (Germany),
00003 // INRIA Sophia-Antipolis (France), Martin-Luther-University Halle-Wittenberg
00004 // (Germany), Max-Planck-Institute Saarbruecken (Germany), RISC Linz (Austria),
00005 // and Tel-Aviv University (Israel).  All rights reserved.
00006 //
00007 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public License as
00009 // published by the Free Software Foundation; version 2.1 of the License.
00010 // See the file LICENSE.LGPL distributed with CGAL.
00011 //
00012 // Licensees holding a valid commercial license may use this file in
00013 // accordance with the commercial license agreement provided with the software.
00014 //
00015 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00016 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00017 //
00018 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Number_types/include/CGAL/leda_integer.h $
00019 // $Id: leda_integer.h 46989 2008-11-23 14:08:51Z afabri $
00020 //
00021 //
00022 // Author(s)     : Andreas Fabri, Michael Hemmer
00023 
00024 #ifndef CGAL_LEDA_INTEGER_H
00025 #define CGAL_LEDA_INTEGER_H
00026 
00027 #include <CGAL/number_type_basic.h>
00028 
00029 #ifdef CGAL_USE_LEDA
00030 
00031 #include <utility>
00032 
00033 #include <CGAL/leda_coercion_traits.h>
00034 #include <CGAL/Interval_nt.h>
00035 
00036 #include <CGAL/LEDA_basic.h>
00037 #if CGAL_LEDA_VERSION < 500
00038 #include <LEDA/integer.h>
00039 #include <LEDA/bigfloat.h>// for To_interval
00040 #else
00041 #include <LEDA/numbers/integer.h>
00042 #include <LEDA/numbers/bigfloat.h>// for To_interval
00043 #endif
00044 
00045 #include <CGAL/Residue.h>
00046 #include <CGAL/Modular_traits.h>
00047 
00048 CGAL_BEGIN_NAMESPACE
00049 
00050 template <> class Algebraic_structure_traits< leda_integer >
00051   : public Algebraic_structure_traits_base< leda_integer,
00052                                             Euclidean_ring_tag >  {
00053   public:
00054     typedef Tag_true            Is_exact;
00055     typedef Tag_false           Is_numerical_sensitive;
00056 
00057     typedef INTERN_AST::Is_square_per_sqrt< Type >
00058                                                                  Is_square;
00059 
00060     class Gcd
00061       : public std::binary_function< Type, Type,
00062                                 Type > {
00063       public:
00064         Type operator()( const Type& x,
00065                                         const Type& y ) const {
00066           // By definition gcd(0,0) == 0
00067           if( x == Type(0) && y == Type(0) )
00068             return Type(0);
00069 
00070           return CGAL_LEDA_SCOPE::gcd( x, y );
00071         }
00072 
00073         CGAL_IMPLICIT_INTEROPERABLE_BINARY_OPERATOR( Type )
00074     };
00075 
00076     // Unfortunately the behaviour of leda has changed here several times
00077     // The following Div_mod is invariant under these changes
00078     // However, the Div and Mod defined below might be more efficient 
00079     // TODO: recover Div Mod implementation for all leda versions
00080     class Div_mod {
00081     public: 
00082         typedef Type first_argument_type;
00083         typedef Type second_argument_type; 
00084         typedef Type& third_argument_type; 
00085         typedef Type& fourth_argument_type; 
00086         typedef void result_type;
00087         
00088         void operator()(const Type& x, const Type& y, Type& q, Type& r) const {
00089             
00090             q = x / y;             
00091             r = x - q*y;
00092             CGAL_postcondition(x == y*q + r);  
00093             
00094             if (r == 0) return;   
00095              
00096             // round q towards zero 
00097             if ( r.sign() != x.sign() ){
00098                 q -= x.sign();
00099                 r -= x.sign()*y;
00100             }
00101 
00102             CGAL_postcondition(x == y*q + r);            
00103             CGAL_postcondition(r.sign() == x.sign());
00104         }  
00105     };
00106     // Div defined via base using Div_mod
00107     // Mod defined via base using Div_mod
00108 
00109     // This code results in an inconsisten div/mod for some leda versions 
00110     // TODO: reactivate this code 
00111 
00112 //     typedef INTERN_AST::Div_per_operator< Type > Div;
00113 //     class Mod
00114 //       : public std::binary_function< Type, Type,
00115 //                                 Type > {
00116 //       public:
00117 //         Type operator()( const Type& x, const Type& y ) const {
00118 //           Type m = x % y;
00119 // #if CGAL_LEDA_VERSION < 520
00120 //           // Fix wrong leda result
00121 //           if( x < 0 && m != 0 )
00122 //             m -= y;
00123 // #elif CGAL_LEDA_VERSION < 600
00124 //           // Fix another wrong leda result
00125 //           if( x < 0 && y > 0 && m != 0 )
00126 //             m -= y;
00127 // #else
00128 //           // Do nothing, it seems to work now!
00129 //           // TODO: be careful for future improvements of LEDA
00130 // #endif
00131 //           return m;
00132 //         }
00133 //         CGAL_IMPLICIT_INTEROPERABLE_BINARY_OPERATOR( Type )
00134 //     };
00135 
00136     class Sqrt
00137       : public std::unary_function< Type, Type > {
00138       public:
00139         Type operator()( const Type& x ) const {
00140           return CGAL_LEDA_SCOPE::sqrt( x );
00141         }
00142     };
00143 };
00144 
00145 template <> class Real_embeddable_traits< leda_integer >
00146   : public INTERN_RET::Real_embeddable_traits_base< leda_integer , CGAL::Tag_true > {
00147   public:
00148   
00149     class Abs
00150       : public std::unary_function< Type, Type > {
00151       public:
00152         Type operator()( const Type& x ) const {
00153             return CGAL_LEDA_SCOPE::abs( x );
00154         }
00155     };
00156 
00157     class Sgn
00158       : public std::unary_function< Type, ::CGAL::Sign > {
00159       public:
00160         ::CGAL::Sign operator()( const Type& x ) const {
00161             return (::CGAL::Sign) CGAL_LEDA_SCOPE::sign( x );
00162         }
00163     };
00164 
00165     class Compare
00166       : public std::binary_function< Type, Type,
00167                                 Comparison_result > {
00168       public:
00169         Comparison_result operator()( const Type& x,
00170                                             const Type& y ) const {
00171           return (Comparison_result) CGAL_LEDA_SCOPE::compare( x, y );
00172         }
00173 
00174     };
00175 
00176     class To_double
00177       : public std::unary_function< Type, double > {
00178       public:
00179         double operator()( const Type& x ) const {
00180           return x.to_double();
00181         }
00182     };
00183 
00184     class To_interval
00185       : public std::unary_function< Type, std::pair< double, double > > {
00186       public:
00187       std::pair<double, double> operator()( const Type& x ) const {
00188         leda::bigfloat h(x);
00189         double abs_err = 0;
00190         double  low =h.to_double(abs_err, leda::TO_N_INF);
00191         double high =h.to_double(abs_err, leda::TO_P_INF);
00192         return std::make_pair(low,high);
00193       }
00194     };
00195 };
00196 
00197 template<>
00198 class Modular_traits< ::leda::integer > {
00199     typedef Residue MOD;
00200  public:
00201     typedef ::leda::integer NT;
00202     typedef ::CGAL::Tag_true Is_modularizable;
00203     typedef MOD Residue_type;
00204 
00205     struct Modular_image{
00206         Residue_type operator()(const NT& a){
00207             return Residue_type ((a%NT(MOD::get_current_prime())).to_long());
00208         }
00209     };
00210     struct Modular_image_representative{
00211         NT operator()(const Residue_type& x){
00212             return NT(x.get_value());
00213         }
00214     };    
00215 };
00216 
00217 //
00218 // Needs_parens_as_product
00219 //
00220 template <>
00221 struct Needs_parens_as_product<leda_integer> {
00222   bool operator()(const leda_integer& x) {
00223     return CGAL_NTS is_negative(x);
00224   }
00225 };
00226 
00227 // missing mixed operators
00228 inline
00229 bool
00230 operator==(int a, const leda_integer& b)
00231 { return b == a; }
00232 
00233 inline
00234 bool
00235 operator!=(int a, const leda_integer& b)
00236 { return b != a; }
00237 
00238 
00239 template <>
00240 struct Split_double<leda_integer>
00241 {
00242   void operator()(double d, leda_integer &num, leda_integer &den) const
00243   {
00244     std::pair<double, double> p = split_numerator_denominator(d);
00245     num = leda_integer(p.first);
00246     den = leda_integer(p.second);
00247   }
00248 };
00249 
00250 // Benchmark_rep specialization 
00251 template<>
00252 class Benchmark_rep< leda_integer > {
00253     const leda_integer& t;
00254 public:
00256     Benchmark_rep( const leda_integer& tt) : t(tt) {}
00258     std::ostream& operator()( std::ostream& out) const { 
00259             out << t;
00260             return out;
00261     }
00262     
00263     static std::string get_benchmark_name() {
00264         return "Integer";
00265     }
00266 };
00267 
00268 
00269 CGAL_END_NAMESPACE
00270 
00271 // Unary + is missing for leda::integer
00272 namespace leda {
00273     inline integer operator+( const integer& i) { return i; }
00274 } // namespace leda
00275 
00276 //since types are included by leda_coercion_traits.h:
00277 #include <CGAL/leda_integer.h>
00278 #include <CGAL/leda_rational.h>
00279 #include <CGAL/leda_bigfloat.h>
00280 #include <CGAL/leda_real.h>
00281 
00282 #endif // CGAL_USE_LEDA
00283 
00284 #endif // CGAL_LEDA_INTEGER_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines