Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 41 additions & 68 deletions qmb/_hamiltonian_cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Copilot AI Jun 24, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Use an early return instead of an empty if‐block to reduce nesting. For example:

if (compare(value, heap[index]))
    return;
Suggested change
} else {
return;
}

Copilot uses AI. Check for mistakes.
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;
}
}

Expand Down
Loading