<commit_before>package org.jgrapht.alg.flow;

import org.jgrapht.DirectedGraph;
import org.jgrapht.alg.interfaces.MaximumFlowAlgorithm;
import org.jgrapht.alg.util.ExtensionManager;
import org.jgrapht.alg.util.ExtensionManager.ExtensionFactory;

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

public abstract class MaximumFlowAlgorithmBase<V, E> implements MaximumFlowAlgorithm<V, E> {

    /**
     * Default tolerance.
     */
    public static final double DEFAULT_EPSILON = 1e-9;

    private ExtensionManager<V, ? extends VertexExtensionBase>  vXs;
    private ExtensionManager<E, ? extends EdgeExtensionBase>    eXs;

    /* package */ abstract DirectedGraph<V, E> getNetwork();

    <VE extends VertexExtensionBase, EE extends EdgeExtensionBase>
    void init(ExtensionFactory<VE> vertexExtensionFactory, ExtensionFactory<EE> edgeExtensionFactory) {
        vXs = new ExtensionManager<V, VE>(vertexExtensionFactory);
        eXs = new ExtensionManager<E, EE>(edgeExtensionFactory);
    }

    protected void buildInternal()
    {
        DirectedGraph<V, E> n = getNetwork();

        for (V u : n.vertexSet()) {
            VertexExtensionBase ux = extendedVertex(u);

            ux.prototype = u;

            for (E e : n.outgoingEdgesOf(u)) {
                V v = n.getEdgeTarget(e);

                VertexExtensionBase vx = extendedVertex(v);
                EdgeExtensionBase   ex = extendedEdge(e);

                ex.source    = ux;
                ex.target    = vx;
                ex.capacity  = n.getEdgeWeight(e);
                ex.prototype = e;

                EdgeExtensionBase iex = createInverse(ex);

                ux.getOutgoing().add(ex);
                vx.getOutgoing().add(iex);
            }
        }
    }

    private EdgeExtensionBase createInverse(EdgeExtensionBase ex) {
        EdgeExtensionBase iex = null;

        try {

            iex = eXs.createInstance();

            iex.source = ex.target;
            iex.target = ex.source;

            ex.inverse  = iex;
            iex.inverse = ex;

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }

        return iex;
    }

    // DIE, JAVA, DIE!
    private VertexExtensionBase extendedVertex(V v) {
        return this.<VertexExtensionBase>vertexExtended(v);
    }
    private EdgeExtensionBase   extendedEdge(E e) {
        return this.<EdgeExtensionBase>edgeExtended(e);
    }

    protected <VE extends VertexExtensionBase> VE vertexExtended(V v) {
        return (VE) vXs.get(v);<commit_msg>(FLOW): Fixed double-(inverse-)edge creation during residual network         composition<commit_after>package org.jgrapht.alg.flow;

import org.jgrapht.DirectedGraph;
import org.jgrapht.alg.interfaces.MaximumFlowAlgorithm;
import org.jgrapht.alg.util.ExtensionManager;
import org.jgrapht.alg.util.ExtensionManager.ExtensionFactory;

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

public abstract class MaximumFlowAlgorithmBase<V, E> implements MaximumFlowAlgorithm<V, E> {

    /**
     * Default tolerance.
     */
    public static final double DEFAULT_EPSILON = 1e-9;

    private ExtensionManager<V, ? extends VertexExtensionBase>  vXs;
    private ExtensionManager<E, ? extends EdgeExtensionBase>    eXs;

    /* package */ abstract DirectedGraph<V, E> getNetwork();

    <VE extends VertexExtensionBase, EE extends EdgeExtensionBase>
    void init(ExtensionFactory<VE> vertexExtensionFactory, ExtensionFactory<EE> edgeExtensionFactory) {
        vXs = new ExtensionManager<V, VE>(vertexExtensionFactory);
        eXs = new ExtensionManager<E, EE>(edgeExtensionFactory);
    }

    protected void buildInternal()
    {
        DirectedGraph<V, E> n = getNetwork();

        for (V u : n.vertexSet()) {
            VertexExtensionBase ux = extendedVertex(u);

            ux.prototype = u;

            for (E e : n.outgoingEdgesOf(u)) {
                V v = n.getEdgeTarget(e);

                VertexExtensionBase vx = extendedVertex(v);

                EdgeExtensionBase ex    = createEdge(ux, vx, e, n.getEdgeWeight(e));
                EdgeExtensionBase iex   = createInverse(ex, n);

                ux.getOutgoing().add(ex);

                // NB: Any better?
                if (iex.prototype == null)
                    vx.getOutgoing().add(iex);
            }
        }
    }

    private EdgeExtensionBase createEdge(VertexExtensionBase source, VertexExtensionBase target, E e, double weight) {
        EdgeExtensionBase ex = extendedEdge(e);

        ex.source    = source;
        ex.target    = target;
        ex.capacity  = weight;
        ex.prototype = e;

        return ex;
    }

    private EdgeExtensionBase createInverse(EdgeExtensionBase ex, DirectedGraph<V, E> n) {
        EdgeExtensionBase iex;

        if (n.containsEdge(ex.target.prototype, ex.source.prototype)) {
            E ie = n.getEdge(ex.target.prototype, ex.source.prototype);
            iex = createEdge(ex.target, ex.source, ie, n.getEdgeWeight(ie));
        } else {
            iex = eXs.createInstance();

            iex.source = ex.target;
            iex.target = ex.source;
        }

        ex.inverse  = iex;
        iex.inverse = ex;

        return iex;
    }

    // DIE, JAVA, DIE!
    private VertexExtensionBase extendedVertex(V v) {
        return this.<VertexExtensionBase>vertexExtended(v);
    }
    private EdgeExtensionBase   extendedEdge(E e) {
        return this.<EdgeExtensionBase>edgeExtended(e);
    }

    protected <VE extends VertexExtensionBase> VE vertexExtended(V v) {
        return (VE) vXs.get(v);
{'fraction_non_alphanumeric': 0.09989576094510076, 'fraction_numerical': 0.0006949270326615705, 'mean_word_length': 2.408525754884547, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, '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': '6442771', 'n_tokens_mistral': 1743, 'n_tokens_neox': 1641, 'n_words': 363}