From ef10f2646ce6edb3be5d26097dabc29a140bf620 Mon Sep 17 00:00:00 2001 From: Hao Zhang Date: Fri, 20 Jun 2025 15:03:07 +0800 Subject: [PATCH] Remove duplicated if checking in add_into_heap for cpu. --- qmb/_hamiltonian_cpu.cpp | 109 +++++++++++++++------------------------ 1 file changed, 41 insertions(+), 68 deletions(-) diff --git a/qmb/_hamiltonian_cpu.cpp b/qmb/_hamiltonian_cpu.cpp index 7c59d94..e1437d1 100644 --- a/qmb/_hamiltonian_cpu.cpp +++ b/qmb/_hamiltonian_cpu.cpp @@ -261,91 +261,64 @@ void add_into_heap(T* heap, std::int64_t heap_size, const T& value) { std::int64_t index = 0; if (compare(value, heap[index])) { } else { - if (compare(value, heap[index])) { - } else { - while (true) { - // Calculate the indices of the left and right children - std::int64_t left = (index << 1) + 1; - std::int64_t right = (index << 1) + 2; - std::int64_t left_present = left < heap_size; - std::int64_t right_present = right < heap_size; - if (left_present) { - if (right_present) { - // Both left and right children are present - if (compare(value, heap[left])) { - if (compare(value, heap[right])) { - // Both children are greater than the value, break - break; - } else { - // The left child is greater than the value, treat it as if only the right child is present - if (compare(value, heap[right])) { - break; - } else { - heap[index] = heap[right]; - index = right; - } - } + while (true) { + // Calculate the indices of the left and right children + std::int64_t left = (index << 1) + 1; + std::int64_t right = (index << 1) + 2; + std::int64_t left_present = left < heap_size; + std::int64_t right_present = right < heap_size; + if (left_present) { + if (right_present) { + // Both left and right children are present + if (compare(value, heap[left])) { + if (compare(value, heap[right])) { + // Both children are greater than the value, break + break; } else { - if (compare(value, heap[right])) { - // The right child is greater than the value, treat it as if only the left child is present - if (compare(value, heap[left])) { - break; - } else { - heap[index] = heap[left]; - index = left; - } - } else { - if (compare(heap[left], heap[right])) { - if (compare(value, heap[left])) { - break; - } else { - heap[index] = heap[left]; - index = left; - } - } else { - if (compare(value, heap[right])) { - break; - } else { - heap[index] = heap[right]; - index = right; - } - } - } + // The left child is greater than the value + heap[index] = heap[right]; + index = right; } } else { - // Only the left child is present - if (compare(value, heap[left])) { - break; + if (compare(value, heap[right])) { + // The right child is greater than the value + heap[index] = heap[left]; + index = left; } else { - if (compare(value, heap[left])) { - break; - } else { + if (compare(heap[left], heap[right])) { heap[index] = heap[left]; index = left; - } - } - } - } else { - if (right_present) { - // Only the right child is present - if (compare(value, heap[right])) { - break; - } else { - if (compare(value, heap[right])) { - break; } else { heap[index] = heap[right]; index = right; } } + } + } else { + // Only the left child is present + if (compare(value, heap[left])) { + break; } else { - // No children are present + heap[index] = heap[left]; + index = left; + } + } + } else { + if (right_present) { + // Only the right child is present + if (compare(value, heap[right])) { break; + } else { + heap[index] = heap[right]; + index = right; } + } else { + // No children are present + break; } } - heap[index] = value; } + heap[index] = value; } }