@@ -261,91 +261,64 @@ void add_into_heap(T* heap, std::int64_t heap_size, const T& value) {
261
261
std::int64_t index = 0 ;
262
262
if (compare (value, heap[index])) {
263
263
} 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 ;
288
277
} 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;
314
281
}
315
282
} 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;
319
287
} else {
320
- if (compare (value, heap[left])) {
321
- break ;
322
- } else {
288
+ if (compare (heap[left], heap[right])) {
323
289
heap[index] = heap[left];
324
290
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 ;
336
291
} else {
337
292
heap[index] = heap[right];
338
293
index = right;
339
294
}
340
295
}
296
+ }
297
+ } else {
298
+ // Only the left child is present
299
+ if (compare (value, heap[left])) {
300
+ break ;
341
301
} 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])) {
343
310
break ;
311
+ } else {
312
+ heap[index] = heap[right];
313
+ index = right;
344
314
}
315
+ } else {
316
+ // No children are present
317
+ break ;
345
318
}
346
319
}
347
- heap[index] = value;
348
320
}
321
+ heap[index] = value;
349
322
}
350
323
}
351
324
0 commit comments