| 508 if (si < elem_count) { |
508 if (si < elem_count) { |
| 509 if (allow_duplicates) { |
509 if (allow_duplicates) { |
| 510 // duplicates allowed or nothing inserted yet: simply copy everything |
510 // duplicates allowed or nothing inserted yet: simply copy everything |
| 511 memcpy(dest, src, elem_size * (elem_count - si)); |
511 memcpy(dest, src, elem_size * (elem_count - si)); |
| 512 } else { |
512 } else { |
| 513 if (dest != *target) { |
513 // we must check the remaining source elements one by one |
| 514 // skip all source elements that equal the last element |
514 // to skip the duplicates. |
| 515 char *last = dest - elem_size; |
515 // Note that no source element can equal the last element in the |
| 516 while (si < elem_count) { |
516 // destination, because that would have created an insertion point |
| 517 if (last != NULL && cmp_func(last, src) == 0) { |
517 // and a buffer, s.t. the above loop already handled the duplicates |
| 518 src += elem_size; |
|
| 519 si++; |
|
| 520 (*size)--; |
|
| 521 } else { |
|
| 522 break; |
|
| 523 } |
|
| 524 } |
|
| 525 } |
|
| 526 // we must check the elements in the chunk one by one |
|
| 527 while (si < elem_count) { |
518 while (si < elem_count) { |
| 528 // find a chain of elements that can be copied |
519 // find a chain of elements that can be copied |
| 529 size_t copy_len = 1, skip_len = 0; |
520 size_t copy_len = 1, skip_len = 0; |
| 530 { |
521 { |
| 531 const char *left_src = src; |
522 const char *left_src = src; |