BWAPI
|
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/int.h $ 00019 // $Id: int.h 45772 2008-09-25 13:41:17Z hemmer $ 00020 // 00021 // 00022 // Author(s) : Stefan Schirra, Michael Hemmer 00023 00024 00025 #ifndef CGAL_INT_H 00026 #define CGAL_INT_H 00027 00028 #include <CGAL/number_type_basic.h> 00029 #include <CGAL/Modular_traits.h> 00030 00031 CGAL_BEGIN_NAMESPACE 00032 00033 namespace INTERN_INT { 00034 template< class Type > 00035 class Is_square_per_double_conversion 00036 : public std::binary_function< Type, Type&, 00037 bool > { 00038 public: 00039 bool operator()( const Type& x, 00040 Type& y ) const { 00041 y = (Type) std::sqrt( (double)x ); 00042 return x == y * y; 00043 } 00044 bool operator()( const Type& x ) const { 00045 Type y = 00046 (Type) std::sqrt( (double)x ); 00047 return x == y * y; 00048 } 00049 00050 }; 00051 } // INTERN_INT 00052 00053 // int 00054 template<> class Algebraic_structure_traits< int > 00055 : public Algebraic_structure_traits_base< int, Euclidean_ring_tag > { 00056 00057 public: 00058 typedef Tag_true Is_exact; 00059 typedef Tag_false Is_numerical_sensitive; 00060 00061 typedef INTERN_AST::Div_per_operator< Type > Div; 00062 typedef INTERN_AST::Mod_per_operator< Type > Mod; 00063 00064 typedef INTERN_INT:: 00065 Is_square_per_double_conversion< Type > Is_square; 00066 }; 00067 00068 template <> class Real_embeddable_traits< int > 00069 : public INTERN_RET::Real_embeddable_traits_base< int , CGAL::Tag_true > {}; 00070 00076 template<> 00077 class Modular_traits<int>{ 00078 public: 00079 typedef int NT; 00080 typedef ::CGAL::Tag_true Is_modularizable; 00081 typedef Residue Residue_type; 00082 00083 struct Modular_image{ 00084 Residue_type operator()(int i){ 00085 return Residue_type(i); 00086 } 00087 }; 00088 struct Modular_image_representative{ 00089 NT operator()(const Residue_type& x){ 00090 return x.get_value(); 00091 } 00092 }; 00093 }; 00094 00095 // long 00096 00097 template<> class Algebraic_structure_traits< long int > 00098 : public Algebraic_structure_traits_base< long int, 00099 Euclidean_ring_tag > { 00100 00101 public: 00102 typedef Tag_true Is_exact; 00103 typedef Tag_false Is_numerical_sensitive; 00104 00105 typedef INTERN_AST::Div_per_operator< Type > Div; 00106 typedef INTERN_AST::Mod_per_operator< Type > Mod; 00107 00108 typedef INTERN_INT:: 00109 Is_square_per_double_conversion< Type > Is_square; 00110 }; 00111 00112 template <> class Real_embeddable_traits< long int > 00113 : public INTERN_RET::Real_embeddable_traits_base< long int , CGAL::Tag_true > {}; 00114 00120 template<> 00121 class Modular_traits<long>{ 00122 public: 00123 typedef long NT; 00124 typedef ::CGAL::Tag_true Is_modularizable; 00125 typedef Residue Residue_type; 00126 00127 struct Modular_image{ 00128 Residue_type operator()(long i){ 00129 return Residue_type(i); 00130 } 00131 }; 00132 struct Modular_image_representative{ 00133 NT operator()(const Residue_type& x){ 00134 return NT(x.get_value()); 00135 } 00136 }; 00137 }; 00138 00139 // short 00140 00141 template<> class Algebraic_structure_traits< short int > 00142 : public Algebraic_structure_traits_base< short int, 00143 Euclidean_ring_tag > { 00144 00145 public: 00146 typedef Tag_true Is_exact; 00147 typedef Tag_false Is_numerical_sensitive; 00148 00149 // Explicitly defined functors which have no support for implicit 00150 // interoperability. This is nescessary because of the implicit conversion 00151 // to int for binary operations between short ints. 00152 class Integral_division 00153 : public std::binary_function< Type, Type, 00154 Type > { 00155 public: 00156 Type operator()( const Type& x, 00157 const Type& y) const { 00158 Algebraic_structure_traits<Type>::Div actual_div; 00159 CGAL_precondition_msg( actual_div( x, y) * y == x, 00160 "'x' must be divisible by 'y' in " 00161 "Algebraic_structure_traits<...>::Integral_div()(x,y)" ); 00162 return actual_div( x, y); 00163 } 00164 }; 00165 00166 class Gcd 00167 : public std::binary_function< Type, Type, 00168 Type > { 00169 public: 00170 Type operator()( const Type& x, 00171 const Type& y) const { 00172 Algebraic_structure_traits<Type>::Mod mod; 00173 Algebraic_structure_traits<Type>::Unit_part unit_part; 00174 Algebraic_structure_traits<Type>::Integral_division integral_div; 00175 // First: the extreme cases and negative sign corrections. 00176 if (x == Type(0)) { 00177 if (y == Type(0)) 00178 return Type(0); 00179 return integral_div( y, unit_part(y) ); 00180 } 00181 if (y == Type(0)) 00182 return integral_div(x, unit_part(x) ); 00183 Type u = integral_div( x, unit_part(x) ); 00184 Type v = integral_div( y, unit_part(y) ); 00185 // Second: assuming mod is the most expensive op here, we don't compute it 00186 // unnecessarily if u < v 00187 if (u < v) { 00188 v = mod(v,u); 00189 // maintain invariant of v > 0 for the loop below 00190 if ( v == Type(0) ) 00191 return u; 00192 } 00193 00194 Type w; 00195 do { 00196 w = mod(u,v); 00197 if ( w == Type(0)) 00198 return v; 00199 u = mod(v,w); 00200 if ( u == Type(0)) 00201 return w; 00202 v = mod(w,u); 00203 } while (v != Type(0)); 00204 return u; 00205 } 00206 }; 00207 00208 class Div_mod { 00209 public: 00210 typedef Type first_argument_type; 00211 typedef Type second_argument_type; 00212 typedef Type& third_argument_type; 00213 typedef Type& fourth_argument_type; 00214 typedef void result_type; 00215 void operator()( const Type& x, 00216 const Type& y, 00217 Type& q, Type& r) const { 00218 q = x / y; 00219 r = x % y; 00220 return; 00221 } 00222 }; 00223 00224 // based on \c Div_mod. 00225 class Div 00226 : public std::binary_function< Type, Type, 00227 Type > { 00228 public: 00229 Type operator()( const Type& x, 00230 const Type& y) const { 00231 return x / y; 00232 }; 00233 }; 00234 00235 // based on \c Div_mod. 00236 class Mod 00237 : public std::binary_function< Type, Type, 00238 Type > { 00239 public: 00240 Type operator()( const Type& x, 00241 const Type& y) const { 00242 return x % y; 00243 }; 00244 }; 00245 00246 typedef INTERN_INT:: 00247 Is_square_per_double_conversion< Type > Is_square; 00248 }; 00249 00250 template <> class Real_embeddable_traits< short int > 00251 : public INTERN_RET::Real_embeddable_traits_base< short int , CGAL::Tag_true > {}; 00252 00253 // Note : "long long" support is in <CGAL/long_long.h> 00254 00255 CGAL_END_NAMESPACE 00256 00257 #endif // CGAL_INT_H