Skip to content

Commit c9a9dee

Browse files
mod : clang-format
1 parent 98f2540 commit c9a9dee

2 files changed

Lines changed: 61 additions & 26 deletions

File tree

src/main/java/com/thealgorithms/datastructures/heaps/IndexedPriorityQueue.java

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.thealgorithms.datastructures.heaps;
22

3+
import java.util.Arrays;
34
import java.util.Comparator;
45
import java.util.IdentityHashMap;
56
import java.util.Objects;
6-
import java.util.Arrays;
77
import java.util.function.Consumer;
88

99
/**
@@ -73,24 +73,32 @@ public IndexedPriorityQueue(Comparator<? super E> cmp) {
7373
}
7474

7575
public IndexedPriorityQueue(int initialCapacity, Comparator<? super E> cmp) {
76-
if (initialCapacity < 1) throw new IllegalArgumentException("initialCapacity < 1");
76+
if (initialCapacity < 1) {
77+
throw new IllegalArgumentException("initialCapacity < 1");
78+
}
7779
this.heap = new Object[initialCapacity];
7880
this.cmp = cmp;
7981
this.index = new IdentityHashMap<>();
8082
}
8183

8284
/** Returns current number of elements. */
83-
public int size() { return size; }
85+
public int size() {
86+
return size;
87+
}
8488

8589
/** Returns {@code true} if empty. */
86-
public boolean isEmpty() { return size == 0; }
90+
public boolean isEmpty() {
91+
return size == 0;
92+
}
8793

8894
/**
8995
* Returns the minimum element without removing it, or {@code null} if empty.
9096
* Matches {@link java.util.PriorityQueue#peek()} behavior.
9197
*/
9298
@SuppressWarnings("unchecked")
93-
public E peek() { return size == 0 ? null : (E) heap[0]; }
99+
public E peek() {
100+
return size == 0 ? null : (E) heap[0];
101+
}
94102

95103
/**
96104
* Inserts the specified element (O(log n)).
@@ -100,7 +108,9 @@ public IndexedPriorityQueue(int initialCapacity, Comparator<? super E> cmp) {
100108
*/
101109
public boolean offer(E e) {
102110
Objects.requireNonNull(e, "element is null");
103-
if (size >= heap.length) grow(size + 1);
111+
if (size >= heap.length) {
112+
grow(size + 1);
113+
}
104114
// Insert at the end and bubble up. siftUp will maintain 'index' for all touched nodes.
105115
siftUp(size, e);
106116
size++;
@@ -112,7 +122,9 @@ public boolean offer(E e) {
112122
*/
113123
@SuppressWarnings("unchecked")
114124
public E poll() {
115-
if (size == 0) return null;
125+
if (size == 0) {
126+
return null;
127+
}
116128
E min = (E) heap[0];
117129
removeAt(0); // updates map and heap structure
118130
return min;
@@ -124,7 +136,9 @@ public E poll() {
124136
*/
125137
public boolean remove(Object o) {
126138
Integer i = index.get(o);
127-
if (i == null) return false;
139+
if (i == null) {
140+
return false;
141+
}
128142
removeAt(i);
129143
return true;
130144
}
@@ -156,11 +170,15 @@ public void clear() {
156170
*/
157171
public void changeKey(E e, Consumer<E> mutator) {
158172
Integer i = index.get(e);
159-
if (i == null) throw new IllegalArgumentException("Element not in queue");
173+
if (i == null) {
174+
throw new IllegalArgumentException("Element not in queue");
175+
}
160176
// Mutate fields used by comparator (do NOT mutate equality/hash if using value-based map)
161177
mutator.accept(e);
162178
// Try bubbling up; if no movement occurred, bubble down.
163-
if (!siftUp(i)) siftDown(i);
179+
if (!siftUp(i)) {
180+
siftDown(i);
181+
}
164182
}
165183

166184
/**
@@ -169,7 +187,9 @@ public void changeKey(E e, Consumer<E> mutator) {
169187
*/
170188
public void decreaseKey(E e, Consumer<E> mutator) {
171189
Integer i = index.get(e);
172-
if (i == null) throw new IllegalArgumentException("Element not in queue");
190+
if (i == null) {
191+
throw new IllegalArgumentException("Element not in queue");
192+
}
173193
mutator.accept(e);
174194
siftUp(i);
175195
}
@@ -180,7 +200,9 @@ public void decreaseKey(E e, Consumer<E> mutator) {
180200
*/
181201
public void increaseKey(E e, Consumer<E> mutator) {
182202
Integer i = index.get(e);
183-
if (i == null) throw new IllegalArgumentException("Element not in queue");
203+
if (i == null) {
204+
throw new IllegalArgumentException("Element not in queue");
205+
}
184206
mutator.accept(e);
185207
siftDown(i);
186208
}
@@ -199,7 +221,9 @@ private void grow(int minCapacity) {
199221

200222
@SuppressWarnings("unchecked")
201223
private int compare(E a, E b) {
202-
if (cmp != null) return cmp.compare(a, b);
224+
if (cmp != null) {
225+
return cmp.compare(a, b);
226+
}
203227
return ((Comparable<? super E>) a).compareTo(b);
204228
}
205229

@@ -212,7 +236,9 @@ private void siftUp(int k, E x) {
212236
while (k > 0) {
213237
int p = (k - 1) >>> 1;
214238
E e = (E) heap[p];
215-
if (compare(x, e) >= 0) break;
239+
if (compare(x, e) >= 0) {
240+
break;
241+
}
216242
heap[k] = e;
217243
index.put(e, k);
218244
k = p;
@@ -232,7 +258,9 @@ private boolean siftUp(int k) {
232258
while (k > 0) {
233259
int p = (k - 1) >>> 1;
234260
E e = (E) heap[p];
235-
if (compare(x, e) >= 0) break;
261+
if (compare(x, e) >= 0) {
262+
break;
263+
}
236264
heap[k] = e;
237265
index.put(e, k);
238266
k = p;
@@ -252,14 +280,16 @@ private void siftDown(int k) {
252280
E x = (E) heap[k];
253281
int half = n >>> 1; // loop while k has at least one child
254282
while (k < half) {
255-
int child = (k << 1) + 1; // assume left is smaller
283+
int child = (k << 1) + 1; // assume left is smaller
256284
E c = (E) heap[child];
257285
int r = child + 1;
258286
if (r < n && compare(c, (E) heap[r]) > 0) {
259287
child = r;
260288
c = (E) heap[child];
261289
}
262-
if (compare(x, c) <= 0) break;
290+
if (compare(x, c) <= 0) {
291+
break;
292+
}
263293
heap[k] = c;
264294
index.put(c, k);
265295
k = child;
@@ -276,18 +306,22 @@ private void siftDown(int k) {
276306
*/
277307
@SuppressWarnings("unchecked")
278308
private void removeAt(int i) {
279-
int n = --size; // last index after removal
309+
int n = --size; // last index after removal
280310
E moved = (E) heap[n];
281311
E removed = (E) heap[i];
282-
heap[n] = null; // help GC
283-
index.remove(removed); // drop mapping for removed element
312+
heap[n] = null; // help GC
313+
index.remove(removed); // drop mapping for removed element
284314

285-
if (i == n) return; // removed last element; done
315+
if (i == n) {
316+
return; // removed last element; done
317+
}
286318

287319
heap[i] = moved;
288320
index.put(moved, i);
289321

290322
// Try sift-up first (cheap if key decreased); if no movement, sift-down.
291-
if (!siftUp(i)) siftDown(i);
323+
if (!siftUp(i)) {
324+
siftDown(i);
325+
}
292326
}
293327
}

src/test/java/com/thealgorithms/datastructures/heaps/IndexedPriorityQueueTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package com.thealgorithms.datastructures.heaps;
22

3+
import java.util.Comparator;
34
import org.junit.jupiter.api.Assertions;
45
import org.junit.jupiter.api.Test;
56

6-
import java.util.Comparator;
7-
87
/**
98
* Tests for {@link IndexedPriorityQueue}.
109
*
@@ -47,7 +46,9 @@ static class NodeWithEquals {
4746

4847
@Override
4948
public boolean equals(Object o) {
50-
if (!(o instanceof NodeWithEquals)) return false;
49+
if (!(o instanceof NodeWithEquals)) {
50+
return false;
51+
}
5152
NodeWithEquals other = (NodeWithEquals) o;
5253
// Intentionally naive equality: equal if priority is equal
5354
return this.prio == other.prio;
@@ -238,7 +239,7 @@ void testDirectMutationWithoutChangeKeyDoesNotReheap_ByDesign() {
238239
@Test
239240
void testDuplicateEqualsElementsAreSupported_IdentityMap() {
240241
IndexedPriorityQueue<NodeWithEquals> pq =
241-
new IndexedPriorityQueue<>(Comparator.comparingInt(n -> n.prio));
242+
new IndexedPriorityQueue<>(Comparator.comparingInt(n -> n.prio));
242243

243244
NodeWithEquals x1 = new NodeWithEquals("X1", 7);
244245
NodeWithEquals x2 = new NodeWithEquals("X2", 7); // equals to X1 by prio, but different instance

0 commit comments

Comments
 (0)