blob: 97be880c5b1c29a8692ae7e0ff5e739536eb7554 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska24c849c2014-10-27 09:53:05 -070015 */
tome3489412014-08-29 02:30:38 -070016package org.onlab.graph;
17
18import com.google.common.collect.ImmutableList;
19
20import java.util.Comparator;
21import java.util.Iterator;
22import java.util.List;
23import java.util.Objects;
24
tomeadbb462014-09-07 16:10:19 -070025import static com.google.common.base.MoreObjects.toStringHelper;
tome3489412014-08-29 02:30:38 -070026import static com.google.common.base.Preconditions.checkNotNull;
27
28/**
29 * Implementation of an array-backed heap structure whose sense of order is
30 * imposed by the provided comparator.
Thomas Vachuska7b652ad2014-10-30 14:10:51 -070031 * <p>
tome3489412014-08-29 02:30:38 -070032 * While this provides similar functionality to {@link java.util.PriorityQueue}
33 * data structure, one key difference is that external entities can control
34 * when to restore the heap property, which is done through invocation of the
35 * {@link #heapify} method.
Thomas Vachuska7b652ad2014-10-30 14:10:51 -070036 * </p>
37 * <p>
tome3489412014-08-29 02:30:38 -070038 * This class is not thread-safe and care must be taken to prevent concurrent
39 * modifications.
Thomas Vachuska7b652ad2014-10-30 14:10:51 -070040 * </p>
tome3489412014-08-29 02:30:38 -070041 *
42 * @param <T> type of the items on the heap
43 */
44public class Heap<T> {
45
tome3489412014-08-29 02:30:38 -070046 private final List<T> data;
47 private final Comparator<T> comparator;
48
49 /**
50 * Creates a new heap backed by the specified list. In the interest of
51 * efficiency, the list should be array-backed. Also, for the same reason,
52 * the data is not copied and therefore, the caller must assure that the
53 * backing data is not altered in any way.
54 *
55 * @param data backing data list
56 * @param comparator comparator for ordering the heap items
57 */
58 public Heap(List<T> data, Comparator<T> comparator) {
59 this.data = checkNotNull(data, "Data cannot be null");
60 this.comparator = checkNotNull(comparator, "Comparator cannot be null");
61 heapify();
62 }
63
64 /**
65 * Restores the heap property by re-arranging the elements in the backing
66 * array as necessary following any heap modifications.
67 */
68 public void heapify() {
69 for (int i = data.size() / 2; i >= 0; i--) {
70 heapify(i);
71 }
72 }
73
74 /**
75 * Returns the current size of the heap.
76 *
77 * @return number of items in the heap
78 */
79 public int size() {
80 return data.size();
81 }
82
83 /**
84 * Returns true if there are no items in the heap.
85 *
86 * @return true if heap is empty
87 */
88 public boolean isEmpty() {
89 return data.isEmpty();
90 }
91
92 /**
93 * Returns the most extreme item in the heap.
94 *
95 * @return heap extreme or null if the heap is empty
96 */
97 public T extreme() {
98 return data.isEmpty() ? null : data.get(0);
99 }
100
101 /**
102 * Extracts and returns the most extreme item from the heap.
103 *
104 * @return heap extreme or null if the heap is empty
105 */
106 public T extractExtreme() {
107 if (!isEmpty()) {
108 T extreme = extreme();
109
110 data.set(0, data.get(data.size() - 1));
111 data.remove(data.size() - 1);
112 heapify();
113 return extreme;
114 }
115 return null;
116 }
117
118 /**
119 * Inserts the specified item into the heap and returns the modified heap.
120 *
121 * @param item item to be inserted
122 * @return the heap self
123 * @throws IllegalArgumentException if the heap is already full
124 */
125 public Heap<T> insert(T item) {
126 data.add(item);
127 bubbleUp();
128 return this;
129 }
130
131 /**
132 * Returns iterator to traverse the heap level-by-level. This iterator
133 * does not permit removal of items.
134 *
135 * @return non-destructive heap iterator
136 */
137 public Iterator<T> iterator() {
138 return ImmutableList.copyOf(data).iterator();
139 }
140
141 // Bubbles up the last item in the heap to its proper position to restore
142 // the heap property.
143 private void bubbleUp() {
144 int child = data.size() - 1;
145 while (child > 0) {
146 int parent = child / 2;
147 if (comparator.compare(data.get(child), data.get(parent)) < 0) {
148 break;
149 }
150 swap(child, parent);
151 child = parent;
152 }
153 }
154
155 // Restores the heap property of the specified heap layer.
156 private void heapify(int i) {
157 int left = 2 * i + 1;
158 int right = 2 * i;
159 int extreme = i;
160
161 if (left < data.size() &&
162 comparator.compare(data.get(extreme), data.get(left)) < 0) {
163 extreme = left;
164 }
165
166 if (right < data.size() &&
167 comparator.compare(data.get(extreme), data.get(right)) < 0) {
168 extreme = right;
169 }
170
171 if (extreme != i) {
172 swap(i, extreme);
173 heapify(extreme);
174 }
175 }
176
177 // Swaps two heap items identified by their respective indexes.
178 private void swap(int i, int k) {
179 T aux = data.get(i);
180 data.set(i, data.get(k));
181 data.set(k, aux);
182 }
183
184 @Override
185 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -0700186 if (this == obj) {
187 return true;
188 }
tome3489412014-08-29 02:30:38 -0700189 if (obj instanceof Heap) {
190 Heap that = (Heap) obj;
191 return this.getClass() == that.getClass() &&
192 Objects.equals(this.comparator, that.comparator) &&
193 Objects.deepEquals(this.data, that.data);
194 }
195 return false;
196 }
197
198 @Override
199 public int hashCode() {
200 return Objects.hash(comparator, data);
201 }
202
203 @Override
204 public String toString() {
tomeadbb462014-09-07 16:10:19 -0700205 return toStringHelper(this)
tome3489412014-08-29 02:30:38 -0700206 .add("data", data)
207 .add("comparator", comparator)
208 .toString();
209 }
210
211}