~ubuntu-branches/ubuntu/warty/aqsis/warty

« back to all changes in this revision

Viewing changes to boost/boost/pool/detail/ct_gcd_lcm.hpp

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:25:04 UTC
  • Revision ID: james.westby@ubuntu.com-20040824072504-zf993vnevvisdsvb
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (C) 2000 Stephen Cleary (shammah@voyager.net)
 
2
//
 
3
// This file can be redistributed and/or modified under the terms found
 
4
//  in "copyright.html"
 
5
// This software and its documentation is provided "as is" without express or
 
6
//  implied warranty, and with no claim as to its suitability for any purpose.
 
7
//
 
8
// See http://www.boost.org for updates, documentation, and revision history.
 
9
 
 
10
#ifndef BOOST_POOL_CT_GCD_LCM_HPP
 
11
#define BOOST_POOL_CT_GCD_LCM_HPP
 
12
 
 
13
#include <boost/static_assert.hpp>
 
14
#include <boost/type_traits/ice.hpp>
 
15
 
 
16
namespace boost {
 
17
 
 
18
namespace details {
 
19
namespace pool {
 
20
 
 
21
// Compile-time calculation of greatest common divisor and least common multiple
 
22
 
 
23
//
 
24
// ct_gcd is a compile-time algorithm that calculates the greatest common
 
25
//  divisor of two unsigned integers, using Euclid's algorithm.
 
26
//
 
27
// assumes: A != 0 && B != 0
 
28
//
 
29
 
 
30
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
 
31
 
 
32
namespace details {
 
33
template <unsigned A, unsigned B, bool Bis0>
 
34
struct ct_gcd_helper;
 
35
template <unsigned A, unsigned B>
 
36
struct ct_gcd_helper<A, B, false>
 
37
{
 
38
  BOOST_STATIC_CONSTANT(unsigned, A_mod_B_ = A % B);
 
39
  BOOST_STATIC_CONSTANT(unsigned, value =
 
40
      (::boost::details::pool::details::ct_gcd_helper<
 
41
        B, static_cast<unsigned>(A_mod_B_),
 
42
        ::boost::type_traits::ice_eq<A_mod_B_, 0>::value
 
43
        >::value) );
 
44
};
 
45
template <unsigned A, unsigned B>
 
46
struct ct_gcd_helper<A, B, true>
 
47
{
 
48
  BOOST_STATIC_CONSTANT(unsigned, value = A);
 
49
};
 
50
} // namespace details
 
51
 
 
52
template <unsigned A, unsigned B>
 
53
struct ct_gcd
 
54
{
 
55
  BOOST_STATIC_ASSERT(A != 0 && B != 0);
 
56
  BOOST_STATIC_CONSTANT(unsigned, value =
 
57
      (::boost::details::pool::details::ct_gcd_helper<A, B, false>::value) );
 
58
};
 
59
 
 
60
#else
 
61
 
 
62
// Thanks to Peter Dimov for providing this workaround!
 
63
namespace details {
 
64
template<unsigned A> struct ct_gcd2
 
65
{
 
66
  template<unsigned B>
 
67
  struct helper
 
68
  {
 
69
    BOOST_STATIC_CONSTANT(unsigned, value = ct_gcd2<B>::helper<A % B>::value);
 
70
  };
 
71
  template<>
 
72
  struct helper<0>
 
73
  {
 
74
    BOOST_STATIC_CONSTANT(unsigned, value = A);
 
75
  };
 
76
};
 
77
} // namespace details
 
78
 
 
79
template<unsigned A, unsigned B> struct ct_gcd
 
80
{
 
81
  BOOST_STATIC_ASSERT(A != 0 && B != 0);
 
82
  enum { value = details::ct_gcd2<A>::helper<B>::value };
 
83
};
 
84
 
 
85
#endif
 
86
 
 
87
//
 
88
// ct_lcm is a compile-time algorithm that calculates the least common
 
89
//  multiple of two unsigned integers.
 
90
//
 
91
// assumes: A != 0 && B != 0
 
92
//
 
93
template <unsigned A, unsigned B>
 
94
struct ct_lcm
 
95
{
 
96
  BOOST_STATIC_CONSTANT(unsigned, value =
 
97
      (A / ::boost::details::pool::ct_gcd<A, B>::value * B) );
 
98
};
 
99
 
 
100
} // namespace pool
 
101
} // namespace details
 
102
 
 
103
} // namespace boost
 
104
 
 
105
#endif