Skip to content

Commit ef10f26

Browse files
committed
Remove duplicated if checking in add_into_heap for cpu.
1 parent 93f162d commit ef10f26

File tree

1 file changed

+41
-68
lines changed

1 file changed

+41
-68
lines changed

qmb/_hamiltonian_cpu.cpp

Lines changed: 41 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -261,91 +261,64 @@ void add_into_heap(T* heap, std::int64_t heap_size, const T& value) {
261261
std::int64_t index = 0;
262262
if (compare(value, heap[index])) {
263263
} else {
264-
if (compare(value, heap[index])) {
265-
} else {
266-
while (true) {
267-
// Calculate the indices of the left and right children
268-
std::int64_t left = (index << 1) + 1;
269-
std::int64_t right = (index << 1) + 2;
270-
std::int64_t left_present = left < heap_size;
271-
std::int64_t right_present = right < heap_size;
272-
if (left_present) {
273-
if (right_present) {
274-
// Both left and right children are present
275-
if (compare(value, heap[left])) {
276-
if (compare(value, heap[right])) {
277-
// Both children are greater than the value, break
278-
break;
279-
} else {
280-
// The left child is greater than the value, treat it as if only the right child is present
281-
if (compare(value, heap[right])) {
282-
break;
283-
} else {
284-
heap[index] = heap[right];
285-
index = right;
286-
}
287-
}
264+
while (true) {
265+
// Calculate the indices of the left and right children
266+
std::int64_t left = (index << 1) + 1;
267+
std::int64_t right = (index << 1) + 2;
268+
std::int64_t left_present = left < heap_size;
269+
std::int64_t right_present = right < heap_size;
270+
if (left_present) {
271+
if (right_present) {
272+
// Both left and right children are present
273+
if (compare(value, heap[left])) {
274+
if (compare(value, heap[right])) {
275+
// Both children are greater than the value, break
276+
break;
288277
} else {
289-
if (compare(value, heap[right])) {
290-
// The right child is greater than the value, treat it as if only the left child is present
291-
if (compare(value, heap[left])) {
292-
break;
293-
} else {
294-
heap[index] = heap[left];
295-
index = left;
296-
}
297-
} else {
298-
if (compare(heap[left], heap[right])) {
299-
if (compare(value, heap[left])) {
300-
break;
301-
} else {
302-
heap[index] = heap[left];
303-
index = left;
304-
}
305-
} else {
306-
if (compare(value, heap[right])) {
307-
break;
308-
} else {
309-
heap[index] = heap[right];
310-
index = right;
311-
}
312-
}
313-
}
278+
// The left child is greater than the value
279+
heap[index] = heap[right];
280+
index = right;
314281
}
315282
} else {
316-
// Only the left child is present
317-
if (compare(value, heap[left])) {
318-
break;
283+
if (compare(value, heap[right])) {
284+
// The right child is greater than the value
285+
heap[index] = heap[left];
286+
index = left;
319287
} else {
320-
if (compare(value, heap[left])) {
321-
break;
322-
} else {
288+
if (compare(heap[left], heap[right])) {
323289
heap[index] = heap[left];
324290
index = left;
325-
}
326-
}
327-
}
328-
} else {
329-
if (right_present) {
330-
// Only the right child is present
331-
if (compare(value, heap[right])) {
332-
break;
333-
} else {
334-
if (compare(value, heap[right])) {
335-
break;
336291
} else {
337292
heap[index] = heap[right];
338293
index = right;
339294
}
340295
}
296+
}
297+
} else {
298+
// Only the left child is present
299+
if (compare(value, heap[left])) {
300+
break;
341301
} else {
342-
// No children are present
302+
heap[index] = heap[left];
303+
index = left;
304+
}
305+
}
306+
} else {
307+
if (right_present) {
308+
// Only the right child is present
309+
if (compare(value, heap[right])) {
343310
break;
311+
} else {
312+
heap[index] = heap[right];
313+
index = right;
344314
}
315+
} else {
316+
// No children are present
317+
break;
345318
}
346319
}
347-
heap[index] = value;
348320
}
321+
heap[index] = value;
349322
}
350323
}
351324

0 commit comments

Comments
 (0)