-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPLZ77_3.cpp
157 lines (130 loc) · 4.34 KB
/
PLZ77_3.cpp
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
// This code is part of the DCC 2013 paper: Practical Parallel Lempel-Ziv
// Factorization
// Copyright (c) 2012 Fuyao Zhao, Julian Shun
//
// 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.
/*
* Parallel suffix tree + sequential searching
* Current version doesn't work. We need to compute
* minimum index at each internal node of suffix
* tree to know when to stop searching
*/
#include <cstdio>
#include <iostream>
#include "suffixTree.h"
#include "sequence.h"
#include "Base.h"
#include "test.h"
#include "utils.h"
using namespace std;
// TODO: this module should be update to the new interface.
pair<int *, int> ParallelLPFtoLZ(int *lpf, int n);
pair<int *, int> ParallelLZ77(int *s, int n) {
startTime();
suffixTree st = buildSuffixTree(s, n);
nextTime("\tSuffix Tree");
//references
stNode<int> *nodes = st.nodes;
int root = st.root;
int *minLabel = new int[st.m];
parallel_for (int i = n; i < st.m; i++) {
minLabel[i] = n;
}
//first round rake, only for children
parallel_for (int i = 0; i < n; i++) {
minLabel[i] = nodes[i].locationInOriginalArray;
int pid = nodes[i].parentID;
utils::writeMin(minLabel + pid, minLabel[i]);
}
bool changed = true;
while (changed) {
changed = false;
parallel_for (int i = n; i < st.m; i++) {
int pid = nodes[i].parentID;
if (utils::writeMin(minLabel + pid, minLabel[i]))
changed = true; //fix this -- don't write to global in parallel
}
}
nextTime("\tTree Contraction");
int dep = getDepth(st.m);
int **up = new int*[dep];
int **minup = new int*[dep];
for (int i = 0; i < dep; i++) {
up[i] = new int[st.m]; //this should be computed for all nodes
minup[i] = new int[st.m];
}
//compute up
parallel_for (int i = 0; i < st.m; i++) {
up[0][i] = nodes[i].parentID;
}
for (int d = 1; d < dep; d++) {
parallel_for (int i = 0; i < st.m; i++) {
up[d][i] = up[d - 1][up[d - 1][i]];
}
}
//compute minup
parallel_for (int i = 0; i < st.m; i++) {
minup[0][i] = min(minLabel[i], minLabel[up[0][i]]);
}
for (int d = 1; d < dep; d++) {
parallel_for (int i = 0; i < st.m; i++) {
minup[d][i] = min(minup[d - 1][i], minup[d - 1][up[d - 1][i]]);
}
}
nextTime("\tget minup and up");
//compute lpf by binary search
int *lpf = new int[n];
parallel_for (int i = 0; i < n; i++) {
int cur = nodes[i].parentID;
int pos = minLabel[cur];;
int d = dep - 1;
while (d > 0 && cur != root) {
if (minup[d][cur] < minLabel[i]) {
d--; //scala down the scope
if (minup[d][cur] == minLabel[i])
cur = up[d][cur];
} else {
break;
}
}
//adjusting
if (minLabel[cur] == minLabel[i] && cur != root) {
cur = nodes[cur].parentID;
}
pos = minLabel[cur];
int len = nodes[cur].depth - 1; //nodes depth -1 = the length of the string path
//fix for the un normal structure
if (s[pos + len] == s[minLabel[i] + len] && pos + len < n && minLabel[i] + len < n) len++;
lpf[minLabel[i]] = len;
}
lpf[0] = 0;
nextTime("\tget lpf");
delete minLabel;
delete up;
delete minup;
st.del();
pair<int *, int> r = ParallelLPFtoLZ(lpf, n);
nextTime("\tget lz");
delete lpf;
return r;
}
int parallel_main(int argc, char *argv[]) {
return test_main(argc, argv, (char *)"LZ77 using suffix tree (nlog n)", ParallelLZ77);
}