Skip to content

Commit 3c47308

Browse files
committed
Remove duplicated if checking in add_into_heap for cpu.
PR: USTC-KnowledgeComputingLab/qmb#49 Signed-off-by: Hao Zhang <[email protected]>
2 parents baa30dc + ef10f26 commit 3c47308

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
@@ -262,91 +262,64 @@ void add_into_heap(T* heap, std::int64_t heap_size, const T& value) {
262262
std::int64_t index = 0;
263263
if (compare(value, heap[index])) {
264264
} else {
265-
if (compare(value, heap[index])) {
266-
} else {
267-
while (true) {
268-
// Calculate the indices of the left and right children
269-
std::int64_t left = (index << 1) + 1;
270-
std::int64_t right = (index << 1) + 2;
271-
std::int64_t left_present = left < heap_size;
272-
std::int64_t right_present = right < heap_size;
273-
if (left_present) {
274-
if (right_present) {
275-
// Both left and right children are present
276-
if (compare(value, heap[left])) {
277-
if (compare(value, heap[right])) {
278-
// Both children are greater than the value, break
279-
break;
280-
} else {
281-
// The left child is greater than the value, treat it as if only the right child is present
282-
if (compare(value, heap[right])) {
283-
break;
284-
} else {
285-
heap[index] = heap[right];
286-
index = right;
287-
}
288-
}
265+
while (true) {
266+
// Calculate the indices of the left and right children
267+
std::int64_t left = (index << 1) + 1;
268+
std::int64_t right = (index << 1) + 2;
269+
std::int64_t left_present = left < heap_size;
270+
std::int64_t right_present = right < heap_size;
271+
if (left_present) {
272+
if (right_present) {
273+
// Both left and right children are present
274+
if (compare(value, heap[left])) {
275+
if (compare(value, heap[right])) {
276+
// Both children are greater than the value, break
277+
break;
289278
} else {
290-
if (compare(value, heap[right])) {
291-
// The right child is greater than the value, treat it as if only the left child is present
292-
if (compare(value, heap[left])) {
293-
break;
294-
} else {
295-
heap[index] = heap[left];
296-
index = left;
297-
}
298-
} else {
299-
if (compare(heap[left], heap[right])) {
300-
if (compare(value, heap[left])) {
301-
break;
302-
} else {
303-
heap[index] = heap[left];
304-
index = left;
305-
}
306-
} else {
307-
if (compare(value, heap[right])) {
308-
break;
309-
} else {
310-
heap[index] = heap[right];
311-
index = right;
312-
}
313-
}
314-
}
279+
// The left child is greater than the value
280+
heap[index] = heap[right];
281+
index = right;
315282
}
316283
} else {
317-
// Only the left child is present
318-
if (compare(value, heap[left])) {
319-
break;
284+
if (compare(value, heap[right])) {
285+
// The right child is greater than the value
286+
heap[index] = heap[left];
287+
index = left;
320288
} else {
321-
if (compare(value, heap[left])) {
322-
break;
323-
} else {
289+
if (compare(heap[left], heap[right])) {
324290
heap[index] = heap[left];
325291
index = left;
326-
}
327-
}
328-
}
329-
} else {
330-
if (right_present) {
331-
// Only the right child is present
332-
if (compare(value, heap[right])) {
333-
break;
334-
} else {
335-
if (compare(value, heap[right])) {
336-
break;
337292
} else {
338293
heap[index] = heap[right];
339294
index = right;
340295
}
341296
}
297+
}
298+
} else {
299+
// Only the left child is present
300+
if (compare(value, heap[left])) {
301+
break;
342302
} else {
343-
// No children are present
303+
heap[index] = heap[left];
304+
index = left;
305+
}
306+
}
307+
} else {
308+
if (right_present) {
309+
// Only the right child is present
310+
if (compare(value, heap[right])) {
344311
break;
312+
} else {
313+
heap[index] = heap[right];
314+
index = right;
345315
}
316+
} else {
317+
// No children are present
318+
break;
346319
}
347320
}
348-
heap[index] = value;
349321
}
322+
heap[index] = value;
350323
}
351324
}
352325

0 commit comments

Comments
 (0)