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
|
/*
* SonarQube
* Copyright (C) 2009-2025 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.duplications.detector.suffixtree;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
public final class Search {
private final SuffixTree tree;
private final TextSet text;
private final Collector reporter;
private final List<Integer> list = new ArrayList<>();
private final List<Node> innerNodes = new ArrayList<>();
private static final Comparator<Node> DEPTH_COMPARATOR = (o1, o2) -> o2.depth - o1.depth;
private Search(SuffixTree tree, TextSet text, Collector reporter) {
this.tree = tree;
this.text = text;
this.reporter = reporter;
}
public static void perform(TextSet text, Collector reporter) {
new Search(SuffixTree.create(text), text, reporter).compute();
}
private void compute() {
// O(N)
dfs();
// O(N * log(N))
Collections.sort(innerNodes, DEPTH_COMPARATOR);
// O(N)
visitInnerNodes();
}
/**
* Depth-first search (DFS).
*/
private void dfs() {
Deque<Node> stack = new LinkedList<>();
stack.add(tree.getRootNode());
while (!stack.isEmpty()) {
Node node = stack.removeLast();
node.startSize = list.size();
if (node.getEdges().isEmpty()) {
// leaf
list.add(node.depth);
node.endSize = list.size();
} else {
if (!node.equals(tree.getRootNode())) {
// inner node = not leaf and not root
innerNodes.add(node);
}
for (Edge edge : node.getEdges()) {
Node endNode = edge.getEndNode();
endNode.depth = node.depth + edge.getSpan() + 1;
stack.addLast(endNode);
}
}
}
// At this point all inner nodes are ordered by the time of entering, so we visit them from last to first
ListIterator<Node> iterator = innerNodes.listIterator(innerNodes.size());
while (iterator.hasPrevious()) {
Node node = iterator.previous();
int max = -1;
for (Edge edge : node.getEdges()) {
max = Math.max(edge.getEndNode().endSize, max);
}
node.endSize = max;
}
}
/**
* Each inner-node represents prefix of some suffixes, thus substring of text.
*/
private void visitInnerNodes() {
for (Node node : innerNodes) {
if (containsOrigin(node)) {
report(node);
}
}
}
/**
* TODO Godin: in fact computations here are the same as in {@link #report(Node)},
* so maybe would be better to remove this duplication,
* however it should be noted that this check can't be done in {@link Collector#endOfGroup()},
* because it might lead to creation of unnecessary new objects
*/
private boolean containsOrigin(Node node) {
for (int i = node.startSize; i < node.endSize; i++) {
int start = tree.text.length() - list.get(i);
int end = start + node.depth;
if (text.isInsideOrigin(end)) {
return true;
}
}
return false;
}
private void report(Node node) {
reporter.startOfGroup(node.endSize - node.startSize, node.depth);
for (int i = node.startSize; i < node.endSize; i++) {
int start = tree.text.length() - list.get(i);
int end = start + node.depth;
reporter.part(start, end);
}
reporter.endOfGroup();
}
public abstract static class Collector {
/**
* Invoked at the beginning of processing for current node.
* <p>
* Length - is a depth of node. And nodes are visited in descending order of depth,
* thus we guaranty that length will not increase between two sequential calls of this method
* (can be equal or less than previous value).
* </p>
*
* @param size number of parts in group
* @param length length of each part in group
*/
abstract void startOfGroup(int size, int length);
/**
* Invoked as many times as leaves in the subtree, where current node is root.
*
* @param start start position in generalised text
* @param end end position in generalised text
*/
abstract void part(int start, int end);
/**
* Invoked at the end of processing for current node.
*/
abstract void endOfGroup();
}
}
|