Search in sources :

Example 31 with IntCursor

use of com.carrotsearch.hppc.cursors.IntCursor in project graphhopper by graphhopper.

the class PrepareRoutingSubnetworks method setSubnetworks.

private int setSubnetworks(Weighting weighting, BooleanEncodedValue subnetworkEnc) {
    // partition graph into strongly connected components using Tarjan's algorithm
    StopWatch sw = new StopWatch().start();
    EdgeBasedTarjanSCC.ConnectedComponents ccs = EdgeBasedTarjanSCC.findComponents(ghStorage, (prev, edge) -> Double.isFinite(GHUtility.calcWeightWithTurnWeightWithAccess(weighting, edge, false, prev)), false);
    List<IntArrayList> components = ccs.getComponents();
    BitSet singleEdgeComponents = ccs.getSingleEdgeComponents();
    long numSingleEdgeComponents = singleEdgeComponents.cardinality();
    logger.info(subnetworkEnc.getName().replaceAll("_subnetwork", "") + " - Found " + ccs.getTotalComponents() + " subnetworks (" + numSingleEdgeComponents + " single edges and " + components.size() + " components with more than one edge, total nodes: " + ccs.getEdgeKeys() + "), took: " + sw.stop().getSeconds() + "s");
    final int minNetworkSizeEdgeKeys = 2 * minNetworkSize;
    // make all small components subnetworks, but keep the biggest (even when its smaller than the given min_network_size)
    sw = new StopWatch().start();
    int subnetworks = 0;
    int markedEdges = 0;
    int smallestNonSubnetwork = ccs.getBiggestComponent().size();
    int biggestSubnetwork = 0;
    for (IntArrayList component : components) {
        if (component == ccs.getBiggestComponent())
            continue;
        if (component.size() < minNetworkSizeEdgeKeys) {
            for (IntCursor cursor : component) markedEdges += setSubnetworkEdge(cursor.value, weighting, subnetworkEnc);
            subnetworks++;
            biggestSubnetwork = Math.max(biggestSubnetwork, component.size());
        } else {
            smallestNonSubnetwork = Math.min(smallestNonSubnetwork, component.size());
        }
    }
    if (minNetworkSizeEdgeKeys > 0) {
        BitSetIterator iter = singleEdgeComponents.iterator();
        for (int edgeKey = iter.nextSetBit(); edgeKey >= 0; edgeKey = iter.nextSetBit()) {
            markedEdges += setSubnetworkEdge(edgeKey, weighting, subnetworkEnc);
            subnetworks++;
            biggestSubnetwork = Math.max(biggestSubnetwork, 1);
        }
    } else if (numSingleEdgeComponents > 0) {
        smallestNonSubnetwork = Math.min(smallestNonSubnetwork, 1);
    }
    int allowedMarked = ghStorage.getEdges() / 2;
    if (markedEdges / 2 > allowedMarked)
        throw new IllegalStateException("Too many total (directed) edges were marked as subnetwork edges: " + markedEdges + " out of " + (2 * ghStorage.getEdges()) + "\n" + "The maximum number of subnetwork edges is: " + (2 * allowedMarked));
    logger.info(subnetworkEnc.getName().replaceAll("_subnetwork", "") + " - Marked " + subnetworks + " subnetworks (biggest: " + biggestSubnetwork + " edges) -> " + (ccs.getTotalComponents() - subnetworks) + " components(s) remain (smallest: " + smallestNonSubnetwork + ", biggest: " + ccs.getBiggestComponent().size() + " edges)" + ", total marked edges: " + markedEdges + ", took: " + sw.stop().getSeconds() + "s");
    return markedEdges;
}
Also used : BitSetIterator(com.carrotsearch.hppc.BitSetIterator) IntCursor(com.carrotsearch.hppc.cursors.IntCursor) BitSet(com.carrotsearch.hppc.BitSet) IntArrayList(com.carrotsearch.hppc.IntArrayList) StopWatch(com.graphhopper.util.StopWatch)

Example 32 with IntCursor

use of com.carrotsearch.hppc.cursors.IntCursor in project graphhopper by graphhopper.

the class EdgeBasedTarjanSCC method findComponentsForStartEdges.

private ConnectedComponents findComponentsForStartEdges(IntContainer startEdges) {
    initForStartEdges(startEdges.size());
    for (IntCursor edge : startEdges) {
        // todo: using getEdgeIteratorState here is not efficient
        EdgeIteratorState edgeState = graph.getEdgeIteratorState(edge.value, Integer.MIN_VALUE);
        if (!edgeTransitionFilter.accept(NO_EDGE, edgeState))
            continue;
        findComponentsForEdgeState(edgeState);
    }
    return components;
}
Also used : EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) IntCursor(com.carrotsearch.hppc.cursors.IntCursor)

Example 33 with IntCursor

use of com.carrotsearch.hppc.cursors.IntCursor in project graphhopper by graphhopper.

the class PtLocationSnapper method snapAll.

public Result snapAll(List<GHLocation> locations, List<EdgeFilter> snapFilters) {
    PointList points = new PointList(2, false);
    ArrayList<Snap> pointSnaps = new ArrayList<>();
    ArrayList<Supplier<Label.NodeId>> allSnaps = new ArrayList<>();
    for (int i = 0; i < locations.size(); i++) {
        GHLocation location = locations.get(i);
        if (location instanceof GHPointLocation) {
            GHPoint point = ((GHPointLocation) location).ghPoint;
            final Snap closest = locationIndex.findClosest(point.lat, point.lon, snapFilters.get(i));
            if (!closest.isValid()) {
                IntHashSet result = new IntHashSet();
                gtfsStorage.getStopIndex().findEdgeIdsInNeighborhood(point.lat, point.lon, 0, result::add);
                gtfsStorage.getStopIndex().findEdgeIdsInNeighborhood(point.lat, point.lon, 1, result::add);
                if (result.isEmpty()) {
                    throw new PointNotFoundException("Cannot find point: " + point, i);
                }
                IntCursor stopNodeId = result.iterator().next();
                for (Map.Entry<GtfsStorage.FeedIdWithStopId, Integer> e : gtfsStorage.getStationNodes().entrySet()) {
                    if (e.getValue() == stopNodeId.value) {
                        Stop stop = gtfsStorage.getGtfsFeeds().get(e.getKey().feedId).stops.get(e.getKey().stopId);
                        final Snap stopSnap = new Snap(stop.stop_lat, stop.stop_lon);
                        stopSnap.setClosestNode(stopNodeId.value);
                        allSnaps.add(() -> new Label.NodeId(Optional.ofNullable(gtfsStorage.getPtToStreet().get(stopSnap.getClosestNode())).orElse(-1), stopSnap.getClosestNode()));
                        points.add(stopSnap.getQueryPoint().lat, stopSnap.getQueryPoint().lon);
                    }
                }
            } else {
                pointSnaps.add(closest);
                allSnaps.add(() -> new Label.NodeId(closest.getClosestNode(), Optional.ofNullable(gtfsStorage.getStreetToPt().get(closest.getClosestNode())).orElse(-1)));
                points.add(closest.getSnappedPoint());
            }
        } else if (location instanceof GHStationLocation) {
            final Snap stopSnap = findByStopId((GHStationLocation) location, i);
            allSnaps.add(() -> new Label.NodeId(Optional.ofNullable(gtfsStorage.getPtToStreet().get(stopSnap.getClosestNode())).orElse(-1), stopSnap.getClosestNode()));
            points.add(stopSnap.getQueryPoint().lat, stopSnap.getQueryPoint().lon);
        }
    }
    // modifies pointSnaps!
    QueryGraph queryGraph = QueryGraph.create(graphHopperStorage, pointSnaps);
    List<Label.NodeId> nodes = new ArrayList<>();
    for (Supplier<Label.NodeId> supplier : allSnaps) {
        nodes.add(supplier.get());
    }
    return new Result(queryGraph, nodes, points);
}
Also used : PointList(com.graphhopper.util.PointList) Stop(com.conveyal.gtfs.model.Stop) PointNotFoundException(com.graphhopper.util.exceptions.PointNotFoundException) ArrayList(java.util.ArrayList) IntHashSet(com.carrotsearch.hppc.IntHashSet) Snap(com.graphhopper.storage.index.Snap) IntCursor(com.carrotsearch.hppc.cursors.IntCursor) Supplier(java.util.function.Supplier) GHPoint(com.graphhopper.util.shapes.GHPoint) GHPoint(com.graphhopper.util.shapes.GHPoint) Map(java.util.Map) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph)

Example 34 with IntCursor

use of com.carrotsearch.hppc.cursors.IntCursor in project crate by crate.

the class JobSetup method prepareOnHandler.

public List<CompletableFuture<StreamBucket>> prepareOnHandler(SessionSettings sessionInfo, Collection<? extends NodeOperation> nodeOperations, RootTask.Builder taskBuilder, List<Tuple<ExecutionPhase, RowConsumer>> handlerPhases, SharedShardContexts sharedShardContexts) {
    Context context = new Context(clusterService.localNode().getId(), sessionInfo, taskBuilder, LOGGER, distributingConsumerFactory, nodeOperations, sharedShardContexts);
    for (Tuple<ExecutionPhase, RowConsumer> handlerPhase : handlerPhases) {
        context.registerLeaf(handlerPhase.v1(), handlerPhase.v2());
    }
    registerContextPhases(nodeOperations, context);
    LOGGER.trace("prepareOnHandler: nodeOperations={}, handlerPhases={}, targetSourceMap={}", nodeOperations, handlerPhases, context.opCtx.targetToSourceMap);
    IntHashSet leafs = new IntHashSet();
    for (Tuple<ExecutionPhase, RowConsumer> handlerPhase : handlerPhases) {
        ExecutionPhase phase = handlerPhase.v1();
        createContexts(phase, context);
        leafs.add(phase.phaseId());
    }
    leafs.addAll(context.opCtx.findLeafs());
    for (IntCursor cursor : leafs) {
        prepareSourceOperations(cursor.value, context);
    }
    assert context.opCtx.allNodeOperationContextsBuilt() : "some nodeOperations haven't been processed";
    return context.directResponseFutures;
}
Also used : TransactionContext(io.crate.metadata.TransactionContext) NodeContext(io.crate.metadata.NodeContext) IntCursor(com.carrotsearch.hppc.cursors.IntCursor) IntHashSet(com.carrotsearch.hppc.IntHashSet) ExecutionPhase(io.crate.execution.dsl.phases.ExecutionPhase) ProjectingRowConsumer(io.crate.execution.engine.pipeline.ProjectingRowConsumer) RowConsumer(io.crate.data.RowConsumer)

Example 35 with IntCursor

use of com.carrotsearch.hppc.cursors.IntCursor in project crate by crate.

the class NodeFetchRequest method writeTo.

@Override
public void writeTo(StreamOutput out) throws IOException {
    super.writeTo(out);
    out.writeLong(jobId.getMostSignificantBits());
    out.writeLong(jobId.getLeastSignificantBits());
    out.writeVInt(fetchPhaseId);
    out.writeBoolean(closeContext);
    if (toFetch == null) {
        out.writeVInt(0);
    } else {
        out.writeVInt(toFetch.size());
        for (IntObjectCursor<? extends IntContainer> toFetchCursor : toFetch) {
            out.writeVInt(toFetchCursor.key);
            out.writeVInt(toFetchCursor.value.size());
            for (IntCursor docCursor : toFetchCursor.value) {
                out.writeInt(docCursor.value);
            }
        }
    }
}
Also used : IntCursor(com.carrotsearch.hppc.cursors.IntCursor)

Aggregations

IntCursor (com.carrotsearch.hppc.cursors.IntCursor)38 Map (java.util.Map)11 IntIndexedContainer (com.carrotsearch.hppc.IntIndexedContainer)8 ArrayList (java.util.ArrayList)7 GraphHopperStorage (com.graphhopper.storage.GraphHopperStorage)5 HashMap (java.util.HashMap)5 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)5 Index (org.elasticsearch.index.Index)5 IndexNotFoundException (org.elasticsearch.index.IndexNotFoundException)5 IntArrayList (com.carrotsearch.hppc.IntArrayList)4 TreeMap (java.util.TreeMap)4 Metadata (org.elasticsearch.cluster.metadata.Metadata)4 IntObjectHashMap (com.carrotsearch.hppc.IntObjectHashMap)3 ConnectedComponents (com.graphhopper.routing.subnetwork.EdgeBasedTarjanSCC.ConnectedComponents)3 GraphBuilder (com.graphhopper.storage.GraphBuilder)3 ShardCollectorProvider (io.crate.execution.engine.collect.ShardCollectorProvider)3 RelationName (io.crate.metadata.RelationName)3 TransactionContext (io.crate.metadata.TransactionContext)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3