libGDF
tools.h
00001 //
00002 // This file is part of libGDF.
00003 //
00004 // libGDF is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU Lesser General Public License as
00006 // published by the Free Software Foundation, either version 3 of
00007 // the License, or (at your option) any later version.
00008 //
00009 // libGDF is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU Lesser General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU Lesser General Public License
00015 // along with libGDF.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 // Copyright 2010 Martin Billinger
00018 
00019 #ifndef __TOOLS_H_INCLUDED__
00020 #define __TOOLS_H_INCLUDED__
00021 
00022 #include <vector>
00023 
00024 namespace gdf
00025 {
00026 
00027     template<class T>
00028     T gcd( T a, T b)
00029     {
00030         if( a == 0 )
00031             return b;
00032         if( b == 0 )
00033             return a;
00034         while( 1 )
00035         {
00036             a = a % b;
00037             if( a == 0 )
00038                 return b;
00039 
00040             b = b % a;
00041             if( b == 0 )
00042                 return a;
00043         }
00044     }
00045 
00046     template<class T>
00047     T gcd( const std::vector<T> &v, size_t p, size_t r )
00048     {
00049         if( v.size() == 0 )
00050             throw exception::empty_container( "calculating GCD" );
00051 
00052         if( p>=v.size() || r>= v.size() )
00053             throw exception::index_out_of_range( "calculating GCD" );
00054 
00055         if( r == p+1 )
00056             return gcd( v[p], v[r] );
00057 
00058         if( r == p )
00059             return v[p];
00060 
00061         int q = (p+r)/2;
00062         return gcd( gcd( v, p, q ), gcd( v, q+1, r ) );
00063     }
00064 
00065     template<class T>
00066     T gcd( const std::vector<T> &v )
00067     {
00068         return gcd( v, size_t(0), v.size()-1 );
00069     }
00070 
00071     template<class T>
00072     T max( const std::vector<T> &v )
00073     {
00074         T m = v[0];
00075         for( size_t i=1; i<v.size(); i++ )
00076             m = std::max( m, v[i] );
00077         return m;
00078     }
00079 
00080     template<class T>
00081     T min( const std::vector<T> &v )
00082     {
00083         T m = v[0];
00084         for( size_t i=1; i<v.size(); i++ )
00085             m = std::min( m, v[i] );
00086         return m;
00087     }
00088 
00089     template<class T>
00090     T sum( const std::vector<T> &v )
00091     {
00092         T m = v[0];
00093         for( size_t i=1; i<v.size(); i++ )
00094             m = m + v[i];
00095         return m;
00096     }
00097 
00098 }
00099 
00100 #endif // TOOLS_H
 All Data Structures Functions Variables Friends