Skip to content

Instantly share code, notes, and snippets.

@heatblazer
Created September 13, 2019 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heatblazer/2ce911681b17c618f7d03c14f4145f39 to your computer and use it in GitHub Desktop.
Save heatblazer/2ce911681b17c618f7d03c14f4145f39 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <assert.h>
#include <random>
#include <string.h>
union lu_val
{
long long val;
struct
{
unsigned char b0 : 1;
unsigned char b1 : 1;
unsigned char b2 : 1;
unsigned char b3 : 1;
unsigned char b4 : 1;
unsigned char b5 : 1;
unsigned char b6 : 1;
unsigned char b7 : 1;
} bitdata[sizeof(long long)];
unsigned char data[sizeof(long long)];
};
template <typename K, typename V>
class HashMap
{
typedef unsigned long long ullong;
unsigned char m_randgen[256];
ullong hfunc(const char* data, size_t len)
{
size_t i=0;
size_t j=0;
unsigned char h;
unsigned char hh[8] = {0};
for(i=0; i < 8; ++i)
{
h = m_randgen[(data[0]+i) % 256];
for(j=1; j < len; ++j)
h = m_randgen[h ^ data[i]];
hh[i] = h;
}
ullong ret =0;
for(size_t ii=0, jj=0; ii < 8; ++ii, jj += 8)
ret |= (0xffL & (ullong)hh[ii]) << jj;
return ret;
}
void install(ullong val)
{
}
public:
HashMap()
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(1, 0xff);
for(int i=0; i < sizeof(m_randgen)/sizeof(m_randgen[0]); ++i)
m_randgen[i] = dis(gen);
}
V& operator[](const char* data)
{
ullong val = hfunc(data, strlen(data));
install(val);
}
};
int main()
{
HashMap<std::string, std::string> map;
map["aaaa"] = "aaa";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment