forked from dase/CLAIMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.h
executable file
·225 lines (198 loc) · 6.94 KB
/
hash.h
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
Copyright 2011, Spyros Blanas.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Modified by wangli On Otc.15,2013
*/
#ifndef __MYHASHFUNCTION__
#define __MYHASHFUNCTION__
#include <stdlib.h>
#include <maths.h>
#include <boost/functional/hash.hpp>
class PartitionFunction {
public:
enum partition_fashion{hash_f,range_f,round_robin_f};
/**
* Creates a new hashing object, rounding the number of buckets
* \a k to the next power of two. Formally \a k will become
* \f$ k = 2^{\lceil{\log_2{k}}\rceil} \f$
* @param min Minimum hash value
* @param max Maximum hash value
* @param k Number of buckets. Value will be rounded to the next
* higher power of two.
*/
PartitionFunction(int min, int max, unsigned int k);
virtual ~PartitionFunction(){};
virtual partition_fashion getPartitionFashion()const=0;
/**
* Returns the bucket number \f$n\in[0,k)\f$ for this \a value.
* @param value Value to hash. Must be within bounds.
* @return Bucket number.
*/
virtual unsigned int get_partition_value(const int& value) const= 0;
virtual unsigned int get_partition_value(const double& value)const=0;
virtual unsigned int get_partition_value(const unsigned long&)const=0;
virtual unsigned int getNumberOfPartitions()const=0;
protected:
int min_, max_;
unsigned long int k_; /**< \f$ \_k=log_2(k) \f$, where \f$k\f$ is number of buckets */
};
/**
* This class is used to achieve Round Robin Partition
*/
class RoundRobinPartitionFunction :public PartitionFunction{
public:
RoundRobinPartitionFunction(int range)
:PartitionFunction(0,0,0),range_(range),cur_(0){}
~RoundRobinPartitionFunction(){};
/*return the id of partition in round robin*/
inline unsigned int get_partition_value(const int& value)const{
return rand()%range_;
}
inline unsigned int get_partition_value(const double& value)const{
return rand()%range_;
}
inline unsigned int get_partition_value(const unsigned long& value)const{
return rand()%range_;
}
inline unsigned int getNumberOfPartitions()const{
return range_;
}
partition_fashion getPartitionFashion()const;
private:
int range_;
unsigned cur_;
};
/*
* This class is used to achieve range partition, where each range is of equal size.
* TODO: implement range partition that can specify each range size.
*/
class UniformRangePartitionFunction : public PartitionFunction {
public:
UniformRangePartitionFunction(int min, int max, unsigned int k)
: PartitionFunction(min, max, k) { }
~UniformRangePartitionFunction(){};
inline unsigned int get_partition_value(const int &value)const {
unsigned long long val = (value-min_);
val <<= k_;
return val / (max_-min_+1);
}
inline unsigned int get_partition_value(const unsigned long &value)const {
unsigned long long val = (value-min_);
val <<= k_;
return val / (max_-min_+1);
}
inline unsigned int get_partition_value(const double& value)const {
unsigned long long val = (value-min_);
val <<= k_;
return val / (max_-min_+1);
}
partition_fashion getPartitionFashion()const;
unsigned getNumberOfPartitions()const{
return 1<<k_;
}
};
class ModuloHashFunction : public PartitionFunction {
public:
/**
* Skipbits parameter defines number of least-significant bits which
* will be discarded before computing the hash.
*/
ModuloHashFunction(int min, unsigned int range, unsigned int skipbits)
: PartitionFunction(min, -1, range) {
skipbits_ = skipbits;
k_ = ((1 << k_) - 1) << skipbits_; // _k is used as the modulo mask
}
~ModuloHashFunction(){};
/** Return h(x) = x mod k. */
inline unsigned int get_partition_value(const int& value)const {
return ((value-min_) & k_) >> skipbits_;
}
inline unsigned int get_partition_value(const double& value)const {
return ((*(long*)&value-min_) & k_) >> skipbits_;
}
inline unsigned int get_partition_value(const unsigned long& value)const {
return ((*(long*)&value-min_) & k_) >> skipbits_;
}
partition_fashion getPartitionFashion()const;
inline unsigned int getNumberOfPartitions()const {
return (k_ >> skipbits_) + 1;
}
private:
unsigned int skipbits_;
};
class GeneralModuloFunction:public PartitionFunction{
public:
GeneralModuloFunction(const int &range, const int & min, const int & skipbits)
: PartitionFunction(min,-1,range*(1>>12)),range_(range),skipbits_(skipbits){
}
~GeneralModuloFunction(){};
inline unsigned int get_partition_value(const int& value)const{
return abs(value)%range_;
}
inline unsigned int get_partition_value(const unsigned long& value)const{
return (value)%range_;
}
inline unsigned int get_partition_value(const double& value)const{
const long tmp=*(long*)&value;
return ((tmp*16807)%2839+(tmp*19))%range_;
}
partition_fashion getPartitionFashion()const;
unsigned getNumberOfPartitions()const{
return range_;
}
private:
unsigned long range_;
unsigned skipbits_;
};
class BoostHashFunctin:public PartitionFunction{
public:
BoostHashFunctin(const int &range)
: PartitionFunction(0,0,1),range_(range){
}
~BoostHashFunctin(){};
inline unsigned int get_partition_value(const int& value)const{
return boost::hash_value(value)%range_;
}
inline unsigned int get_partition_value(const unsigned long& value)const{
return boost::hash_value(value)%range_;
}
inline unsigned int get_partition_value(const double& value)const{
return boost::hash_value(value)%range_;
}
inline unsigned int get_partition_value(const long& value)const{
return boost::hash_value(value)%range_;
}
partition_fashion getPartitionFashion()const{
return hash_f;
}
unsigned getNumberOfPartitions()const{
return range_;
}
private:
unsigned long range_;
};
class PartitionFunctionFactory {
public:
/* Modulo hash function supporting arbitary range.*/
static PartitionFunction* createGeneralModuloFunction(const int &range, const int &min=0,const int &skipbits=0);
/* The range of ModuloFunction should be a power of 2. If you want to use other range, you should consider
* to use GeneralModuloFunction. */
static PartitionFunction* createModuloFunction(const int &range, const int &min=0, const int &skipbits=0);
static PartitionFunction* createUniformRangeHashFunction(const int &range, const int &min, const int &max);
static PartitionFunction* createRoundRobinPartitionHashFunction(int range);
static PartitionFunction* createBoostHashFunction(const int &range){
return new BoostHashFunctin(range);
}
};
#endif