forked from minings/coyote_miner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashtable.hpp
111 lines (101 loc) · 3.34 KB
/
hashtable.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#pragma once
#include <fc/crypto/city.hpp>
#include <vector>
#include <array>
#define BIG_TABLE 1
#if BIG_TABLE
const int TABLE_SIZE = ((1<<26)*1.5);
#else
const int TABLE_SIZE = (1<<26);
#endif
class hashtable
{
public:
hashtable() :
itable(new std::array< std::pair<uint64_t,uint32_t>,TABLE_SIZE >),
table(*itable)
{
reset();
}
void reset()
{
memset( (char*)table.data(), 0, table.size()*sizeof(std::pair<uint64_t,uint32_t>) );
}
uint32_t store( uint64_t key, uint32_t val )
{
uint64_t next_key = key;
auto index = next_key % table.size() ;
//if matching collision in table, return it
if( table[index].first == key )
{
return table[index].second;
}
//no collision, add to table
table[index].first = key;
table[index].second = val;
return -1;
}
private:
std::array< std::pair<uint64_t,uint32_t>,TABLE_SIZE >* itable;
std::array< std::pair<uint64_t,uint32_t>,TABLE_SIZE >& table;
};
#if 0
const int TABLE_SIZE = (1<<26);
class hashtable
{
public:
hashtable() :
itable( new std::array< std::pair<uint64_t,uint32_t> , TABLE_SIZE > ),
itable2( new std::array< std::pair<uint64_t,uint32_t> , TABLE_SIZE > ),
table(*itable),
table2(*itable2)
{
reset();
}
void reset()
{
//table.resize( (1<<26) );
memset( (char*)table.data(), 0, table.size()*sizeof(std::pair<uint64_t,uint32_t>) );
//table2.resize( (1<<26) );
memset( (char*)table2.data(), 0, table2.size()*sizeof(std::pair<uint64_t,uint32_t>) );
}
uint32_t store( uint64_t key, uint32_t val )
{
uint64_t next_key = key;
auto index = next_key % table.size() ;
//if collision
if ( table[index].first != 0 )
{
//if matching collision in first table, return it
if( table[index].first == key )
{
return table[index].second;
}
//if matching collision in second table, return it
else if( table2[index].first == key )
{
return table2[index].second;
}
//add to second table
else
{
table2[index].first = key;
table2[index].second = val;
return -1;
}
}
//no collision, add to first table
table[index].first = key;
table[index].second = val;
return -1;
}
private:
std::array< std::pair<uint64_t,uint32_t>,TABLE_SIZE>* itable;
std::array< std::pair<uint64_t,uint32_t>,TABLE_SIZE >* itable2;
std::array< std::pair<uint64_t,uint32_t>,TABLE_SIZE >& table;
std::array< std::pair<uint64_t,uint32_t>,TABLE_SIZE >& table2;
// std::vector< std::pair<uint64_t,uint32_t> > table;
// std::vector< std::pair<uint64_t,uint32_t> > table2;
};
#else
#endif