-
Notifications
You must be signed in to change notification settings - Fork 18
/
smmh2.h
164 lines (148 loc) · 2.76 KB
/
smmh2.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
#pragma once
namespace smmh2{
template<class T>
__device__
void inline swap(T& a, T& b){
T c(a); a=b; b=c;
}
template<class T>
__device__
int adjust_sibling(T* smmh,int Y,int max_size){
int s;
if(Y & 1){ // Y is left child)
s = Y + 1;
if(s >= max_size)
return Y;
if(smmh[Y] > smmh[s]){
swap(smmh[Y],smmh[s]);
return s;
}
}else{ // Y is right child
s = Y - 1;
if(smmh[Y] < smmh[s]){
swap(smmh[Y],smmh[s]);
return s;
}
}
return Y;
}
__device__
int inline parent(int x){
return ((x - 1) >> 1);
}
__device__
int inline grandparent(int x){
return ((x - 3) >> 2);
}
__device__
int inline leftchild(int x){
return ((x << 1) + 1);
}
__device__
int inline rightchild(int x){
return ((x << 1) + 2);
}
__device__
bool inline is_leaf(unsigned int x,int max_size){
return leftchild(x) >= max_size;
}
template<class T>
__device__
int adjust_grandparent(T* smmh,int Y,int max_size){
if(Y <= 2)
return Y;
int G = grandparent(Y);
int GL = leftchild(G),GR = rightchild(G);
if(smmh[GL] > smmh[Y]){
swap(smmh[GL],smmh[Y]);
return GL;
}else if(smmh[GR] < smmh[Y]){
swap(smmh[GR],smmh[Y]);
return GR;
}
return Y;
}
template<class T>
__device__
void insert(T* smmh,int& max_size,T& entry){
int Y = max_size;
smmh[max_size++] = entry;
//printf("T %d %f\n",(int)entry.second,entry.first);
while(1){
Y = adjust_sibling(smmh,Y,max_size);
int X = adjust_grandparent(smmh,Y,max_size);
if(X == Y)
break;
Y = X;
}
}
template<class T>
__device__
int adjust_grandchild(T* smmh,int Y,int max_size){
if(Y & 1){
if(is_leaf(Y,max_size))
return Y;
int CL = leftchild(Y),CR = leftchild(Y + 1);
int C = CL;
if(CR < max_size && smmh[CR] < smmh[CL])
C = CR;
if(smmh[C] < smmh[Y]){
swap(smmh[C], smmh[Y]);
return C;
}
}else{ // Y is a rightchild
int CL = rightchild(Y - 1),CR = rightchild(Y);
if(CL >= max_size)
return Y;
int C = CL;
if(CR < max_size && smmh[CR] > smmh[CL])
C = CR;
if(smmh[C] > smmh[Y]){
swap(smmh[C], smmh[Y]);
return C;
}
}
return Y;
}
template<class T>
__device__
void deletion(T* smmh,int idx,int& max_size){
smmh[idx] = smmh[--max_size];
int Y = idx;
while(1){
Y = adjust_sibling(smmh,Y,max_size);
int X = adjust_grandchild(smmh,Y,max_size);
if(X == Y)
break;
Y = X;
}
}
template<class T>
__device__
T pop_min(T* smmh,int& max_size){
T ret = smmh[1];
deletion(smmh,1,max_size);
return ret;
}
// NOTICE: must ensure max_size > 2
template<class T>
__device__
T pop_max(T* smmh,int& max_size){
T ret = smmh[2];
deletion(smmh,2,max_size);
return ret;
}
template<class T>
__device__
void pretty_print(T* smmh,int& max_size){
int border = 2;
for(int i = 0;i < max_size;++i){
printf("%d\t",smmh[i]);
if(i + 2 == border){
printf("\n");
border <<= 1;
}
}
printf("\n");
}
};