MultiTreeSPEntry.java

  1. /*  This file is part of Openrouteservice.
  2.  *
  3.  *  Openrouteservice is free software; you can redistribute it and/or modify it under the terms of the
  4.  *  GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1
  5.  *  of the License, or (at your option) any later version.

  6.  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  7.  *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8.  *  See the GNU Lesser General Public License for more details.

  9.  *  You should have received a copy of the GNU Lesser General Public License along with this library;
  10.  *  if not, see <https://www.gnu.org/licenses/>.
  11.  */
  12. package org.heigit.ors.routing.graphhopper.extensions.storages;

  13. /**
  14.  * This class is used to create the shortest-path-tree from linked entities.
  15.  * <p>
  16.  */
  17. public class MultiTreeSPEntry implements Comparable<MultiTreeSPEntry> {

  18.     private int adjNode;
  19.     protected int edge;
  20.     private boolean visited = false;
  21.     private final MultiTreeSPEntryItem[] items;
  22.     private double totalWeight = 0.0;

  23.     public MultiTreeSPEntry(int adjNode, int edgeId, double edgeWeight, boolean updated, MultiTreeSPEntry parent, int numTrees) {
  24.         this.adjNode = adjNode;
  25.         this.edge = edgeId;
  26.         this.items = new MultiTreeSPEntryItem[numTrees];
  27.         double entryWeight;

  28.         for (int i = 0; i < numTrees; ++i) {
  29.             MultiTreeSPEntryItem item = new MultiTreeSPEntryItem();
  30.             items[i] = item;

  31.             entryWeight = parent == null ? Double.POSITIVE_INFINITY : parent.items[i].getWeight();
  32.             if (entryWeight == Double.POSITIVE_INFINITY && parent != null)
  33.                 continue;

  34.             item.setWeight(edgeWeight + entryWeight);
  35.             item.setParent(parent);
  36.             item.setEdge(edgeId);
  37.             item.setOriginalEdge(edgeId);
  38.             item.setUpdate(updated);
  39.             totalWeight += item.getWeight();
  40.         }
  41.     }

  42.     public int getAdjNode() {
  43.         return adjNode;
  44.     }

  45.     public void setAdjNode(int adjNode) {
  46.         this.adjNode = adjNode;
  47.     }

  48.     public int getEdge() {
  49.         return edge;
  50.     }

  51.     public boolean isVisited() {
  52.         return visited;
  53.     }

  54.     public void setVisited(boolean visited) {
  55.         this.visited = visited;
  56.     }

  57.     public int getSize() {
  58.         return items.length;
  59.     }

  60.     public MultiTreeSPEntryItem getItem(int index) {
  61.         return items[index];
  62.     }

  63.     public void resetUpdate(boolean value) {
  64.         for (int i = 0; i < items.length; i++) {
  65.             items[i].setUpdate(value);
  66.         }
  67.     }

  68.     public void updateWeights() {
  69.         totalWeight = 0.0;

  70.         for (int i = 0; i < items.length; i++) {
  71.             if (items[i].getWeight() == Double.POSITIVE_INFINITY) continue;
  72.             totalWeight += items[i].getWeight();
  73.         }
  74.     }

  75.     @Override
  76.     public int compareTo(MultiTreeSPEntry o) {
  77.         if (totalWeight < o.totalWeight)
  78.             return -1;

  79.         // assumption no NaN and no -0
  80.         return totalWeight > o.totalWeight ? 1 : 0;
  81.     }

  82.     @Override
  83.     public String toString() {
  84.         return "adjNode: " + adjNode + ", totalWeight: " + totalWeight; // TODO
  85.     }
  86. }