1 package org.matsim.core.router.speedy;
3 import java.util.NoSuchElementException;
9 private final int[] heap;
10 private final int[] pos;
12 private final CostGetter costGetter;
13 private final CostSetter costSetter;
20 void setCost(
int index,
double cost);
24 this.heap =
new int[nodeCount];
25 this.pos =
new int[nodeCount];
26 this.costGetter = costGetter;
27 this.costSetter = costSetter;
30 void insert(
int node) {
34 double nodeCost = this.costGetter.
getCost(node);
36 int parent = parent(i);
37 while (i > 0 && nodeCost < this.costGetter.
getCost(
this.heap[parent])) {
38 this.heap[i] = this.heap[parent];
39 this.pos[this.heap[parent]] = i;
41 parent = parent(parent);
47 void decreaseKey(
int node,
double cost) {
49 for (i = 0; i < this.size; i++) {
50 if (this.heap[i] == node) {
54 if (this.costGetter.
getCost(
this.heap[i]) < cost) {
55 throw new IllegalArgumentException(
"existing cost is already smaller than new cost.");
58 this.costSetter.
setCost(node, cost);
61 int parent = parent(i);
62 while (i > 0 && cost < this.costGetter.
getCost(
this.heap[parent])) {
63 this.heap[i] = this.heap[parent];
64 this.pos[this.heap[parent]] = i;
66 parent = parent(parent);
74 throw new NoSuchElementException(
"heap is empty");
81 int root = this.heap[0];
84 int lastNode = this.heap[this.size - 1];
86 this.heap[0] = lastNode;
87 this.pos[lastNode] = 0;
97 throw new NoSuchElementException(
"heap is empty");
107 return this.size == 0;
114 private void minHeapify(
int i) {
116 int right = right(i);
119 if (left <= (this.size - 1) && this.costGetter.
getCost(
this.heap[left]) < this.costGetter.
getCost(this.heap[i])) {
122 if (right <= (this.size - 1) && this.costGetter.
getCost(this.heap[right]) < this.costGetter.
getCost(this.heap[smallest])) {
127 minHeapify(smallest);
131 private int right(
int i) {
135 private int left(
int i) {
139 private int parent(
int i) {
143 private void swap(
int i,
int parent) {
144 int tmp = this.heap[parent];
145 this.heap[parent] = this.heap[i];
146 this.pos[this.heap[i]] = parent;
double getCost(int index)
void setCost(int index, double cost)