Hash Pointers
Use a fast container to store pointers
I felt the need to store unique pointers to objects in a container for quick retrieval.
Example:
#include <cstdint>
#include <math.h>
#include <errno.h>
#include <iostream>
#include <bitset>
#include <unordered_map>
//#include <tsl/robin_set.h>
class Ptr2Hash
{
public:
template<typename type>
size_t operator()(type* ptr) const
{
static const /*constexpr*/ size_t shift = static_cast<size_t>(log2(1 + sizeof(ptr)));
return static_cast<size_t>(reinterpret_cast<uintptr_t>(ptr) >> shift);
};
};
#define OID long long*
typedef std::unordered_map<OID, long long*, Ptr2Hash > TMMObjectMap;
//typedef tsl::robin_set<OID, Ptr2Hash> TMObjectIDUnorderedSetbyOID;
const auto break_here = Ptr2Hash().operator()(&errno);
int main() {
std::cout << std::bitset<64>(reinterpret_cast<uintptr_t>(&errno)) << '\n';
std::cout << std::bitset<64>(break_here) << '\n';
}