Skip to content

Commit c329c19

Browse files
fix(#8): the longest commit sequences are now retrieved non-recursively to prevent stack overflow
1 parent 22481c9 commit c329c19

1 file changed

Lines changed: 37 additions & 14 deletions

File tree

src/main/java/org/variantsync/vevos/simulation/variability/sequenceextraction/LongestNonOverlappingSequences.java

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,29 +83,52 @@ public List<NonEmptyList<SPLCommit>> extract(final Collection<SPLCommit> commits
8383
}
8484
}
8585

86+
// Sort the sequences once more, after filtering them
87+
commitSequences.sort((o1, o2) -> Integer.compare(o2.size(), o1.size()));
8688
return commitSequences;
8789
}
8890

89-
// Recursively build sequences
9091
private static Set<LinkedList<SPLCommit>> retrieveSequencesForStart(final Map<SPLCommit, Set<SPLCommit>> parentChildMap, final SPLCommit start) {
91-
if (!parentChildMap.containsKey(start)) {
92-
// Create a new sequence that contains the start commit
93-
final Set<LinkedList<SPLCommit>> sequenceSet = new HashSet<>();
92+
// Set for holding all retrieved sequences
93+
Set<LinkedList<SPLCommit>> sequenceSet = new HashSet<>();
94+
{
95+
// We start with the 'start' commit
9496
final LinkedList<SPLCommit> sequence = new LinkedList<>();
9597
sequence.add(start);
9698
sequenceSet.add(sequence);
97-
return sequenceSet;
98-
} else {
99-
// Collect the sequences of the children and prepend the commit to each of them as parent
100-
final Set<LinkedList<SPLCommit>> sequences = new HashSet<>();
101-
for (final SPLCommit child : parentChildMap.get(start)) {
102-
final Set<LinkedList<SPLCommit>> childSequences = retrieveSequencesForStart(parentChildMap, child);
103-
for (final LinkedList<SPLCommit> childSequence : childSequences) {
104-
childSequence.addFirst(start);
105-
sequences.add(childSequence);
99+
}
100+
101+
// We continue to build sequences, going from parent to descendents, until the ends of all possible sequences have been reached
102+
boolean incomplete = true;
103+
while(incomplete) {
104+
Set<LinkedList<SPLCommit>> updatedSet = new HashSet<>();
105+
// Set to false at the start of the iteration, it is set to true if at least one sequence has been extended
106+
incomplete = false;
107+
// For all found sequences, check whether their last commit has more children that can be added to the sequence
108+
// if multiple children exist, new sequences are created
109+
for (LinkedList<SPLCommit> sequence : sequenceSet) {
110+
final SPLCommit parent = sequence.getLast();
111+
if (parentChildMap.containsKey(parent)) {
112+
final Set<SPLCommit> children = parentChildMap.get(parent);
113+
for (SPLCommit child : children) {
114+
// There is at least one child, we are not done yet
115+
incomplete = true;
116+
// Create a new sequence from the old sequence for each child
117+
final LinkedList<SPLCommit> anotherSequence = new LinkedList<>(sequence);
118+
anotherSequence.add(child);
119+
// Add the new sequence to the new set
120+
updatedSet.add(anotherSequence);
121+
}
122+
} else {
123+
// There are no children, simply add the old sequence to the updated set
124+
updatedSet.add(sequence);
106125
}
107126
}
108-
return sequences;
127+
// Update the sequence set
128+
sequenceSet = updatedSet;
109129
}
130+
131+
return sequenceSet;
110132
}
111133
}
134+

0 commit comments

Comments
 (0)