Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 16 | package org.onlab.graph; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
| 19 | |
| 20 | import java.util.Comparator; |
| 21 | import java.util.Iterator; |
| 22 | import java.util.List; |
| 23 | import java.util.Objects; |
| 24 | |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 25 | import static com.google.common.base.MoreObjects.toStringHelper; |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 26 | import 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 Vachuska | 7b652ad | 2014-10-30 14:10:51 -0700 | [diff] [blame] | 31 | * <p> |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 32 | * 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 Vachuska | 7b652ad | 2014-10-30 14:10:51 -0700 | [diff] [blame] | 36 | * </p> |
| 37 | * <p> |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 38 | * This class is not thread-safe and care must be taken to prevent concurrent |
| 39 | * modifications. |
Thomas Vachuska | 7b652ad | 2014-10-30 14:10:51 -0700 | [diff] [blame] | 40 | * </p> |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 41 | * |
| 42 | * @param <T> type of the items on the heap |
| 43 | */ |
| 44 | public class Heap<T> { |
| 45 | |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 46 | 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) { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 186 | if (this == obj) { |
| 187 | return true; |
| 188 | } |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 189 | 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() { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 205 | return toStringHelper(this) |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 206 | .add("data", data) |
| 207 | .add("comparator", comparator) |
| 208 | .toString(); |
| 209 | } |
| 210 | |
| 211 | } |