use of org.sirix.utils.Pair in project sirix by sirixdb.
the class FMSE method match.
/**
* Actual matching.
*
* @param oldLabels nodes in tree1, sorted by node type (element, attribute, text, comment, ...)
* @param newLabels nodes in tree2, sorted by node type (element, attribute, text, comment, ...)
* @param matching {@link Matching} reference
* @param cmp functional class
*/
private void match(final Map<Kind, List<Long>> oldLabels, final Map<Kind, List<Long>> newLabels, final Matching matching, final Comparator<Long> cmp) {
final Set<Kind> labels = oldLabels.keySet();
// intersection
labels.retainAll(newLabels.keySet());
// 2 - for each label do
for (final Kind label : labels) {
// 2(a)
final List<Long> first = oldLabels.get(label);
// 2(b)
final List<Long> second = newLabels.get(label);
// 2(c)
final List<Pair<Long, Long>> common = Util.longestCommonSubsequence(first, second, cmp);
// Used to remove the nodes in common from s1 and s2 in step 2(e).
final Map<Long, Boolean> seen = new HashMap<>();
// 2(d) - for each pair of nodes in the lcs: add to matching.
for (final Pair<Long, Long> p : common) {
matching.add(p.getFirst(), p.getSecond());
seen.put(p.getFirst(), true);
seen.put(p.getSecond(), true);
}
// 2(e) (prepare) - remove nodes in common from s1, s2.
removeCommonNodes(first, seen);
removeCommonNodes(second, seen);
// 2(e) - For each unmatched node x \in s1.
final Iterator<Long> firstIterator = first.iterator();
while (firstIterator.hasNext()) {
final Long firstItem = firstIterator.next();
boolean firstIter = true;
// If there is an unmatched node y \in s2.
final Iterator<Long> secondIterator = second.iterator();
while (secondIterator.hasNext()) {
final Long secondItem = secondIterator.next();
// Such that equal.
if (cmp.isEqual(firstItem, secondItem)) {
// 2(e)A
matching.add(firstItem, secondItem);
// 2(e)B
if (firstIter) {
firstIter = false;
firstIterator.remove();
}
secondIterator.remove();
break;
}
}
}
}
}