-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbag.h
200 lines (176 loc) · 4.13 KB
/
bag.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
#ifndef Bag_H
#define Bag_H
#include <cstdlib>
#include <vector>
#include <iostream>
#include <pthread.h>
#define S_DEFAULT 20
class Node {
public:
int val;
Node *left;
Node *right;
Node() : val(0), left(NULL), right(NULL) {
}
Node(int v) : val(v), left(NULL), right(NULL) {
}
Node(int v, Node *left, Node *right) :
val(v), left(left), right(right) {
}
void set(int v) {
val = v;
}
~Node(){
delete(left);
delete(right);
}
};
class Pennant
{
public:
int k;
Node *root;
Pennant() {
k = -1;
root = new Node();
}
Pennant(int a) {
k = 0;
root = new Node(a);
}
Pennant(int a, int b) {
k = 1;
root = new Node(a);
root->left = new Node(b);
}
Pennant(int d, Node* n) {
k = d;
root = n;
}
void pennant_union(Pennant* y) {
y->root->right = this->root->left;
this->root->left = y->root;
k++;
}
bool is_empty() {
return k == -1;
}
Pennant* pennant_split() {
Node n;
Pennant* y = new Pennant(--k, root->left);
root->left = y->root->right;
root->right = NULL;
y->root->right = NULL;
return y;
}
~Pennant(){
delete(root);
}
};
class Bag {
public:
int r;
std::vector<Pennant *> S;
pthread_mutex_t lock;
Bag() : r(S_DEFAULT) {
S.reserve(r);
for(int i = 0; i < r; i++){
S.push_back(new Pennant());
}
pthread_mutex_init(&(lock),NULL);
}
Bag(int s) : r(s) {
S.reserve(r);
for(int i = 0; i < r; i++){
S.push_back(new Pennant());
}
pthread_mutex_init(&(lock),NULL);
}
void insert(int v) {
int i = 0;
Pennant* x = new Pennant(v);
pthread_mutex_lock(&lock);
while (!S[i]->is_empty()) {
S[i]->pennant_union(x);
x = S[i];
S[i] = new Pennant();
i++;
}
S[i] = x;
pthread_mutex_unlock(&lock);
}
void bag_union(Bag* P) {
Pennant* y = new Pennant();
pthread_mutex_lock(&lock);
for (int i = 0; i < r; ++i) {
if (S[i]->is_empty()) {
if (P->S[i]->is_empty()) {
if (!y->is_empty()) {
S[i] = y;
y = new Pennant();
}
} else { // !P.S[i].is_empty()
if (y->is_empty()) {
S[i] = P->S[i];
P->S[i] = new Pennant();
}
}
}
else { // !this.S[i].is_empty()
if (P->S[i]->is_empty()) {
if (!y->is_empty()) {
y->pennant_union(S[i]);
S[i] = new Pennant();
}
} else { // !P.S[i].is_empty()
if (y->is_empty()) {
S[i]->pennant_union(P->S[i]);
y = S[i];
S[i] = new Pennant();
P->S[i] = new Pennant();
} else {
y->pennant_union(P->S[i]);
}
}
}
}
pthread_mutex_unlock(&lock);
//delete(P);
}
bool is_empty() {
for (int i = 0; i < r; ++i) {
if (!S[i]->is_empty()) {
return false;
}
}
return true;
}
int get_size() {
int sum = 0;
int mult = 1;
for (int i = 0; i < r; ++i) {
if (!S[i]->is_empty()) {
sum += mult;
}
mult = mult << 1;
}
return sum;
}
int get_depth() {
int ret = -1;
for (int i = 0; i < r; ++i) {
if (!S[i]->is_empty()) {
ret = i;
}
}
return ret + 1;
}
~Bag(){
int size = S.size();
for(int i = 0; i < size; i++){
delete(S[i]);
}
//delete S;
}
};
#endif