-
Notifications
You must be signed in to change notification settings - Fork 8
/
hash.h
250 lines (219 loc) · 7.09 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// This code is part of the Problem Based Benchmark Suite (PBBS)
// Copyright (c) 2011 Guy Blelloch, Julian Shun and the PBBS team
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights (to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#ifndef A_HASH_INCLUDED
#define A_HASH_INCLUDED
#include <iostream>
#include <cstdlib>
#include "gettime.h"
#include "seq.h"
#include "utils.h"
#include "parallel.h"
using namespace std;
template <class ET, class CMP, class HASH>
class Table {
private:
int m;
int mask;
CMP cmp;
HASH hash;
ET least;
ET* TA;
int* compactL;
// needs to be in separate routine due to Cilk bugs
static void clearA(ET* A, int n, ET v) {
parallel_grainsize_256
parallel_for (int i=0; i < n; i++) A[i] = v;
}
struct addF { int operator() (int a, int b) {return a+b;}};
struct emptyF {
ET least; emptyF(ET ll) : least(ll) {}
int operator() (ET a) {return least != a;}};
int hashToRange(int h) {return h & mask;}
int firstHash(ET v) {return hashToRange(hash(v));}
int incHash(int h) {return hashToRange(h+1);}
public:
// Size is the maximum number of values the hash table will hold.
// Overfilling the table could put it into an infinite loop.
Table(int size, CMP cmpF, HASH hashF, ET leastv) :
m(utils::nextPower(2*size)),
mask(m-1),
cmp(cmpF),
hash(hashF),
least(leastv),
TA(newA(ET,m)),
compactL(NULL)
{ clearA(TA,m,leastv); }
// Deletes the allocated arrays
void del() {
free(TA);
if (compactL != NULL) free(compactL);
}
bool insert(ET v) {
int h = firstHash(v);
do {
ET c;
bool r = 0;
do {
c = TA[h];
int b = (c==least) ? 1 : cmp(v,c);
if (!b) return 1;
while (b==1 && !(r=utils::CAS(&TA[h],c,v))) {
if (!(b=cmp(v,c = TA[h]))) return 1;
}
h = incHash(h);
} while (!r);
v = c;
} while (least < v);
return 0;
}
// Returns the value if an equal value is found in the table
// otherwise returns the "least" element.
ET find(ET v) {
int h = firstHash(v);
ET c = TA[h];
int b = 1;
while (c != least && (1==(b=cmp(c,v)))) {
h = incHash(h);
c = TA[h];
}
return (!b) ? c : least;
}
template <class SCMP>
ET findF(int hashVal, SCMP scmp) {
int h = hashToRange(hashVal);
ET c = TA[h];
int b = 1;
while (c != least && (1==(b=scmp(c)))) {
h = incHash(h);
c = TA[h];
}
return (!b) ? c : least;
}
// If called after entries have been added gives each entry a label
// in the range [0..n], where n is the number of entries.
int compactLabels() {
compactL = newA(int,m);
parallel_for (int i=0; i < m; i++) compactL[i] = (least <TA[i]);
int r = sequence::scan(compactL,compactL,m,addF(),0);
return r;
}
// returns a unique integer name in the range [0...m-1] for each
// distinct element, or -1 if not found.
// If compactLabels has been run, then returns the labels in
// range [0..l], where l is the number of entries in the table
int findLabel(ET v) {
int h = firstHash(v);
ET c = TA[h];
int b = 1;
while (c != least && (1==(b=cmp(c,v)))) c = TA[h = incHash(h)];
if (compactL == NULL) return (!b) ? h : -1;
else return (!b) ? compactL[h] : -1;
}
// returns the number of entries
int count() {
int *A = newA(int,m);
parallel_grainsize_256
parallel_for (int i=0; i < m; i++) A[i] = (least <TA[i]);
int r = sequence::reduce(A,0,m,addF());
free(A);
return r;
}
// returns all the current entries compacted into a sequence
seq<ET> entries() {
seq<ET> R = seq<ET>(TA,m).filter(emptyF(least));
return R;
}
// prints the current entries along with the index they are stored at
void print() {
cout << "vals = ";
for (int i=0; i < m; i++)
if (TA[i]!=least) cout << i << ":" << TA[i] << ",";
cout << endl;
}
};
// returns a unique name for each distinct element
// second return value is the number of unique values
template <class ET, class CMP, class HASH>
pair<int*,int> name(seq<ET> A, int m, CMP cmpF, HASH hashF, ET least) {
Table<ET,CMP,HASH> T(m,cmpF,hashF,least);
{parallel_for(int i=0;i<A.size();i++) T.insert(A[i]);}
int n = T.compactLabels();
int *R = newA(int,A.size());
{parallel_for(int i=0;i<A.size();i++) R[i] = T.findLabel(A[i]);}
T.del();
return pair<int*,int>(R,n);
}
template <class ET, class CMP, class HASH>
seq<ET> removeDuplicates(seq<ET> A, int m, CMP cmpF, HASH hashF, ET least) {
//startTime();
Table<ET,CMP,HASH> T(m,cmpF,hashF,least);
//nextTime("make table");
parallel_grainsize_256
parallel_for(int i=0;i<A.size();i++) T.insert(A[i]);
//nextTime("insert");
seq<ET> R = T.entries();
//nextTime("entries");
T.del();
return R;
}
template <class ET, class CMP, class HASH>
seq<ET> removeDuplicates(seq<ET> A, CMP cmpF, HASH hashF, ET least) {
return removeDuplicates(A, A.size(), cmpF,hashF,least);
}
struct intHash{unsigned int operator() (int a) {return utils::hash(a);}};
struct intCmp {
int operator() (int a, int b) {
return (a > b) ? 1 : ((a == b) ? 0 : -1);}
};
// works for non-negative integers (uses -1 to mark cell as empty)
static seq<int> removeDuplicates(seq<int> A) {
return removeDuplicates(A,intCmp(),intHash(),-1);
}
static pair<int*,int> name(seq<int> A, int m) {
return name(A,m, intCmp(),intHash(),-1);
}
typedef Table<int,intCmp,intHash> IntTable;
static IntTable makeIntTable(int m) {
return IntTable(m,intCmp(),intHash(),-1);
}
struct strHash {
unsigned int operator() (char* s) {
unsigned int hash = 0;
while (*s) hash = *s++ + (hash << 6) + (hash << 16) - hash;
return hash;
}
};
struct strCmp {
int operator() (char* s1, char* s2) {
while (*s1 && *s1==*s2) {s1++; s2++;};
return (*s1 > *s2) ? 1 : ((*s1 == *s2) ? 0 : -1);
}
};
static seq<char*> removeDuplicates(seq<char*> S) {
return removeDuplicates(S,strCmp(),strHash(),(char*) NULL);}
static seq<char*> removeDuplicates(seq<char*> S, int m) {
return removeDuplicates(S,m,strCmp(),strHash(),(char*) NULL);}
typedef Table<char*,strCmp,strHash> StrTable;
static StrTable makeStrTable(int m) {
return StrTable(m,strCmp(),strHash(),(char*) NULL);
}
#endif // _A_HASH_INCLUDED