15 #ifndef __gfftfactor_h
16 #define __gfftfactor_h
27 template<
class TList>
struct Print;
29 template<>
struct Print<Loki::NullType> { };
31 template<
class Head,
class Tail>
32 struct Print<Loki::Typelist<Head,Tail> > {
33 typedef typename Print<Tail>::Result Result;
37 template<int_t N, int_t Factor,
38 bool C = (N % Factor == 0)>
41 template<
int_t N,
int_t Factor>
42 struct IsMultipleOf<N, Factor, true> {
43 static const int_t value = IsMultipleOf<N/Factor, Factor>::value + 1;
46 template<
int_t N,
int_t Factor>
47 struct IsMultipleOf<N, Factor, false> {
48 static const int_t value = 0;
51 template<
int_t Factor>
52 struct IsMultipleOf<0, Factor, true> {
53 static const int_t value = 0;
56 template<int_t N, int_t K,
57 template<
int_t>
class IntHolder =
SInt,
59 bool C1 = ((6*K+1)*(6*K+1) <= N),
60 bool C2 = ((N % (6*K+1) == 0) || (N % (6*K+5) == 0))>
61 struct FactorizationLoop;
63 template<int_t N, int_t K,
64 template<
int_t>
class IntHolder, int_t AddPower>
65 struct FactorizationLoop<N, K, IntHolder, AddPower, true, true>
67 static const int_t Candidate1 = 6*K + 1;
68 static const int_t Candidate2 = 6*K + 5;
69 static const int_t P1 = IsMultipleOf<N, Candidate1>::value + AddPower;
70 static const int_t P2 = IsMultipleOf<N, Candidate2>::value + AddPower;
71 static const int_t F1 = IPow<Candidate1, P1>::value;
72 static const int_t F2 = IPow<Candidate2, P2>::value;
73 typedef Pair<IntHolder<Candidate1>, IntHolder<P1> > T1;
74 typedef Pair<IntHolder<Candidate2>, IntHolder<P2> > T2;
76 static const int_t NextN = N/F1/F2;
77 typedef typename FactorizationLoop<NextN, K+1, IntHolder, AddPower>::Result NextIter;
79 typedef typename Loki::Select<(P1>0) && (P2>0),
80 Loki::Typelist<T1, Loki::Typelist<T2, NextIter> >,
81 typename Loki::Select<(P1>0), Loki::Typelist<T1, NextIter>,
82 typename Loki::Select<(P2>0), Loki::Typelist<T2, NextIter>, NextIter>::Result>::Result>::Result Result;
85 template<int_t N, int_t K,
86 template<
int_t>
class IntHolder, int_t AddPower>
87 struct FactorizationLoop<N, K, IntHolder, AddPower, true, false>
88 :
public FactorizationLoop<N, K+1, IntHolder, AddPower> {};
90 template<int_t N, int_t K,
91 template<
int_t>
class IntHolder, int_t AddPower,
bool C>
92 struct FactorizationLoop<N, K, IntHolder, AddPower, false, C>
94 typedef Pair<IntHolder<N>, IntHolder<1+AddPower> > T;
95 typedef Loki::Typelist<T, Loki::NullType> Result;
99 template<
int_t>
class IntHolder, int_t AddPower,
bool C>
100 struct FactorizationLoop<1, K, IntHolder, AddPower, false, C>
102 typedef Loki::NullType Result;
108 template<typename Num,
109 template<int_t> class IntHolder =
SInt,
110 typename StartList = InitialPrimesList,
112 struct Factorization;
115 template<int_t N, template<int_t> class IntHolder, typename H, typename Tail, int_t AddPower>
116 struct Factorization<
SIntID<N>, IntHolder, Loki::Typelist<H,Tail>, AddPower>
119 static const int_t P = IsMultipleOf<N, H::value>::value;
120 typedef SIntID<N / IPow<H::value,P>::value> NextNum;
121 typedef typename Factorization<NextNum,IntHolder,Tail,AddPower>::Result Next;
122 typedef typename Loki::Select<(P > 0),
123 Loki::Typelist<Pair<IntHolder<H::value>, IntHolder<P+AddPower> >, Next>, Next>::Result Result;
127 template<
int_t N,
template<
int_t>
class IntHolder, int_t AddPower>
128 struct Factorization<
SIntID<N>, IntHolder, Loki::NullType, AddPower>
129 :
public FactorizationLoop<N, 2, IntHolder, AddPower> {};
132 template<
template<
int_t>
class IntHolder,
typename H,
typename Tail, int_t AddPower>
133 struct Factorization<
SIntID<1>, IntHolder, Loki::Typelist<H,Tail>, AddPower> {
134 typedef Loki::NullType Result;
138 template<
int_t M,
int_t P>
142 template<int_t M, int_t P,
143 template<
int_t>
class IntHolder,
typename StartList, int_t AddPower>
144 struct Factorization<PowerHolder<M,P>, IntHolder, StartList, AddPower>
145 :
public Factorization<SIntID<M>, IntHolder, StartList, P-1> { };