-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparseSet.h
169 lines (151 loc) · 4.83 KB
/
sparseSet.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
// This code is based on the paper "Phase-Concurrent Hash Tables for
// Determinism" by Julian Shun and Guy Blelloch from SPAA 2014.
// Copyright (c) 2014 Julian Shun and Guy Blelloch
//
// 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.
#pragma once
#include "parallel.h"
#include "utils.h"
#include "math.h"
#include "myVector.h"
using namespace std;
// returns the log base 2 rounded up (works on ints or longs or unsigned versions)
template <class T>
static int log2RoundUp(T i) {
int a=0;
T b=i-1;
while (b > 0) {b = b >> 1; a++;}
return a;
}
typedef pair<uintE,myVector*> kvPair;
class sparseSet {
public:
uintT m;
intT mask;
myVector* vectors;
kvPair* TA;
float loadFactor;
void clearA() {
parallel_for (long i=0; i < m; i++) {
TA[i].first = UINT_E_MAX;
}
}
void initA() {
parallel_for (long i=0; i < m; i++) {
TA[i].second = &vectors[i];
vectors[i].init();
}
}
struct notEmptyF {
int operator() (kvPair a) {return a.first != UINT_E_MAX;}};
inline uintT hashToRange(uintT h) {return h & mask;}
inline uintT firstIndex(uintT v) {return hashToRange(hashInt(v));}
inline uintT incrementIndex(uintT h) {return hashToRange(h+1);}
// Size is the maximum number of values the hash table will hold.
// Overfilling the table could put it into an infinite loop.
sparseSet(long size, float _loadFactor) :
loadFactor(_loadFactor),
m((uintT) 1 << log2RoundUp((uintT)(_loadFactor*size)+100)),
mask(m-1),
TA(newA(kvPair,m))
{
vectors = newA(myVector,m);
initA();
}
// Deletes the allocated arrays
void del() {
free(TA);
free(vectors);
}
// nondeterministic insert
bool insert(uintE src, uintE dst) {
uintT h = firstIndex(src);
while (1) {
if(TA[h].first == UINT_E_MAX && CAS(&TA[h].first,UINT_E_MAX,src)) {
TA[h].second->reset(); //reset myVector to size 0
TA[h].second->add(dst);
return 1; //return true if value originally didn't exist
}
else if (TA[h].first == src) {
TA[h].second->add(dst);
return 0;
}
// move to next bucket
h = incrementIndex(h);
}
return 0; // should never get here
}
myVector* find(uintE v) {
uintT h = firstIndex(v);
kvPair c = TA[h];
while (1) {
if (c.first == UINT_E_MAX) return NULL;
else if (v == c.first)
return c.second;
h = incrementIndex(h);
c = TA[h];
}
}
template <class F>
void map(F f){
parallel_for(long i=0;i<m;i++)
if(TA[i].first != UINT_E_MAX) f(TA[i]);
}
template <class F>
void mapIndex(F f){
parallel_for(long i=0;i<m;i++)
if(TA[i].first != UINT_E_MAX) f(TA[i],i);
}
// returns all the current entries compacted into a sequence
_seq<kvPair> entries() {
bool *FL = newA(bool,m);
parallel_for (long i=0; i < m; i++)
FL[i] = (TA[i].first != UINT_E_MAX);
_seq<kvPair> R = pack((kvPair*)NULL, FL, (uintT) 0, m, sequence::getA<kvPair,uintE>(TA));
//sequence::pack(TA,(entry*)NULL,FL,m);
free(FL);
return R;
}
// returns all the current entries satisfying predicate f compacted into a sequence
template <class F>
_seq<kvPair> entries(F f) {
bool *FL = newA(bool,m);
parallel_for (long i=0; i < m; i++)
FL[i] = (TA[i].first != UINT_E_MAX && f(TA[i]));
_seq<kvPair> R = pack((kvPair*)NULL, FL, (uintT) 0, m, sequence::getA<kvPair,uintE>(TA));
//sequence::pack(TA,(entry*)NULL,FL,m);
free(FL);
return R;
}
// returns the number of entries
intT count() {
return sequence::mapReduce<intT>(TA,m,addF<intT>(),notEmptyF());
}
// prints the current entries along with the index they are stored at
void print() {
cout << "vals = ";
for (long i=0; i < m; i++) {
if (TA[i].first != UINT_E_MAX)
{ cout << "(" << TA[i].first << "," << TA[i].second << ") ";}
}
cout << endl;
}
};