using namespace std
std::vector vs. std::map
You want a type indexed by integers starting with 0 and increasing to an arbitrarily large amount in sequence, as that is what Fibonacci is is a sequence. So why use a std::map, which allows for sparse, out of sequence results? Just use
std::vector<std::uint128_t>& m = __fib_result_vector;__fib_results;
Changing the name to __fib_results won't affect performance, but it saves having to rename the variable if you change types.
It will use less memory because a map has to leave room to add new elements between other elements in an arbitrary order. Which you don't need. And a map has to store the keys as well as the values. A vector uses position to do that, so the fourth is always index 3.
Headers
There's an argument that you should only do includes like this in header files if needed in the header file itself. That's not true in this case, as the header file only uses primitives. So you could include vector or map in fibonacci.cpp directly. The header file only needs to include the function prototype.
Working example
#include <vector>
#include <stdint.h>
std::vector<std::uint64_t> initializeFibonacci() {
std::vector<std::uint64_t> m;
m.push_back(0);
m.push_back(1);
return m;
}
std::vector<std::uint64_t> __fib_results = initializeFibonacci();
std::uint64_t fibonacci(int seq) {
std::vector<std::uint64_t>& m = __fib_results;
while (m.size() <= seq) {
auto i = m.size();
m.push_back(m[i-1] + m[i-2]);
}
return m[seq];
}
The compiler that I used for testing did not seem to have uint128_t, so I substituted uint64_t. I don't see any reason why uint128_t wouldn't work if available, but this is what I actually tested.
Instead of recursively working down to the last known value, this works up from it. This saves doing any recursive calls, so it doesn't have to worry about overloading the stack. Also, this makes it work more easily with push_back.