<commit_before>package nl.tudelft.lifetiles.traverser.models;

import nl.tudelft.lifetiles.graph.models.Edge;
import nl.tudelft.lifetiles.graph.models.Graph;
import nl.tudelft.lifetiles.graph.models.sequence.SequenceSegment;

/**
 * UnifiedPositionTraverser which generates unified positions for each graph so
 * all vertices independent of it's reference in the original are unified within
 * the same coordinate system.
 *
 * @author Jos
 *
 */
public class UnifiedPositionTraverser implements Traverser {
    /**
     * Graph which is being traversed.
     */
    private Graph<SequenceSegment> graphVar;

    /**
     * Traverses the graph, calculates the unified position. Unified positions
     * are needed to visualize a comprehensible model of the graph.
     *
     * @param graph
     *            Graph to be traversed, calculate unified positions.
     * @return traversed graph.
     */
    public final Graph<SequenceSegment> traverseGraph(
            final Graph<SequenceSegment> graph) {
        this.graphVar = graph;
        unifyGraph();
        return graphVar;
    }

    /**
     * Traverses the graph, calculates unified positions. Unified positions
     * are needed to visualize a comprehensible model of the graph.
     */
    private void unifyGraph() {
        for (SequenceSegment vertex : graphVar.getSources()) {
            vertex.setUnifiedStart(1);
            vertex.setUnifiedEnd(vertex.getUnifiedStart()
                    + vertex.getContent().getLength());
            unifyVertex(vertex);
        }
    }

    /**
     * Traverses the vertex, calculates its unified position.
     *
     * @param vertex
     *            Vertex to be traversed.
     */
    private void unifyVertex(final SequenceSegment vertex) {
        for (Edge<SequenceSegment> edge : graphVar.getOutgoing(vertex)) {
            SequenceSegment destination = graphVar.getDestination(edge);
            if (destination.getUnifiedStart() < vertex.getUnifiedEnd()) {
                destination.setUnifiedStart(vertex.getUnifiedEnd());
                destination.setUnifiedEnd(destination.getUnifiedStart()
                        + destination.getContent().getLength());
                unifyVertex(destination);
            }
        }
    }

}
<commit_msg>Fixed UnifiedPositionTraverser to use temporary map<commit_after>package nl.tudelft.lifetiles.traverser.models;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;

import nl.tudelft.lifetiles.graph.models.Edge;
import nl.tudelft.lifetiles.graph.models.Graph;
import nl.tudelft.lifetiles.graph.models.sequence.SequenceSegment;

/**
 * UnifiedPositionTraverser which generates unified positions for each graph so
 * all vertices independent of it's reference in the original are unified within
 * the same coordinate system.
 *
 * @author Jos
 *
 */
public class UnifiedPositionTraverser implements Traverser {
    /**
     * Graph which is being traversed.
     */
    private Graph<SequenceSegment> graphVar;

    /**
     * Map which maps sequence segments to unifiedPositions, used to make
     * modifications on during traversal. Inserted into graph at end of
     * traversal to avoid concurrent modification-like exceptions.
     */
    private HashMap<SequenceSegment, Long> unifiedPositions;

    /**
     * Traverses the graph, calculates the unified position. Unified positions
     * are needed to visualize a comprehensible model of the graph.
     *
     * @param graph
     *            Graph to be traversed, calculate unified positions.
     * @return traversed graph.
     */
    public final Graph<SequenceSegment> traverseGraph(
            final Graph<SequenceSegment> graph) {
        this.graphVar = graph;
        unifyGraph();
        return graphVar;
    }

    /**
     * Traverses the graph, calculates unified positions. Unified positions
     * are needed to visualize a comprehensible model of the graph.
     */
    private void unifyGraph() {
        unifiedPositions = new HashMap<SequenceSegment, Long>();
        for (SequenceSegment vertex : graphVar.getSources()) {
            unifiedPositions.put(vertex, (long) 1);
            unifyVertex(vertex);
        }
        for (Entry<SequenceSegment, Long> entry : unifiedPositions.entrySet()) {
            SequenceSegment vertex = entry.getKey();
            Long position = entry.getValue();
            vertex.setUnifiedStart(position);
            vertex.setUnifiedEnd(position + vertex.getContent().getLength());
        }
    }

    /**
     * Traverses the vertex, calculates its unified position.
     *
     * @param vertex
     *            Vertex to be traversed.
     */
    private void unifyVertex(final SequenceSegment vertex) {
        long vertexPosition = unifiedPositions.get(vertex)
                + vertex.getContent().getLength();
        for (Edge<SequenceSegment> edge : new ArrayList<Edge<SequenceSegment>>(
                graphVar.getOutgoing(vertex))) {
            SequenceSegment destination = graphVar.getDestination(edge);
            if (!unifiedPositions.containsKey(destination)
                    || unifiedPositions.get(destination) < vertexPosition) {
                unifiedPositions.put(destination, vertexPosition);
                unifyVertex(destination);
            }
        }
    }

}

{'fraction_non_alphanumeric': 0.08586997369409996, 'fraction_numerical': 0.0003757985719654265, 'mean_word_length': 2.5676943699731902, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3113329', 'n_tokens_mistral': 1407, 'n_tokens_neox': 1361, 'n_words': 369}