Sha256: b3db37d02ace04c3cf353b33e198fa9d1d6ba3d3082b7afb466c58581267d9da

Contents?: true

Size: 1.62 KB

Versions: 12

Compression:

Stored size: 1.62 KB

Contents

package org.cx4a.rsense.typing;

import java.util.Set;
import java.util.HashSet;

import org.cx4a.rsense.typing.vertex.Vertex;

public class Propagation {
    public final static int MAX_DEPTH = 10;
    
    private Graph graph;
    private Set<Object> visited;
    private int depth;
    private int refCount;

    public Propagation(Graph graph) {
        this.graph = graph;
        this.depth = 0;
        this.refCount = 0;
    }

    public Graph getGraph() {
        return graph;
    }

    public boolean isVisited(Vertex vertex) {
        return visited != null ? visited.contains(getVisitTag(vertex)) : false;
    }

    public void addVisited(Vertex vertex) {
        if (visited == null) {
            // lazy allocation
            visited = new HashSet<Object>();
        }
        visited.add(getVisitTag(vertex));
    }

    public boolean checkVisited(Vertex vertex) {
        if (isVisited(vertex)) {
            return true;
        } else {
            addVisited(vertex);
            return false;
        }
    }

    public boolean visit(Vertex vertex, Vertex src) {
        // Cut out deper branches.
        // This makes results inprecise but also performace improved.
        if (depth >= MAX_DEPTH) {
            return false;
        }

        try {
            depth++;
            return vertex.accept(this, src);
        } finally {
            depth--;
        }
    }

    public void retain() {
        refCount++;
    }

    public boolean release() {
        return --refCount == 0;
    }

    private Object getVisitTag(Vertex vertex) {
        return vertex.getNode() != null ? vertex.getNode() : vertex;
    }
}

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
rsense-core-0.6.6 src/org/cx4a/rsense/typing/Propagation.java
rsense-core-0.6.5 src/org/cx4a/rsense/typing/Propagation.java
rsense-core-0.6.4 src/org/cx4a/rsense/typing/Propagation.java
rsense-core-0.6.2 src/org/cx4a/rsense/typing/Propagation.java
rsense-core-0.6.1 src/org/cx4a/rsense/typing/Propagation.java
rsense-core-0.6.0 src/org/cx4a/rsense/typing/Propagation.java
rsense-core-0.5.9 src/org/cx4a/rsense/typing/Propagation.java
rsense-core-0.5.8 src/org/cx4a/rsense/typing/Propagation.java
rsense-core-0.5.6 src/org/cx4a/rsense/typing/Propagation.java
rsense-core-0.5.2 src/org/cx4a/rsense/typing/Propagation.java
rsense-core-0.5.1 src/org/cx4a/rsense/typing/Propagation.java
rsense-core-0.5.0 src/org/cx4a/rsense/typing/Propagation.java