Skip to content
Snippets Groups Projects
Commit 5f5af8f4 authored by Markus Blatt's avatar Markus Blatt
Browse files

Added template meta programms for calculatation of the greatest common

divisor and the least common multiple of two longs.

[[Imported from SVN: r2077]]
parent ca05199b
No related branches found
No related tags found
No related merge requests found
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_GCD_HH
#define DUNE_GCD_HH
#include "helpertemplates.hh"
namespace Dune
{
/**
* @addtogroup Common
* @{
*/
/**
* @file
* This file provides template constructs for calculation the
* greatest common divisor.
*/
/**
* @brief Helper for calculating the gcd.
*/
template<long a, long b, bool bo>
struct GcdHelper
{};
template<long a, long b>
struct GcdHelper<a,b,true>
{
/**
* @brief Check that a>b.
*/
static void conceptCheck()
{
IsTrue<b<a>::yes();
IsTrue<0<b>::yes();
}
/**
* @brief The greatest common divisor of the numbers a and b.
*/
const static long gcd = GcdHelper<b,a%b,true>::gcd;
};
template<long a, long b>
struct GcdHelper<a,b,false>
{
/**
* @brief The greatest common divisor of the numbers a and b.
*/
const static long gcd = GcdHelper<b,a,true>::gcd;
};
template<long a>
struct GcdHelper<a,0,true>
{
const static long gcd=a;
};
/**
* @brief Calculator of the greatest common divisor.
*/
template<long a, long b>
struct Gcd
{
/**
* @brief The greatest common divisior of a and b. */
const static long value = GcdHelper<a,b,(a>b)>::gcd;
};
/**
* @}
*/
}
#endif
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_LCM_HH
#define DUNE_LCM_HH
#include <dune/common/helpertemplates.hh>
namespace Dune
{
/**
* @addtogroup Common
* @{
*/
/**
* @file
* This file provides template constructs for calculation the
* least common multiple.
*/
/**
* @brief Calculate the least common multiple of two numbers
*/
template<long m, long n>
struct Lcm
{
static void conceptCheck()
{
IsTrue<0<m>::yes();
IsTrue<0<n>::yes();
}
/**
* @brief The least common multiple of the template parameters
* m and n.
*/
const static long value = (m/Gcd<m,n>::value)*n;
};
}
#endif
......@@ -15,4 +15,5 @@ fmatrixtest
poolallocatortest
*.gcda
*.gcno
gmon.out
\ No newline at end of file
gmon.out
gcdlcdtest
\ No newline at end of file
......@@ -2,7 +2,7 @@
TESTPROGS = parsetest test-stack arraylisttest smartpointertest \
sllisttest iteratorfacadetest tuplestest fmatrixtest \
poolallocatortest settest
poolallocatortest settest gcdlcdtest
# which tests to run
TESTS = $(TESTPROGS)
......@@ -37,4 +37,6 @@ poolallocatortest_SOURCES = poolallocatortest.cc
settest_SOURCES=settest.cc
gcdlcdtest_SOURCES = gcdlcdtest.cc
include $(top_srcdir)/am/sourcescheck
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#include <dune/common/gcd.hh>
#include <dune/common/lcm.hh>
#include <dune/common/helpertemplates.hh>
#include <iostream>
void test()
{
IsTrue<(Dune::Gcd<2*2*2*5*5*5*11, 2*2*5*13>::value == 2*2*5)>::yes();
IsTrue<Dune::Lcm<11,3>::value == 33>::yes();
IsTrue<Dune::Lcm<18,15>::value == 18*15/3>::yes();
IsTrue<Dune::Lcm<10800,Dune::Lcm<36000,7680>::value>::value==1728000>::yes();
}
int main()
{
std::cout<<" gcd(2,5)="<<Dune::Gcd<2,5>::value<<" gcd(3, 18)="
<<Dune::Gcd<3,18>::value<<" gcd("<<2*2*2*5*5*5*11<<", "
<< 2*2*5*13<<")="<<Dune::Gcd<2*2*2*5*5*5*11, 2*2*5*13>::value
<<std::endl;
std::cout<<" lcm(18,15)="<<Dune::Lcm<18,15>::value
<<" lcm(10800,36000,7680)="<<Dune::Lcm<10800,Dune::Lcm<36000,7680>::value>::value<<std::endl;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment