S
skywalker
For example, How to do if we want to define a bitset which size is
30th
fibonacci number?
Plan A:
#include<bitset>
#include<cstdio>
/* You can run this code in py3k to get the 30th fibonacci number.
* fib = [1, 1]
* while len(fib) != 30:
* fib += [fib[-1] + fib[-2]]
* print(fib[-1])
*/
const size_t FIB30 = 832040;
int main()
{
std::bitset< FIB30 > a;
std:rintf("%d\n", a.size());
}
This is the most common way. It works fine, but we have to run
another
program to get the number.
Plan B:
#include<bitset>
#include<cstdio>
template<size_t n> struct fib {
enum { ret = fib<n - 1>::ret + fib<n - 2>::ret };
};
template<> struct fib<1> { enum { ret = 1 }; };
template<> struct fib<2> { enum { ret = 1 }; };
int main()
{
std::bitset< fib<30> > a;
std:rintf("%d\n", a.size());
}
By using template metaprogramming technique, we can use compile to
calculate
for us. However, this syntax is weird. It doesn't looks like a C++
program.
The natural way is using c++ function.
Plan C:
#include<bitset>
#include<cstdio>
const size_t fib(const size_t n)
{
size_t f[2] = {1, 1};
for(size_t i = 3; i <= n; ++i)
f[i & 1] += f[(i - 1) & 1];
return f[n & 1];
}
int main()
{
std::bitset< fib(30) > a; // Compile error: Boom!
}
Oops, this code cannot be compiling successfully.
All we know fib(30) is a const number, it will not be changed in the
running time.
So I think the compile should treat it as a const integer,
instead of calculating it every time.
By the way, I think C++0x's new keyword constexpr doesn't work in
here.
Perhaps std::vector<bool> or boost::dynamic_bitset is a good choice,
but this is not in the range of discussion.
Why C++ standard doesn't allow this code?
30th
fibonacci number?
Plan A:
#include<bitset>
#include<cstdio>
/* You can run this code in py3k to get the 30th fibonacci number.
* fib = [1, 1]
* while len(fib) != 30:
* fib += [fib[-1] + fib[-2]]
* print(fib[-1])
*/
const size_t FIB30 = 832040;
int main()
{
std::bitset< FIB30 > a;
std:rintf("%d\n", a.size());
}
This is the most common way. It works fine, but we have to run
another
program to get the number.
Plan B:
#include<bitset>
#include<cstdio>
template<size_t n> struct fib {
enum { ret = fib<n - 1>::ret + fib<n - 2>::ret };
};
template<> struct fib<1> { enum { ret = 1 }; };
template<> struct fib<2> { enum { ret = 1 }; };
int main()
{
std::bitset< fib<30> > a;
std:rintf("%d\n", a.size());
}
By using template metaprogramming technique, we can use compile to
calculate
for us. However, this syntax is weird. It doesn't looks like a C++
program.
The natural way is using c++ function.
Plan C:
#include<bitset>
#include<cstdio>
const size_t fib(const size_t n)
{
size_t f[2] = {1, 1};
for(size_t i = 3; i <= n; ++i)
f[i & 1] += f[(i - 1) & 1];
return f[n & 1];
}
int main()
{
std::bitset< fib(30) > a; // Compile error: Boom!
}
Oops, this code cannot be compiling successfully.
All we know fib(30) is a const number, it will not be changed in the
running time.
So I think the compile should treat it as a const integer,
instead of calculating it every time.
By the way, I think C++0x's new keyword constexpr doesn't work in
here.
Perhaps std::vector<bool> or boost::dynamic_bitset is a good choice,
but this is not in the range of discussion.
Why C++ standard doesn't allow this code?