MATSIM
WrappedBinaryMinHeap.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * WrappedBinaryMinHeap.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2013 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.router.priorityqueue;
22 
23 import java.util.IdentityHashMap;
24 import java.util.Iterator;
25 import java.util.Map;
26 
35 public class WrappedBinaryMinHeap<E> implements MinHeap<E> {
36 
38  private final Map<E, WrappedEntry> map;
39 
40  public WrappedBinaryMinHeap(int maxSize) {
41  this.delegate = new BinaryMinHeap<>(maxSize);
42  this.map = new IdentityHashMap<>(maxSize);
43  }
44 
45  public WrappedBinaryMinHeap(int maxSize, int fanout, boolean classicalRemove) {
46  this.delegate = new BinaryMinHeap<>(maxSize, fanout, classicalRemove);
47  this.map = new IdentityHashMap<>(maxSize);
48  }
49 
50  @Override
51  public boolean add(E value, double priority) {
52  return this.delegate.add(this.getOrCreateWrappedEntry(value), priority);
53  }
54 
55  @Override
56  public boolean remove(E value) {
57  if (value == null) return false;
58  else return this.delegate.remove(this.getWrappedEntry(value));
59  }
60 
61  @Override
62  public E poll() {
63  WrappedEntry entry = this.delegate.poll();
64  if (entry != null) return entry.getValue();
65  else return null;
66  }
67 
68  @Override
69  public boolean decreaseKey(E value, double priority) {
70  return this.delegate.decreaseKey(this.getWrappedEntry(value), priority);
71  }
72 
73  @Override
74  public void reset() {
75  // I don't see why we need this? Might make sense if after a reset are added new elements -
76  // but in that case create a new heap.
77  // cdobler, sep'17
78 // this.map.clear();
79 
80  this.delegate.reset();
81  }
82 
83  @Override
84  public Iterator<E> iterator() {
85  return new ArrayIterator(this.delegate.iterator());
86  }
87 
88  private WrappedEntry getWrappedEntry(E value) {
89  if (value == null) {
90  throw new NullPointerException("null values are not supported!");
91  } else return this.map.get(value);
92  }
93 
95 
96  WrappedEntry wrappedEntry = this.getWrappedEntry(value);
97  if (wrappedEntry == null) {
98  int index = map.size();
99 
100 // if (index > maxSize) throw new RuntimeException("Number of elements exceeds the number of specified elements.");
101 
102  wrappedEntry = new WrappedEntry(value, index);
103  map.put(value, wrappedEntry);
104  }
105  return wrappedEntry;
106  }
107 
108  @Override
109  public E peek() {
110  WrappedEntry entry = this.delegate.peek();
111  if (entry != null) return entry.getValue();
112  else return null;
113  }
114 
115  @Override
116  public int size() {
117  return this.delegate.size();
118  }
119 
120  @Override
121  public boolean isEmpty() {
122  return this.delegate.isEmpty();
123  }
124 
125  private final class ArrayIterator implements Iterator<E> {
126 
127  private final Iterator<WrappedEntry> delegate;
128 
129  public ArrayIterator(Iterator<WrappedEntry> delegate) {
130  this.delegate = delegate;
131  }
132 
133  @Override
134  public boolean hasNext() {
135  return this.delegate.hasNext();
136  }
137 
138  @Override
139  public E next() {
140  return this.delegate.next().getValue();
141  }
142 
143  @Override
144  public void remove() {
145  this.delegate.remove();
146  }
147  }
148 
149  private final class WrappedEntry implements HasIndex {
150 
151  private final E value;
152  private final int index;
153 
154  public WrappedEntry(E value, int index) {
155  this.value = value;
156  this.index = index;
157  }
158 
159  public E getValue() {
160  return this.value;
161  }
162 
163  @Override
164  public int getArrayIndex() {
165  return this.index;
166  }
167  }
168 }
WrappedBinaryMinHeap(int maxSize, int fanout, boolean classicalRemove)
final E value
int getArrayIndex()
E getValue()
final int index
WrappedEntry(E value, int index)