BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Scalar_factor_traits.h
Go to the documentation of this file.
00001 // Copyright (c) 2006-2007 Max-Planck-Institute Saarbruecken (Germany).
00002 // All rights reserved.
00003 //
00004 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public License as
00006 // published by the Free Software Foundation; version 2.1 of the License.
00007 // See the file LICENSE.LGPL distributed with CGAL.
00008 //
00009 // Licensees holding a valid commercial license may use this file in
00010 // accordance with the commercial license agreement provided with the software.
00011 //
00012 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00013 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00014 //
00015 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Algebraic_foundations/include/CGAL/Scalar_factor_traits.h $
00016 // $Id: Scalar_factor_traits.h 42663 2008-03-31 12:32:17Z hemmer $
00017 //
00018 //
00019 // Author(s)     : Michael Hemmer    <hemmer@mpi-inf.mpg.de>
00020 //
00021 // =============================================================================
00022 
00023 // TODO: The comments are all original EXACUS comments and aren't adapted. So
00024 //         they may be wrong now.
00025 
00026 #ifndef CGAL_SCALAR_FACTOR_TRAITS_H
00027 #define CGAL_SCALAR_FACTOR_TRAITS_H
00028 
00029 #include <CGAL/Algebraic_structure_traits.h>
00030 
00034 CGAL_BEGIN_NAMESPACE
00035 
00045 template< class NT >
00046 class Scalar_factor_traits {
00047 public:
00048     // the supported number type
00049     typedef NT Type;
00050     // NT is also 
00051     typedef NT Scalar;
00052 
00053     // functor computing the scalar factor of a basic number type. 
00054     class Scalar_factor {
00055         static Scalar scalar_factor(const NT& a, Integral_domain_tag) {
00056             typename Algebraic_structure_traits<NT>::Unit_part upart;
00057             typename Algebraic_structure_traits<NT>::Integral_division idiv;
00058             return idiv(a, upart(a));
00059         }
00060         static Scalar scalar_factor(const NT& a, Field_tag) {
00061             return (a == NT(0)) ? NT(0) : NT(1);
00062         }
00063         static Scalar scalar_factor(const NT& a, 
00064                                     const Scalar& d,
00065                                     Integral_domain_tag) {
00066             return Scalar(1);
00067         }
00068         static Scalar scalar_factor(const NT& a, 
00069                                     const Scalar& d,
00070                                     Field_tag) {
00071             return (a == NT(0) && d == Scalar(0)) ? NT(0) : NT(1);
00072         }
00073         static Scalar scalar_factor(const NT& a, 
00074                                     const Scalar& d,
00075                                     Unique_factorization_domain_tag) { 
00076             if( d == Scalar(0)) {
00077                 return a;
00078             } else {
00079                 typename Algebraic_structure_traits<NT>::Gcd gcd;
00080                 return gcd(a,d);
00081             }
00082         }
00083     public: 
00084         // argument type
00085         typedef NT argument_type;
00086         // first argument type
00087         typedef NT first_argument_type;
00088         // second argument type
00089         typedef Scalar second_argument_type;
00090         // result type
00091         typedef Scalar result_type;
00092   
00093         // determine extractable scalar factor
00094         Scalar operator () (const NT& a) {
00095             BOOST_STATIC_ASSERT(( ::boost::is_same< NT,Scalar >::value));  
00096             typedef typename Algebraic_structure_traits<NT>::Algebraic_category SAT;
00097             return scalar_factor(a, SAT());
00098         }
00099         // determine extractable scalar factor
00100         Scalar operator () (const NT& a, const Scalar& d) {     
00101             BOOST_STATIC_ASSERT(( ::boost::is_same< NT,Scalar >::value));  
00102             typedef typename Algebraic_structure_traits<NT>::Algebraic_category SAT;
00103             return scalar_factor(a,d,SAT());
00104         }
00105     };
00106 
00107     class Scalar_div {
00108     public:
00109         // first argument type
00110         typedef NT& first_argument_type;
00111         // second argument type
00112         typedef Scalar second_argument_type;
00113         // result type
00114         typedef void result_type;
00115         // divide \a a in place by scalar factor \a b
00116         void operator () (NT& a, const Scalar& b) {
00117             CGAL_precondition(b != Scalar(0));
00118             typename Algebraic_structure_traits<NT>::Integral_division idiv;
00119             a = idiv(a, b);
00120         }
00121     };
00122 };
00123 
00129 template <class NT>
00130 void remove_scalar_factor(NT& x){
00131     typedef CGAL::Scalar_factor_traits<NT> SFT;
00132     typedef typename SFT::Scalar Scalar;
00133     typename SFT::Scalar_factor sfac;
00134     typename SFT::Scalar_div    sdiv;
00135     
00136     Scalar scalar=sfac(x);
00137     if(scalar != Scalar(0))
00138         sdiv(x,scalar);
00139 }
00140 
00141 template <class NT>
00142 inline
00143 typename CGAL::Scalar_factor_traits<NT>::Scalar
00144 scalar_factor(const NT& x){
00145     typename CGAL::Scalar_factor_traits<NT>::Scalar_factor scalar_factor;
00146         return scalar_factor(x);
00147 }
00148 
00149 template <class NT>
00150 inline
00151 typename CGAL::Scalar_factor_traits<NT>::Scalar 
00152 scalar_factor(const NT& x,
00153         const typename CGAL::Scalar_factor_traits<NT>::Scalar& d)
00154 {
00155     typename CGAL::Scalar_factor_traits<NT>::Scalar_factor scalar_factor;
00156     return scalar_factor(x,d);
00157 }
00158 
00159 
00160 CGAL_END_NAMESPACE
00161 
00162 #endif // NiX_SCALAR_FACTOR_TRAITS_H
00163 // EOF
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines