All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jgrapht.alg.isomorphism.VF2MappingIterator Maven / Gradle / Ivy

The newest version!
/*
 * (C) Copyright 2015-2023, by Fabian Späh and Contributors.
 *
 * JGraphT : a free Java graph-theory library
 *
 * See the CONTRIBUTORS.md file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0, or the
 * GNU Lesser General Public License v2.1 or later
 * which is available at
 * http://www.gnu.org/licenses/old-licenses/lgpl-2.1-standalone.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR LGPL-2.1-or-later
 */
package org.jgrapht.alg.isomorphism;

import org.jgrapht.*;

import java.util.*;

abstract class VF2MappingIterator
    implements Iterator>
{
    protected Comparator vertexComparator;
    protected Comparator edgeComparator;

    protected IsomorphicGraphMapping nextMapping;
    protected Boolean hadOneMapping;

    protected GraphOrdering ordering1, ordering2;

    protected ArrayDeque> stateStack;

    public VF2MappingIterator(
        GraphOrdering ordering1, GraphOrdering ordering2,
        Comparator vertexComparator, Comparator edgeComparator)
    {
        this.ordering1 = ordering1;
        this.ordering2 = ordering2;
        this.vertexComparator = vertexComparator;
        this.edgeComparator = edgeComparator;
        this.stateStack = new ArrayDeque<>();
    }

    /**
     * This function moves over all mappings between graph1 and graph2. It changes the state of the
     * whole iterator.
     *
     * @return null or one matching between graph1 and graph2
     */
    protected abstract IsomorphicGraphMapping match();

    protected IsomorphicGraphMapping matchAndCheck()
    {
        IsomorphicGraphMapping rel = match();
        if (rel != null) {
            hadOneMapping = true;
        }
        return rel;
    }

    @Override
    public boolean hasNext()
    {
        return nextMapping != null || (nextMapping = matchAndCheck()) != null;

    }

    @Override
    public IsomorphicGraphMapping next()
    {
        if (nextMapping != null) {
            IsomorphicGraphMapping tmp = nextMapping;
            nextMapping = null;
            return tmp;
        }

        IsomorphicGraphMapping rel = matchAndCheck();
        if (rel == null) {
            throw new NoSuchElementException();
        }
        return rel;
    }

    @Override
    public void remove()
    {
        throw new UnsupportedOperationException();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy