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