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